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 4.0))
98 (("Key_item" "Time_signature") . (extra_space 1.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 1.5)) ;double check this.
109 (("Time_signature" "begin-of-note") . (extra_space 2.0)) ;double check this.
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))
121 (define (break-align-spacer this next)
122 (let ((entry (assoc `(,this ,next) space-alist)))
125 (begin (ly-warn (string-append "Unknown spacing pair `" this "', `" next "'"))
126 '(minimum_space 0.0)))))
134 ("dynamic" . "feta-din")
135 ("finger" . "feta-nummer")
136 ("typewriter" . "cmtt")
141 ("mark" . "feta-nummer")
142 ("number" . "feta-nummer")
143 ("volta" . "feta-nummer"))
146 (define script-alist '())
147 (define (articulation-to-scriptdef a)
148 (assoc a script-alist)
151 ;; Map style names to TeX font names. Return false if
152 ;; no font name found.
153 (define (style-to-cmr s)
158 (define (tex-scm action-name)
163 (define font-alist '())
164 (define font-count 0)
165 (define current-font "")
166 (define (clear-fontcache)
168 (set! font-alist '())
170 (set! current-font "")))
172 (define (cached-fontname i)
175 (make-string 1 (integer->char (+ 65 i)))))
177 (define (select-font font-name magnification)
178 (if (not (equal? font-name current-font))
180 (set! current-font font-name)
181 (define font-cmd (assoc font-name font-alist))
182 (if (eq? font-cmd #f)
184 (set! font-cmd (cached-fontname font-count))
185 (set! font-alist (acons font-name font-cmd font-alist))
186 (set! font-count (+ 1 font-count))
187 (if (equal? font-name "")
188 (error "Empty fontname -- SELECT-FONT"))
189 (if (> magnification 0)
190 (string-append "\\font" font-cmd "=" font-name
192 (number->string magnification) font-cmd)
193 (string-append "\\font" font-cmd "=" font-name font-cmd)))
199 (define (beam width slope thick)
200 (embedded-ps ((ps-scm 'beam) width slope thick)))
203 (embedded-ps ((ps-scm 'bracket) h)))
205 (define (dashed-slur thick dash l)
206 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
208 (define (crescendo thick w h cont)
209 (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
212 (string-append "\\char" (inexact->string i 10) " "))
214 (define (decrescendo thick w h cont)
215 (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
217 ;This sets CTM so that you get to the currentpoint
218 ; by executing a 0 0 moveto
220 (define (embedded-ps s)
221 (string-append "\\embeddedps{" s "}"))
224 "\n\\EndLilyPondOutput")
226 (define (experimental-on)
229 (define (font-switch i)
233 (define (font-def i s)
235 "\\font" (font-switch i) "=" s "\n"))
239 (define (header-end) "\\turnOnPostScript")
241 (define (header creator generate)
243 "%created by: " creator generate "\n"))
245 (define (invoke-char s i)
247 "\n\\" s "{" (inexact->string i 10) "}" ))
249 (define (invoke-dim1 s d)
251 "\n\\" s "{" (number->dim d) "}"))
256 ;; need to do something to make this really safe.
258 (if security-paranoia
259 (define (output-tex-string s)
260 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post))
261 (define (output-tex-string s) s))
263 (define (lily-def key val)
265 "\\def\\" (output-tex-string key) "{" (output-tex-string val) "}\n"))
267 (define (number->dim x)
269 (number->string (chop-decimal x)) " pt "))
271 (define (placebox x y s)
274 (number->dim y) "}{" (number->dim x) "}{" s "}"))
277 (define (pianobrace y staffht)
279 (minht (* 2 staffht))
283 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
284 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
289 (define (rulesym h w)
291 "\\vrule height " (number->dim (/ h 2))
292 " depth " (number->dim (/ h 2))
293 " width " (number->dim w)
297 (define (bezier-sandwich l thick)
298 (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
301 (define (start-line ht)
304 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
307 "}\\vss}\\interscoreline")
308 (define (stop-last-line)
310 (define (filledbox breapth width depth height)
312 "\\kern" (number->dim (- breapth))
313 "\\vrule width " (number->dim (+ breapth width))
314 "depth " (number->dim depth)
315 "height " (number->dim height) " "))
318 (string-append "\\hbox{" (output-tex-string s) "}"))
320 (define (tuplet ht gapx dx dy thick dir)
321 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
323 (define (volta h w thick vert_start vert_end)
324 (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
327 ;; The procedures listed below form the public interface of TeX-scm.
328 ;; (should merge the 2 lists)
329 (cond ((eq? action-name 'all-definitions)
332 (define bezier-sandwich ,bezier-sandwich)
333 (define bracket ,bracket)
335 (define crescendo ,crescendo)
336 (define dashed-slur ,dashed-slur)
337 (define decrescendo ,decrescendo)
338 (define end-output ,end-output)
339 (define experimental-on ,experimental-on)
340 (define filledbox ,filledbox)
341 (define font-def ,font-def)
342 (define font-switch ,font-switch)
343 (define header-end ,header-end)
344 (define lily-def ,lily-def)
345 (define header ,header)
346 (define invoke-char ,invoke-char)
347 (define invoke-dim1 ,invoke-dim1)
348 (define pianobrace ,pianobrace)
349 (define placebox ,placebox)
350 (define rulesym ,rulesym)
351 (define select-font ,select-font)
352 (define start-line ,start-line)
353 (define stop-line ,stop-line)
354 (define stop-last-line ,stop-last-line)
356 (define tuplet ,tuplet)
357 (define volta ,volta)
360 ((eq? action-name 'beam) beam)
361 ((eq? action-name 'tuplet) tuplet)
362 ((eq? action-name 'bracket) bracket)
363 ((eq? action-name 'crescendo) crescendo)
364 ((eq? action-name 'dashed-slur) dashed-slur)
365 ((eq? action-name 'decrescendo) decrescendo)
366 ((eq? action-name 'end-output) end-output)
367 ((eq? action-name 'experimental-on) experimental-on)
368 ((eq? action-name 'font-def) font-def)
369 ((eq? action-name 'font-switch) font-switch)
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 (mag-to-size m)
407 (number->string (case m
410 ('2 14) ; really: 14.400
411 ('3 17) ; really: 17.280
412 ('4 21) ; really: 20.736
413 ('5 24) ; really: 24.888
414 ('6 30) ; really: 29.856
417 (define (select-font font-name magnification)
418 (if (not (equal? font-name current-font))
420 (set! current-font font-name)
421 (define font-cmd (assoc font-name font-alist))
422 (if (eq? font-cmd #f)
424 (set! font-cmd (cached-fontname font-count))
425 (set! font-alist (acons font-name font-cmd font-alist))
426 (set! font-count (+ 1 font-count))
427 (string-append "\n/" font-cmd " {/"
428 font-name " findfont "
429 (mag-to-size magnification)
430 " scalefont setfont} bind def \n"
432 (string-append (cdr font-cmd) " ")))
433 ; font-name == current-font no switch needed
437 (define (beam width slope thick)
439 (numbers->string (list width slope thick)) " draw_beam" ))
442 (invoke-dim1 " draw_bracket" h))
445 (invoke-char " show" i))
447 (define (crescendo thick w h cont )
449 (numbers->string (list w h (inexact->exact cont) thick))
452 (define (dashed-slur thick dash l)
454 (apply string-append (map control->string l))
455 (number->string thick)
457 (if (> 1 dash) (number->string (- (* thick dash) thick)) "0") " "
458 (number->string (* 2 thick))
459 " ] 0 draw_dashed_slur"))
461 (define (decrescendo thick w h cont)
463 (numbers->string (list w h (inexact->exact cont) thick))
464 " draw_decrescendo"))
470 (define (experimental-on) "")
472 (define (filledbox breapth width depth height)
473 (string-append (numbers->string (list breapth width depth height))
477 (define (font-def i s)
480 (substring s 0 (- (string-length s) 4))
481 " findfont 12 scalefont setfont} bind def \n"))
483 (define (font-switch i)
484 (string-append (font i) " "))
486 (define (header-end) "")
487 (define (lily-def key val)
489 (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
490 (string-append "/" key " {" val "} bind def\n")
491 (string-append "/" key " (" val ") def\n")
495 (define (header creator generate)
498 "%%Creator: " creator generate "\n"))
500 (define (invoke-char s i)
502 "(\\" (inexact->string i 8) ") " s " " ))
504 (define (invoke-dim1 s d)
506 (number->string (* d (/ 72.27 72))) " " s ))
508 (define (placebox x y s)
510 (number->string x) " " (number->string y) " {" s "} placebox "))
512 (define (pianobrace y staffht)
514 (minht (* 2 staffht))
518 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
519 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
524 (define (rulesym x y)
526 (number->string x) " "
527 (number->string y) " "
530 (define (bezier-sandwich l thick)
532 (apply string-append (map control->string l))
533 (number->string thick)
534 " draw_bezier_sandwich"))
536 (define (start-line height)
541 (define (stem breapth width depth height)
542 (string-append (numbers->string (list breapth width depth height))
549 (string-append "(" s ") show "))
552 (define (volta h w thick vert_start vert_end)
554 (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
557 (define (tuplet ht gap dx dy thick dir)
559 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
568 (cond ((eq? action-name 'all-definitions)
571 (define tuplet ,tuplet)
572 (define bracket ,bracket)
574 (define crescendo ,crescendo)
575 (define volta ,volta)
576 (define bezier-sandwich ,bezier-sandwich)
577 (define dashed-slur ,dashed-slur)
578 (define decrescendo ,decrescendo)
579 (define end-output ,end-output)
580 (define experimental-on ,experimental-on)
581 (define filledbox ,filledbox)
582 (define font-def ,font-def)
583 (define font-switch ,font-switch)
584 (define pianobrace ,pianobrace)
585 (define header-end ,header-end)
586 (define lily-def ,lily-def)
587 (define header ,header)
588 (define invoke-char ,invoke-char)
589 (define invoke-dim1 ,invoke-dim1)
590 (define placebox ,placebox)
591 (define rulesym ,rulesym)
592 (define select-font ,select-font)
593 (define start-line ,start-line)
595 (define stop-line ,stop-line)
596 (define stop-last-line ,stop-line)
599 ((eq? action-name 'tuplet) tuplet)
600 ((eq? action-name 'beam) beam)
601 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
602 ((eq? action-name 'bracket) bracket)
603 ((eq? action-name 'char) char)
604 ((eq? action-name 'crescendo) crescendo)
605 ((eq? action-name 'dashed-slur) dashed-slur)
606 ((eq? action-name 'decrescendo) decrescendo)
607 ((eq? action-name 'experimental-on) experimental-on)
608 ((eq? action-name 'filledbox) filledbox)
609 ((eq? action-name 'select-font) select-font)
610 ((eq? action-name 'volta) volta)
611 (else (error "unknown tag -- PS-SCM " action-name))
616 ; Russ McManus, <mcmanus@IDT.NET>
618 ; I use the following, which should definitely be provided somewhere
619 ; in guile, but isn't, AFAIK:
623 (define (hash-table-for-each fn ht)
625 ((= i (vector-length ht)))
626 (do ((alist (vector-ref ht i) (cdr alist)))
628 (fn (car (car alist)) (cdr (car alist))))))
630 (define (hash-table-map fn ht)
633 ((= i (vector-length ht)) (reverse ret-ls))
634 (do ((alist (vector-ref ht i) (cdr alist)))
636 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
639 ;;;; print a SCM expression. Isn't this part of the std lib?
641 (define (scmlist->string exp)
643 ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
644 ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
645 (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
648 (define (scm->string exp)
650 ((pair? exp) (string-append "(" (scmlist->string exp)))
651 ((number? exp) (number->string exp))
652 ((symbol? exp) (symbol->string exp))
653 ((string? exp) (string-append "\"" exp "\""))
656 (define (test-scm->string)
657 (list (scmlist->string '(a))
658 (scmlist->string '(a b))
659 (scmlist->string '(a b . c))
663 (scm->string '(a b ))
664 (scm->string '(a b . c))
665 (scm->string '(a b (c . d)))
666 (scm->string '(a "bla" (c . 1.5)))