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)
12 (use-modules (ice-9 regex))
14 ;; do nothing in .scm output
22 (map (lambda (n) (string-append (number->string n ) " ")) l)))
28 (define (cons-map f x)
29 (cons (f (car x)) (f (cdr x))))
31 (define (reduce operator list)
32 (if (null? (cdr list)) (car list)
33 (operator (car list) (reduce operator (cdr list)))
38 (define (glue-2-strings a b) (string-append a " " b))
42 (reduce glue-2-strings (map number->string l)))
44 (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
46 (define (number->octal-string x)
47 (let* ((n (inexact->exact x))
49 (n8 (quotient (- n (* n64 64)) 8)))
53 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
55 (define (inexact->string x radix)
56 (let ((n (inexact->exact x)))
57 (number->string n radix)))
63 (string-append (number->string (car c)) " ")
64 (string-append (number->string (cdr c)) " ")))
70 (make-string 1 (integer->char (+ (char->integer #\A) i)))
75 (define (scm-scm action-name)
78 (define security-paranoia #f)
81 ;; See documentation of Item::visibility_lambda_
82 (define (postbreak-only-visibility d) (if (= d 1) '(#f . #f) '(#t . #t)))
83 (define (spanbar-non-postbreak-visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
84 (define (all-visibility d) '(#f . #f))
85 (define (non-postbreak-visibility d) (if (= d 1) '(#t . #t) '(#f . #f)))
86 (define (non-prebreak-visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
89 ;; Score_span_bars are only visible at start of line
90 ;; i.e. if break_dir == RIGHT == 1
91 (define Span_bar_engraver-visibility non-postbreak-visibility)
92 (define Base_span_bar_engraver-visibility non-postbreak-visibility)
93 (define mark-visibility non-prebreak-visibility)
94 (define Span_score_bar_engraver-visibility postbreak-only-visibility)
95 (define Piano_bar_engraver-visibility postbreak-only-visibility)
96 (define Staff_group_bar_engraver-visibility postbreak-only-visibility)
98 ;; Spacing constants for prefatory matter.
100 ;; rules for this spacing are much more complicated than this. See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147
104 ;; (Measured in interlines? -- jcn)
107 (("" "Clef_item") . (minimum-space 1.0))
108 (("" "Staff_bar") . (minimum-space 0.0))
109 (("" "Clef_item") . (minimum-space 1.0))
110 (("" "Key_item") . (minimum-space 0.5))
111 (("" "Span_bar") . (extra-space 0.0))
112 (("" "Time_signature") . (extra-space 0.0))
113 (("" "begin-of-note") . (minimum-space 1.5))
114 (("Clef_item" "Key_item") . (minimum-space 4.0))
115 (("Key_item" "Time_signature") . (extra-space 1.0))
116 (("Clef_item" "Time_signature") . (minimum-space 3.5))
117 (("Staff_bar" "Clef_item") . (minimum-space 1.0))
118 (("Clef_item" "Staff_bar") . (minimum-space 3.7))
119 (("Time_signature" "Staff_bar") . (minimum-space 2.0))
120 (("Key_item" "Staff_bar") . (extra-space 1.0))
121 (("Span_bar" "Clef_item") . (extra-space 1.0))
122 (("Clef_item" "Span_bar") . (minimum-space 3.7))
123 (("Time_signature" "Span_bar") . (minimum-space 2.0))
124 (("Key_item" "Span_bar") . (minimum-space 2.5))
125 (("Staff_bar" "Time_signature") . (minimum-space 1.5)) ;double check this.
126 (("Time_signature" "begin-of-note") . (extra-space 2.0)) ;double check this.
127 (("Key_item" "begin-of-note") . (extra-space 2.5))
128 (("Staff_bar" "begin-of-note") . (extra-space 1.0))
129 (("Clef_item" "begin-of-note") . (minimum-space 5.0))
130 (("" "Breathing_sign") . (minimum-space 0.0))
131 (("Breathing_sign" "Key_item") . (minimum-space 1.5))
132 (("Breathing_sign" "begin-of-note") . (minimum-space 1.0))
133 (("Breathing_sign" "Staff_bar") . (minimum-space 1.5))
134 (("Breathing_sign" "Clef_item") . (minimum-space 2.0))
138 (define (break-align-spacer this next)
139 (let ((entry (assoc `(,this ,next) space-alist)))
142 (begin (ly-warn (string-append "Unknown spacing pair `" this "', `" next "'"))
143 '(minimum-space 0.0)))))
149 ;; this is silly, can't we use something like
150 ;; roman-0, roman-1 roman+1 ?
153 ("dynamic" . "feta-din")
157 ("finger" . "feta-nummer")
158 ("typewriter" . "cmtt")
164 ("mark" . "feta-nummer")
165 ("number" . "feta-nummer")
166 ("volta" . "feta-nummer"))
169 (define (string-encode-integer i)
172 ((< i 0) (string-append "n" (string-encode-integer (- i))))
174 (make-string 1 (integer->char (+ 65 (modulo i 26))))
175 (string-encode-integer (quotient i 26))
182 (cdr (assoc i '((-4 . 482)
195 (define script-alist '())
196 (define (articulation-to-scriptdef a)
197 (assoc a script-alist)
200 ;; Map style names to TeX font names. Return false if
201 ;; no font name found.
202 (define (style-to-cmr s)
208 (define font-name-alist '())
209 (define (font-command name-mag)
211 (string-append "magfont"
212 (string-encode-integer (hashq (car name-mag) 1000000))
214 (string-encode-integer (cdr name-mag)))
218 (define (define-fonts names)
219 (set! font-name-alist (map font-command names))
222 (font-load-command (car x) (cdr x))) font-name-alist)
226 (define (tex-scm action-name)
231 (define (select-font font-name-symbol)
234 (c (assoc font-name-symbol font-name-alist))
239 (ly-warn (string-append
240 "Programming error: No such font known " (car font-name-symbol)))
241 "") ; issue no command
242 (string-append "\\" (cdr c)))
247 (define (beam width slope thick)
248 (embedded-ps ((ps-scm 'beam) width slope thick)))
250 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
251 (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
253 (define (dashed-slur thick dash l)
254 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
256 (define (crescendo thick w h cont)
257 (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
260 (string-append "\\char" (inexact->string i 10) " "))
262 (define (decrescendo thick w h cont)
263 (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
265 ;This sets CTM so that you get to the currentpoint
266 ; by executing a 0 0 moveto
271 (define (font-load-command name-mag command)
273 "\\font\\" command "="
274 (symbol->string (car name-mag))
276 (number->string (magstep (cdr name-mag)))
280 (define (embedded-ps s)
281 (string-append "\\embeddedps{" s "}"))
284 (string-append "% " s))
287 "\n\\EndLilyPondOutput")
289 (define (experimental-on)
292 (define (font-switch i)
296 (define (font-def i s)
298 "\\font" (font-switch i) "=" s "\n"))
304 ;(ly-gulp-file "lily.ps")
306 (regexp-substitute/global #f "\n" (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
308 "\\input lilyponddefs \\turnOnPostScript"))
310 (define (header creator generate)
312 "%created by: " creator generate))
314 (define (invoke-char s i)
316 "\n\\" s "{" (inexact->string i 10) "}" ))
318 (define (invoke-dim1 s d)
320 "\n\\" s "{" (number->dim d) "}"))
325 ;; need to do something to make this really safe.
327 (define (output-tex-string s)
328 (if security-paranoia
329 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
332 (define (lily-def key val)
336 (regexp-substitute/global #f "_" (output-tex-string key) 'pre "X" 'post)
337 ;(output-tex-string key)
338 "{" (output-tex-string val) "}\n"))
340 (define (number->dim x)
342 (number->string (chop-decimal x)) " pt "))
344 (define (placebox x y s)
347 (number->dim y) "}{" (number->dim x) "}{" s "}\n"))
349 (define (bezier-sandwich l thick)
350 (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
352 (define (start-line ht)
354 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
357 "}\\vss}\\interscoreline")
358 (define (stop-last-line)
360 (define (filledbox breapth width depth height)
362 "\\kern" (number->dim (- breapth))
363 "\\vrule width " (number->dim (+ breapth width))
364 "depth " (number->dim depth)
365 "height " (number->dim height) " "))
368 (string-append "\\hbox{" (output-tex-string s) "}"))
370 (define (tuplet ht gapx dx dy thick dir)
371 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
373 (define (volta h w thick vert_start vert_end)
374 (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
377 ;; The procedures listed below form the public interface of TeX-scm.
378 ;; (should merge the 2 lists)
379 (cond ((eq? action-name 'all-definitions)
381 (define font-load-command ,font-load-command)
383 (define bezier-sandwich ,bezier-sandwich)
384 (define bracket ,bracket)
386 (define crescendo ,crescendo)
387 (define dashed-slur ,dashed-slur)
388 (define decrescendo ,decrescendo)
389 (define end-output ,end-output)
390 (define experimental-on ,experimental-on)
391 (define filledbox ,filledbox)
392 (define font-def ,font-def)
393 (define font-switch ,font-switch)
394 (define header-end ,header-end)
395 (define lily-def ,lily-def)
396 (define header ,header)
397 (define invoke-char ,invoke-char)
398 (define invoke-dim1 ,invoke-dim1)
399 (define placebox ,placebox)
400 (define select-font ,select-font)
401 (define start-line ,start-line)
402 (define stop-line ,stop-line)
403 (define stop-last-line ,stop-last-line)
405 (define tuplet ,tuplet)
406 (define volta ,volta)
409 ((eq? action-name 'beam) beam)
410 ((eq? action-name 'tuplet) tuplet)
411 ((eq? action-name 'bracket) bracket)
412 ((eq? action-name 'crescendo) crescendo)
413 ((eq? action-name 'dashed-slur) dashed-slur)
414 ((eq? action-name 'decrescendo) decrescendo)
415 ((eq? action-name 'end-output) end-output)
416 ((eq? action-name 'experimental-on) experimental-on)
417 ((eq? action-name 'font-def) font-def)
418 ((eq? action-name 'font-switch) font-switch)
419 ((eq? action-name 'header-end) header-end)
420 ((eq? action-name 'lily-def) lily-def)
421 ((eq? action-name 'header) header)
422 ((eq? action-name 'invoke-char) invoke-char)
423 ((eq? action-name 'invoke-dim1) invoke-dim1)
424 ((eq? action-name 'placebox) placebox)
425 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
426 ((eq? action-name 'start-line) start-line)
427 ((eq? action-name 'stem) stem)
428 ((eq? action-name 'stop-line) stop-line)
429 ((eq? action-name 'stop-last-line) stop-last-line)
430 ((eq? action-name 'volta) volta)
431 (else (error "unknown tag -- PS-TEX " action-name))
437 (define (ps-scm action-name)
440 ;; alist containing fontname -> fontcommand assoc (both strings)
441 (define font-alist '())
442 (define font-count 0)
443 (define current-font "")
446 (define (cached-fontname i)
449 (make-string 1 (integer->char (+ 65 i)))))
451 (define (mag-to-size m)
452 (number->string (case m
455 (2 14) ; really: 14.400
456 (3 17) ; really: 17.280
457 (4 21) ; really: 20.736
458 (5 24) ; really: 24.888
459 (6 30) ; really: 29.856
463 (define (select-font font-name-symbol)
466 (c (assoc font-name-symbol font-name-alist))
471 (ly-warn (string-append
472 "Programming error: No such font known " (car font-name-symbol)))
473 "") ; issue no command
474 (string-append " " (cdr c) " "))
479 (define (font-load-command name-mag command)
483 (symbol->string (car name-mag))
485 (number->string (magstep (cdr name-mag)))
486 " 1000 div 12 mul scalefont setfont } bind def "
490 (define (beam width slope thick)
492 (numbers->string (list width slope thick)) " draw_beam" ))
495 (string-append "% " s))
497 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
499 (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
502 (invoke-char " show" i))
504 (define (crescendo thick w h cont )
506 (numbers->string (list w h (inexact->exact cont) thick))
509 ;; what the heck is this interface ?
510 (define (dashed-slur thick dash l)
512 (apply string-append (map control->string l))
513 (number->string thick)
515 (number->string dash)
517 (number->string (* 10 thick)) ;UGH. 10 ?
518 " ] 0 draw_dashed_slur"))
520 (define (decrescendo thick w h cont)
522 (numbers->string (list w h (inexact->exact cont) thick))
523 " draw_decrescendo"))
529 (define (experimental-on) "")
531 (define (filledbox breapth width depth height)
532 (string-append (numbers->string (list breapth width depth height))
536 (define (font-def i s)
539 (substring s 0 (- (string-length s) 4))
540 " findfont 12 scalefont setfont} bind def \n"))
542 (define (font-switch i)
543 (string-append (font i) " "))
547 (ly-gulp-file "lilyponddefs.ps")
548 " {exch pop //systemdict /run get exec} "
549 (ly-gulp-file "lily.ps")
550 "{ exch pop //systemdict /run get exec } "
553 (define (lily-def key val)
555 (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
556 (string-append "/" key " {" val "} bind def\n")
557 (string-append "/" key " (" val ") def\n")
561 (define (header creator generate)
564 "%%Creator: " creator generate "\n"))
566 (define (invoke-char s i)
568 "(\\" (inexact->string i 8) ") " s " " ))
570 (define (invoke-dim1 s d)
572 (number->string (* d (/ 72.27 72))) " " s ))
574 (define (placebox x y s)
576 (number->string x) " " (number->string y) " {" s "} placebox "))
578 (define (bezier-sandwich l thick)
580 (apply string-append (map control->string l))
581 (number->string thick)
582 " draw_bezier_sandwich"))
584 (define (start-line height)
588 (define (stem breapth width depth height)
589 (string-append (numbers->string (list breapth width depth height))
596 (string-append "(" s ") show "))
599 (define (volta h w thick vert_start vert_end)
601 (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
604 (define (tuplet ht gap dx dy thick dir)
606 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
615 (cond ((eq? action-name 'all-definitions)
618 (define tuplet ,tuplet)
619 (define bracket ,bracket)
621 (define crescendo ,crescendo)
622 (define volta ,volta)
623 (define bezier-sandwich ,bezier-sandwich)
624 (define dashed-slur ,dashed-slur)
625 (define decrescendo ,decrescendo)
626 (define end-output ,end-output)
627 (define experimental-on ,experimental-on)
628 (define filledbox ,filledbox)
629 (define font-def ,font-def)
630 (define font-switch ,font-switch)
631 (define header-end ,header-end)
632 (define lily-def ,lily-def)
633 (define font-load-command ,font-load-command)
634 (define header ,header)
635 (define invoke-char ,invoke-char)
636 (define invoke-dim1 ,invoke-dim1)
637 (define placebox ,placebox)
638 (define select-font ,select-font)
639 (define start-line ,start-line)
641 (define stop-line ,stop-line)
642 (define stop-last-line ,stop-line)
645 ((eq? action-name 'tuplet) tuplet)
646 ((eq? action-name 'beam) beam)
647 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
648 ((eq? action-name 'bracket) bracket)
649 ((eq? action-name 'char) char)
650 ((eq? action-name 'crescendo) crescendo)
651 ((eq? action-name 'dashed-slur) dashed-slur)
652 ((eq? action-name 'decrescendo) decrescendo)
653 ((eq? action-name 'experimental-on) experimental-on)
654 ((eq? action-name 'filledbox) filledbox)
655 ((eq? action-name 'select-font) select-font)
656 ((eq? action-name 'volta) volta)
657 (else (error "unknown tag -- PS-SCM " action-name))
662 (define (gulp-file name)
663 (let* ((port (open-file name "r"))
664 (content (let loop ((text ""))
665 (let ((line (read-line port)))
666 (if (or (eof-object? line)
669 (loop (string-append text line "\n")))))))
673 (define (scm-gulp-file name)
676 (getenv 'LILYPONDPREFIX) "/ps") %load-path))
677 (let ((path (%search-load-path name)))
682 (define (scm-tex-output)
683 (eval (tex-scm 'all-definitions)))
685 (define (scm-ps-output)
686 (eval (ps-scm 'all-definitions)))
689 ; Russ McManus, <mcmanus@IDT.NET>
691 ; I use the following, which should definitely be provided somewhere
692 ; in guile, but isn't, AFAIK:
696 (define (hash-table-for-each fn ht)
698 ((= i (vector-length ht)))
699 (do ((alist (vector-ref ht i) (cdr alist)))
701 (fn (car (car alist)) (cdr (car alist))))))
703 (define (hash-table-map fn ht)
706 ((= i (vector-length ht)) (reverse ret-ls))
707 (do ((alist (vector-ref ht i) (cdr alist)))
709 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
712 ;; guile-1.3.4 has list->string
713 (define (scmlist->string exp)
716 ;; obsolete, maybe handy for testing
717 ;; print a SCM expression. Isn't this part of the std lib?
718 (define (xxscmlist->string exp)
720 ((null? (car exp)) (begin (display ("urg:") (newline))))
721 ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
722 ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
723 ;; howto check for quote?
724 (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
727 (define (scm->string exp)
729 ((pair? exp) (string-append "(" (scmlist->string exp)))
730 ((number? exp) (number->string exp))
731 ((symbol? exp) (symbol->string exp))
732 ((string? exp) (string-append "\"" exp "\""))
734 (else (begin (display "programming error: scm->string: ") (newline) "'"))
737 (define (index-cell cell dir)
745 ; How should a bar line behave at a break?
747 (define (break-barline glyph dir)
748 (let ((result (assoc glyph
749 '((":|:" . (":|" . "|:"))
752 ("|:" . ("|" . "|:"))
753 ("|." . ("|." . nil))
754 (":|" . (":|" . nil))
755 ("||" . ("||" . nil))
756 (".|." . (".|." . nil))
757 ("scorebar" . (nil . "scorepostbreak"))
758 ("brace" . (nil . "brace"))
759 ("bracket" . (nil . "bracket"))
763 (if (equal? result #f)
764 (ly-warn (string-append "Unknown bar glyph: `" glyph "'"))
765 (index-cell (cdr result) dir))
770 (define (slur-ugly ind ht)
775 (> ht (+ (* 2 ind) -4))
776 (< ht (+ (* -2 ind) 8)))