1 ; lily.scm -- implement Scheme output routines for TeX and PostScript
3 ; source file of the GNU LilyPond music typesetter
5 ; (c) 1998 Jan Nieuwenhuizen <janneke@gnu.org>
8 ;(debug-enable 'backtrace)
14 (map (lambda (n) (string-append (number->string n ) " ")) l)))
16 (define (reduce operator list)
17 (if (null? (cdr list)) (car list)
18 (operator (car list) (reduce operator (cdr list)))
23 (define (glue-2-strings a b) (string-append a " " b))
27 (reduce glue-2-strings (map number->string l)))
29 (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
31 (define (number->octal-string x)
32 (let* ((n (inexact->exact x))
34 (n8 (quotient (- n (* n64 64)) 8)))
38 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
40 (define (inexact->string x radix)
41 (let ((n (inexact->exact x)))
42 (number->string n radix)))
48 (string-append (number->string (car c)) " ")
49 (string-append (number->string (cadr c)) " ")))
55 (make-string 1 (integer->char (+ (char->integer #\A) i)))
60 (define (scm-scm action-name)
63 (define security-paranoia #f)
66 ;; See documentation of Item::visibility_lambda_
67 (define (postbreak_only_visibility d) (if (= d 1) '(#f . #f) '(#t . #t)))
68 (define (spanbar_non_postbreak_visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
70 (define (non_postbreak_visibility d) (if (= d 1) '(#t . #t) '(#f . #f)))
71 (define (non_prebreak_visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
74 ;; Score_span_bars are only visible at start of line
75 ;; i.e. if break_dir == RIGHT == 1
76 (define Span_bar_engraver_visibility non_postbreak_visibility)
77 (define mark-visibility non_prebreak_visibility)
78 (define Span_score_bar_engraver_visibility postbreak_only_visibility)
79 (define Piano_bar_engraver_visibility postbreak_only_visibility)
80 (define Staff_group_bar_engraver_visibility postbreak_only_visibility)
82 ;; Spacing constants for prefatory matter.
84 ;; rules for this spacing are much more complicated than this. See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147
90 (("" "Clef_item") . (minimum_space 1.0))
91 (("" "Staff_bar") . (minimum_space 0.0))
92 (("" "Clef_item") . (minimum_space 1.0))
93 (("" "Key_item") . (minimum_space 0.5))
94 (("" "Span_bar") . (extra_space 0.0))
95 (("" "Time_signature") . (extra_space 0.0))
96 (("" "begin-of-note") . (minimum_space 1.5))
97 (("Clef_item" "Key_item") . (minimum_space 3.5))
98 (("Key_item" "Time_signature") . (extra_space 2.0))
99 (("Clef_item" "Time_signature") . (minimum_space 3.5))
100 (("Staff_bar" "Clef_item") . (minimum_space 1.0))
101 (("Clef_item" "Staff_bar") . (minimum_space 3.7))
102 (("Time_signature" "Staff_bar") . (minimum_space 2.0))
103 (("Key_item" "Staff_bar") . (extra_space 1.0))
104 (("Span_bar" "Clef_item") . (extra_space 1.0))
105 (("Clef_item" "Span_bar") . (minimum_space 3.7))
106 (("Time_signature" "Span_bar") . (minimum_space 2.0))
107 (("Key_item" "Span_bar") . (minimum_space 2.5))
108 (("Staff_bar" "Time_signature") . (minimum_space 2.0))
109 (("Time_signature" "begin-of-note") . (extra_space 1.0)) ; Huh? see twinkle.ly
110 (("Key_item" "begin-of-note") . (extra_space 2.5))
111 (("Staff_bar" "begin-of-note") . (extra_space 1.0))
112 (("Clef_item" "begin-of-note") . (minimum_space 5.0))
113 (("" "Breathing_sign") . (minimum_space 0.0))
114 (("Breathing_sign" "Key_item") . (minimum_space 1.5))
115 (("Breathing_sign" "begin-of-note") . (minimum_space 1.0))
116 (("Breathing_sign" "Staff_bar") . (minimum_space 1.5))
117 (("Breathing_sign" "Clef_item") . (minimum_space 2.0))
123 (define (break-align-spacer this next)
124 (let ((entry (assoc `(,this ,next) space-alist)))
127 (begin (ly-warn (string-append "Unknown spacing pair `" this "', `" next "'"))
128 '(minimum_space 0.0)))))
136 ("dynamic" . "feta-din")
137 ("finger" . "feta-nummer")
138 ("typewriter" . "cmtt")
143 ("mark" . "feta-nummer")
144 ("number" . "feta-nummer")
145 ("volta" . "feta-nummer"))
148 (define script-alist '())
149 (define (articulation-to-scriptdef a)
150 (assoc a script-alist)
153 ;; Map style names to TeX font names. Return false if
154 ;; no font name found.
155 (define (style-to-cmr s)
160 (define (tex-scm action-name)
165 (define font-alist '())
166 (define font-count 0)
167 (define current-font "")
168 (define (clear-fontcache)
170 (set! font-alist '())
172 (set! current-font "")))
174 (define (cached-fontname i)
177 (make-string 1 (integer->char (+ 65 i)))))
179 (define (select-font font-name)
180 (if (not (equal? font-name current-font))
182 (set! current-font font-name)
183 (define font-cmd (assoc font-name font-alist))
184 (if (eq? font-cmd #f)
186 (set! font-cmd (cached-fontname font-count))
187 (set! font-alist (acons font-name font-cmd font-alist))
188 (set! font-count (+ 1 font-count))
189 (if (equal? font-name "")
190 (error "Empty fontname -- SELECT-FONT"))
191 (string-append "\\font" font-cmd "=" font-name font-cmd))
196 (define (beam width slope thick)
197 (embedded-ps ((ps-scm 'beam) width slope thick)))
200 (embedded-ps ((ps-scm 'bracket) h)))
202 (define (dashed-slur thick dash l)
203 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
205 (define (crescendo w h cont)
206 (embedded-ps ((ps-scm 'crescendo) w h cont)))
209 (string-append "\\show{" (inexact->string i 10) "}"))
211 (define (decrescendo w h cont)
212 (embedded-ps ((ps-scm 'decrescendo) w h cont)))
214 (define (embedded-ps s)
215 (string-append "\\embeddedps{" s "}"))
218 "\n\\EndLilyPondOutput")
220 (define (experimental-on)
221 "\\turnOnExperimentalFeatures")
223 (define (font-switch i)
227 (define (font-def i s)
229 "\\font" (font-switch i) "=" s "\n"))
231 (define (generalmeter num den)
233 "\\generalmeter{" (number->string (inexact->exact num)) "}{" (number->string (inexact->exact den)) "}"))
235 (define (header-end) "\\turnOnPostScript")
237 (define (header creator generate)
239 "%created by: " creator generate "\n"))
241 (define (invoke-char s i)
243 "\n\\" s "{" (inexact->string i 10) "}" ))
245 (define (invoke-dim1 s d)
247 "\n\\" s "{" (number->dim d) "}"))
252 ;; need to do something to make this really safe.
254 (if security-paranoia
255 (define (output-tex-string s)
256 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post))
257 (define (output-tex-string s) s))
259 (define (lily-def key val)
261 "\\def\\" (output-tex-string key) "{" (output-tex-string val) "}\n"))
263 (define (number->dim x)
265 (number->string (chop-decimal x)) " pt "))
267 (define (placebox x y s)
270 (number->dim y) "}{" (number->dim x) "}{" s "}"))
273 ;;;; UGH! Junk \\bracefont !
274 (define (pianobrace y)
276 (define minht (* 2 mudelapaperstaffheight))
277 (define maxht (* 7 minht))
279 "{\\bracefont " (char (max
281 (/ (- (min y (- maxht step)) minht) step))) "}"))
285 (define (rulesym h w)
287 "\\vrule height " (number->dim (/ h 2))
288 " depth " (number->dim (/ h 2))
289 " width " (number->dim w)
293 (define (bezier-sandwich l)
294 (embedded-ps ((ps-scm 'bezier-sandwich) l)))
297 (define (start-line ht)
300 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
303 "}\\vss}\\interscoreline")
304 (define (stop-last-line)
306 (define (filledbox breapth width depth height)
308 "\\kern" (number->dim (- breapth))
309 "\\vrule width " (number->dim (+ breapth width))
310 "depth " (number->dim depth)
311 "height " (number->dim height) " "))
316 (string-append "\\hbox{" (output-tex-string s) "}"))
318 (define (tuplet ht gapx dx dy thick dir)
319 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
321 (define (volta h w thick last)
322 (embedded-ps ((ps-scm 'volta) h w thick last)))
325 ;; The procedures listed below form the public interface of TeX-scm.
326 ;; (should merge the 2 lists)
327 (cond ((eq? action-name 'all-definitions)
330 (define bezier-sandwich ,bezier-sandwich)
331 (define bracket ,bracket)
333 (define crescendo ,crescendo)
334 (define dashed-slur ,dashed-slur)
335 (define decrescendo ,decrescendo)
336 (define end-output ,end-output)
337 (define experimental-on ,experimental-on)
338 (define filledbox ,filledbox)
339 (define font-def ,font-def)
340 (define font-switch ,font-switch)
341 (define generalmeter ,generalmeter)
342 (define header-end ,header-end)
343 (define lily-def ,lily-def)
344 (define header ,header)
345 (define invoke-char ,invoke-char)
346 (define invoke-dim1 ,invoke-dim1)
347 (define pianobrace ,pianobrace)
348 (define placebox ,placebox)
349 (define rulesym ,rulesym)
350 (define select-font ,select-font)
351 (define start-line ,start-line)
352 (define stop-line ,stop-line)
353 (define stop-last-line ,stop-last-line)
355 (define tuplet ,tuplet)
356 (define volta ,volta)
359 ((eq? action-name 'beam) beam)
360 ((eq? action-name 'tuplet) tuplet)
361 ((eq? action-name 'bracket) bracket)
362 ((eq? action-name 'crescendo) crescendo)
363 ((eq? action-name 'dashed-slur) dashed-slur)
364 ((eq? action-name 'decrescendo) decrescendo)
365 ((eq? action-name 'end-output) end-output)
366 ((eq? action-name 'experimental-on) experimental-on)
367 ((eq? action-name 'font-def) font-def)
368 ((eq? action-name 'font-switch) font-switch)
369 ((eq? action-name 'generalmeter) generalmeter)
370 ((eq? action-name 'header-end) header-end)
371 ((eq? action-name 'lily-def) lily-def)
372 ((eq? action-name 'header) header)
373 ((eq? action-name 'invoke-char) invoke-char)
374 ((eq? action-name 'invoke-dim1) invoke-dim1)
375 ((eq? action-name 'placebox) placebox)
376 ((eq? action-name 'rulesym) rulesym)
377 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
378 ((eq? action-name 'start-line) start-line)
379 ((eq? action-name 'stem) stem)
380 ((eq? action-name 'stop-line) stop-line)
381 ((eq? action-name 'stop-last-line) stop-last-line)
382 ((eq? action-name 'volta) volta)
383 (else (error "unknown tag -- PS-TEX " action-name))
388 (define (ps-scm action-name)
391 ;; alist containing fontname -> fontcommand assoc (both strings)
392 (define font-alist '())
393 (define font-count 0)
394 (define current-font "")
395 (define (clear-fontcache)
397 (set! font-alist '())
399 (set! current-font "")))
401 (define (cached-fontname i)
404 (make-string 1 (integer->char (+ 65 i)))))
406 (define (select-font font-name)
407 (if (not (equal? font-name current-font))
409 (set! current-font font-name)
410 (define font-cmd (assoc font-name font-alist))
411 (if (eq? font-cmd #f)
413 (set! font-cmd (cached-fontname font-count))
414 (set! font-alist (acons font-name font-cmd font-alist))
415 (set! font-count (+ 1 font-count))
416 (string-append "\n/" font-cmd " {/"
418 " findfont 12 scalefont setfont} bind def \n"
420 (string-append (cdr font-cmd) " ")))
421 ; font-name == current-font no switch needed
425 (define (beam width slope thick)
427 (numbers->string (list width slope thick)) " draw_beam " ))
430 (invoke-dim1 " draw_bracket" h))
433 (invoke-char " show" i))
435 (define (crescendo w h cont)
437 (numbers->string (list w h (inexact->exact cont)))
440 (define (dashed-slur thick dash l)
442 (apply string-append (map control->string l))
443 (number->string thick)
445 (if (> 1 dash) (number->string (- (* thick dash) thick)) "0") " "
446 (number->string (* 2 thick))
447 " ] 0 draw_dashed_slur"))
449 (define (decrescendo w h cont)
451 (numbers->string (list w h (inexact->exact cont)))
452 " draw_decrescendo"))
458 (define (experimental-on) "")
460 (define (filledbox breapth width depth height)
461 (string-append (numbers->string (list breapth width depth height))
465 (define (font-def i s)
468 (substring s 0 (- (string-length s) 4))
469 " findfont 12 scalefont setfont} bind def \n"))
471 (define (font-switch i)
472 (string-append (font i) " "))
474 (define (generalmeter num den)
475 (string-append (number->string (inexact->exact num)) " " (number->string (inexact->exact den)) " generalmeter "))
477 (define (header-end) "")
478 (define (lily-def key val)
480 (if (string=? (substring key 0 (string-length "mudelapaper") ) "mudelapaper")
481 (string-append "/" key " {" val "} bind def\n")
482 (string-append "/" key " (" val ") def\n")
485 (define (header creator generate)
488 "%%Creator: " creator generate "\n"))
490 (define (invoke-char s i)
492 "(\\" (inexact->string i 8) ") " s " " ))
494 (define (invoke-dim1 s d)
496 (number->string (* d (/ 72.27 72))) " " s ))
498 (define (placebox x y s)
500 (number->string x) " " (number->string y) " {" s "} placebox "))
501 (define (pianobrace y)
505 (define (rulesym x y)
507 (number->string x) " "
508 (number->string y) " "
511 (define (bezier-sandwich l)
513 (apply string-append (map control->string l))
514 " draw_bezier_sandwich"))
516 (define (start-line height)
521 (define (stem breapth width depth height)
522 (string-append (numbers->string (list breapth width depth height))
529 (string-append "(" s ") show "))
532 (define (volta h w thick last)
534 (numbers->string (list h w thick (inexact->exact last)))
537 (define (tuplet ht gap dx dy thick dir)
539 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
548 (cond ((eq? action-name 'all-definitions)
551 (define tuplet ,tuplet)
552 (define bracket ,bracket)
554 (define crescendo ,crescendo)
555 (define volta ,volta)
556 (define bezier-sandwich ,bezier-sandwich)
557 (define dashed-slur ,dashed-slur)
558 (define decrescendo ,decrescendo)
559 (define end-output ,end-output)
560 (define experimental-on ,experimental-on)
561 (define filledbox ,filledbox)
562 (define font-def ,font-def)
563 (define font-switch ,font-switch)
564 (define generalmeter ,generalmeter)
565 (define pianobrace ,pianobrace)
566 (define header-end ,header-end)
567 (define lily-def ,lily-def)
568 (define header ,header)
569 (define invoke-char ,invoke-char)
570 (define invoke-dim1 ,invoke-dim1)
571 (define placebox ,placebox)
572 (define rulesym ,rulesym)
573 (define select-font ,select-font)
574 (define start-line ,start-line)
576 (define stop-line ,stop-line)
577 (define stop-last-line ,stop-line)
580 ((eq? action-name 'tuplet) tuplet)
581 ((eq? action-name 'beam) beam)
582 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
583 ((eq? action-name 'bracket) bracket)
584 ((eq? action-name 'char) char)
585 ((eq? action-name 'crescendo) crescendo)
586 ((eq? action-name 'dashed-slur) dashed-slur)
587 ((eq? action-name 'decrescendo) decrescendo)
588 ((eq? action-name 'experimental-on) experimental-on)
589 ((eq? action-name 'filledbox) filledbox)
590 ((eq? action-name 'select-font) select-font)
591 ((eq? action-name 'volta) volta)
592 (else (error "unknown tag -- PS-SCM " action-name))
597 ; Russ McManus, <mcmanus@IDT.NET>
599 ; I use the following, which should definitely be provided somewhere
600 ; in guile, but isn't, AFAIK:
604 (define (hash-table-for-each fn ht)
606 ((= i (vector-length ht)))
607 (do ((alist (vector-ref ht i) (cdr alist)))
609 (fn (car (car alist)) (cdr (car alist))))))
611 (define (hash-table-map fn ht)
614 ((= i (vector-length ht)) (reverse ret-ls))
615 (do ((alist (vector-ref ht i) (cdr alist)))
617 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
620 ;;;; print a SCM expression. Isn't this part of the std lib?
622 (define (scmlist->string exp)
624 ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
625 ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
626 (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
629 (define (scm->string exp)
631 ((pair? exp) (string-append "(" (scmlist->string exp)))
632 ((number? exp) (number->string exp))
633 ((symbol? exp) (symbol->string exp))
634 ((string? exp) (string-append "\"" exp "\""))
637 (define (test-scm->string)
638 (list (scmlist->string '(a))
639 (scmlist->string '(a b))
640 (scmlist->string '(a b . c))
644 (scm->string '(a b ))
645 (scm->string '(a b . c))
646 (scm->string '(a b (c . d)))
647 (scm->string '(a "bla" (c . 1.5)))