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)
14 (map (lambda (n) (string-append (number->string n ) " ")) l)))
16 (define (reduce operator list)
17 (if (null? (cdr list)) (car list)
18 (operator (car list) (reduce operator (cdr list)))
23 (define (glue-2-strings a b) (string-append a " " b))
27 (reduce glue-2-strings (map number->string l)))
29 (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
31 (define (number->octal-string x)
32 (let* ((n (inexact->exact x))
34 (n8 (quotient (- n (* n64 64)) 8)))
38 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
40 (define (inexact->string x radix)
41 (let ((n (inexact->exact x)))
42 (number->string n radix)))
48 (string-append (number->string (car c)) " ")
49 (string-append (number->string (cadr c)) " ")))
55 (make-string 1 (integer->char (+ (char->integer #\A) i)))
60 (define (scm-scm action-name)
63 (define security-paranoia #f)
66 ;; See documentation of Item::visibility_lambda_
67 (define (postbreak_only_visibility d) (if (= d 1) '(#f . #f) '(#t . #t)))
68 (define (spanbar_non_postbreak_visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
70 (define (non_postbreak_visibility d) (if (= d 1) '(#t . #t) '(#f . #f)))
71 (define (non_prebreak_visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
74 ;; Score_span_bars are only visible at start of line
75 ;; i.e. if break_dir == RIGHT == 1
76 (define Span_bar_engraver_visibility non_postbreak_visibility)
77 (define mark-visibility non_prebreak_visibility)
78 (define Span_score_bar_engraver_visibility postbreak_only_visibility)
79 (define Piano_bar_engraver_visibility postbreak_only_visibility)
80 (define Staff_group_bar_engraver_visibility postbreak_only_visibility)
82 ;; Spacing constants for prefatory matter.
84 ;; rules for this spacing are much more complicated than this. See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147
90 (("" "Clef_item") . (minimum_space 1.0))
91 (("" "Staff_bar") . (minimum_space 0.0))
92 (("" "Clef_item") . (minimum_space 1.0))
93 (("" "Key_item") . (minimum_space 0.5))
94 (("" "Span_bar") . (extra_space 0.0))
95 (("" "Time_signature") . (extra_space 0.0))
96 (("" "begin-of-note") . (minimum_space 1.5))
97 (("Clef_item" "Key_item") . (minimum_space 4.0))
98 (("Key_item" "Time_signature") . (extra_space 1.0))
99 (("Clef_item" "Time_signature") . (minimum_space 3.5))
100 (("Staff_bar" "Clef_item") . (minimum_space 1.0))
101 (("Clef_item" "Staff_bar") . (minimum_space 3.7))
102 (("Time_signature" "Staff_bar") . (minimum_space 2.0))
103 (("Key_item" "Staff_bar") . (extra_space 1.0))
104 (("Span_bar" "Clef_item") . (extra_space 1.0))
105 (("Clef_item" "Span_bar") . (minimum_space 3.7))
106 (("Time_signature" "Span_bar") . (minimum_space 2.0))
107 (("Key_item" "Span_bar") . (minimum_space 2.5))
108 (("Staff_bar" "Time_signature") . (minimum_space 1.5)) ;double check this.
109 (("Time_signature" "begin-of-note") . (extra_space 2.0)) ;double check this.
110 (("Key_item" "begin-of-note") . (extra_space 2.5))
111 (("Staff_bar" "begin-of-note") . (extra_space 1.0))
112 (("Clef_item" "begin-of-note") . (minimum_space 5.0))
113 (("" "Breathing_sign") . (minimum_space 0.0))
114 (("Breathing_sign" "Key_item") . (minimum_space 1.5))
115 (("Breathing_sign" "begin-of-note") . (minimum_space 1.0))
116 (("Breathing_sign" "Staff_bar") . (minimum_space 1.5))
117 (("Breathing_sign" "Clef_item") . (minimum_space 2.0))
121 (define (break-align-spacer this next)
122 (let ((entry (assoc `(,this ,next) space-alist)))
125 (begin (ly-warn (string-append "Unknown spacing pair `" this "', `" next "'"))
126 '(minimum_space 0.0)))))
134 ("dynamic" . "feta-din")
135 ("finger" . "feta-nummer")
136 ("typewriter" . "cmtt")
141 ("mark" . "feta-nummer")
142 ("number" . "feta-nummer")
143 ("volta" . "feta-nummer"))
146 (define script-alist '())
147 (define (articulation-to-scriptdef a)
148 (assoc a script-alist)
151 ;; Map style names to TeX font names. Return false if
152 ;; no font name found.
153 (define (style-to-cmr s)
158 (define (tex-scm action-name)
163 (define font-alist '())
164 (define font-count 0)
165 (define current-font "")
166 (define (clear-fontcache)
168 (set! font-alist '())
170 (set! current-font "")))
172 (define (cached-fontname i)
175 (make-string 1 (integer->char (+ 65 i)))))
177 (define (select-font font-name magnification)
178 (if (not (equal? font-name current-font))
179 (let* ((font-cmd (assoc font-name font-alist)))
180 (set! current-font font-name)
181 (if (eq? font-cmd #f)
183 (set! font-cmd (cached-fontname font-count))
184 (set! font-alist (acons font-name font-cmd font-alist))
185 (set! font-count (+ 1 font-count))
186 (if (equal? font-name "")
187 (error "Empty fontname -- SELECT-FONT"))
188 (if (> magnification 0)
189 (string-append "\\font" font-cmd "=" font-name
191 (number->string magnification) font-cmd)
192 (string-append "\\font" font-cmd "=" font-name font-cmd)))
198 (define (beam width slope thick)
199 (embedded-ps ((ps-scm 'beam) width slope thick)))
202 (embedded-ps ((ps-scm 'bracket) h)))
204 (define (dashed-slur thick dash l)
205 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
207 (define (crescendo thick w h cont)
208 (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
211 (string-append "\\char" (inexact->string i 10) " "))
213 (define (decrescendo thick w h cont)
214 (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
216 ;This sets CTM so that you get to the currentpoint
217 ; by executing a 0 0 moveto
219 (define (embedded-ps s)
220 (string-append "\\embeddedps{" s "}"))
223 "\n\\EndLilyPondOutput")
225 (define (experimental-on)
228 (define (font-switch i)
232 (define (font-def i s)
234 "\\font" (font-switch i) "=" s "\n"))
239 (ly-gulp-file "lily.ps")
241 ;; (regexp-substitute/global #f "\n" (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
243 "\\input lilyponddefs \\turnOnPostScript"))
245 (define (header creator generate)
247 "%created by: " creator generate "\n"))
249 (define (invoke-char s i)
251 "\n\\" s "{" (inexact->string i 10) "}" ))
253 (define (invoke-dim1 s d)
255 "\n\\" s "{" (number->dim d) "}"))
260 ;; need to do something to make this really safe.
262 (define (output-tex-string s)
263 (if security-paranoia
264 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
268 (define (lily-def key val)
270 "\\def\\" (output-tex-string key) "{" (output-tex-string val) "}\n"))
272 (define (number->dim x)
274 (number->string (chop-decimal x)) " pt "))
276 (define (placebox x y s)
279 (number->dim y) "}{" (number->dim x) "}{" s "}"))
282 (define (pianobrace y staffht)
284 (minht (* 2 staffht))
288 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
289 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
294 (define (bezier-sandwich l thick)
295 (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
298 (define (start-line ht)
301 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
304 "}\\vss}\\interscoreline")
305 (define (stop-last-line)
307 (define (filledbox breapth width depth height)
309 "\\kern" (number->dim (- breapth))
310 "\\vrule width " (number->dim (+ breapth width))
311 "depth " (number->dim depth)
312 "height " (number->dim height) " "))
315 (string-append "\\hbox{" (output-tex-string s) "}"))
317 (define (tuplet ht gapx dx dy thick dir)
318 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
320 (define (volta h w thick vert_start vert_end)
321 (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
324 ;; The procedures listed below form the public interface of TeX-scm.
325 ;; (should merge the 2 lists)
326 (cond ((eq? action-name 'all-definitions)
329 (define bezier-sandwich ,bezier-sandwich)
330 (define bracket ,bracket)
332 (define crescendo ,crescendo)
333 (define dashed-slur ,dashed-slur)
334 (define decrescendo ,decrescendo)
335 (define end-output ,end-output)
336 (define experimental-on ,experimental-on)
337 (define filledbox ,filledbox)
338 (define font-def ,font-def)
339 (define font-switch ,font-switch)
340 (define header-end ,header-end)
341 (define lily-def ,lily-def)
342 (define header ,header)
343 (define invoke-char ,invoke-char)
344 (define invoke-dim1 ,invoke-dim1)
345 (define pianobrace ,pianobrace)
346 (define placebox ,placebox)
347 (define select-font ,select-font)
348 (define start-line ,start-line)
349 (define stop-line ,stop-line)
350 (define stop-last-line ,stop-last-line)
352 (define tuplet ,tuplet)
353 (define volta ,volta)
356 ((eq? action-name 'beam) beam)
357 ((eq? action-name 'tuplet) tuplet)
358 ((eq? action-name 'bracket) bracket)
359 ((eq? action-name 'crescendo) crescendo)
360 ((eq? action-name 'dashed-slur) dashed-slur)
361 ((eq? action-name 'decrescendo) decrescendo)
362 ((eq? action-name 'end-output) end-output)
363 ((eq? action-name 'experimental-on) experimental-on)
364 ((eq? action-name 'font-def) font-def)
365 ((eq? action-name 'font-switch) font-switch)
366 ((eq? action-name 'header-end) header-end)
367 ((eq? action-name 'lily-def) lily-def)
368 ((eq? action-name 'header) header)
369 ((eq? action-name 'invoke-char) invoke-char)
370 ((eq? action-name 'invoke-dim1) invoke-dim1)
371 ((eq? action-name 'placebox) placebox)
372 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
373 ((eq? action-name 'start-line) start-line)
374 ((eq? action-name 'stem) stem)
375 ((eq? action-name 'stop-line) stop-line)
376 ((eq? action-name 'stop-last-line) stop-last-line)
377 ((eq? action-name 'volta) volta)
378 (else (error "unknown tag -- PS-TEX " action-name))
383 (define (ps-scm action-name)
386 ;; alist containing fontname -> fontcommand assoc (both strings)
387 (define font-alist '())
388 (define font-count 0)
389 (define current-font "")
390 (define (clear-fontcache)
392 (set! font-alist '())
394 (set! current-font "")))
396 (define (cached-fontname i)
399 (make-string 1 (integer->char (+ 65 i)))))
401 (define (mag-to-size m)
402 (number->string (case m
405 ('2 14) ; really: 14.400
406 ('3 17) ; really: 17.280
407 ('4 21) ; really: 20.736
408 ('5 24) ; really: 24.888
409 ('6 30) ; really: 29.856
412 (define (select-font font-name magnification)
413 (define font-cmd (assoc font-name font-alist))
414 (if (not (equal? font-name current-font))
416 (set! current-font font-name)
417 (if (eq? font-cmd #f)
419 (set! font-cmd (cached-fontname font-count))
420 (set! font-alist (acons font-name font-cmd font-alist))
421 (set! font-count (+ 1 font-count))
422 (string-append "\n/" font-cmd " {/"
423 font-name " findfont "
424 (mag-to-size magnification)
425 " scalefont setfont} bind def \n"
427 (string-append (cdr font-cmd) " ")))
428 ; font-name == current-font no switch needed
432 (define (beam width slope thick)
434 (numbers->string (list width slope thick)) " draw_beam" ))
437 (invoke-dim1 " draw_bracket" h))
440 (invoke-char " show" i))
442 (define (crescendo thick w h cont )
444 (numbers->string (list w h (inexact->exact cont) thick))
447 (define (dashed-slur thick dash l)
449 (apply string-append (map control->string l))
450 (number->string thick)
453 (number->string (- (* thick dash) thick))
456 (number->string (* 2 thick))
457 " ] 0 draw_dashed_slur"))
459 (define (decrescendo thick w h cont)
461 (numbers->string (list w h (inexact->exact cont) thick))
462 " draw_decrescendo"))
468 (define (experimental-on) "")
470 (define (filledbox breapth width depth height)
471 (string-append (numbers->string (list breapth width depth height))
475 (define (font-def i s)
478 (substring s 0 (- (string-length s) 4))
479 " findfont 12 scalefont setfont} bind def \n"))
481 (define (font-switch i)
482 (string-append (font i) " "))
486 (ly-gulp-file "lilyponddefs.ps")
487 " {exch pop //systemdict /run get exec} "
488 (ly-gulp-file "lily.ps")
489 "{ exch pop //systemdict /run get exec } "
492 (define (lily-def key val)
494 (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
495 (string-append "/" key " {" val "} bind def\n")
496 (string-append "/" key " (" val ") def\n")
500 (define (header creator generate)
503 "%%Creator: " creator generate "\n"))
505 (define (invoke-char s i)
507 "(\\" (inexact->string i 8) ") " s " " ))
509 (define (invoke-dim1 s d)
511 (number->string (* d (/ 72.27 72))) " " s ))
513 (define (placebox x y s)
515 (number->string x) " " (number->string y) " {" s "} placebox "))
517 (define (pianobrace y staffht)
519 (minht (* 2 staffht))
523 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
524 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
529 (define (bezier-sandwich l thick)
531 (apply string-append (map control->string l))
532 (number->string thick)
533 " draw_bezier_sandwich"))
535 (define (start-line height)
540 (define (stem breapth width depth height)
541 (string-append (numbers->string (list breapth width depth height))
548 (string-append "(" s ") show "))
551 (define (volta h w thick vert_start vert_end)
553 (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
556 (define (tuplet ht gap dx dy thick dir)
558 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
567 (cond ((eq? action-name 'all-definitions)
570 (define tuplet ,tuplet)
571 (define bracket ,bracket)
573 (define crescendo ,crescendo)
574 (define volta ,volta)
575 (define bezier-sandwich ,bezier-sandwich)
576 (define dashed-slur ,dashed-slur)
577 (define decrescendo ,decrescendo)
578 (define end-output ,end-output)
579 (define experimental-on ,experimental-on)
580 (define filledbox ,filledbox)
581 (define font-def ,font-def)
582 (define font-switch ,font-switch)
583 (define pianobrace ,pianobrace)
584 (define header-end ,header-end)
585 (define lily-def ,lily-def)
586 (define header ,header)
587 (define invoke-char ,invoke-char)
588 (define invoke-dim1 ,invoke-dim1)
589 (define placebox ,placebox)
590 (define select-font ,select-font)
591 (define start-line ,start-line)
593 (define stop-line ,stop-line)
594 (define stop-last-line ,stop-line)
597 ((eq? action-name 'tuplet) tuplet)
598 ((eq? action-name 'beam) beam)
599 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
600 ((eq? action-name 'bracket) bracket)
601 ((eq? action-name 'char) char)
602 ((eq? action-name 'crescendo) crescendo)
603 ((eq? action-name 'dashed-slur) dashed-slur)
604 ((eq? action-name 'decrescendo) decrescendo)
605 ((eq? action-name 'experimental-on) experimental-on)
606 ((eq? action-name 'filledbox) filledbox)
607 ((eq? action-name 'select-font) select-font)
608 ((eq? action-name 'volta) volta)
609 (else (error "unknown tag -- PS-SCM " action-name))
614 ; Russ McManus, <mcmanus@IDT.NET>
616 ; I use the following, which should definitely be provided somewhere
617 ; in guile, but isn't, AFAIK:
621 (define (hash-table-for-each fn ht)
623 ((= i (vector-length ht)))
624 (do ((alist (vector-ref ht i) (cdr alist)))
626 (fn (car (car alist)) (cdr (car alist))))))
628 (define (hash-table-map fn ht)
631 ((= i (vector-length ht)) (reverse ret-ls))
632 (do ((alist (vector-ref ht i) (cdr alist)))
634 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
637 ;;;; print a SCM expression. Isn't this part of the std lib?
639 (define (scmlist->string exp)
641 ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
642 ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
643 (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
646 (define (scm->string exp)
648 ((pair? exp) (string-append "(" (scmlist->string exp)))
649 ((number? exp) (number->string exp))
650 ((symbol? exp) (symbol->string exp))
651 ((string? exp) (string-append "\"" exp "\""))
654 (define (test-scm->string)
655 (list (scmlist->string '(a))
656 (scmlist->string '(a b))
657 (scmlist->string '(a b . c))
661 (scm->string '(a b ))
662 (scm->string '(a b . c))
663 (scm->string '(a b (c . d)))
664 (scm->string '(a "bla" (c . 1.5)))