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 3.5))
98 (("Key_item" "Time_signature") . (extra_space 2.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 2.0))
109 (("Time_signature" "begin-of-note") . (extra_space 1.0)) ; Huh? see twinkle.ly
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))
123 (define (break-align-spacer this next)
124 (let ((entry (assoc `(,this ,next) space-alist)))
127 (begin (ly-warn (string-append "Unknown spacing pair `" this "', `" next "'"))
128 '(minimum_space 0.0)))))
136 ("dynamic" . "feta-din")
137 ("finger" . "feta-nummer")
138 ("typewriter" . "cmtt")
143 ("mark" . "feta-nummer")
144 ("number" . "feta-nummer")
145 ("volta" . "feta-nummer"))
148 (define script-alist '())
149 (define (articulation-to-scriptdef a)
150 (assoc a script-alist)
153 ;; Map style names to TeX font names. Return false if
154 ;; no font name found.
155 (define (style-to-cmr s)
160 (define (tex-scm action-name)
165 (define font-alist '())
166 (define font-count 0)
167 (define current-font "")
168 (define (clear-fontcache)
170 (set! font-alist '())
172 (set! current-font "")))
174 (define (cached-fontname i)
177 (make-string 1 (integer->char (+ 65 i)))))
179 (define (select-font font-name magnification)
180 (if (not (equal? font-name current-font))
182 (set! current-font font-name)
183 (define font-cmd (assoc font-name font-alist))
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 "\\special{ps: @beginspecial @setspecial " s " @endspecial}"))
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"))
241 (define (header-end) (string-append
243 "/stafflinethickness \\mudelapaperstaffline0 def " ;may we burn in hell.
244 "/interline \\mudelapaperinterline0 %\n def "
245 "interline 3 div /bracket_b exch def "
246 "interline 2 mul /bracket_w exch def "
247 "stafflinethickness 2 mul /bracket_t exch def "
248 "interline 1.5 mul /bracket_v exch def "
249 "bracket_v /bracket_u exch def "
250 "50 /bracket_alpha exch def "
253 (define (header creator generate)
255 "%created by: " creator generate "\n"))
257 (define (invoke-char s i)
259 "\n\\" s "{" (inexact->string i 10) "}" ))
261 (define (invoke-dim1 s d)
263 "\n\\" s "{" (number->dim d) "}"))
268 ;; need to do something to make this really safe.
270 (if security-paranoia
271 (define (output-tex-string s)
272 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post))
273 (define (output-tex-string s) s))
275 (define (lily-def key val)
277 "\\def\\" (output-tex-string key) "{" (output-tex-string val) "}\n"))
279 (define (number->dim x)
281 (number->string (chop-decimal x)) " pt "))
283 (define (placebox x y s)
286 (number->dim y) "}{" (number->dim x) "}{" s "}"))
289 (define (pianobrace y staffht)
291 (minht (* 2 staffht))
295 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
296 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
301 (define (rulesym h w)
303 "\\vrule height " (number->dim (/ h 2))
304 " depth " (number->dim (/ h 2))
305 " width " (number->dim w)
309 (define (bezier-sandwich l thick)
310 (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
313 (define (start-line ht)
316 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
319 "}\\vss}\\interscoreline")
320 (define (stop-last-line)
322 (define (filledbox breapth width depth height)
324 "\\kern" (number->dim (- breapth))
325 "\\vrule width " (number->dim (+ breapth width))
326 "depth " (number->dim depth)
327 "height " (number->dim height) " "))
330 (string-append "\\hbox{" (output-tex-string s) "}"))
332 (define (tuplet ht gapx dx dy thick dir)
333 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
335 (define (volta h w thick vert_start vert_end)
336 (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
339 ;; The procedures listed below form the public interface of TeX-scm.
340 ;; (should merge the 2 lists)
341 (cond ((eq? action-name 'all-definitions)
344 (define bezier-sandwich ,bezier-sandwich)
345 (define bracket ,bracket)
347 (define crescendo ,crescendo)
348 (define dashed-slur ,dashed-slur)
349 (define decrescendo ,decrescendo)
350 (define end-output ,end-output)
351 (define experimental-on ,experimental-on)
352 (define filledbox ,filledbox)
353 (define font-def ,font-def)
354 (define font-switch ,font-switch)
355 (define header-end ,header-end)
356 (define lily-def ,lily-def)
357 (define header ,header)
358 (define invoke-char ,invoke-char)
359 (define invoke-dim1 ,invoke-dim1)
360 (define pianobrace ,pianobrace)
361 (define placebox ,placebox)
362 (define rulesym ,rulesym)
363 (define select-font ,select-font)
364 (define start-line ,start-line)
365 (define stop-line ,stop-line)
366 (define stop-last-line ,stop-last-line)
368 (define tuplet ,tuplet)
369 (define volta ,volta)
372 ((eq? action-name 'beam) beam)
373 ((eq? action-name 'tuplet) tuplet)
374 ((eq? action-name 'bracket) bracket)
375 ((eq? action-name 'crescendo) crescendo)
376 ((eq? action-name 'dashed-slur) dashed-slur)
377 ((eq? action-name 'decrescendo) decrescendo)
378 ((eq? action-name 'end-output) end-output)
379 ((eq? action-name 'experimental-on) experimental-on)
380 ((eq? action-name 'font-def) font-def)
381 ((eq? action-name 'font-switch) font-switch)
382 ((eq? action-name 'header-end) header-end)
383 ((eq? action-name 'lily-def) lily-def)
384 ((eq? action-name 'header) header)
385 ((eq? action-name 'invoke-char) invoke-char)
386 ((eq? action-name 'invoke-dim1) invoke-dim1)
387 ((eq? action-name 'placebox) placebox)
388 ((eq? action-name 'rulesym) rulesym)
389 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
390 ((eq? action-name 'start-line) start-line)
391 ((eq? action-name 'stem) stem)
392 ((eq? action-name 'stop-line) stop-line)
393 ((eq? action-name 'stop-last-line) stop-last-line)
394 ((eq? action-name 'volta) volta)
395 (else (error "unknown tag -- PS-TEX " action-name))
400 (define (ps-scm action-name)
403 ;; alist containing fontname -> fontcommand assoc (both strings)
404 (define font-alist '())
405 (define font-count 0)
406 (define current-font "")
407 (define (clear-fontcache)
409 (set! font-alist '())
411 (set! current-font "")))
413 (define (cached-fontname i)
416 (make-string 1 (integer->char (+ 65 i)))))
418 (define (mag-to-size m)
419 (number->string (case m
422 ('2 14) ; really: 14.400
423 ('3 17) ; really: 17.280
424 ('4 21) ; really: 20.736
425 ('5 24) ; really: 24.888
426 ('6 30) ; really: 29.856
429 (define (select-font font-name magnification)
430 (if (not (equal? font-name current-font))
432 (set! current-font font-name)
433 (define font-cmd (assoc font-name font-alist))
434 (if (eq? font-cmd #f)
436 (set! font-cmd (cached-fontname font-count))
437 (set! font-alist (acons font-name font-cmd font-alist))
438 (set! font-count (+ 1 font-count))
439 (string-append "\n/" font-cmd " {/"
440 font-name " findfont "
441 (mag-to-size magnification)
442 " scalefont setfont} bind def \n"
444 (string-append (cdr font-cmd) " ")))
445 ; font-name == current-font no switch needed
449 (define (beam width slope thick)
451 (numbers->string (list width slope thick)) " draw_beam " ))
454 (invoke-dim1 " draw_bracket" h))
457 (invoke-char " show" i))
459 (define (crescendo thick w h cont )
461 (numbers->string (list w h (inexact->exact cont) thick))
464 (define (dashed-slur thick dash l)
466 (apply string-append (map control->string l))
467 (number->string thick)
469 (if (> 1 dash) (number->string (- (* thick dash) thick)) "0") " "
470 (number->string (* 2 thick))
471 " ] 0 draw_dashed_slur"))
473 (define (decrescendo w h cont thick)
475 (numbers->string (list w h (inexact->exact cont) thick))
476 " draw_decrescendo"))
482 (define (experimental-on) "")
484 (define (filledbox breapth width depth height)
485 (string-append (numbers->string (list breapth width depth height))
489 (define (font-def i s)
492 (substring s 0 (- (string-length s) 4))
493 " findfont 12 scalefont setfont} bind def \n"))
495 (define (font-switch i)
496 (string-append (font i) " "))
498 (define (header-end) "")
499 (define (lily-def key val)
501 (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
502 (string-append "/" key " {" val "} bind def\n")
503 (string-append "/" key " (" val ") def\n")
507 (define (header creator generate)
510 "%%Creator: " creator generate "\n"))
512 (define (invoke-char s i)
514 "(\\" (inexact->string i 8) ") " s " " ))
516 (define (invoke-dim1 s d)
518 (number->string (* d (/ 72.27 72))) " " s ))
520 (define (placebox x y s)
522 (number->string x) " " (number->string y) " {" s "} placebox "))
524 (define (pianobrace y staffht)
526 (minht (* 2 staffht))
530 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
531 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
536 (define (rulesym x y)
538 (number->string x) " "
539 (number->string y) " "
542 (define (bezier-sandwich l thick)
544 (apply string-append (map control->string l))
545 (number->string thick)
546 " draw_bezier_sandwich"))
548 (define (start-line height)
553 (define (stem breapth width depth height)
554 (string-append (numbers->string (list breapth width depth height))
561 (string-append "(" s ") show "))
564 (define (volta h w thick vert_start vert_end)
566 (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
569 (define (tuplet ht gap dx dy thick dir)
571 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
580 (cond ((eq? action-name 'all-definitions)
583 (define tuplet ,tuplet)
584 (define bracket ,bracket)
586 (define crescendo ,crescendo)
587 (define volta ,volta)
588 (define bezier-sandwich ,bezier-sandwich)
589 (define dashed-slur ,dashed-slur)
590 (define decrescendo ,decrescendo)
591 (define end-output ,end-output)
592 (define experimental-on ,experimental-on)
593 (define filledbox ,filledbox)
594 (define font-def ,font-def)
595 (define font-switch ,font-switch)
596 (define pianobrace ,pianobrace)
597 (define header-end ,header-end)
598 (define lily-def ,lily-def)
599 (define header ,header)
600 (define invoke-char ,invoke-char)
601 (define invoke-dim1 ,invoke-dim1)
602 (define placebox ,placebox)
603 (define rulesym ,rulesym)
604 (define select-font ,select-font)
605 (define start-line ,start-line)
607 (define stop-line ,stop-line)
608 (define stop-last-line ,stop-line)
611 ((eq? action-name 'tuplet) tuplet)
612 ((eq? action-name 'beam) beam)
613 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
614 ((eq? action-name 'bracket) bracket)
615 ((eq? action-name 'char) char)
616 ((eq? action-name 'crescendo) crescendo)
617 ((eq? action-name 'dashed-slur) dashed-slur)
618 ((eq? action-name 'decrescendo) decrescendo)
619 ((eq? action-name 'experimental-on) experimental-on)
620 ((eq? action-name 'filledbox) filledbox)
621 ((eq? action-name 'select-font) select-font)
622 ((eq? action-name 'volta) volta)
623 (else (error "unknown tag -- PS-SCM " action-name))
628 ; Russ McManus, <mcmanus@IDT.NET>
630 ; I use the following, which should definitely be provided somewhere
631 ; in guile, but isn't, AFAIK:
635 (define (hash-table-for-each fn ht)
637 ((= i (vector-length ht)))
638 (do ((alist (vector-ref ht i) (cdr alist)))
640 (fn (car (car alist)) (cdr (car alist))))))
642 (define (hash-table-map fn ht)
645 ((= i (vector-length ht)) (reverse ret-ls))
646 (do ((alist (vector-ref ht i) (cdr alist)))
648 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
651 ;;;; print a SCM expression. Isn't this part of the std lib?
653 (define (scmlist->string exp)
655 ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
656 ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
657 (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
660 (define (scm->string exp)
662 ((pair? exp) (string-append "(" (scmlist->string exp)))
663 ((number? exp) (number->string exp))
664 ((symbol? exp) (symbol->string exp))
665 ((string? exp) (string-append "\"" exp "\""))
668 (define (test-scm->string)
669 (list (scmlist->string '(a))
670 (scmlist->string '(a b))
671 (scmlist->string '(a b . c))
675 (scm->string '(a b ))
676 (scm->string '(a b . c))
677 (scm->string '(a b (c . d)))
678 (scm->string '(a "bla" (c . 1.5)))