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)
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 (string-append "\\font" font-cmd "=" font-name font-cmd))
196 (define (beam width slope thick)
197 (embedded-ps ((ps-scm 'beam) width slope thick)))
200 (embedded-ps ((ps-scm 'bracket) h)))
202 (define (dashed-slur thick dash l)
203 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
205 (define (crescendo w h cont)
206 (embedded-ps ((ps-scm 'crescendo) w h cont)))
209 (string-append "\\char" (inexact->string i 10) " "))
211 (define (decrescendo w h cont)
212 (embedded-ps ((ps-scm 'decrescendo) w h cont)))
214 ;This sets CTM so that you get to the currentpoint
215 ; by executing a 0 0 moveto
217 (define (embedded-ps s)
218 (string-append "\\special{ps: @beginspecial @setspecial " s " @endspecial}"))
221 "\n\\EndLilyPondOutput")
223 (define (experimental-on)
226 (define (font-switch i)
230 (define (font-def i s)
232 "\\font" (font-switch i) "=" s "\n"))
236 (define (header-end) (string-append
238 "/stafflinethickness \\mudelapaperstaffline0 def " ;may we burn in hell.
239 "/interline \\mudelapaperinterline0 %\n def "
240 "interline 3 div /bracket_b exch def "
241 "interline 2 mul /bracket_w exch def "
242 "stafflinethickness 2 mul /bracket_t exch def "
243 "interline 1.5 mul /bracket_v exch def "
244 "bracket_v /bracket_u exch def "
245 "50 /bracket_alpha exch def "
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 (if security-paranoia
266 (define (output-tex-string s)
267 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post))
268 (define (output-tex-string s) s))
270 (define (lily-def key val)
272 "\\def\\" (output-tex-string key) "{" (output-tex-string val) "}\n"))
274 (define (number->dim x)
276 (number->string (chop-decimal x)) " pt "))
278 (define (placebox x y s)
281 (number->dim y) "}{" (number->dim x) "}{" s "}"))
284 (define (pianobrace y staffht)
286 (minht (* 2 staffht))
290 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))))
291 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
296 (define (rulesym h w)
298 "\\vrule height " (number->dim (/ h 2))
299 " depth " (number->dim (/ h 2))
300 " width " (number->dim w)
304 (define (bezier-sandwich l thick)
305 (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
308 (define (start-line ht)
311 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
314 "}\\vss}\\interscoreline")
315 (define (stop-last-line)
317 (define (filledbox breapth width depth height)
319 "\\kern" (number->dim (- breapth))
320 "\\vrule width " (number->dim (+ breapth width))
321 "depth " (number->dim depth)
322 "height " (number->dim height) " "))
325 (string-append "\\hbox{" (output-tex-string s) "}"))
327 (define (tuplet ht gapx dx dy thick dir)
328 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
330 (define (volta h w thick last)
331 (embedded-ps ((ps-scm 'volta) h w thick last)))
334 ;; The procedures listed below form the public interface of TeX-scm.
335 ;; (should merge the 2 lists)
336 (cond ((eq? action-name 'all-definitions)
339 (define bezier-sandwich ,bezier-sandwich)
340 (define bracket ,bracket)
342 (define crescendo ,crescendo)
343 (define dashed-slur ,dashed-slur)
344 (define decrescendo ,decrescendo)
345 (define end-output ,end-output)
346 (define experimental-on ,experimental-on)
347 (define filledbox ,filledbox)
348 (define font-def ,font-def)
349 (define font-switch ,font-switch)
350 (define header-end ,header-end)
351 (define lily-def ,lily-def)
352 (define header ,header)
353 (define invoke-char ,invoke-char)
354 (define invoke-dim1 ,invoke-dim1)
355 (define pianobrace ,pianobrace)
356 (define placebox ,placebox)
357 (define rulesym ,rulesym)
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 'rulesym) rulesym)
384 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
385 ((eq? action-name 'start-line) start-line)
386 ((eq? action-name 'stem) stem)
387 ((eq? action-name 'stop-line) stop-line)
388 ((eq? action-name 'stop-last-line) stop-last-line)
389 ((eq? action-name 'volta) volta)
390 (else (error "unknown tag -- PS-TEX " action-name))
395 (define (ps-scm action-name)
398 ;; alist containing fontname -> fontcommand assoc (both strings)
399 (define font-alist '())
400 (define font-count 0)
401 (define current-font "")
402 (define (clear-fontcache)
404 (set! font-alist '())
406 (set! current-font "")))
408 (define (cached-fontname i)
411 (make-string 1 (integer->char (+ 65 i)))))
413 (define (select-font font-name)
414 (if (not (equal? font-name current-font))
416 (set! current-font font-name)
417 (define font-cmd (assoc font-name font-alist))
418 (if (eq? font-cmd #f)
420 (set! font-cmd (cached-fontname font-count))
421 (set! font-alist (acons font-name font-cmd font-alist))
422 (set! font-count (+ 1 font-count))
423 (string-append "\n/" font-cmd " {/"
425 " findfont 12 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 w h cont thick)
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)
452 (if (> 1 dash) (number->string (- (* thick dash) thick)) "0") " "
453 (number->string (* 2 thick))
454 " ] 0 draw_dashed_slur"))
456 (define (decrescendo thick w h cont)
458 (numbers->string (list w h (inexact->exact cont) thick))
459 " draw_decrescendo"))
465 (define (experimental-on) "")
467 (define (filledbox breapth width depth height)
468 (string-append (numbers->string (list breapth width depth height))
472 (define (font-def i s)
475 (substring s 0 (- (string-length s) 4))
476 " findfont 12 scalefont setfont} bind def \n"))
478 (define (font-switch i)
479 (string-append (font i) " "))
481 (define (header-end) "")
482 (define (lily-def key val)
484 (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
485 (string-append "/" key " {" val "} bind def\n")
486 (string-append "/" key " (" val ") def\n")
490 (define (header creator generate)
493 "%%Creator: " creator generate "\n"))
495 (define (invoke-char s i)
497 "(\\" (inexact->string i 8) ") " s " " ))
499 (define (invoke-dim1 s d)
501 (number->string (* d (/ 72.27 72))) " " s ))
503 (define (placebox x y s)
505 (number->string x) " " (number->string y) " {" s "} placebox "))
507 (define (pianobrace y staffht)
509 (minht (* 2 staffht))
513 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))))
514 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
519 (define (rulesym x y)
521 (number->string x) " "
522 (number->string y) " "
525 (define (bezier-sandwich l thick)
527 (apply string-append (map control->string l))
528 (number->string thick)
529 " draw_bezier_sandwich"))
531 (define (start-line height)
536 (define (stem breapth width depth height)
537 (string-append (numbers->string (list breapth width depth height))
544 (string-append "(" s ") show "))
547 (define (volta h w thick last)
549 (numbers->string (list h w thick (inexact->exact last)))
552 (define (tuplet ht gap dx dy thick dir)
554 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
563 (cond ((eq? action-name 'all-definitions)
566 (define tuplet ,tuplet)
567 (define bracket ,bracket)
569 (define crescendo ,crescendo)
570 (define volta ,volta)
571 (define bezier-sandwich ,bezier-sandwich)
572 (define dashed-slur ,dashed-slur)
573 (define decrescendo ,decrescendo)
574 (define end-output ,end-output)
575 (define experimental-on ,experimental-on)
576 (define filledbox ,filledbox)
577 (define font-def ,font-def)
578 (define font-switch ,font-switch)
579 (define pianobrace ,pianobrace)
580 (define header-end ,header-end)
581 (define lily-def ,lily-def)
582 (define header ,header)
583 (define invoke-char ,invoke-char)
584 (define invoke-dim1 ,invoke-dim1)
585 (define placebox ,placebox)
586 (define rulesym ,rulesym)
587 (define select-font ,select-font)
588 (define start-line ,start-line)
590 (define stop-line ,stop-line)
591 (define stop-last-line ,stop-line)
594 ((eq? action-name 'tuplet) tuplet)
595 ((eq? action-name 'beam) beam)
596 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
597 ((eq? action-name 'bracket) bracket)
598 ((eq? action-name 'char) char)
599 ((eq? action-name 'crescendo) crescendo)
600 ((eq? action-name 'dashed-slur) dashed-slur)
601 ((eq? action-name 'decrescendo) decrescendo)
602 ((eq? action-name 'experimental-on) experimental-on)
603 ((eq? action-name 'filledbox) filledbox)
604 ((eq? action-name 'select-font) select-font)
605 ((eq? action-name 'volta) volta)
606 (else (error "unknown tag -- PS-SCM " action-name))
611 ; Russ McManus, <mcmanus@IDT.NET>
613 ; I use the following, which should definitely be provided somewhere
614 ; in guile, but isn't, AFAIK:
618 (define (hash-table-for-each fn ht)
620 ((= i (vector-length ht)))
621 (do ((alist (vector-ref ht i) (cdr alist)))
623 (fn (car (car alist)) (cdr (car alist))))))
625 (define (hash-table-map fn ht)
628 ((= i (vector-length ht)) (reverse ret-ls))
629 (do ((alist (vector-ref ht i) (cdr alist)))
631 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
634 ;;;; print a SCM expression. Isn't this part of the std lib?
636 (define (scmlist->string exp)
638 ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
639 ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
640 (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
643 (define (scm->string exp)
645 ((pair? exp) (string-append "(" (scmlist->string exp)))
646 ((number? exp) (number->string exp))
647 ((symbol? exp) (symbol->string exp))
648 ((string? exp) (string-append "\"" exp "\""))
651 (define (test-scm->string)
652 (list (scmlist->string '(a))
653 (scmlist->string '(a b))
654 (scmlist->string '(a b . c))
658 (scm->string '(a b ))
659 (scm->string '(a b . c))
660 (scm->string '(a b (c . d)))
661 (scm->string '(a "bla" (c . 1.5)))