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 (cadr 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 mark-visibility non-prebreak-visibility)
93 (define Span_score_bar_engraver-visibility postbreak-only-visibility)
94 (define Piano_bar_engraver-visibility postbreak-only-visibility)
95 (define Staff_group_bar_engraver-visibility postbreak-only-visibility)
97 ;; Spacing constants for prefatory matter.
99 ;; rules for this spacing are much more complicated than this. See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147
103 ;; (Measured in interlines? -- jcn)
106 (("" "Clef_item") . (minimum-space 1.0))
107 (("" "Staff_bar") . (minimum-space 0.0))
108 (("" "Clef_item") . (minimum-space 1.0))
109 (("" "Key_item") . (minimum-space 0.5))
110 (("" "Span_bar") . (extra-space 0.0))
111 (("" "Time_signature") . (extra-space 0.0))
112 (("" "begin-of-note") . (minimum-space 1.5))
113 (("Clef_item" "Key_item") . (minimum-space 4.0))
114 (("Key_item" "Time_signature") . (extra-space 1.0))
115 (("Clef_item" "Time_signature") . (minimum-space 3.5))
116 (("Staff_bar" "Clef_item") . (minimum-space 1.0))
117 (("Clef_item" "Staff_bar") . (minimum-space 3.7))
118 (("Time_signature" "Staff_bar") . (minimum-space 2.0))
119 (("Key_item" "Staff_bar") . (extra-space 1.0))
120 (("Span_bar" "Clef_item") . (extra-space 1.0))
121 (("Clef_item" "Span_bar") . (minimum-space 3.7))
122 (("Time_signature" "Span_bar") . (minimum-space 2.0))
123 (("Key_item" "Span_bar") . (minimum-space 2.5))
124 (("Staff_bar" "Time_signature") . (minimum-space 1.5)) ;double check this.
125 (("Time_signature" "begin-of-note") . (extra-space 2.0)) ;double check this.
126 (("Key_item" "begin-of-note") . (extra-space 2.5))
127 (("Staff_bar" "begin-of-note") . (extra-space 1.0))
128 (("Clef_item" "begin-of-note") . (minimum-space 5.0))
129 (("" "Breathing_sign") . (minimum-space 0.0))
130 (("Breathing_sign" "Key_item") . (minimum-space 1.5))
131 (("Breathing_sign" "begin-of-note") . (minimum-space 1.0))
132 (("Breathing_sign" "Staff_bar") . (minimum-space 1.5))
133 (("Breathing_sign" "Clef_item") . (minimum-space 2.0))
137 (define (break-align-spacer this next)
138 (let ((entry (assoc `(,this ,next) space-alist)))
141 (begin (ly-warn (string-append "Unknown spacing pair `" this "', `" next "'"))
142 '(minimum-space 0.0)))))
148 ;; this is silly, can't we use something like
149 ;; roman-0, roman-1 roman+1 ?
152 ("dynamic" . "feta-din")
156 ("finger" . "feta-nummer")
157 ("typewriter" . "cmtt")
163 ("mark" . "feta-nummer")
164 ("number" . "feta-nummer")
165 ("volta" . "feta-nummer"))
168 (define (string-encode-integer i)
171 ((< i 0) (string-append "n" (string-encode-integer (- i))))
173 (make-string 1 (integer->char (+ 65 (modulo i 26))))
174 (string-encode-integer (quotient i 26))
181 (cdr (assoc i '((-4 . 482)
194 (define script-alist '())
195 (define (articulation-to-scriptdef a)
196 (assoc a script-alist)
199 ;; Map style names to TeX font names. Return false if
200 ;; no font name found.
201 (define (style-to-cmr s)
207 (define font-name-alist '())
208 (define (font-command name-mag)
210 (string-append "magfont"
211 (string-encode-integer (hashq (car name-mag) 1000000))
213 (string-encode-integer (cdr name-mag)))
217 (define (define-fonts names)
218 (set! font-name-alist (map font-command names))
221 (font-load-command (car x) (cdr x))) font-name-alist)
225 (define (tex-scm action-name)
230 (define (select-font font-name-symbol)
233 (c (assoc font-name-symbol font-name-alist))
238 (ly-warn (string-append
239 "Programming error: No such font known " (car font-name-symbol)))
240 "") ; issue no command
241 (string-append "\\" (cdr c)))
246 (define (beam width slope thick)
247 (embedded-ps ((ps-scm 'beam) width slope thick)))
249 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
250 (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
252 (define (dashed-slur thick dash l)
253 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
255 (define (crescendo thick w h cont)
256 (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
259 (string-append "\\char" (inexact->string i 10) " "))
261 (define (decrescendo thick w h cont)
262 (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
264 ;This sets CTM so that you get to the currentpoint
265 ; by executing a 0 0 moveto
270 (define (font-load-command name-mag command)
272 "\\font\\" command "="
273 (symbol->string (car name-mag))
275 (number->string (magstep (cdr name-mag)))
279 (define (embedded-ps s)
280 (string-append "\\embeddedps{" s "}"))
283 (string-append "% " s))
286 "\n\\EndLilyPondOutput")
288 (define (experimental-on)
291 (define (font-switch i)
295 (define (font-def i s)
297 "\\font" (font-switch i) "=" s "\n"))
303 ;(ly-gulp-file "lily.ps")
305 (regexp-substitute/global #f "\n" (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
307 "\\input lilyponddefs \\turnOnPostScript"))
309 (define (header creator generate)
311 "%created by: " creator generate))
313 (define (invoke-char s i)
315 "\n\\" s "{" (inexact->string i 10) "}" ))
317 (define (invoke-dim1 s d)
319 "\n\\" s "{" (number->dim d) "}"))
324 ;; need to do something to make this really safe.
326 (define (output-tex-string s)
327 (if security-paranoia
328 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
331 (define (lily-def key val)
335 (regexp-substitute/global #f "_" (output-tex-string key) 'pre "X" 'post)
336 ;(output-tex-string key)
337 "{" (output-tex-string val) "}\n"))
339 (define (number->dim x)
341 (number->string (chop-decimal x)) " pt "))
343 (define (placebox x y s)
346 (number->dim y) "}{" (number->dim x) "}{" s "}\n"))
350 (define (bezier-sandwich l thick)
351 (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
354 (define (start-line ht)
356 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
359 "}\\vss}\\interscoreline")
360 (define (stop-last-line)
362 (define (filledbox breapth width depth height)
364 "\\kern" (number->dim (- breapth))
365 "\\vrule width " (number->dim (+ breapth width))
366 "depth " (number->dim depth)
367 "height " (number->dim height) " "))
370 (string-append "\\hbox{" (output-tex-string s) "}"))
372 (define (tuplet ht gapx dx dy thick dir)
373 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
375 (define (volta h w thick vert_start vert_end)
376 (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
379 ;; The procedures listed below form the public interface of TeX-scm.
380 ;; (should merge the 2 lists)
381 (cond ((eq? action-name 'all-definitions)
383 (define font-load-command ,font-load-command)
385 (define bezier-sandwich ,bezier-sandwich)
386 (define bracket ,bracket)
388 (define crescendo ,crescendo)
389 (define dashed-slur ,dashed-slur)
390 (define decrescendo ,decrescendo)
391 (define end-output ,end-output)
392 (define experimental-on ,experimental-on)
393 (define filledbox ,filledbox)
394 (define font-def ,font-def)
395 (define font-switch ,font-switch)
396 (define header-end ,header-end)
397 (define lily-def ,lily-def)
398 (define header ,header)
399 (define invoke-char ,invoke-char)
400 (define invoke-dim1 ,invoke-dim1)
401 (define placebox ,placebox)
402 (define select-font ,select-font)
403 (define start-line ,start-line)
404 (define stop-line ,stop-line)
405 (define stop-last-line ,stop-last-line)
407 (define tuplet ,tuplet)
408 (define volta ,volta)
411 ((eq? action-name 'beam) beam)
412 ((eq? action-name 'tuplet) tuplet)
413 ((eq? action-name 'bracket) bracket)
414 ((eq? action-name 'crescendo) crescendo)
415 ((eq? action-name 'dashed-slur) dashed-slur)
416 ((eq? action-name 'decrescendo) decrescendo)
417 ((eq? action-name 'end-output) end-output)
418 ((eq? action-name 'experimental-on) experimental-on)
419 ((eq? action-name 'font-def) font-def)
420 ((eq? action-name 'font-switch) font-switch)
421 ((eq? action-name 'header-end) header-end)
422 ((eq? action-name 'lily-def) lily-def)
423 ((eq? action-name 'header) header)
424 ((eq? action-name 'invoke-char) invoke-char)
425 ((eq? action-name 'invoke-dim1) invoke-dim1)
426 ((eq? action-name 'placebox) placebox)
427 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
428 ((eq? action-name 'start-line) start-line)
429 ((eq? action-name 'stem) stem)
430 ((eq? action-name 'stop-line) stop-line)
431 ((eq? action-name 'stop-last-line) stop-last-line)
432 ((eq? action-name 'volta) volta)
433 (else (error "unknown tag -- PS-TEX " action-name))
439 (define (ps-scm action-name)
442 ;; alist containing fontname -> fontcommand assoc (both strings)
443 (define font-alist '())
444 (define font-count 0)
445 (define current-font "")
448 (define (cached-fontname i)
451 (make-string 1 (integer->char (+ 65 i)))))
453 (define (mag-to-size m)
454 (number->string (case m
457 (2 14) ; really: 14.400
458 (3 17) ; really: 17.280
459 (4 21) ; really: 20.736
460 (5 24) ; really: 24.888
461 (6 30) ; really: 29.856
465 (define (select-font font-name-symbol)
468 (c (assoc font-name-symbol font-name-alist))
473 (ly-warn (string-append
474 "Programming error: No such font known " (car font-name-symbol)))
475 "") ; issue no command
476 (string-append " " (cdr c) " "))
481 (define (font-load-command name-mag command)
485 (symbol->string (car name-mag))
487 (number->string (magstep (cdr name-mag)))
488 " 1000 div 12 mul scalefont setfont } bind def "
492 (define (beam width slope thick)
494 (numbers->string (list width slope thick)) " draw_beam" ))
497 (string-append "% " s))
499 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
501 (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
504 (invoke-char " show" i))
506 (define (crescendo thick w h cont )
508 (numbers->string (list w h (inexact->exact cont) thick))
511 (define (dashed-slur thick dash l)
513 (apply string-append (map control->string l))
514 (number->string thick)
517 (number->string (- (* thick dash) thick))
520 (number->string (* 2 thick))
521 " ] 0 draw_dashed_slur"))
523 (define (decrescendo thick w h cont)
525 (numbers->string (list w h (inexact->exact cont) thick))
526 " draw_decrescendo"))
532 (define (experimental-on) "")
534 (define (filledbox breapth width depth height)
535 (string-append (numbers->string (list breapth width depth height))
539 (define (font-def i s)
542 (substring s 0 (- (string-length s) 4))
543 " findfont 12 scalefont setfont} bind def \n"))
545 (define (font-switch i)
546 (string-append (font i) " "))
550 (ly-gulp-file "lilyponddefs.ps")
551 " {exch pop //systemdict /run get exec} "
552 (ly-gulp-file "lily.ps")
553 "{ exch pop //systemdict /run get exec } "
556 (define (lily-def key val)
558 (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
559 (string-append "/" key " {" val "} bind def\n")
560 (string-append "/" key " (" val ") def\n")
564 (define (header creator generate)
567 "%%Creator: " creator generate "\n"))
569 (define (invoke-char s i)
571 "(\\" (inexact->string i 8) ") " s " " ))
573 (define (invoke-dim1 s d)
575 (number->string (* d (/ 72.27 72))) " " s ))
577 (define (placebox x y s)
579 (number->string x) " " (number->string y) " {" s "} placebox "))
581 (define (bezier-sandwich l thick)
583 (apply string-append (map control->string l))
584 (number->string thick)
585 " draw_bezier_sandwich"))
587 (define (start-line height)
591 (define (stem breapth width depth height)
592 (string-append (numbers->string (list breapth width depth height))
599 (string-append "(" s ") show "))
602 (define (volta h w thick vert_start vert_end)
604 (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
607 (define (tuplet ht gap dx dy thick dir)
609 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
618 (cond ((eq? action-name 'all-definitions)
621 (define tuplet ,tuplet)
622 (define bracket ,bracket)
624 (define crescendo ,crescendo)
625 (define volta ,volta)
626 (define bezier-sandwich ,bezier-sandwich)
627 (define dashed-slur ,dashed-slur)
628 (define decrescendo ,decrescendo)
629 (define end-output ,end-output)
630 (define experimental-on ,experimental-on)
631 (define filledbox ,filledbox)
632 (define font-def ,font-def)
633 (define font-switch ,font-switch)
634 (define header-end ,header-end)
635 (define lily-def ,lily-def)
636 (define font-load-command ,font-load-command)
637 (define header ,header)
638 (define invoke-char ,invoke-char)
639 (define invoke-dim1 ,invoke-dim1)
640 (define placebox ,placebox)
641 (define select-font ,select-font)
642 (define start-line ,start-line)
644 (define stop-line ,stop-line)
645 (define stop-last-line ,stop-line)
648 ((eq? action-name 'tuplet) tuplet)
649 ((eq? action-name 'beam) beam)
650 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
651 ((eq? action-name 'bracket) bracket)
652 ((eq? action-name 'char) char)
653 ((eq? action-name 'crescendo) crescendo)
654 ((eq? action-name 'dashed-slur) dashed-slur)
655 ((eq? action-name 'decrescendo) decrescendo)
656 ((eq? action-name 'experimental-on) experimental-on)
657 ((eq? action-name 'filledbox) filledbox)
658 ((eq? action-name 'select-font) select-font)
659 ((eq? action-name 'volta) volta)
660 (else (error "unknown tag -- PS-SCM " action-name))
665 (define (gulp-file name)
666 (let* ((port (open-file name "r"))
667 (content (let loop ((text ""))
668 (let ((line (read-line port)))
669 (if (or (eof-object? line)
672 (loop (string-append text line "\n")))))))
676 (define (scm-gulp-file name)
679 (getenv 'LILYPONDPREFIX) "/ps") %load-path))
680 (let ((path (%search-load-path name)))
685 (define (scm-tex-output)
686 (eval (tex-scm 'all-definitions)))
688 (define (scm-ps-output)
689 (eval (ps-scm 'all-definitions)))
692 ; Russ McManus, <mcmanus@IDT.NET>
694 ; I use the following, which should definitely be provided somewhere
695 ; in guile, but isn't, AFAIK:
699 (define (hash-table-for-each fn ht)
701 ((= i (vector-length ht)))
702 (do ((alist (vector-ref ht i) (cdr alist)))
704 (fn (car (car alist)) (cdr (car alist))))))
706 (define (hash-table-map fn ht)
709 ((= i (vector-length ht)) (reverse ret-ls))
710 (do ((alist (vector-ref ht i) (cdr alist)))
712 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
715 ;; guile-1.3.4 has list->string
716 (define (scmlist->string exp)
719 ;; obsolete, maybe handy for testing
720 ;; print a SCM expression. Isn't this part of the std lib?
721 (define (xxscmlist->string exp)
723 ((null? (car exp)) (begin (display ("urg:") (newline))))
724 ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
725 ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
726 ;; howto check for quote?
727 (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
730 (define (scm->string exp)
732 ((pair? exp) (string-append "(" (scmlist->string exp)))
733 ((number? exp) (number->string exp))
734 ((symbol? exp) (symbol->string exp))
735 ((string? exp) (string-append "\"" exp "\""))
737 (else (begin (display "programming error: scm->string: ") (newline) "'"))
740 (define (index-cell cell dir)
748 ; How should a bar line behave at a break?
751 ; move this to paper.scm?
752 ; to override, see input/test/no-span.ly
753 (define bar-break-glyph-alist
754 '((":|:" . (":|" . "|:"))
757 ("|:" . ("|" . "|:"))
758 ("|." . ("|." . nil))
759 (":|" . (":|" . nil))
760 ("||" . ("||" . nil))
761 (".|." . (".|." . nil))
762 ("scorebar" . (nil . "scorepostbreak"))
763 ("brace" . (nil . "brace"))
764 ("bracket" . (nil . "bracket"))
767 (define (break-barline glyph dir)
768 (let ((result (assoc glyph bar-break-glyph-alist)))
769 (if (equal? result #f)
770 (ly-warn (string-append "Unknown bar glyph: `" glyph "'"))
771 (index-cell (cdr result) dir))