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)))))
135 ;; this is silly, can't we use something like
136 ;; roman-0, roman-1 roman+1 ?
139 ("dynamic" . "feta-din")
143 ("finger" . "feta-nummer")
144 ("typewriter" . "cmtt")
150 ("mark" . "feta-nummer")
151 ("number" . "feta-nummer")
152 ("volta" . "feta-nummer"))
155 (define script-alist '())
156 (define (articulation-to-scriptdef a)
157 (assoc a script-alist)
160 ;; Map style names to TeX font names. Return false if
161 ;; no font name found.
162 (define (style-to-cmr s)
167 (define (tex-scm action-name)
172 (define font-alist '())
173 (define font-count 0)
174 (define current-font "")
175 (define (clear-fontcache)
177 (set! font-alist '())
179 (set! current-font "")))
181 (define (cached-fontname i)
184 (make-string 1 (integer->char (+ 65 i)))))
186 (define (select-font font-name magnification)
187 (if (not (equal? font-name current-font))
188 (let* ((font-cmd (assoc font-name font-alist)))
189 (set! current-font font-name)
190 (if (eq? font-cmd #f)
192 (set! font-cmd (cached-fontname font-count))
193 (set! font-alist (acons font-name font-cmd font-alist))
194 (set! font-count (+ 1 font-count))
195 (if (equal? font-name "")
196 (error "Empty fontname -- SELECT-FONT"))
197 (if (> magnification 0)
198 (string-append "\\font" font-cmd "=" font-name
200 (number->string magnification) font-cmd)
201 (string-append "\\font" font-cmd "=" font-name font-cmd)))
207 (define (beam width slope thick)
208 (embedded-ps ((ps-scm 'beam) width slope thick)))
210 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
211 (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
213 (define (dashed-slur thick dash l)
214 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
216 (define (crescendo thick w h cont)
217 (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
220 (string-append "\\char" (inexact->string i 10) " "))
222 (define (decrescendo thick w h cont)
223 (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
225 ;This sets CTM so that you get to the currentpoint
226 ; by executing a 0 0 moveto
228 (define (embedded-ps s)
229 (string-append "\\embeddedps{" s "}"))
232 "\n\\EndLilyPondOutput")
234 (define (experimental-on)
237 (define (font-switch i)
241 (define (font-def i s)
243 "\\font" (font-switch i) "=" s "\n"))
248 (ly-gulp-file "lily.ps")
250 ;; (regexp-substitute/global #f "\n" (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
252 "\\input lilyponddefs \\turnOnPostScript"))
254 (define (header creator generate)
256 "%created by: " creator generate "\n"))
258 (define (invoke-char s i)
260 "\n\\" s "{" (inexact->string i 10) "}" ))
262 (define (invoke-dim1 s d)
264 "\n\\" s "{" (number->dim d) "}"))
269 ;; need to do something to make this really safe.
271 (define (output-tex-string s)
272 (if security-paranoia
273 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
276 (define (lily-def key val)
279 ; (regexp-substitute/global #f "_" (output-tex-string key) 'pre "X" 'post)
280 (output-tex-string key)
281 "{" (output-tex-string val) "}\n"))
283 (define (number->dim x)
285 (number->string (chop-decimal x)) " pt "))
287 (define (placebox x y s)
290 (number->dim y) "}{" (number->dim x) "}{" s "}"))
293 (define (pianobrace y staffht)
295 (minht (* 2 staffht))
299 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
300 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
305 (define (bezier-sandwich l thick)
306 (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
309 (define (start-line ht)
312 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
315 "}\\vss}\\interscoreline")
316 (define (stop-last-line)
318 (define (filledbox breapth width depth height)
320 "\\kern" (number->dim (- breapth))
321 "\\vrule width " (number->dim (+ breapth width))
322 "depth " (number->dim depth)
323 "height " (number->dim height) " "))
326 (string-append "\\hbox{" (output-tex-string s) "}"))
328 (define (tuplet ht gapx dx dy thick dir)
329 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
331 (define (volta h w thick vert_start vert_end)
332 (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
335 ;; The procedures listed below form the public interface of TeX-scm.
336 ;; (should merge the 2 lists)
337 (cond ((eq? action-name 'all-definitions)
340 (define bezier-sandwich ,bezier-sandwich)
341 (define bracket ,bracket)
343 (define crescendo ,crescendo)
344 (define dashed-slur ,dashed-slur)
345 (define decrescendo ,decrescendo)
346 (define end-output ,end-output)
347 (define experimental-on ,experimental-on)
348 (define filledbox ,filledbox)
349 (define font-def ,font-def)
350 (define font-switch ,font-switch)
351 (define header-end ,header-end)
352 (define lily-def ,lily-def)
353 (define header ,header)
354 (define invoke-char ,invoke-char)
355 (define invoke-dim1 ,invoke-dim1)
356 (define pianobrace ,pianobrace)
357 (define placebox ,placebox)
358 (define select-font ,select-font)
359 (define start-line ,start-line)
360 (define stop-line ,stop-line)
361 (define stop-last-line ,stop-last-line)
363 (define tuplet ,tuplet)
364 (define volta ,volta)
367 ((eq? action-name 'beam) beam)
368 ((eq? action-name 'tuplet) tuplet)
369 ((eq? action-name 'bracket) bracket)
370 ((eq? action-name 'crescendo) crescendo)
371 ((eq? action-name 'dashed-slur) dashed-slur)
372 ((eq? action-name 'decrescendo) decrescendo)
373 ((eq? action-name 'end-output) end-output)
374 ((eq? action-name 'experimental-on) experimental-on)
375 ((eq? action-name 'font-def) font-def)
376 ((eq? action-name 'font-switch) font-switch)
377 ((eq? action-name 'header-end) header-end)
378 ((eq? action-name 'lily-def) lily-def)
379 ((eq? action-name 'header) header)
380 ((eq? action-name 'invoke-char) invoke-char)
381 ((eq? action-name 'invoke-dim1) invoke-dim1)
382 ((eq? action-name 'placebox) placebox)
383 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
384 ((eq? action-name 'start-line) start-line)
385 ((eq? action-name 'stem) stem)
386 ((eq? action-name 'stop-line) stop-line)
387 ((eq? action-name 'stop-last-line) stop-last-line)
388 ((eq? action-name 'volta) volta)
389 (else (error "unknown tag -- PS-TEX " action-name))
394 (define (ps-scm action-name)
397 ;; alist containing fontname -> fontcommand assoc (both strings)
398 (define font-alist '())
399 (define font-count 0)
400 (define current-font "")
401 (define (clear-fontcache)
403 (set! font-alist '())
405 (set! current-font "")))
407 (define (cached-fontname i)
410 (make-string 1 (integer->char (+ 65 i)))))
412 (define (mag-to-size m)
413 (number->string (case m
416 ('2 14) ; really: 14.400
417 ('3 17) ; really: 17.280
418 ('4 21) ; really: 20.736
419 ('5 24) ; really: 24.888
420 ('6 30) ; really: 29.856
423 (define (select-font font-name magnification)
424 (define font-cmd (assoc font-name font-alist))
425 (if (not (equal? font-name current-font))
427 (set! current-font font-name)
428 (if (eq? font-cmd #f)
430 (set! font-cmd (cached-fontname font-count))
431 (set! font-alist (acons font-name font-cmd font-alist))
432 (set! font-count (+ 1 font-count))
433 (string-append "\n/" font-cmd " {/"
434 font-name " findfont "
435 (mag-to-size magnification)
436 " scalefont setfont} bind def \n"
438 (string-append (cdr font-cmd) " ")))
439 ; font-name == current-font no switch needed
443 (define (beam width slope thick)
445 (numbers->string (list width slope thick)) " draw_beam" ))
447 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
449 (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
452 (invoke-char " show" i))
454 (define (crescendo thick w h cont )
456 (numbers->string (list w h (inexact->exact cont) thick))
459 (define (dashed-slur thick dash l)
461 (apply string-append (map control->string l))
462 (number->string thick)
465 (number->string (- (* thick dash) thick))
468 (number->string (* 2 thick))
469 " ] 0 draw_dashed_slur"))
471 (define (decrescendo thick w h cont)
473 (numbers->string (list w h (inexact->exact cont) thick))
474 " draw_decrescendo"))
480 (define (experimental-on) "")
482 (define (filledbox breapth width depth height)
483 (string-append (numbers->string (list breapth width depth height))
487 (define (font-def i s)
490 (substring s 0 (- (string-length s) 4))
491 " findfont 12 scalefont setfont} bind def \n"))
493 (define (font-switch i)
494 (string-append (font i) " "))
498 (ly-gulp-file "lilyponddefs.ps")
499 " {exch pop //systemdict /run get exec} "
500 (ly-gulp-file "lily.ps")
501 "{ exch pop //systemdict /run get exec } "
504 (define (lily-def key val)
506 (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
507 (string-append "/" key " {" val "} bind def\n")
508 (string-append "/" key " (" val ") def\n")
512 (define (header creator generate)
515 "%%Creator: " creator generate "\n"))
517 (define (invoke-char s i)
519 "(\\" (inexact->string i 8) ") " s " " ))
521 (define (invoke-dim1 s d)
523 (number->string (* d (/ 72.27 72))) " " s ))
525 (define (placebox x y s)
527 (number->string x) " " (number->string y) " {" s "} placebox "))
529 (define (pianobrace y staffht)
531 (minht (* 2 staffht))
535 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
536 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
541 (define (bezier-sandwich l thick)
543 (apply string-append (map control->string l))
544 (number->string thick)
545 " draw_bezier_sandwich"))
547 (define (start-line height)
552 (define (stem breapth width depth height)
553 (string-append (numbers->string (list breapth width depth height))
560 (string-append "(" s ") show "))
563 (define (volta h w thick vert_start vert_end)
565 (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
568 (define (tuplet ht gap dx dy thick dir)
570 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
579 (cond ((eq? action-name 'all-definitions)
582 (define tuplet ,tuplet)
583 (define bracket ,bracket)
585 (define crescendo ,crescendo)
586 (define volta ,volta)
587 (define bezier-sandwich ,bezier-sandwich)
588 (define dashed-slur ,dashed-slur)
589 (define decrescendo ,decrescendo)
590 (define end-output ,end-output)
591 (define experimental-on ,experimental-on)
592 (define filledbox ,filledbox)
593 (define font-def ,font-def)
594 (define font-switch ,font-switch)
595 (define pianobrace ,pianobrace)
596 (define header-end ,header-end)
597 (define lily-def ,lily-def)
598 (define header ,header)
599 (define invoke-char ,invoke-char)
600 (define invoke-dim1 ,invoke-dim1)
601 (define placebox ,placebox)
602 (define select-font ,select-font)
603 (define start-line ,start-line)
605 (define stop-line ,stop-line)
606 (define stop-last-line ,stop-line)
609 ((eq? action-name 'tuplet) tuplet)
610 ((eq? action-name 'beam) beam)
611 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
612 ((eq? action-name 'bracket) bracket)
613 ((eq? action-name 'char) char)
614 ((eq? action-name 'crescendo) crescendo)
615 ((eq? action-name 'dashed-slur) dashed-slur)
616 ((eq? action-name 'decrescendo) decrescendo)
617 ((eq? action-name 'experimental-on) experimental-on)
618 ((eq? action-name 'filledbox) filledbox)
619 ((eq? action-name 'select-font) select-font)
620 ((eq? action-name 'volta) volta)
621 (else (error "unknown tag -- PS-SCM " action-name))
626 ; Russ McManus, <mcmanus@IDT.NET>
628 ; I use the following, which should definitely be provided somewhere
629 ; in guile, but isn't, AFAIK:
633 (define (hash-table-for-each fn ht)
635 ((= i (vector-length ht)))
636 (do ((alist (vector-ref ht i) (cdr alist)))
638 (fn (car (car alist)) (cdr (car alist))))))
640 (define (hash-table-map fn ht)
643 ((= i (vector-length ht)) (reverse ret-ls))
644 (do ((alist (vector-ref ht i) (cdr alist)))
646 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
649 ;;;; print a SCM expression. Isn't this part of the std lib?
651 (define (scmlist->string exp)
653 ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
654 ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
655 (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
658 (define (scm->string exp)
660 ((pair? exp) (string-append "(" (scmlist->string exp)))
661 ((number? exp) (number->string exp))
662 ((symbol? exp) (symbol->string exp))
663 ((string? exp) (string-append "\"" exp "\""))
666 (define (test-scm->string)
667 (list (scmlist->string '(a))
668 (scmlist->string '(a b))
669 (scmlist->string '(a b . c))
673 (scm->string '(a b ))
674 (scm->string '(a b . c))
675 (scm->string '(a b (c . d)))
676 (scm->string '(a "bla" (c . 1.5)))