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))
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"))
351 (define (bezier-sandwich l thick)
352 (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
355 (define (start-line ht)
357 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
360 "}\\vss}\\interscoreline")
361 (define (stop-last-line)
363 (define (filledbox breapth width depth height)
365 "\\kern" (number->dim (- breapth))
366 "\\vrule width " (number->dim (+ breapth width))
367 "depth " (number->dim depth)
368 "height " (number->dim height) " "))
371 (string-append "\\hbox{" (output-tex-string s) "}"))
373 (define (tuplet ht gapx dx dy thick dir)
374 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
376 (define (volta h w thick vert_start vert_end)
377 (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
380 ;; The procedures listed below form the public interface of TeX-scm.
381 ;; (should merge the 2 lists)
382 (cond ((eq? action-name 'all-definitions)
384 (define font-load-command ,font-load-command)
386 (define bezier-sandwich ,bezier-sandwich)
387 (define bracket ,bracket)
389 (define crescendo ,crescendo)
390 (define dashed-slur ,dashed-slur)
391 (define decrescendo ,decrescendo)
392 (define end-output ,end-output)
393 (define experimental-on ,experimental-on)
394 (define filledbox ,filledbox)
395 (define font-def ,font-def)
396 (define font-switch ,font-switch)
397 (define header-end ,header-end)
398 (define lily-def ,lily-def)
399 (define header ,header)
400 (define invoke-char ,invoke-char)
401 (define invoke-dim1 ,invoke-dim1)
402 (define placebox ,placebox)
403 (define select-font ,select-font)
404 (define start-line ,start-line)
405 (define stop-line ,stop-line)
406 (define stop-last-line ,stop-last-line)
408 (define tuplet ,tuplet)
409 (define volta ,volta)
412 ((eq? action-name 'beam) beam)
413 ((eq? action-name 'tuplet) tuplet)
414 ((eq? action-name 'bracket) bracket)
415 ((eq? action-name 'crescendo) crescendo)
416 ((eq? action-name 'dashed-slur) dashed-slur)
417 ((eq? action-name 'decrescendo) decrescendo)
418 ((eq? action-name 'end-output) end-output)
419 ((eq? action-name 'experimental-on) experimental-on)
420 ((eq? action-name 'font-def) font-def)
421 ((eq? action-name 'font-switch) font-switch)
422 ((eq? action-name 'header-end) header-end)
423 ((eq? action-name 'lily-def) lily-def)
424 ((eq? action-name 'header) header)
425 ((eq? action-name 'invoke-char) invoke-char)
426 ((eq? action-name 'invoke-dim1) invoke-dim1)
427 ((eq? action-name 'placebox) placebox)
428 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
429 ((eq? action-name 'start-line) start-line)
430 ((eq? action-name 'stem) stem)
431 ((eq? action-name 'stop-line) stop-line)
432 ((eq? action-name 'stop-last-line) stop-last-line)
433 ((eq? action-name 'volta) volta)
434 (else (error "unknown tag -- PS-TEX " action-name))
440 (define (ps-scm action-name)
443 ;; alist containing fontname -> fontcommand assoc (both strings)
444 (define font-alist '())
445 (define font-count 0)
446 (define current-font "")
449 (define (cached-fontname i)
452 (make-string 1 (integer->char (+ 65 i)))))
454 (define (mag-to-size m)
455 (number->string (case m
458 (2 14) ; really: 14.400
459 (3 17) ; really: 17.280
460 (4 21) ; really: 20.736
461 (5 24) ; really: 24.888
462 (6 30) ; really: 29.856
466 (define (select-font font-name-symbol)
469 (c (assoc font-name-symbol font-name-alist))
474 (ly-warn (string-append
475 "Programming error: No such font known " (car font-name-symbol)))
476 "") ; issue no command
477 (string-append " " (cdr c) " "))
482 (define (font-load-command name-mag command)
486 (symbol->string (car name-mag))
488 (number->string (magstep (cdr name-mag)))
489 " 1000 div 12 mul scalefont setfont } bind def "
493 (define (beam width slope thick)
495 (numbers->string (list width slope thick)) " draw_beam" ))
498 (string-append "% " s))
500 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
502 (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
505 (invoke-char " show" i))
507 (define (crescendo thick w h cont )
509 (numbers->string (list w h (inexact->exact cont) thick))
512 (define (dashed-slur thick dash l)
514 (apply string-append (map control->string l))
515 (number->string thick)
518 (number->string (- (* thick dash) thick))
521 (number->string (* 2 thick))
522 " ] 0 draw_dashed_slur"))
524 (define (decrescendo thick w h cont)
526 (numbers->string (list w h (inexact->exact cont) thick))
527 " draw_decrescendo"))
533 (define (experimental-on) "")
535 (define (filledbox breapth width depth height)
536 (string-append (numbers->string (list breapth width depth height))
540 (define (font-def i s)
543 (substring s 0 (- (string-length s) 4))
544 " findfont 12 scalefont setfont} bind def \n"))
546 (define (font-switch i)
547 (string-append (font i) " "))
551 (ly-gulp-file "lilyponddefs.ps")
552 " {exch pop //systemdict /run get exec} "
553 (ly-gulp-file "lily.ps")
554 "{ exch pop //systemdict /run get exec } "
557 (define (lily-def key val)
559 (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
560 (string-append "/" key " {" val "} bind def\n")
561 (string-append "/" key " (" val ") def\n")
565 (define (header creator generate)
568 "%%Creator: " creator generate "\n"))
570 (define (invoke-char s i)
572 "(\\" (inexact->string i 8) ") " s " " ))
574 (define (invoke-dim1 s d)
576 (number->string (* d (/ 72.27 72))) " " s ))
578 (define (placebox x y s)
580 (number->string x) " " (number->string y) " {" s "} placebox "))
582 (define (bezier-sandwich l thick)
584 (apply string-append (map control->string l))
585 (number->string thick)
586 " draw_bezier_sandwich"))
588 (define (start-line height)
592 (define (stem breapth width depth height)
593 (string-append (numbers->string (list breapth width depth height))
600 (string-append "(" s ") show "))
603 (define (volta h w thick vert_start vert_end)
605 (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
608 (define (tuplet ht gap dx dy thick dir)
610 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
619 (cond ((eq? action-name 'all-definitions)
622 (define tuplet ,tuplet)
623 (define bracket ,bracket)
625 (define crescendo ,crescendo)
626 (define volta ,volta)
627 (define bezier-sandwich ,bezier-sandwich)
628 (define dashed-slur ,dashed-slur)
629 (define decrescendo ,decrescendo)
630 (define end-output ,end-output)
631 (define experimental-on ,experimental-on)
632 (define filledbox ,filledbox)
633 (define font-def ,font-def)
634 (define font-switch ,font-switch)
635 (define header-end ,header-end)
636 (define lily-def ,lily-def)
637 (define font-load-command ,font-load-command)
638 (define header ,header)
639 (define invoke-char ,invoke-char)
640 (define invoke-dim1 ,invoke-dim1)
641 (define placebox ,placebox)
642 (define select-font ,select-font)
643 (define start-line ,start-line)
645 (define stop-line ,stop-line)
646 (define stop-last-line ,stop-line)
649 ((eq? action-name 'tuplet) tuplet)
650 ((eq? action-name 'beam) beam)
651 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
652 ((eq? action-name 'bracket) bracket)
653 ((eq? action-name 'char) char)
654 ((eq? action-name 'crescendo) crescendo)
655 ((eq? action-name 'dashed-slur) dashed-slur)
656 ((eq? action-name 'decrescendo) decrescendo)
657 ((eq? action-name 'experimental-on) experimental-on)
658 ((eq? action-name 'filledbox) filledbox)
659 ((eq? action-name 'select-font) select-font)
660 ((eq? action-name 'volta) volta)
661 (else (error "unknown tag -- PS-SCM " action-name))
666 (define (gulp-file name)
667 (let* ((port (open-file name "r"))
668 (content (let loop ((text ""))
669 (let ((line (read-line port)))
670 (if (or (eof-object? line)
673 (loop (string-append text line "\n")))))))
677 (define (scm-gulp-file name)
680 (getenv 'LILYPONDPREFIX) "/ps") %load-path))
681 (let ((path (%search-load-path name)))
686 (define (scm-tex-output)
687 (eval (tex-scm 'all-definitions)))
689 (define (scm-ps-output)
690 (eval (ps-scm 'all-definitions)))
693 ; Russ McManus, <mcmanus@IDT.NET>
695 ; I use the following, which should definitely be provided somewhere
696 ; in guile, but isn't, AFAIK:
700 (define (hash-table-for-each fn ht)
702 ((= i (vector-length ht)))
703 (do ((alist (vector-ref ht i) (cdr alist)))
705 (fn (car (car alist)) (cdr (car alist))))))
707 (define (hash-table-map fn ht)
710 ((= i (vector-length ht)) (reverse ret-ls))
711 (do ((alist (vector-ref ht i) (cdr alist)))
713 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
716 ;; guile-1.3.4 has list->string
717 (define (scmlist->string exp)
720 ;; obsolete, maybe handy for testing
721 ;; print a SCM expression. Isn't this part of the std lib?
722 (define (xxscmlist->string exp)
724 ((null? (car exp)) (begin (display ("urg:") (newline))))
725 ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
726 ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
727 ;; howto check for quote?
728 (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
731 (define (scm->string exp)
733 ((pair? exp) (string-append "(" (scmlist->string exp)))
734 ((number? exp) (number->string exp))
735 ((symbol? exp) (symbol->string exp))
736 ((string? exp) (string-append "\"" exp "\""))
738 (else (begin (display "programming error: scm->string: ") (newline) "'"))
741 (define (index-cell cell dir)
749 ; How should a bar line behave at a break?
751 (define (break-barline glyph dir)
752 (let ((result (assoc glyph
753 '((":|:" . (":|" . "|:"))
756 ("|:" . ("|" . "|:"))
757 ("|." . ("|." . nil))
758 (":|" . (":|" . nil))
759 ("||" . ("||" . nil))
760 (".|." . (".|." . nil))
761 ("scorebar" . (nil . "scorepostbreak"))
762 ("brace" . (nil . "brace"))
763 ("bracket" . (nil . "bracket"))
767 (if (equal? result #f)
768 (ly-warn (string-append "Unknown bar glyph: `" glyph "'"))
769 (index-cell (cdr result) dir))