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))
17 (map (lambda (n) (string-append (number->string n ) " ")) l)))
19 (define (reduce operator list)
20 (if (null? (cdr list)) (car list)
21 (operator (car list) (reduce operator (cdr list)))
26 (define (glue-2-strings a b) (string-append a " " b))
30 (reduce glue-2-strings (map number->string l)))
32 (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
34 (define (number->octal-string x)
35 (let* ((n (inexact->exact x))
37 (n8 (quotient (- n (* n64 64)) 8)))
41 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
43 (define (inexact->string x radix)
44 (let ((n (inexact->exact x)))
45 (number->string n radix)))
51 (string-append (number->string (car c)) " ")
52 (string-append (number->string (cadr c)) " ")))
58 (make-string 1 (integer->char (+ (char->integer #\A) i)))
63 (define (scm-scm action-name)
66 (define security-paranoia #f)
69 ;; See documentation of Item::visibility_lambda_
70 (define (postbreak_only_visibility d) (if (= d 1) '(#f . #f) '(#t . #t)))
71 (define (spanbar_non_postbreak_visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
73 (define (non_postbreak_visibility d) (if (= d 1) '(#t . #t) '(#f . #f)))
74 (define (non_prebreak_visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
77 ;; Score_span_bars are only visible at start of line
78 ;; i.e. if break_dir == RIGHT == 1
79 (define Span_bar_engraver_visibility non_postbreak_visibility)
80 (define mark-visibility non_prebreak_visibility)
81 (define Span_score_bar_engraver_visibility postbreak_only_visibility)
82 (define Piano_bar_engraver_visibility postbreak_only_visibility)
83 (define Staff_group_bar_engraver_visibility postbreak_only_visibility)
85 ;; Spacing constants for prefatory matter.
87 ;; rules for this spacing are much more complicated than this. See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147
93 (("" "Clef_item") . (minimum-space 1.0))
94 (("" "Staff_bar") . (minimum-space 0.0))
95 (("" "Clef_item") . (minimum-space 1.0))
96 (("" "Key_item") . (minimum-space 0.5))
97 (("" "Span_bar") . (extra-space 0.0))
98 (("" "Time_signature") . (extra-space 0.0))
99 (("" "begin-of-note") . (minimum-space 1.5))
100 (("Clef_item" "Key_item") . (minimum-space 4.0))
101 (("Key_item" "Time_signature") . (extra-space 1.0))
102 (("Clef_item" "Time_signature") . (minimum-space 3.5))
103 (("Staff_bar" "Clef_item") . (minimum-space 1.0))
104 (("Clef_item" "Staff_bar") . (minimum-space 3.7))
105 (("Time_signature" "Staff_bar") . (minimum-space 2.0))
106 (("Key_item" "Staff_bar") . (extra-space 1.0))
107 (("Span_bar" "Clef_item") . (extra-space 1.0))
108 (("Clef_item" "Span_bar") . (minimum-space 3.7))
109 (("Time_signature" "Span_bar") . (minimum-space 2.0))
110 (("Key_item" "Span_bar") . (minimum-space 2.5))
111 (("Staff_bar" "Time_signature") . (minimum-space 1.5)) ;double check this.
112 (("Time_signature" "begin-of-note") . (extra-space 2.0)) ;double check this.
113 (("Key_item" "begin-of-note") . (extra-space 2.5))
114 (("Staff_bar" "begin-of-note") . (extra-space 1.0))
115 (("Clef_item" "begin-of-note") . (minimum-space 5.0))
116 (("" "Breathing_sign") . (minimum-space 0.0))
117 (("Breathing_sign" "Key_item") . (minimum-space 1.5))
118 (("Breathing_sign" "begin-of-note") . (minimum-space 1.0))
119 (("Breathing_sign" "Staff_bar") . (minimum-space 1.5))
120 (("Breathing_sign" "Clef_item") . (minimum-space 2.0))
124 (define (break-align-spacer this next)
125 (let ((entry (assoc `(,this ,next) space-alist)))
128 (begin (ly-warn (string-append "Unknown spacing pair `" this "', `" next "'"))
129 '(minimum-space 0.0)))))
137 ("dynamic" . "feta-din")
138 ("finger" . "feta-nummer")
139 ("typewriter" . "cmtt")
144 ("mark" . "feta-nummer")
145 ("number" . "feta-nummer")
146 ("volta" . "feta-nummer"))
149 (define script-alist '())
150 (define (articulation-to-scriptdef a)
151 (assoc a script-alist)
154 ;; Map style names to TeX font names. Return false if
155 ;; no font name found.
156 (define (style-to-cmr s)
161 (define (tex-scm action-name)
166 (define font-alist '())
167 (define font-count 0)
168 (define current-font "")
169 (define (clear-fontcache)
171 (set! font-alist '())
173 (set! current-font "")))
175 (define (cached-fontname i)
178 (make-string 1 (integer->char (+ 65 i)))))
180 (define (select-font font-name magnification)
181 (if (not (equal? font-name current-font))
182 (let* ((font-cmd (assoc font-name font-alist)))
183 (set! current-font font-name)
184 (if (eq? font-cmd #f)
186 (set! font-cmd (cached-fontname font-count))
187 (set! font-alist (acons font-name font-cmd font-alist))
188 (set! font-count (+ 1 font-count))
189 (if (equal? font-name "")
190 (error "Empty fontname -- SELECT-FONT"))
191 (if (> magnification 0)
192 (string-append "\\font" font-cmd "=" font-name
194 (number->string magnification) font-cmd)
195 (string-append "\\font" font-cmd "=" font-name font-cmd)))
201 (define (beam width slope thick)
202 (embedded-ps ((ps-scm 'beam) width slope thick)))
204 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
205 (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
207 (define (dashed-slur thick dash l)
208 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
210 (define (crescendo thick w h cont)
211 (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
214 (string-append "\\char" (inexact->string i 10) " "))
216 (define (decrescendo thick w h cont)
217 (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
219 ;This sets CTM so that you get to the currentpoint
220 ; by executing a 0 0 moveto
222 (define (embedded-ps s)
223 (string-append "\\embeddedps{" s "}"))
226 "\n\\EndLilyPondOutput")
228 (define (experimental-on)
231 (define (font-switch i)
235 (define (font-def i s)
237 "\\font" (font-switch i) "=" s "\n"))
242 (ly-gulp-file "lily.ps")
244 ;; (regexp-substitute/global #f "\n" (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
246 "\\input lilyponddefs \\turnOnPostScript"))
248 (define (header creator generate)
250 "%created by: " creator generate "\n"))
252 (define (invoke-char s i)
254 "\n\\" s "{" (inexact->string i 10) "}" ))
256 (define (invoke-dim1 s d)
258 "\n\\" s "{" (number->dim d) "}"))
263 ;; need to do something to make this really safe.
265 (define (output-tex-string s)
266 (if security-paranoia
267 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
270 (define (lily-def key val)
273 ; (regexp-substitute/global #f "_" (output-tex-string key) 'pre "X" 'post)
274 (output-tex-string key)
275 "{" (output-tex-string val) "}\n"))
277 (define (number->dim x)
279 (number->string (chop-decimal x)) " pt "))
281 (define (placebox x y s)
284 (number->dim y) "}{" (number->dim x) "}{" s "}"))
287 (define (pianobrace y staffht)
289 (minht (* 2 staffht))
293 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
294 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
299 (define (bezier-sandwich l thick)
300 (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
303 (define (start-line ht)
306 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
309 "}\\vss}\\interscoreline")
310 (define (stop-last-line)
312 (define (filledbox breapth width depth height)
314 "\\kern" (number->dim (- breapth))
315 "\\vrule width " (number->dim (+ breapth width))
316 "depth " (number->dim depth)
317 "height " (number->dim height) " "))
320 (string-append "\\hbox{" (output-tex-string s) "}"))
322 (define (tuplet ht gapx dx dy thick dir)
323 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
325 (define (volta h w thick vert_start vert_end)
326 (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
329 ;; The procedures listed below form the public interface of TeX-scm.
330 ;; (should merge the 2 lists)
331 (cond ((eq? action-name 'all-definitions)
334 (define bezier-sandwich ,bezier-sandwich)
335 (define bracket ,bracket)
337 (define crescendo ,crescendo)
338 (define dashed-slur ,dashed-slur)
339 (define decrescendo ,decrescendo)
340 (define end-output ,end-output)
341 (define experimental-on ,experimental-on)
342 (define filledbox ,filledbox)
343 (define font-def ,font-def)
344 (define font-switch ,font-switch)
345 (define header-end ,header-end)
346 (define lily-def ,lily-def)
347 (define header ,header)
348 (define invoke-char ,invoke-char)
349 (define invoke-dim1 ,invoke-dim1)
350 (define pianobrace ,pianobrace)
351 (define placebox ,placebox)
352 (define select-font ,select-font)
353 (define start-line ,start-line)
354 (define stop-line ,stop-line)
355 (define stop-last-line ,stop-last-line)
357 (define tuplet ,tuplet)
358 (define volta ,volta)
361 ((eq? action-name 'beam) beam)
362 ((eq? action-name 'tuplet) tuplet)
363 ((eq? action-name 'bracket) bracket)
364 ((eq? action-name 'crescendo) crescendo)
365 ((eq? action-name 'dashed-slur) dashed-slur)
366 ((eq? action-name 'decrescendo) decrescendo)
367 ((eq? action-name 'end-output) end-output)
368 ((eq? action-name 'experimental-on) experimental-on)
369 ((eq? action-name 'font-def) font-def)
370 ((eq? action-name 'font-switch) font-switch)
371 ((eq? action-name 'header-end) header-end)
372 ((eq? action-name 'lily-def) lily-def)
373 ((eq? action-name 'header) header)
374 ((eq? action-name 'invoke-char) invoke-char)
375 ((eq? action-name 'invoke-dim1) invoke-dim1)
376 ((eq? action-name 'placebox) placebox)
377 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
378 ((eq? action-name 'start-line) start-line)
379 ((eq? action-name 'stem) stem)
380 ((eq? action-name 'stop-line) stop-line)
381 ((eq? action-name 'stop-last-line) stop-last-line)
382 ((eq? action-name 'volta) volta)
383 (else (error "unknown tag -- PS-TEX " action-name))
388 (define (ps-scm action-name)
391 ;; alist containing fontname -> fontcommand assoc (both strings)
392 (define font-alist '())
393 (define font-count 0)
394 (define current-font "")
395 (define (clear-fontcache)
397 (set! font-alist '())
399 (set! current-font "")))
401 (define (cached-fontname i)
404 (make-string 1 (integer->char (+ 65 i)))))
406 (define (mag-to-size m)
407 (number->string (case m
410 ('2 14) ; really: 14.400
411 ('3 17) ; really: 17.280
412 ('4 21) ; really: 20.736
413 ('5 24) ; really: 24.888
414 ('6 30) ; really: 29.856
417 (define (select-font font-name magnification)
418 (define font-cmd (assoc font-name font-alist))
419 (if (not (equal? font-name current-font))
421 (set! current-font font-name)
422 (if (eq? font-cmd #f)
424 (set! font-cmd (cached-fontname font-count))
425 (set! font-alist (acons font-name font-cmd font-alist))
426 (set! font-count (+ 1 font-count))
427 (string-append "\n/" font-cmd " {/"
428 font-name " findfont "
429 (mag-to-size magnification)
430 " scalefont setfont} bind def \n"
432 (string-append (cdr font-cmd) " ")))
433 ; font-name == current-font no switch needed
437 (define (beam width slope thick)
439 (numbers->string (list width slope thick)) " draw_beam" ))
441 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
443 (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
446 (invoke-char " show" i))
448 (define (crescendo thick w h cont )
450 (numbers->string (list w h (inexact->exact cont) thick))
453 (define (dashed-slur thick dash l)
455 (apply string-append (map control->string l))
456 (number->string thick)
459 (number->string (- (* thick dash) thick))
462 (number->string (* 2 thick))
463 " ] 0 draw_dashed_slur"))
465 (define (decrescendo thick w h cont)
467 (numbers->string (list w h (inexact->exact cont) thick))
468 " draw_decrescendo"))
474 (define (experimental-on) "")
476 (define (filledbox breapth width depth height)
477 (string-append (numbers->string (list breapth width depth height))
481 (define (font-def i s)
484 (substring s 0 (- (string-length s) 4))
485 " findfont 12 scalefont setfont} bind def \n"))
487 (define (font-switch i)
488 (string-append (font i) " "))
492 (ly-gulp-file "lilyponddefs.ps")
493 " {exch pop //systemdict /run get exec} "
494 (ly-gulp-file "lily.ps")
495 "{ exch pop //systemdict /run get exec } "
498 (define (lily-def key val)
500 (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
501 (string-append "/" key " {" val "} bind def\n")
502 (string-append "/" key " (" val ") def\n")
506 (define (header creator generate)
509 "%%Creator: " creator generate "\n"))
511 (define (invoke-char s i)
513 "(\\" (inexact->string i 8) ") " s " " ))
515 (define (invoke-dim1 s d)
517 (number->string (* d (/ 72.27 72))) " " s ))
519 (define (placebox x y s)
521 (number->string x) " " (number->string y) " {" s "} placebox "))
523 (define (pianobrace y staffht)
525 (minht (* 2 staffht))
529 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
530 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
535 (define (bezier-sandwich l thick)
537 (apply string-append (map control->string l))
538 (number->string thick)
539 " draw_bezier_sandwich"))
541 (define (start-line height)
546 (define (stem breapth width depth height)
547 (string-append (numbers->string (list breapth width depth height))
554 (string-append "(" s ") show "))
557 (define (volta h w thick vert_start vert_end)
559 (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
562 (define (tuplet ht gap dx dy thick dir)
564 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
573 (cond ((eq? action-name 'all-definitions)
576 (define tuplet ,tuplet)
577 (define bracket ,bracket)
579 (define crescendo ,crescendo)
580 (define volta ,volta)
581 (define bezier-sandwich ,bezier-sandwich)
582 (define dashed-slur ,dashed-slur)
583 (define decrescendo ,decrescendo)
584 (define end-output ,end-output)
585 (define experimental-on ,experimental-on)
586 (define filledbox ,filledbox)
587 (define font-def ,font-def)
588 (define font-switch ,font-switch)
589 (define pianobrace ,pianobrace)
590 (define header-end ,header-end)
591 (define lily-def ,lily-def)
592 (define header ,header)
593 (define invoke-char ,invoke-char)
594 (define invoke-dim1 ,invoke-dim1)
595 (define placebox ,placebox)
596 (define select-font ,select-font)
597 (define start-line ,start-line)
599 (define stop-line ,stop-line)
600 (define stop-last-line ,stop-line)
603 ((eq? action-name 'tuplet) tuplet)
604 ((eq? action-name 'beam) beam)
605 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
606 ((eq? action-name 'bracket) bracket)
607 ((eq? action-name 'char) char)
608 ((eq? action-name 'crescendo) crescendo)
609 ((eq? action-name 'dashed-slur) dashed-slur)
610 ((eq? action-name 'decrescendo) decrescendo)
611 ((eq? action-name 'experimental-on) experimental-on)
612 ((eq? action-name 'filledbox) filledbox)
613 ((eq? action-name 'select-font) select-font)
614 ((eq? action-name 'volta) volta)
615 (else (error "unknown tag -- PS-SCM " action-name))
620 ; Russ McManus, <mcmanus@IDT.NET>
622 ; I use the following, which should definitely be provided somewhere
623 ; in guile, but isn't, AFAIK:
627 (define (hash-table-for-each fn ht)
629 ((= i (vector-length ht)))
630 (do ((alist (vector-ref ht i) (cdr alist)))
632 (fn (car (car alist)) (cdr (car alist))))))
634 (define (hash-table-map fn ht)
637 ((= i (vector-length ht)) (reverse ret-ls))
638 (do ((alist (vector-ref ht i) (cdr alist)))
640 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
643 ;;;; print a SCM expression. Isn't this part of the std lib?
645 (define (scmlist->string exp)
647 ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
648 ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
649 (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
652 (define (scm->string exp)
654 ((pair? exp) (string-append "(" (scmlist->string exp)))
655 ((number? exp) (number->string exp))
656 ((symbol? exp) (symbol->string exp))
657 ((string? exp) (string-append "\"" exp "\""))
660 (define (test-scm->string)
661 (list (scmlist->string '(a))
662 (scmlist->string '(a b))
663 (scmlist->string '(a b . c))
667 (scm->string '(a b ))
668 (scm->string '(a b . c))
669 (scm->string '(a b (c . d)))
670 (scm->string '(a "bla" (c . 1.5)))