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
15 (define (comment s) "")
21 (define (cons-map f x)
22 (cons (f (car x)) (f (cdr x))))
24 (define (reduce operator list)
25 (if (null? (cdr list)) (car list)
26 (operator (car list) (reduce operator (cdr list)))
30 (define (glue-2-strings a b)
31 (string-append a " " b))
33 (define (numbers->string l)
34 (reduce glue-2-strings (map number->string l)))
36 (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
38 (define (number->octal-string x)
39 (let* ((n (inexact->exact x))
41 (n8 (quotient (- n (* n64 64)) 8)))
45 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
47 (define (inexact->string x radix)
48 (let ((n (inexact->exact x)))
49 (number->string n radix)))
55 (string-append (number->string (car c)) " ")
56 (string-append (number->string (cdr c)) " ")))
62 (make-string 1 (integer->char (+ (char->integer #\A) i)))
67 (define (scm-scm action-name)
70 (define security-paranoia #f)
73 ;; See documentation of Item::visibility_lambda_
74 (define (postbreak-only-visibility d) (if (= d 1) '(#f . #f) '(#t . #t)))
75 (define (spanbar-non-postbreak-visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
76 (define (all-visibility d) '(#f . #f))
77 (define (non-postbreak-visibility d) (if (= d 1) '(#t . #t) '(#f . #f)))
78 (define (non-prebreak-visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
81 ;; Score_span_bars are only visible at start of line
82 ;; i.e. if break_dir == RIGHT == 1
83 (define Span_bar_engraver-visibility non-postbreak-visibility)
84 (define Base_span_bar_engraver-visibility non-postbreak-visibility)
85 (define mark-visibility non-prebreak-visibility)
86 (define Span_score_bar_engraver-visibility postbreak-only-visibility)
87 (define Piano_bar_engraver-visibility postbreak-only-visibility)
88 (define Staff_group_bar_engraver-visibility postbreak-only-visibility)
90 ;; Spacing constants for prefatory matter.
92 ;; rules for this spacing are much more complicated than this. See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147
96 ;; (Measured in interlines? -- jcn)
99 (("" "Clef_item") . (minimum-space 1.0))
100 (("" "Staff_bar") . (minimum-space 0.0))
101 (("" "Clef_item") . (minimum-space 1.0))
102 (("" "Key_item") . (minimum-space 0.5))
103 (("" "Span_bar") . (extra-space 0.0))
104 (("" "Time_signature") . (extra-space 0.0))
105 (("" "begin-of-note") . (minimum-space 1.5))
106 (("Clef_item" "Key_item") . (minimum-space 4.0))
107 (("Key_item" "Time_signature") . (extra-space 1.0))
108 (("Clef_item" "Time_signature") . (minimum-space 3.5))
109 (("Staff_bar" "Clef_item") . (minimum-space 1.0))
110 (("Clef_item" "Staff_bar") . (minimum-space 3.7))
111 (("Time_signature" "Staff_bar") . (minimum-space 2.0))
112 (("Key_item" "Staff_bar") . (extra-space 1.0))
113 (("Span_bar" "Clef_item") . (extra-space 1.0))
114 (("Clef_item" "Span_bar") . (minimum-space 3.7))
115 (("Time_signature" "Span_bar") . (minimum-space 2.0))
116 (("Key_item" "Span_bar") . (minimum-space 2.5))
117 (("Staff_bar" "Time_signature") . (minimum-space 1.5)) ;double check this.
118 (("Time_signature" "begin-of-note") . (extra-space 2.0)) ;double check this.
119 (("Key_item" "begin-of-note") . (extra-space 2.5))
120 (("Staff_bar" "begin-of-note") . (extra-space 1.0))
121 (("Clef_item" "begin-of-note") . (minimum-space 5.0))
122 (("" "Breathing_sign") . (minimum-space 0.0))
123 (("Breathing_sign" "Key_item") . (minimum-space 1.5))
124 (("Breathing_sign" "begin-of-note") . (minimum-space 1.0))
125 (("Breathing_sign" "Staff_bar") . (minimum-space 1.5))
126 (("Breathing_sign" "Clef_item") . (minimum-space 2.0))
130 (define (break-align-spacer this next)
131 (let ((entry (assoc `(,this ,next) space-alist)))
134 (begin (ly-warn (string-append "Unknown spacing pair `" this "', `" next "'"))
135 '(minimum-space 0.0)))))
141 ;; this is silly, can't we use something like
142 ;; roman-0, roman-1 roman+1 ?
145 ("brace" . "feta-braces")
146 ("dynamic" . "feta-din")
150 ("finger" . "feta-nummer")
151 ("typewriter" . "cmtt")
157 ("mark" . "feta-nummer")
158 ("number" . "feta-nummer")
159 ("volta" . "feta-nummer"))
162 (define (string-encode-integer i)
165 ((< i 0) (string-append "n" (string-encode-integer (- i))))
167 (make-string 1 (integer->char (+ 65 (modulo i 26))))
168 (string-encode-integer (quotient i 26))
175 (cdr (assoc i '((-4 . 482)
188 (define script-alist '())
189 (define (articulation-to-scriptdef a)
190 (assoc a script-alist)
193 ;; Map style names to TeX font names. Return false if
194 ;; no font name found.
195 (define (style-to-cmr s)
201 (define font-name-alist '())
202 (define (font-command name-mag)
204 (string-append "magfont"
205 (string-encode-integer (hashq (car name-mag) 1000000))
207 (string-encode-integer (cdr name-mag)))
211 (define (define-fonts names)
212 (set! font-name-alist (map font-command names))
215 (font-load-command (car x) (cdr x))) font-name-alist)
219 (define (tex-scm action-name)
224 (define (select-font font-name-symbol)
227 (c (assoc font-name-symbol font-name-alist))
232 (ly-warn (string-append
233 "Programming error: No such font known " (car font-name-symbol)))
234 "") ; issue no command
235 (string-append "\\" (cdr c)))
240 (define (beam width slope thick)
241 (embedded-ps ((ps-scm 'beam) width slope thick)))
243 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
244 (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
246 (define (dashed-slur thick dash l)
247 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
249 (define (crescendo thick w h cont)
250 (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
253 (string-append "\\char" (inexact->string i 10) " "))
255 (define (decrescendo thick w h cont)
256 (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
258 ;This sets CTM so that you get to the currentpoint
259 ; by executing a 0 0 moveto
264 (define (font-load-command name-mag command)
266 "\\font\\" command "="
267 (symbol->string (car name-mag))
269 (number->string (magstep (cdr name-mag)))
273 (define (embedded-ps s)
274 (string-append "\\embeddedps{" s "}"))
277 (string-append "% " s))
280 "\n\\EndLilyPondOutput")
282 (define (experimental-on)
285 (define (font-switch i)
289 (define (font-def i s)
291 "\\font" (font-switch i) "=" s "\n"))
297 ;(ly-gulp-file "lily.ps")
299 (regexp-substitute/global #f "\n" (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
301 "\\input lilyponddefs \\turnOnPostScript"))
303 (define (header creator generate)
305 "%created by: " creator generate "\n"))
307 (define (invoke-char s i)
309 "\n\\" s "{" (inexact->string i 10) "}" ))
311 (define (invoke-dim1 s d)
313 "\n\\" s "{" (number->dim d) "}"))
318 ;; need to do something to make this really safe.
320 (define (output-tex-string s)
321 (if security-paranoia
322 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
325 (define (lily-def key val)
329 (regexp-substitute/global #f "_" (output-tex-string key) 'pre "X" 'post)
330 ;(output-tex-string key)
331 "{" (output-tex-string val) "}\n"))
333 (define (number->dim x)
335 (number->string (chop-decimal x)) " pt "))
337 (define (placebox x y s)
340 (number->dim y) "}{" (number->dim x) "}{" s "}\n"))
342 (define (bezier-sandwich l thick)
343 (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
345 (define (start-line ht)
346 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
349 "}\\vss}\\interscoreline")
350 (define (stop-last-line)
352 (define (filledbox breapth width depth height)
354 "\\kern" (number->dim (- breapth))
355 "\\vrule width " (number->dim (+ breapth width))
356 "depth " (number->dim depth)
357 "height " (number->dim height) " "))
360 (string-append "\\hbox{" (output-tex-string s) "}"))
362 (define (tuplet ht gapx dx dy thick dir)
363 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
365 (define (volta h w thick vert_start vert_end)
366 (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
369 ;; The procedures listed below form the public interface of TeX-scm.
370 ;; (should merge the 2 lists)
371 (cond ((eq? action-name 'all-definitions)
373 (define font-load-command ,font-load-command)
375 (define bezier-sandwich ,bezier-sandwich)
376 (define bracket ,bracket)
378 (define crescendo ,crescendo)
379 (define dashed-slur ,dashed-slur)
380 (define decrescendo ,decrescendo)
381 (define end-output ,end-output)
382 (define experimental-on ,experimental-on)
383 (define filledbox ,filledbox)
384 (define font-def ,font-def)
385 (define font-switch ,font-switch)
386 (define header-end ,header-end)
387 (define lily-def ,lily-def)
388 (define header ,header)
389 (define invoke-char ,invoke-char)
390 (define invoke-dim1 ,invoke-dim1)
391 (define placebox ,placebox)
392 (define select-font ,select-font)
393 (define start-line ,start-line)
394 (define stop-line ,stop-line)
395 (define stop-last-line ,stop-last-line)
397 (define tuplet ,tuplet)
398 (define volta ,volta)
401 ((eq? action-name 'beam) beam)
402 ((eq? action-name 'tuplet) tuplet)
403 ((eq? action-name 'bracket) bracket)
404 ((eq? action-name 'crescendo) crescendo)
405 ((eq? action-name 'dashed-slur) dashed-slur)
406 ((eq? action-name 'decrescendo) decrescendo)
407 ((eq? action-name 'end-output) end-output)
408 ((eq? action-name 'experimental-on) experimental-on)
409 ((eq? action-name 'font-def) font-def)
410 ((eq? action-name 'font-switch) font-switch)
411 ((eq? action-name 'header-end) header-end)
412 ((eq? action-name 'lily-def) lily-def)
413 ((eq? action-name 'header) header)
414 ((eq? action-name 'invoke-char) invoke-char)
415 ((eq? action-name 'invoke-dim1) invoke-dim1)
416 ((eq? action-name 'placebox) placebox)
417 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
418 ((eq? action-name 'start-line) start-line)
419 ((eq? action-name 'stem) stem)
420 ((eq? action-name 'stop-line) stop-line)
421 ((eq? action-name 'stop-last-line) stop-last-line)
422 ((eq? action-name 'volta) volta)
423 (else (error "unknown tag -- PS-TEX " action-name))
429 (define (ps-scm action-name)
432 ;; alist containing fontname -> fontcommand assoc (both strings)
433 (define font-alist '())
434 (define font-count 0)
435 (define current-font "")
438 (define (cached-fontname i)
441 (make-string 1 (integer->char (+ 65 i)))))
443 (define (mag-to-size m)
444 (number->string (case m
447 (2 14) ; really: 14.400
448 (3 17) ; really: 17.280
449 (4 21) ; really: 20.736
450 (5 24) ; really: 24.888
451 (6 30) ; really: 29.856
455 (define (select-font font-name-symbol)
458 (c (assoc font-name-symbol font-name-alist))
463 (ly-warn (string-append
464 "Programming error: No such font known " (car font-name-symbol)))
465 "") ; issue no command
466 (string-append " " (cdr c) " "))
471 (define (font-load-command name-mag command)
475 (symbol->string (car name-mag))
477 (number->string (magstep (cdr name-mag)))
478 " 1000 div 12 mul scalefont setfont } bind def "
482 (define (beam width slope thick)
484 (numbers->string (list width slope thick)) " draw_beam" ))
487 (string-append "% " s))
489 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
491 (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
494 (invoke-char " show" i))
496 (define (crescendo thick w h cont )
498 (numbers->string (list w h (inexact->exact cont) thick))
501 ;; what the heck is this interface ?
502 (define (dashed-slur thick dash l)
504 (apply string-append (map control->string l))
505 (number->string thick)
507 (number->string dash)
509 (number->string (* 10 thick)) ;UGH. 10 ?
510 " ] 0 draw_dashed_slur"))
512 (define (decrescendo thick w h cont)
514 (numbers->string (list w h (inexact->exact cont) thick))
515 " draw_decrescendo"))
521 (define (experimental-on) "")
523 (define (filledbox breapth width depth height)
524 (string-append (numbers->string (list breapth width depth height))
528 (define (font-def i s)
531 (substring s 0 (- (string-length s) 4))
532 " findfont 12 scalefont setfont} bind def \n"))
534 (define (font-switch i)
535 (string-append (font i) " "))
539 (ly-gulp-file "lilyponddefs.ps")
540 " {exch pop //systemdict /run get exec} "
541 (ly-gulp-file "lily.ps")
542 "{ exch pop //systemdict /run get exec } "
545 (define (lily-def key val)
547 (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
548 (string-append "/" key " {" val "} bind def\n")
549 (string-append "/" key " (" val ") def\n")
553 (define (header creator generate)
556 "%%Creator: " creator generate "\n"))
558 (define (invoke-char s i)
560 "(\\" (inexact->string i 8) ") " s " " ))
562 (define (invoke-dim1 s d)
564 (number->string (* d (/ 72.27 72))) " " s ))
566 (define (placebox x y s)
568 (number->string x) " " (number->string y) " {" s "} placebox "))
570 (define (bezier-sandwich l thick)
572 (apply string-append (map control->string l))
573 (number->string thick)
574 " draw_bezier_sandwich"))
576 (define (start-line height)
579 (define (stem breapth width depth height)
580 (string-append (numbers->string (list breapth width depth height))
587 (string-append "(" s ") show "))
590 (define (volta h w thick vert_start vert_end)
592 (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
595 (define (tuplet ht gap dx dy thick dir)
597 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
606 (cond ((eq? action-name 'all-definitions)
609 (define tuplet ,tuplet)
610 (define bracket ,bracket)
612 (define crescendo ,crescendo)
613 (define volta ,volta)
614 (define bezier-sandwich ,bezier-sandwich)
615 (define dashed-slur ,dashed-slur)
616 (define decrescendo ,decrescendo)
617 (define end-output ,end-output)
618 (define experimental-on ,experimental-on)
619 (define filledbox ,filledbox)
620 (define font-def ,font-def)
621 (define font-switch ,font-switch)
622 (define header-end ,header-end)
623 (define lily-def ,lily-def)
624 (define font-load-command ,font-load-command)
625 (define header ,header)
626 (define invoke-char ,invoke-char)
627 (define invoke-dim1 ,invoke-dim1)
628 (define placebox ,placebox)
629 (define select-font ,select-font)
630 (define start-line ,start-line)
632 (define stop-line ,stop-line)
633 (define stop-last-line ,stop-line)
636 ((eq? action-name 'tuplet) tuplet)
637 ((eq? action-name 'beam) beam)
638 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
639 ((eq? action-name 'bracket) bracket)
640 ((eq? action-name 'char) char)
641 ((eq? action-name 'crescendo) crescendo)
642 ((eq? action-name 'dashed-slur) dashed-slur)
643 ((eq? action-name 'decrescendo) decrescendo)
644 ((eq? action-name 'experimental-on) experimental-on)
645 ((eq? action-name 'filledbox) filledbox)
646 ((eq? action-name 'select-font) select-font)
647 ((eq? action-name 'volta) volta)
648 (else (error "unknown tag -- PS-SCM " action-name))
653 (define (arg->string arg)
654 (cond ((number? arg) (inexact->string arg 10))
655 ((string? arg) (string-append "\"" arg "\""))
656 ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
658 (define (func name . args)
664 (map (lambda (x) (string-append " " (arg->string x))) args)))
669 (define (as-scm action-name)
671 (define (beam width slope thick)
673 (func "set-line-char" "#")
674 (func "rline-to" width (* width slope))
677 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
679 (func "rmove-to" (+ width 1) (- (/ height -2) 1))
681 (func "set-line-char" "|")
682 (func "rmove-to" 0 1)
683 (func "v-line" (+ height 1))
684 (func "rmove-to" 0 (+ height 1))
694 (define (experimental-on)
697 (define (filledbox breapth width depth height)
698 (let ((dx (+ width breapth))
699 (dy (+ depth height)))
701 (func "rmove-to" (* -1 breapth) (* -1 depth))
704 (func "set-line-char"
705 (if (<= dx 1) "|" "#"))
708 (func "set-line-char"
709 (if (<= dy 1) "-" "="))
710 (func "h-line" dx))))))
712 (define (font-load-command name-mag command)
713 (func "load-font" (car name-mag) (magstep (cdr name-mag))))
715 (define (header creator generate)
716 (func "header" creator generate))
721 (define (lily-def key val)
722 (string-append "(define " key " " (arg->string val) ")\n"))
724 (define (placebox x y s)
725 (string-append (func "move-to" x y) s))
727 (define (select-font font-name-symbol)
728 (let* ((c (assoc font-name-symbol font-name-alist)))
733 "Programming error: No such font known "
734 (car font-name-symbol)))
735 "") ; issue no command
736 (func "select-font" (car font-name-symbol)))))
738 (define (start-line height)
739 (func "start-line" height))
747 ; (define (volta h w thick vert_start vert_end)
748 ; (func "draw-volta" h w thick vert_start vert_end))
750 (define (volta h w thick vert-start vert-end)
752 (if #t ;(= 0 vert-start)
756 ;(func "rmove-to" 0 h)
758 (if #t ;(= 0 vert-end)
761 (func "rmove-to" w 0)
762 (func "v-line" (* -1 h))))))
764 (cond ((eq? action-name 'all-definitions)
767 (define bracket ,bracket)
769 ;;(define crescendo ,crescendo)
770 ;(define bezier-sandwich ,bezier-sandwich)
771 ;;(define dashed-slur ,dashed-slur)
772 ;;(define decrescendo ,decrescendo)
773 (define end-output ,end-output)
774 (define experimental-on ,experimental-on)
775 (define filledbox ,filledbox)
776 ;;(define font-def ,font-def)
777 (define font-load-command ,font-load-command)
778 ;;(define font-switch ,font-switch)
779 (define header ,header)
780 (define header-end ,header-end)
781 (define lily-def ,lily-def)
782 ;;(define invoke-char ,invoke-char)
783 ;;(define invoke-dim1 ,invoke-dim1)
784 (define placebox ,placebox)
785 (define select-font ,select-font)
786 (define start-line ,start-line)
787 ;;(define stem ,stem)
788 (define stop-line ,stop-line)
789 (define stop-last-line ,stop-line)
791 ;;(define tuplet ,tuplet)
792 (define volta ,volta)
794 ;;((eq? action-name 'tuplet) tuplet)
795 ;;((eq? action-name 'beam) beam)
796 ;;((eq? action-name 'bezier-sandwich) bezier-sandwich)
797 ;;((eq? action-name 'bracket) bracket)
798 ((eq? action-name 'char) char)
799 ;;((eq? action-name 'crescendo) crescendo)
800 ;;((eq? action-name 'dashed-slur) dashed-slur)
801 ;;((eq? action-name 'decrescendo) decrescendo)
802 ;;((eq? action-name 'experimental-on) experimental-on)
803 ((eq? action-name 'filledbox) filledbox)
804 ((eq? action-name 'select-font) select-font)
805 ;;((eq? action-name 'volta) volta)
806 (else (error "unknown tag -- MUSA-SCM " action-name))
811 (define (gulp-file name)
812 (let* ((port (open-file name "r"))
813 (content (let loop ((text ""))
814 (let ((line (read-line port)))
815 (if (or (eof-object? line)
818 (loop (string-append text line "\n")))))))
822 (define (scm-gulp-file name)
825 (getenv 'LILYPONDPREFIX) "/ps") %load-path))
826 (let ((path (%search-load-path name)))
831 (define (scm-tex-output)
832 (eval (tex-scm 'all-definitions)))
834 (define (scm-ps-output)
835 (eval (ps-scm 'all-definitions)))
837 (define (scm-as-output)
838 (eval (as-scm 'all-definitions)))
840 ; Russ McManus, <mcmanus@IDT.NET>
842 ; I use the following, which should definitely be provided somewhere
843 ; in guile, but isn't, AFAIK:
847 (define (hash-table-for-each fn ht)
849 ((= i (vector-length ht)))
850 (do ((alist (vector-ref ht i) (cdr alist)))
852 (fn (car (car alist)) (cdr (car alist))))))
854 (define (hash-table-map fn ht)
857 ((= i (vector-length ht)) (reverse ret-ls))
858 (do ((alist (vector-ref ht i) (cdr alist)))
860 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
864 (define (index-cell cell dir)
870 ; How should a bar line behave at a break?
872 (define (break-barline glyph dir)
873 (let ((result (assoc glyph
874 '((":|:" . (":|" . "|:"))
877 ("|:" . ("|" . "|:"))
878 ("|." . ("|." . nil))
879 (":|" . (":|" . nil))
880 ("||" . ("||" . nil))
881 (".|." . (".|." . nil))
882 ("scorebar" . (nil . "scorepostbreak"))
883 ("brace" . (nil . "brace"))
884 ("bracket" . (nil . "bracket"))
888 (if (equal? result #f)
889 (ly-warn (string-append "Unknown bar glyph: `" glyph "'"))
890 (index-cell (cdr result) dir))
895 (define (slur-ugly ind ht)
900 (> ht (+ (* 2 ind) -4))
901 (< ht (+ (* -2 ind) 8)))