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-module (ice-9 regex))
14 ;; do nothing in .scm output
23 (map (lambda (n) (string-append (number->string n ) " ")) l)))
29 (define (cons-map f x)
30 (cons (f (car x)) (f (cdr x))))
32 (define (reduce operator list)
33 (if (null? (cdr list)) (car list)
34 (operator (car list) (reduce operator (cdr list)))
39 (define (glue-2-strings a b) (string-append a " " b))
43 (reduce glue-2-strings (map number->string l)))
45 (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
47 (define (number->octal-string x)
48 (let* ((n (inexact->exact x))
50 (n8 (quotient (- n (* n64 64)) 8)))
54 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
56 (define (inexact->string x radix)
57 (let ((n (inexact->exact x)))
58 (number->string n radix)))
64 (string-append (number->string (car c)) " ")
65 (string-append (number->string (cadr c)) " ")))
71 (make-string 1 (integer->char (+ (char->integer #\A) i)))
76 (define (scm-scm action-name)
79 (define security-paranoia #f)
82 ;; See documentation of Item::visibility_lambda_
83 (define (postbreak-only-visibility d) (if (= d 1) '(#f . #f) '(#t . #t)))
84 (define (spanbar-non-postbreak-visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
85 (define (all-visibility d) '(#f . #f))
86 (define (non-postbreak-visibility d) (if (= d 1) '(#t . #t) '(#f . #f)))
87 (define (non-prebreak-visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
90 ;; Score_span_bars are only visible at start of line
91 ;; i.e. if break_dir == RIGHT == 1
92 (define 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
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)
205 (define (define-font name-mag)
207 (string-append "\\magfont"
208 (string-encode-integer (hash (car name-mag) 1000000))
210 (string-encode-integer (cdr name-mag)))
215 (define font-name-alist '())
216 (define (define-fonts names)
217 (set! font-name-alist (map define-font names))
218 (apply string-append (map (lambda (x)
219 (string-append "\\font" (cdr x) "="
220 (symbol->string (caar x))
222 (number->string (magstep (cdar x)))
231 (define (tex-scm action-name)
236 (define (select-font font-name-symbol)
239 (c (assoc font-name-symbol font-name-alist))
244 (ly-warn (string-append
245 "Programming error: No such font known " (car font-name-symbol)))
246 "") ; issue no command
252 (define (beam width slope thick)
253 (embedded-ps ((ps-scm 'beam) width slope thick)))
255 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
256 (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
258 (define (dashed-slur thick dash l)
259 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
261 (define (crescendo thick w h cont)
262 (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
265 (string-append "\\char" (inexact->string i 10) " "))
267 (define (decrescendo thick w h cont)
268 (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
270 ;This sets CTM so that you get to the currentpoint
271 ; by executing a 0 0 moveto
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"))
296 (ly-gulp-file "lily.ps")
298 ;; (regexp-substitute/global #f "\n" (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
300 "\\input lilyponddefs \\turnOnPostScript"))
302 (define (header creator generate)
304 "%created by: " creator generate "\n"))
306 (define (invoke-char s i)
308 "\n\\" s "{" (inexact->string i 10) "}" ))
310 (define (invoke-dim1 s d)
312 "\n\\" s "{" (number->dim d) "}"))
317 ;; need to do something to make this really safe.
319 (define (output-tex-string s)
320 (if security-paranoia
321 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
324 (define (lily-def key val)
327 ; (regexp-substitute/global #f "_" (output-tex-string key) 'pre "X" 'post)
328 (output-tex-string key)
329 "{" (output-tex-string val) "}\n"))
331 (define (number->dim x)
333 (number->string (chop-decimal x)) " pt "))
335 (define (placebox x y s)
338 (number->dim y) "}{" (number->dim x) "}{" s "}"))
341 (define (pianobrace y staffht)
343 (minht (* 2 staffht))
347 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
348 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
353 (define (bezier-sandwich l thick)
354 (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
357 (define (start-line ht)
359 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
362 "}\\vss}\\interscoreline")
363 (define (stop-last-line)
365 (define (filledbox breapth width depth height)
367 "\\kern" (number->dim (- breapth))
368 "\\vrule width " (number->dim (+ breapth width))
369 "depth " (number->dim depth)
370 "height " (number->dim height) " "))
373 (string-append "\\hbox{" (output-tex-string s) "}"))
375 (define (tuplet ht gapx dx dy thick dir)
376 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
378 (define (volta h w thick vert_start vert_end)
379 (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
382 ;; The procedures listed below form the public interface of TeX-scm.
383 ;; (should merge the 2 lists)
384 (cond ((eq? action-name 'all-definitions)
387 (define bezier-sandwich ,bezier-sandwich)
388 (define bracket ,bracket)
390 (define crescendo ,crescendo)
391 (define dashed-slur ,dashed-slur)
392 (define decrescendo ,decrescendo)
393 (define end-output ,end-output)
394 (define experimental-on ,experimental-on)
395 (define filledbox ,filledbox)
396 (define font-def ,font-def)
397 (define font-switch ,font-switch)
398 (define header-end ,header-end)
399 (define lily-def ,lily-def)
400 (define header ,header)
401 (define invoke-char ,invoke-char)
402 (define invoke-dim1 ,invoke-dim1)
403 (define pianobrace ,pianobrace)
404 (define placebox ,placebox)
405 (define select-font ,select-font)
406 (define start-line ,start-line)
407 (define stop-line ,stop-line)
408 (define stop-last-line ,stop-last-line)
410 (define tuplet ,tuplet)
411 (define volta ,volta)
414 ((eq? action-name 'beam) beam)
415 ((eq? action-name 'tuplet) tuplet)
416 ((eq? action-name 'bracket) bracket)
417 ((eq? action-name 'crescendo) crescendo)
418 ((eq? action-name 'dashed-slur) dashed-slur)
419 ((eq? action-name 'decrescendo) decrescendo)
420 ((eq? action-name 'end-output) end-output)
421 ((eq? action-name 'experimental-on) experimental-on)
422 ((eq? action-name 'font-def) font-def)
423 ((eq? action-name 'font-switch) font-switch)
424 ((eq? action-name 'header-end) header-end)
425 ((eq? action-name 'lily-def) lily-def)
426 ((eq? action-name 'header) header)
427 ((eq? action-name 'invoke-char) invoke-char)
428 ((eq? action-name 'invoke-dim1) invoke-dim1)
429 ((eq? action-name 'placebox) placebox)
430 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
431 ((eq? action-name 'start-line) start-line)
432 ((eq? action-name 'stem) stem)
433 ((eq? action-name 'stop-line) stop-line)
434 ((eq? action-name 'stop-last-line) stop-last-line)
435 ((eq? action-name 'volta) volta)
436 (else (error "unknown tag -- PS-TEX " action-name))
441 (define (ps-scm action-name)
444 ;; alist containing fontname -> fontcommand assoc (both strings)
445 (define font-alist '())
446 (define font-count 0)
447 (define current-font "")
450 (define (cached-fontname i)
453 (make-string 1 (integer->char (+ 65 i)))))
455 (define (mag-to-size m)
456 (number->string (case m
459 (2 14) ; really: 14.400
460 (3 17) ; really: 17.280
461 (4 21) ; really: 20.736
462 (5 24) ; really: 24.888
463 (6 30) ; really: 29.856
466 (define (select-font font-name magnification)
467 (define font-cmd (assoc font-name font-alist))
468 (if (not (equal? font-name current-font))
470 (set! current-font font-name)
471 (if (eq? font-cmd #f)
473 (set! font-cmd (cached-fontname font-count))
474 (set! font-alist (acons font-name font-cmd font-alist))
475 (set! font-count (+ 1 font-count))
476 (string-append "\n/" font-cmd " {/"
477 font-name " findfont "
478 (mag-to-size magnification)
479 " scalefont setfont} bind def \n"
481 (string-append (cdr font-cmd) " ")))
482 ; font-name == current-font no switch needed
486 (define (beam width slope thick)
488 (numbers->string (list width slope thick)) " draw_beam" ))
491 (string-append "% " s))
493 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
495 (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
498 (invoke-char " show" i))
500 (define (crescendo thick w h cont )
502 (numbers->string (list w h (inexact->exact cont) thick))
505 (define (dashed-slur thick dash l)
507 (apply string-append (map control->string l))
508 (number->string thick)
511 (number->string (- (* thick dash) thick))
514 (number->string (* 2 thick))
515 " ] 0 draw_dashed_slur"))
517 (define (decrescendo thick w h cont)
519 (numbers->string (list w h (inexact->exact cont) thick))
520 " draw_decrescendo"))
526 (define (experimental-on) "")
528 (define (filledbox breapth width depth height)
529 (string-append (numbers->string (list breapth width depth height))
533 (define (font-def i s)
536 (substring s 0 (- (string-length s) 4))
537 " findfont 12 scalefont setfont} bind def \n"))
539 (define (font-switch i)
540 (string-append (font i) " "))
544 (ly-gulp-file "lilyponddefs.ps")
545 " {exch pop //systemdict /run get exec} "
546 (ly-gulp-file "lily.ps")
547 "{ exch pop //systemdict /run get exec } "
550 (define (lily-def key val)
552 (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
553 (string-append "/" key " {" val "} bind def\n")
554 (string-append "/" key " (" val ") def\n")
558 (define (header creator generate)
561 "%%Creator: " creator generate "\n"))
563 (define (invoke-char s i)
565 "(\\" (inexact->string i 8) ") " s " " ))
567 (define (invoke-dim1 s d)
569 (number->string (* d (/ 72.27 72))) " " s ))
571 (define (placebox x y s)
573 (number->string x) " " (number->string y) " {" s "} placebox "))
575 (define (pianobrace y staffht)
577 (minht (* 2 staffht))
581 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
582 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
587 (define (bezier-sandwich l thick)
589 (apply string-append (map control->string l))
590 (number->string thick)
591 " draw_bezier_sandwich"))
593 (define (start-line height)
597 (define (stem breapth width depth height)
598 (string-append (numbers->string (list breapth width depth height))
605 (string-append "(" s ") show "))
608 (define (volta h w thick vert_start vert_end)
610 (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
613 (define (tuplet ht gap dx dy thick dir)
615 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
624 (cond ((eq? action-name 'all-definitions)
627 (define tuplet ,tuplet)
628 (define bracket ,bracket)
630 (define crescendo ,crescendo)
631 (define volta ,volta)
632 (define bezier-sandwich ,bezier-sandwich)
633 (define dashed-slur ,dashed-slur)
634 (define decrescendo ,decrescendo)
635 (define end-output ,end-output)
636 (define experimental-on ,experimental-on)
637 (define filledbox ,filledbox)
638 (define font-def ,font-def)
639 (define font-switch ,font-switch)
640 (define pianobrace ,pianobrace)
641 (define header-end ,header-end)
642 (define lily-def ,lily-def)
643 (define header ,header)
644 (define invoke-char ,invoke-char)
645 (define invoke-dim1 ,invoke-dim1)
646 (define placebox ,placebox)
647 (define select-font ,select-font)
648 (define start-line ,start-line)
650 (define stop-line ,stop-line)
651 (define stop-last-line ,stop-line)
654 ((eq? action-name 'tuplet) tuplet)
655 ((eq? action-name 'beam) beam)
656 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
657 ((eq? action-name 'bracket) bracket)
658 ((eq? action-name 'char) char)
659 ((eq? action-name 'crescendo) crescendo)
660 ((eq? action-name 'dashed-slur) dashed-slur)
661 ((eq? action-name 'decrescendo) decrescendo)
662 ((eq? action-name 'experimental-on) experimental-on)
663 ((eq? action-name 'filledbox) filledbox)
664 ((eq? action-name 'select-font) select-font)
665 ((eq? action-name 'volta) volta)
666 (else (error "unknown tag -- PS-SCM " action-name))
671 ; Russ McManus, <mcmanus@IDT.NET>
673 ; I use the following, which should definitely be provided somewhere
674 ; in guile, but isn't, AFAIK:
678 (define (hash-table-for-each fn ht)
680 ((= i (vector-length ht)))
681 (do ((alist (vector-ref ht i) (cdr alist)))
683 (fn (car (car alist)) (cdr (car alist))))))
685 (define (hash-table-map fn ht)
688 ((= i (vector-length ht)) (reverse ret-ls))
689 (do ((alist (vector-ref ht i) (cdr alist)))
691 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
694 ;;;; print a SCM expression. Isn't this part of the std lib?
696 (define (scmlist->string exp)
698 ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
699 ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
700 (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
703 (define (scm->string exp)
705 ((pair? exp) (string-append "(" (scmlist->string exp)))
706 ((number? exp) (number->string exp))
707 ((symbol? exp) (symbol->string exp))
708 ((string? exp) (string-append "\"" exp "\""))