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 "\n"))
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)
353 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
356 "}\\vss}\\interscoreline")
357 (define (stop-last-line)
359 (define (filledbox breapth width depth height)
361 "\\kern" (number->dim (- breapth))
362 "\\vrule width " (number->dim (+ breapth width))
363 "depth " (number->dim depth)
364 "height " (number->dim height) " "))
367 (string-append "\\hbox{" (output-tex-string s) "}"))
369 (define (tuplet ht gapx dx dy thick dir)
370 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
372 (define (volta h w thick vert_start vert_end)
373 (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
376 ;; The procedures listed below form the public interface of TeX-scm.
377 ;; (should merge the 2 lists)
378 (cond ((eq? action-name 'all-definitions)
380 (define font-load-command ,font-load-command)
382 (define bezier-sandwich ,bezier-sandwich)
383 (define bracket ,bracket)
385 (define crescendo ,crescendo)
386 (define dashed-slur ,dashed-slur)
387 (define decrescendo ,decrescendo)
388 (define end-output ,end-output)
389 (define experimental-on ,experimental-on)
390 (define filledbox ,filledbox)
391 (define font-def ,font-def)
392 (define font-switch ,font-switch)
393 (define header-end ,header-end)
394 (define lily-def ,lily-def)
395 (define header ,header)
396 (define invoke-char ,invoke-char)
397 (define invoke-dim1 ,invoke-dim1)
398 (define placebox ,placebox)
399 (define select-font ,select-font)
400 (define start-line ,start-line)
401 (define stop-line ,stop-line)
402 (define stop-last-line ,stop-last-line)
404 (define tuplet ,tuplet)
405 (define volta ,volta)
408 ((eq? action-name 'beam) beam)
409 ((eq? action-name 'tuplet) tuplet)
410 ((eq? action-name 'bracket) bracket)
411 ((eq? action-name 'crescendo) crescendo)
412 ((eq? action-name 'dashed-slur) dashed-slur)
413 ((eq? action-name 'decrescendo) decrescendo)
414 ((eq? action-name 'end-output) end-output)
415 ((eq? action-name 'experimental-on) experimental-on)
416 ((eq? action-name 'font-def) font-def)
417 ((eq? action-name 'font-switch) font-switch)
418 ((eq? action-name 'header-end) header-end)
419 ((eq? action-name 'lily-def) lily-def)
420 ((eq? action-name 'header) header)
421 ((eq? action-name 'invoke-char) invoke-char)
422 ((eq? action-name 'invoke-dim1) invoke-dim1)
423 ((eq? action-name 'placebox) placebox)
424 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
425 ((eq? action-name 'start-line) start-line)
426 ((eq? action-name 'stem) stem)
427 ((eq? action-name 'stop-line) stop-line)
428 ((eq? action-name 'stop-last-line) stop-last-line)
429 ((eq? action-name 'volta) volta)
430 (else (error "unknown tag -- PS-TEX " action-name))
436 (define (ps-scm action-name)
439 ;; alist containing fontname -> fontcommand assoc (both strings)
440 (define font-alist '())
441 (define font-count 0)
442 (define current-font "")
445 (define (cached-fontname i)
448 (make-string 1 (integer->char (+ 65 i)))))
450 (define (mag-to-size m)
451 (number->string (case m
454 (2 14) ; really: 14.400
455 (3 17) ; really: 17.280
456 (4 21) ; really: 20.736
457 (5 24) ; really: 24.888
458 (6 30) ; really: 29.856
462 (define (select-font font-name-symbol)
465 (c (assoc font-name-symbol font-name-alist))
470 (ly-warn (string-append
471 "Programming error: No such font known " (car font-name-symbol)))
472 "") ; issue no command
473 (string-append " " (cdr c) " "))
478 (define (font-load-command name-mag command)
482 (symbol->string (car name-mag))
484 (number->string (magstep (cdr name-mag)))
485 " 1000 div 12 mul scalefont setfont } bind def "
489 (define (beam width slope thick)
491 (numbers->string (list width slope thick)) " draw_beam" ))
494 (string-append "% " s))
496 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
498 (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
501 (invoke-char " show" i))
503 (define (crescendo thick w h cont )
505 (numbers->string (list w h (inexact->exact cont) thick))
508 ;; what the heck is this interface ?
509 (define (dashed-slur thick dash l)
511 (apply string-append (map control->string l))
512 (number->string thick)
514 (number->string dash)
516 (number->string (* 10 thick)) ;UGH. 10 ?
517 " ] 0 draw_dashed_slur"))
519 (define (decrescendo thick w h cont)
521 (numbers->string (list w h (inexact->exact cont) thick))
522 " draw_decrescendo"))
528 (define (experimental-on) "")
530 (define (filledbox breapth width depth height)
531 (string-append (numbers->string (list breapth width depth height))
535 (define (font-def i s)
538 (substring s 0 (- (string-length s) 4))
539 " findfont 12 scalefont setfont} bind def \n"))
541 (define (font-switch i)
542 (string-append (font i) " "))
546 (ly-gulp-file "lilyponddefs.ps")
547 " {exch pop //systemdict /run get exec} "
548 (ly-gulp-file "lily.ps")
549 "{ exch pop //systemdict /run get exec } "
552 (define (lily-def key val)
554 (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
555 (string-append "/" key " {" val "} bind def\n")
556 (string-append "/" key " (" val ") def\n")
560 (define (header creator generate)
563 "%%Creator: " creator generate "\n"))
565 (define (invoke-char s i)
567 "(\\" (inexact->string i 8) ") " s " " ))
569 (define (invoke-dim1 s d)
571 (number->string (* d (/ 72.27 72))) " " s ))
573 (define (placebox x y s)
575 (number->string x) " " (number->string y) " {" s "} placebox "))
577 (define (bezier-sandwich l thick)
579 (apply string-append (map control->string l))
580 (number->string thick)
581 " draw_bezier_sandwich"))
583 (define (start-line height)
586 (define (stem breapth width depth height)
587 (string-append (numbers->string (list breapth width depth height))
594 (string-append "(" s ") show "))
597 (define (volta h w thick vert_start vert_end)
599 (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
602 (define (tuplet ht gap dx dy thick dir)
604 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
613 (cond ((eq? action-name 'all-definitions)
616 (define tuplet ,tuplet)
617 (define bracket ,bracket)
619 (define crescendo ,crescendo)
620 (define volta ,volta)
621 (define bezier-sandwich ,bezier-sandwich)
622 (define dashed-slur ,dashed-slur)
623 (define decrescendo ,decrescendo)
624 (define end-output ,end-output)
625 (define experimental-on ,experimental-on)
626 (define filledbox ,filledbox)
627 (define font-def ,font-def)
628 (define font-switch ,font-switch)
629 (define header-end ,header-end)
630 (define lily-def ,lily-def)
631 (define font-load-command ,font-load-command)
632 (define header ,header)
633 (define invoke-char ,invoke-char)
634 (define invoke-dim1 ,invoke-dim1)
635 (define placebox ,placebox)
636 (define select-font ,select-font)
637 (define start-line ,start-line)
639 (define stop-line ,stop-line)
640 (define stop-last-line ,stop-line)
643 ((eq? action-name 'tuplet) tuplet)
644 ((eq? action-name 'beam) beam)
645 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
646 ((eq? action-name 'bracket) bracket)
647 ((eq? action-name 'char) char)
648 ((eq? action-name 'crescendo) crescendo)
649 ((eq? action-name 'dashed-slur) dashed-slur)
650 ((eq? action-name 'decrescendo) decrescendo)
651 ((eq? action-name 'experimental-on) experimental-on)
652 ((eq? action-name 'filledbox) filledbox)
653 ((eq? action-name 'select-font) select-font)
654 ((eq? action-name 'volta) volta)
655 (else (error "unknown tag -- PS-SCM " action-name))
659 (define (musa-scm action-name)
662 (string-append "char (" (inexact->string i 10) ")\n"))
667 (define (experimental-on)
670 (define (filledbox breapth width depth height)
671 (let ((dx (+ width breapth))
672 (dy (+ depth height)))
675 (inexact->string (* -1 breapth) 10)
677 (inexact->string (* -1 depth) 10)
680 (string-append "vline (" (inexact->string dy 10) ")\n")
681 (string-append "hline (" (inexact->string dx 10) ")\n")))))
683 (define (font-load-command name-mag command)
686 (symbol->string (car name-mag)) "\", "
687 (number->string (magstep (cdr name-mag)))
690 (define (header creator generate)
691 (string-append "header (\"" creator "\", \"" generate "\")\n"))
696 (define (lily-def key val)
699 (define (placebox x y s)
701 "move_to (" (inexact->string x 10) ", " (inexact->string y 10) ")\n" s))
703 (define (select-font font-name-symbol)
704 (let* ((c (assoc font-name-symbol font-name-alist)))
709 "Programming error: No such font known "
710 (car font-name-symbol)))
711 "") ; issue no command
712 (string-append "select_font (\"" (symbol->string (car font-name-symbol)) "\")\n"))))
714 (define (start-line height)
715 (string-append "start_line (" (inexact->string height 10) ")\n"))
721 (string-append "text (\"" s "\")\n"))
723 (cond ((eq? action-name 'all-definitions)
725 ;;(define beam ,beam)
726 ;;(define tuplet ,tuplet)
727 ;;(define bracket ,bracket)
729 ;;(define crescendo ,crescendo)
730 ;;(define volta ,volta)
731 ;(define bezier-sandwich ,bezier-sandwich)
732 ;;(define dashed-slur ,dashed-slur)
733 ;;(define decrescendo ,decrescendo)
734 (define end-output ,end-output)
735 (define experimental-on ,experimental-on)
736 (define filledbox ,filledbox)
737 ;;(define font-def ,font-def)
738 (define font-load-command ,font-load-command)
739 ;;(define font-switch ,font-switch)
740 (define header ,header)
741 (define header-end ,header-end)
742 (define lily-def ,lily-def)
743 ;;(define invoke-char ,invoke-char)
744 ;;(define invoke-dim1 ,invoke-dim1)
745 (define placebox ,placebox)
746 (define select-font ,select-font)
747 (define start-line ,start-line)
748 ;;(define stem ,stem)
749 (define stop-line ,stop-line)
750 (define stop-last-line ,stop-line)
753 ;;((eq? action-name 'tuplet) tuplet)
754 ;;((eq? action-name 'beam) beam)
755 ;;((eq? action-name 'bezier-sandwich) bezier-sandwich)
756 ;;((eq? action-name 'bracket) bracket)
757 ((eq? action-name 'char) char)
758 ;;((eq? action-name 'crescendo) crescendo)
759 ;;((eq? action-name 'dashed-slur) dashed-slur)
760 ;;((eq? action-name 'decrescendo) decrescendo)
761 ;;((eq? action-name 'experimental-on) experimental-on)
762 ((eq? action-name 'filledbox) filledbox)
763 ((eq? action-name 'select-font) select-font)
764 ;;((eq? action-name 'volta) volta)
765 (else (error "unknown tag -- MUSA-SCM " action-name))
770 (define (gulp-file name)
771 (let* ((port (open-file name "r"))
772 (content (let loop ((text ""))
773 (let ((line (read-line port)))
774 (if (or (eof-object? line)
777 (loop (string-append text line "\n")))))))
781 (define (scm-gulp-file name)
784 (getenv 'LILYPONDPREFIX) "/ps") %load-path))
785 (let ((path (%search-load-path name)))
790 (define (scm-tex-output)
791 (eval (tex-scm 'all-definitions)))
793 (define (scm-ps-output)
794 (eval (ps-scm 'all-definitions)))
796 (define (scm-musa-output)
797 (display (gulp-file "musa.py"))
798 (eval (musa-scm 'all-definitions)))
801 ; Russ McManus, <mcmanus@IDT.NET>
803 ; I use the following, which should definitely be provided somewhere
804 ; in guile, but isn't, AFAIK:
808 (define (hash-table-for-each fn ht)
810 ((= i (vector-length ht)))
811 (do ((alist (vector-ref ht i) (cdr alist)))
813 (fn (car (car alist)) (cdr (car alist))))))
815 (define (hash-table-map fn ht)
818 ((= i (vector-length ht)) (reverse ret-ls))
819 (do ((alist (vector-ref ht i) (cdr alist)))
821 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
824 ;; guile-1.3.4 has list->string
825 (define (scmlist->string exp)
828 ;; obsolete, maybe handy for testing
829 ;; print a SCM expression. Isn't this part of the std lib?
830 (define (xxscmlist->string exp)
832 ((null? (car exp)) (begin (display ("urg:") (newline))))
833 ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
834 ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
835 ;; howto check for quote?
836 (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
839 (define (scm->string exp)
841 ((pair? exp) (string-append "(" (scmlist->string exp)))
842 ((number? exp) (number->string exp))
843 ((symbol? exp) (symbol->string exp))
844 ((string? exp) (string-append "\"" exp "\""))
846 (else (begin (display "programming error: scm->string: ") (newline) "'"))
849 (define (index-cell cell dir)
857 ; How should a bar line behave at a break?
859 (define (break-barline glyph dir)
860 (let ((result (assoc glyph
861 '((":|:" . (":|" . "|:"))
864 ("|:" . ("|" . "|:"))
865 ("|." . ("|." . nil))
866 (":|" . (":|" . nil))
867 ("||" . ("||" . nil))
868 (".|." . (".|." . nil))
869 ("scorebar" . (nil . "scorepostbreak"))
870 ("brace" . (nil . "brace"))
871 ("bracket" . (nil . "bracket"))
875 (if (equal? result #f)
876 (ly-warn (string-append "Unknown bar glyph: `" glyph "'"))
877 (index-cell (cdr result) dir))
882 (define (slur-ugly ind ht)
887 (> ht (+ (* 2 ind) -4))
888 (< ht (+ (* -2 ind) 8)))