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)))
205 (embedded-ps ((ps-scm 'bracket) h)))
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)
271 (define (lily-def key val)
274 ; (regexp-substitute/global #f "_" (output-tex-string key) 'pre "X" 'post)
275 (output-tex-string key)
276 "{" (output-tex-string val) "}\n"))
278 (define (number->dim x)
280 (number->string (chop-decimal x)) " pt "))
282 (define (placebox x y s)
285 (number->dim y) "}{" (number->dim x) "}{" s "}"))
288 (define (pianobrace y staffht)
290 (minht (* 2 staffht))
294 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
295 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
300 (define (bezier-sandwich l thick)
301 (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
304 (define (start-line ht)
307 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
310 "}\\vss}\\interscoreline")
311 (define (stop-last-line)
313 (define (filledbox breapth width depth height)
315 "\\kern" (number->dim (- breapth))
316 "\\vrule width " (number->dim (+ breapth width))
317 "depth " (number->dim depth)
318 "height " (number->dim height) " "))
321 (string-append "\\hbox{" (output-tex-string s) "}"))
323 (define (tuplet ht gapx dx dy thick dir)
324 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
326 (define (volta h w thick vert_start vert_end)
327 (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
330 ;; The procedures listed below form the public interface of TeX-scm.
331 ;; (should merge the 2 lists)
332 (cond ((eq? action-name 'all-definitions)
335 (define bezier-sandwich ,bezier-sandwich)
336 (define bracket ,bracket)
338 (define crescendo ,crescendo)
339 (define dashed-slur ,dashed-slur)
340 (define decrescendo ,decrescendo)
341 (define end-output ,end-output)
342 (define experimental-on ,experimental-on)
343 (define filledbox ,filledbox)
344 (define font-def ,font-def)
345 (define font-switch ,font-switch)
346 (define header-end ,header-end)
347 (define lily-def ,lily-def)
348 (define header ,header)
349 (define invoke-char ,invoke-char)
350 (define invoke-dim1 ,invoke-dim1)
351 (define pianobrace ,pianobrace)
352 (define placebox ,placebox)
353 (define select-font ,select-font)
354 (define start-line ,start-line)
355 (define stop-line ,stop-line)
356 (define stop-last-line ,stop-last-line)
358 (define tuplet ,tuplet)
359 (define volta ,volta)
362 ((eq? action-name 'beam) beam)
363 ((eq? action-name 'tuplet) tuplet)
364 ((eq? action-name 'bracket) bracket)
365 ((eq? action-name 'crescendo) crescendo)
366 ((eq? action-name 'dashed-slur) dashed-slur)
367 ((eq? action-name 'decrescendo) decrescendo)
368 ((eq? action-name 'end-output) end-output)
369 ((eq? action-name 'experimental-on) experimental-on)
370 ((eq? action-name 'font-def) font-def)
371 ((eq? action-name 'font-switch) font-switch)
372 ((eq? action-name 'header-end) header-end)
373 ((eq? action-name 'lily-def) lily-def)
374 ((eq? action-name 'header) header)
375 ((eq? action-name 'invoke-char) invoke-char)
376 ((eq? action-name 'invoke-dim1) invoke-dim1)
377 ((eq? action-name 'placebox) placebox)
378 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
379 ((eq? action-name 'start-line) start-line)
380 ((eq? action-name 'stem) stem)
381 ((eq? action-name 'stop-line) stop-line)
382 ((eq? action-name 'stop-last-line) stop-last-line)
383 ((eq? action-name 'volta) volta)
384 (else (error "unknown tag -- PS-TEX " action-name))
389 (define (ps-scm action-name)
392 ;; alist containing fontname -> fontcommand assoc (both strings)
393 (define font-alist '())
394 (define font-count 0)
395 (define current-font "")
396 (define (clear-fontcache)
398 (set! font-alist '())
400 (set! current-font "")))
402 (define (cached-fontname i)
405 (make-string 1 (integer->char (+ 65 i)))))
407 (define (mag-to-size m)
408 (number->string (case m
411 ('2 14) ; really: 14.400
412 ('3 17) ; really: 17.280
413 ('4 21) ; really: 20.736
414 ('5 24) ; really: 24.888
415 ('6 30) ; really: 29.856
418 (define (select-font font-name magnification)
419 (define font-cmd (assoc font-name font-alist))
420 (if (not (equal? font-name current-font))
422 (set! current-font font-name)
423 (if (eq? font-cmd #f)
425 (set! font-cmd (cached-fontname font-count))
426 (set! font-alist (acons font-name font-cmd font-alist))
427 (set! font-count (+ 1 font-count))
428 (string-append "\n/" font-cmd " {/"
429 font-name " findfont "
430 (mag-to-size magnification)
431 " scalefont setfont} bind def \n"
433 (string-append (cdr font-cmd) " ")))
434 ; font-name == current-font no switch needed
438 (define (beam width slope thick)
440 (numbers->string (list width slope thick)) " draw_beam" ))
443 (invoke-dim1 " draw_bracket" h))
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)))