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
105 (("" "Clef_item") . (minimum-space 1.0))
106 (("" "Staff_bar") . (minimum-space 0.0))
107 (("" "Clef_item") . (minimum-space 1.0))
108 (("" "Key_item") . (minimum-space 0.5))
109 (("" "Span_bar") . (extra-space 0.0))
110 (("" "Time_signature") . (extra-space 0.0))
111 (("" "begin-of-note") . (minimum-space 1.5))
112 (("Clef_item" "Key_item") . (minimum-space 4.0))
113 (("Key_item" "Time_signature") . (extra-space 1.0))
114 (("Clef_item" "Time_signature") . (minimum-space 3.5))
115 (("Staff_bar" "Clef_item") . (minimum-space 1.0))
116 (("Clef_item" "Staff_bar") . (minimum-space 3.7))
117 (("Time_signature" "Staff_bar") . (minimum-space 2.0))
118 (("Key_item" "Staff_bar") . (extra-space 1.0))
119 (("Span_bar" "Clef_item") . (extra-space 1.0))
120 (("Clef_item" "Span_bar") . (minimum-space 3.7))
121 (("Time_signature" "Span_bar") . (minimum-space 2.0))
122 (("Key_item" "Span_bar") . (minimum-space 2.5))
123 (("Staff_bar" "Time_signature") . (minimum-space 1.5)) ;double check this.
124 (("Time_signature" "begin-of-note") . (extra-space 2.0)) ;double check this.
125 (("Key_item" "begin-of-note") . (extra-space 2.5))
126 (("Staff_bar" "begin-of-note") . (extra-space 1.0))
127 (("Clef_item" "begin-of-note") . (minimum-space 5.0))
128 (("" "Breathing_sign") . (minimum-space 0.0))
129 (("Breathing_sign" "Key_item") . (minimum-space 1.5))
130 (("Breathing_sign" "begin-of-note") . (minimum-space 1.0))
131 (("Breathing_sign" "Staff_bar") . (minimum-space 1.5))
132 (("Breathing_sign" "Clef_item") . (minimum-space 2.0))
136 (define (break-align-spacer this next)
137 (let ((entry (assoc `(,this ,next) space-alist)))
140 (begin (ly-warn (string-append "Unknown spacing pair `" this "', `" next "'"))
141 '(minimum-space 0.0)))))
147 ;; this is silly, can't we use something like
148 ;; roman-0, roman-1 roman+1 ?
151 ("dynamic" . "feta-din")
155 ("finger" . "feta-nummer")
156 ("typewriter" . "cmtt")
162 ("mark" . "feta-nummer")
163 ("number" . "feta-nummer")
164 ("volta" . "feta-nummer"))
167 (define (string-encode-integer i)
170 ((< i 0) (string-append "n" (string-encode-integer (- i))))
172 (make-string 1 (integer->char (+ 65 (modulo i 26))))
173 (string-encode-integer (quotient i 26))
180 (cdr (assoc i '((-4 . 482)
193 (define script-alist '())
194 (define (articulation-to-scriptdef a)
195 (assoc a script-alist)
198 ;; Map style names to TeX font names. Return false if
199 ;; no font name found.
200 (define (style-to-cmr s)
206 (define font-name-alist '())
207 (define (font-command name-mag)
209 (string-append "magfont"
210 (string-encode-integer (hash (car name-mag) 1000000))
212 (string-encode-integer (cdr name-mag)))
216 (define (define-fonts names)
217 (set! font-name-alist (map font-command names))
220 (font-load-command (car x) (cdr x))) font-name-alist)
224 (define (tex-scm action-name)
229 (define (select-font font-name-symbol)
232 (c (assoc font-name-symbol font-name-alist))
237 (ly-warn (string-append
238 "Programming error: No such font known " (car font-name-symbol)))
239 "") ; issue no command
240 (string-append "\\" (cdr c)))
245 (define (beam width slope thick)
246 (embedded-ps ((ps-scm 'beam) width slope thick)))
248 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
249 (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
251 (define (dashed-slur thick dash l)
252 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
254 (define (crescendo thick w h cont)
255 (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
258 (string-append "\\char" (inexact->string i 10) " "))
260 (define (decrescendo thick w h cont)
261 (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
263 ;This sets CTM so that you get to the currentpoint
264 ; by executing a 0 0 moveto
269 (define (font-load-command name-mag command)
271 "\\font\\" command "="
272 (symbol->string (car name-mag))
274 (number->string (magstep (cdr name-mag)))
278 (define (embedded-ps s)
279 (string-append "\\embeddedps{" s "}"))
282 (string-append "% " s))
285 "\n\\EndLilyPondOutput")
287 (define (experimental-on)
290 (define (font-switch i)
294 (define (font-def i s)
296 "\\font" (font-switch i) "=" s "\n"))
302 ;(ly-gulp-file "lily.ps")
304 (regexp-substitute/global #f "\n" (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
306 "\\input lilyponddefs \\turnOnPostScript"))
308 (define (header creator generate)
310 "%created by: " creator generate))
312 (define (invoke-char s i)
314 "\n\\" s "{" (inexact->string i 10) "}" ))
316 (define (invoke-dim1 s d)
318 "\n\\" s "{" (number->dim d) "}"))
323 ;; need to do something to make this really safe.
325 (define (output-tex-string s)
326 (if security-paranoia
327 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
330 (define (lily-def key val)
334 (regexp-substitute/global #f "_" (output-tex-string key) 'pre "X" 'post)
335 ;(output-tex-string key)
336 "{" (output-tex-string val) "}\n"))
338 (define (number->dim x)
340 (number->string (chop-decimal x)) " pt "))
342 (define (placebox x y s)
345 (number->dim y) "}{" (number->dim x) "}{" s "}\n"))
349 (define (bezier-sandwich l thick)
350 (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
353 (define (start-line ht)
355 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
358 "}\\vss}\\interscoreline")
359 (define (stop-last-line)
361 (define (filledbox breapth width depth height)
363 "\\kern" (number->dim (- breapth))
364 "\\vrule width " (number->dim (+ breapth width))
365 "depth " (number->dim depth)
366 "height " (number->dim height) " "))
369 (string-append "\\hbox{" (output-tex-string s) "}"))
371 (define (tuplet ht gapx dx dy thick dir)
372 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
374 (define (volta h w thick vert_start vert_end)
375 (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
378 ;; The procedures listed below form the public interface of TeX-scm.
379 ;; (should merge the 2 lists)
380 (cond ((eq? action-name 'all-definitions)
382 (define font-load-command ,font-load-command)
384 (define bezier-sandwich ,bezier-sandwich)
385 (define bracket ,bracket)
387 (define crescendo ,crescendo)
388 (define dashed-slur ,dashed-slur)
389 (define decrescendo ,decrescendo)
390 (define end-output ,end-output)
391 (define experimental-on ,experimental-on)
392 (define filledbox ,filledbox)
393 (define font-def ,font-def)
394 (define font-switch ,font-switch)
395 (define header-end ,header-end)
396 (define lily-def ,lily-def)
397 (define header ,header)
398 (define invoke-char ,invoke-char)
399 (define invoke-dim1 ,invoke-dim1)
400 (define placebox ,placebox)
401 (define select-font ,select-font)
402 (define start-line ,start-line)
403 (define stop-line ,stop-line)
404 (define stop-last-line ,stop-last-line)
406 (define tuplet ,tuplet)
407 (define volta ,volta)
410 ((eq? action-name 'beam) beam)
411 ((eq? action-name 'tuplet) tuplet)
412 ((eq? action-name 'bracket) bracket)
413 ((eq? action-name 'crescendo) crescendo)
414 ((eq? action-name 'dashed-slur) dashed-slur)
415 ((eq? action-name 'decrescendo) decrescendo)
416 ((eq? action-name 'end-output) end-output)
417 ((eq? action-name 'experimental-on) experimental-on)
418 ((eq? action-name 'font-def) font-def)
419 ((eq? action-name 'font-switch) font-switch)
420 ((eq? action-name 'header-end) header-end)
421 ((eq? action-name 'lily-def) lily-def)
422 ((eq? action-name 'header) header)
423 ((eq? action-name 'invoke-char) invoke-char)
424 ((eq? action-name 'invoke-dim1) invoke-dim1)
425 ((eq? action-name 'placebox) placebox)
426 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
427 ((eq? action-name 'start-line) start-line)
428 ((eq? action-name 'stem) stem)
429 ((eq? action-name 'stop-line) stop-line)
430 ((eq? action-name 'stop-last-line) stop-last-line)
431 ((eq? action-name 'volta) volta)
432 (else (error "unknown tag -- PS-TEX " action-name))
438 (define (ps-scm action-name)
441 ;; alist containing fontname -> fontcommand assoc (both strings)
442 (define font-alist '())
443 (define font-count 0)
444 (define current-font "")
447 (define (cached-fontname i)
450 (make-string 1 (integer->char (+ 65 i)))))
452 (define (mag-to-size m)
453 (number->string (case m
456 (2 14) ; really: 14.400
457 (3 17) ; really: 17.280
458 (4 21) ; really: 20.736
459 (5 24) ; really: 24.888
460 (6 30) ; really: 29.856
464 (define (select-font font-name-symbol)
467 (c (assoc font-name-symbol font-name-alist))
472 (ly-warn (string-append
473 "Programming error: No such font known " (car font-name-symbol)))
474 "") ; issue no command
475 (string-append " " (cdr c) " "))
480 (define (font-load-command name-mag command)
484 (symbol->string (car name-mag))
486 (number->string (magstep (cdr name-mag)))
487 " 1000 div 12 mul scalefont setfont } bind def "
491 (define (beam width slope thick)
493 (numbers->string (list width slope thick)) " draw_beam" ))
496 (string-append "% " s))
498 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
500 (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
503 (invoke-char " show" i))
505 (define (crescendo thick w h cont )
507 (numbers->string (list w h (inexact->exact cont) thick))
510 (define (dashed-slur thick dash l)
512 (apply string-append (map control->string l))
513 (number->string thick)
516 (number->string (- (* thick dash) thick))
519 (number->string (* 2 thick))
520 " ] 0 draw_dashed_slur"))
522 (define (decrescendo thick w h cont)
524 (numbers->string (list w h (inexact->exact cont) thick))
525 " draw_decrescendo"))
531 (define (experimental-on) "")
533 (define (filledbox breapth width depth height)
534 (string-append (numbers->string (list breapth width depth height))
538 (define (font-def i s)
541 (substring s 0 (- (string-length s) 4))
542 " findfont 12 scalefont setfont} bind def \n"))
544 (define (font-switch i)
545 (string-append (font i) " "))
549 (ly-gulp-file "lilyponddefs.ps")
550 " {exch pop //systemdict /run get exec} "
551 (ly-gulp-file "lily.ps")
552 "{ exch pop //systemdict /run get exec } "
555 (define (lily-def key val)
557 (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
558 (string-append "/" key " {" val "} bind def\n")
559 (string-append "/" key " (" val ") def\n")
563 (define (header creator generate)
566 "%%Creator: " creator generate "\n"))
568 (define (invoke-char s i)
570 "(\\" (inexact->string i 8) ") " s " " ))
572 (define (invoke-dim1 s d)
574 (number->string (* d (/ 72.27 72))) " " s ))
576 (define (placebox x y s)
578 (number->string x) " " (number->string y) " {" s "} placebox "))
580 (define (bezier-sandwich l thick)
582 (apply string-append (map control->string l))
583 (number->string thick)
584 " draw_bezier_sandwich"))
586 (define (start-line height)
590 (define (stem breapth width depth height)
591 (string-append (numbers->string (list breapth width depth height))
598 (string-append "(" s ") show "))
601 (define (volta h w thick vert_start vert_end)
603 (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
606 (define (tuplet ht gap dx dy thick dir)
608 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
617 (cond ((eq? action-name 'all-definitions)
620 (define tuplet ,tuplet)
621 (define bracket ,bracket)
623 (define crescendo ,crescendo)
624 (define volta ,volta)
625 (define bezier-sandwich ,bezier-sandwich)
626 (define dashed-slur ,dashed-slur)
627 (define decrescendo ,decrescendo)
628 (define end-output ,end-output)
629 (define experimental-on ,experimental-on)
630 (define filledbox ,filledbox)
631 (define font-def ,font-def)
632 (define font-switch ,font-switch)
633 (define header-end ,header-end)
634 (define lily-def ,lily-def)
635 (define font-load-command ,font-load-command)
636 (define header ,header)
637 (define invoke-char ,invoke-char)
638 (define invoke-dim1 ,invoke-dim1)
639 (define placebox ,placebox)
640 (define select-font ,select-font)
641 (define start-line ,start-line)
643 (define stop-line ,stop-line)
644 (define stop-last-line ,stop-line)
647 ((eq? action-name 'tuplet) tuplet)
648 ((eq? action-name 'beam) beam)
649 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
650 ((eq? action-name 'bracket) bracket)
651 ((eq? action-name 'char) char)
652 ((eq? action-name 'crescendo) crescendo)
653 ((eq? action-name 'dashed-slur) dashed-slur)
654 ((eq? action-name 'decrescendo) decrescendo)
655 ((eq? action-name 'experimental-on) experimental-on)
656 ((eq? action-name 'filledbox) filledbox)
657 ((eq? action-name 'select-font) select-font)
658 ((eq? action-name 'volta) volta)
659 (else (error "unknown tag -- PS-SCM " action-name))
664 (define (gulp-file name)
665 (let* ((port (open-file name "r"))
666 (content (let loop ((text ""))
667 (let ((line (read-line port)))
668 (if (or (eof-object? line)
671 (loop (string-append text line "\n")))))))
675 (define (scm-gulp-file name)
678 (getenv 'LILYPONDPREFIX) "/ps") %load-path))
679 (let ((path (%search-load-path name)))
684 (define (scm-tex-output)
685 (eval (tex-scm 'all-definitions)))
687 (define (scm-ps-output)
688 (eval (ps-scm 'all-definitions)))
691 ; Russ McManus, <mcmanus@IDT.NET>
693 ; I use the following, which should definitely be provided somewhere
694 ; in guile, but isn't, AFAIK:
698 (define (hash-table-for-each fn ht)
700 ((= i (vector-length ht)))
701 (do ((alist (vector-ref ht i) (cdr alist)))
703 (fn (car (car alist)) (cdr (car alist))))))
705 (define (hash-table-map fn ht)
708 ((= i (vector-length ht)) (reverse ret-ls))
709 (do ((alist (vector-ref ht i) (cdr alist)))
711 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
714 ;; guile-1.3.4 has list->string
715 (define (scmlist->string exp)
718 ;; obsolete, maybe handy for testing
719 ;; print a SCM expression. Isn't this part of the std lib?
720 (define (xxscmlist->string exp)
722 ((null? (car exp)) (begin (display ("urg:") (newline))))
723 ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
724 ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
725 ;; howto check for quote?
726 (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
729 (define (scm->string exp)
731 ((pair? exp) (string-append "(" (scmlist->string exp)))
732 ((number? exp) (number->string exp))
733 ((symbol? exp) (symbol->string exp))
734 ((string? exp) (string-append "\"" exp "\""))
736 (else (begin (display "programming error: scm->string: ") (newline) "'"))