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)) " ")))
56 (make-string 1 (integer->char (+ (char->integer #\A) i)))
61 (define (scm-scm action-name)
64 (define security-paranoia #f)
67 ;; See documentation of Item::visibility_lambda_
68 (define (postbreak_only_visibility d) (if (= d 1) '(#f . #f) '(#t . #t)))
69 (define (spanbar_non_postbreak_visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
71 (define (non_postbreak_visibility d) (if (= d 1) '(#t . #t) '(#f . #f)))
72 (define (non_prebreak_visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
75 ;; Score_span_bars are only visible at start of line
76 ;; i.e. if break_dir == RIGHT == 1
77 (define Span_bar_engraver_visibility non_postbreak_visibility)
78 (define mark-visibility non_prebreak_visibility)
79 (define Span_score_bar_engraver_visibility postbreak_only_visibility)
80 (define Piano_bar_engraver_visibility postbreak_only_visibility)
81 (define Staff_group_bar_engraver_visibility postbreak_only_visibility)
83 ;; Spacing constants for prefatory matter.
85 ;; rules for this spacing are much more complicated than this. See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147
91 (("" "Clef_item") . (minimum_space 1.0))
92 (("" "Staff_bar") . (minimum_space 0.0))
93 (("" "Clef_item") . (minimum_space 1.0))
94 (("" "Key_item") . (minimum_space 0.5))
95 (("" "Span_bar") . (extra_space 0.0))
96 (("" "Time_signature") . (extra_space 0.0))
97 (("" "begin-of-note") . (minimum_space 1.5))
98 (("Clef_item" "Key_item") . (minimum_space 3.5))
99 (("Key_item" "Time_signature") . (extra_space 2.0))
100 (("Clef_item" "Time_signature") . (minimum_space 3.5))
101 (("Staff_bar" "Clef_item") . (minimum_space 1.0))
102 (("Clef_item" "Staff_bar") . (minimum_space 3.7))
103 (("Time_signature" "Staff_bar") . (minimum_space 2.0))
104 (("Key_item" "Staff_bar") . (extra_space 1.0))
105 (("Span_bar" "Clef_item") . (extra_space 1.0))
106 (("Clef_item" "Span_bar") . (minimum_space 3.7))
107 (("Time_signature" "Span_bar") . (minimum_space 2.0))
108 (("Key_item" "Span_bar") . (minimum_space 2.5))
109 (("Staff_bar" "Time_signature") . (minimum_space 2.0))
110 (("Time_signature" "begin-of-note") . (extra_space 1.0)) ; Huh? see twinkle.ly
111 (("Key_item" "begin-of-note") . (extra_space 2.5))
112 (("Staff_bar" "begin-of-note") . (extra_space 1.0))
113 (("Clef_item" "begin-of-note") . (minimum_space 5.0))
114 (("" "Breathing_sign") . (minimum_space 0.0))
115 (("Breathing_sign" "Key_item") . (minimum_space 1.5))
116 (("Breathing_sign" "begin-of-note") . (minimum_space 1.0))
117 (("Breathing_sign" "Staff_bar") . (minimum_space 1.5))
118 (("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)
181 (if (not (equal? font-name current-font))
183 (set! current-font font-name)
184 (define font-cmd (assoc font-name font-alist))
185 (if (eq? font-cmd #f)
187 (set! font-cmd (cached-fontname font-count))
188 (set! font-alist (acons font-name font-cmd font-alist))
189 (set! font-count (+ 1 font-count))
190 (if (equal? font-name "")
191 (error "Empty fontname -- SELECT-FONT"))
192 (string-append "\\font" font-cmd "=" font-name font-cmd))
197 (define (beam width slope thick)
198 (embedded-ps ((ps-scm 'beam) width slope thick)))
201 (embedded-ps ((ps-scm 'bracket) h)))
203 (define (dashed-slur thick dash l)
204 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
206 (define (crescendo w h cont)
207 (embedded-ps ((ps-scm 'crescendo) w h cont)))
210 (string-append "\\show{" (inexact->string i 10) "}"))
212 (define (decrescendo w h cont)
213 (embedded-ps ((ps-scm 'decrescendo) w h cont)))
215 (define (embedded-ps s)
216 (string-append "\\embeddedps{" s "}"))
219 "\n\\EndLilyPondOutput")
221 (define (experimental-on)
222 "\\turnOnExperimentalFeatures")
224 (define (font-switch i)
228 (define (font-def i s)
230 "\\font" (font-switch i) "=" s "\n"))
232 (define (generalmeter num den)
234 "\\generalmeter{" (number->string (inexact->exact num)) "}{" (number->string (inexact->exact den)) "}"))
236 (define (header-end) "\\turnOnPostScript")
238 (define (header creator generate)
240 "%created by: " creator generate "\n"))
242 (define (invoke-char s i)
244 "\n\\" s "{" (inexact->string i 10) "}" ))
246 (define (invoke-dim1 s d)
248 "\n\\" s "{" (number->dim d) "}"))
253 ;; need to do something to make this really safe.
255 (if security-paranoia
256 (define (output-tex-string s)
257 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post))
258 (define (output-tex-string s) s))
260 (define (lily-def key val)
262 "\\def\\" (output-tex-string key) "{" (output-tex-string val) "}\n"))
264 (define (number->dim x)
266 (number->string (chop-decimal x)) " pt "))
268 (define (placebox x y s)
271 (number->dim y) "}{" (number->dim x) "}{" s "}"))
273 (define (pianobrace y)
275 (define minht (* 2 mudelapaperstaffheight))
276 (define maxht (* 7 minht))
278 "{\\bracefont " (char (max
280 (/ (- (min y (- maxht step)) minht) step))) "}"))
284 (define (rulesym h w)
286 "\\vrule height " (number->dim (/ h 2))
287 " depth " (number->dim (/ h 2))
288 " width " (number->dim w)
292 (define (bezier-sandwich l)
293 (embedded-ps ((ps-scm 'bezier-sandwich) l)))
296 (define (start-line ht)
299 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
302 "}\\vss}\\interscoreline")
303 (define (stop-last-line)
305 (define (filledbox breapth width depth height)
307 "\\kern" (number->dim (- breapth))
308 "\\vrule width " (number->dim (+ breapth width))
309 "depth " (number->dim depth)
310 "height " (number->dim height) " "))
315 (string-append "\\hbox{" (output-tex-string s) "}"))
317 (define (tuplet ht dx dy thick dir)
318 (embedded-ps ((ps-scm 'tuplet) ht dx dy thick dir)))
320 (define (volta w thick last)
321 (embedded-ps ((ps-scm 'volta) w thick last)))
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 generalmeter ,generalmeter)
341 (define header-end ,header-end)
342 (define lily-def ,lily-def)
343 (define header ,header)
344 (define invoke-char ,invoke-char)
345 (define invoke-dim1 ,invoke-dim1)
346 (define pianobrace ,pianobrace)
347 (define placebox ,placebox)
348 (define rulesym ,rulesym)
349 (define select-font ,select-font)
350 (define start-line ,start-line)
351 (define stop-line ,stop-line)
352 (define stop-last-line ,stop-last-line)
354 (define tuplet ,tuplet)
355 (define volta ,volta)
358 ((eq? action-name 'beam) beam)
359 ((eq? action-name 'tuplet) tuplet)
360 ((eq? action-name 'bracket) bracket)
361 ((eq? action-name 'crescendo) crescendo)
362 ((eq? action-name 'dashed-slur) dashed-slur)
363 ((eq? action-name 'decrescendo) decrescendo)
364 ((eq? action-name 'end-output) end-output)
365 ((eq? action-name 'experimental-on) experimental-on)
366 ((eq? action-name 'font-def) font-def)
367 ((eq? action-name 'font-switch) font-switch)
368 ((eq? action-name 'generalmeter) generalmeter)
369 ((eq? action-name 'header-end) header-end)
370 ((eq? action-name 'lily-def) lily-def)
371 ((eq? action-name 'header) header)
372 ((eq? action-name 'invoke-char) invoke-char)
373 ((eq? action-name 'invoke-dim1) invoke-dim1)
374 ((eq? action-name 'placebox) placebox)
375 ((eq? action-name 'rulesym) rulesym)
376 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
377 ((eq? action-name 'start-line) start-line)
378 ((eq? action-name 'stem) stem)
379 ((eq? action-name 'stop-line) stop-line)
380 ((eq? action-name 'stop-last-line) stop-last-line)
381 ((eq? action-name 'volta) volta)
382 (else (error "unknown tag -- PS-TEX " action-name))
387 (define (ps-scm action-name)
390 ;; alist containing fontname -> fontcommand assoc (both strings)
391 (define font-alist '())
392 (define font-count 0)
393 (define current-font "")
394 (define (clear-fontcache)
396 (set! font-alist '())
398 (set! current-font "")))
400 (define (cached-fontname i)
403 (make-string 1 (integer->char (+ 65 i)))))
405 (define (select-font font-name)
406 (if (not (equal? font-name current-font))
408 (set! current-font font-name)
409 (define font-cmd (assoc font-name font-alist))
410 (if (eq? font-cmd #f)
412 (set! font-cmd (cached-fontname font-count))
413 (set! font-alist (acons font-name font-cmd font-alist))
414 (set! font-count (+ 1 font-count))
415 (string-append "\n/" font-cmd " {/"
417 " findfont 12 scalefont setfont} bind def \n"
419 (string-append (cdr font-cmd) " ")))
420 ; font-name == current-font no switch needed
424 (define (beam width slope thick)
426 (numbers->string (list width slope thick)) " draw_beam " ))
429 (invoke-dim1 " draw_bracket" h))
432 (invoke-char " show" i))
434 (define (crescendo w h cont)
436 (numbers->string (list w h (inexact->exact cont)))
439 (define (dashed-slur thick dash l)
441 (apply string-append (map control->string l))
442 (number->string thick)
444 (if (> 1 dash) (number->string (- (* thick dash) thick)) "0") " "
445 (number->string (* 2 thick))
446 " ] 0 draw_dashed_slur"))
448 (define (decrescendo w h cont)
450 (numbers->string (list w h (inexact->exact cont)))
451 " draw_decrescendo"))
457 (define (experimental-on) "")
459 (define (filledbox breapth width depth height)
460 (string-append (numbers->string (list breapth width depth height))
464 (define (font-def i s)
467 (substring s 0 (- (string-length s) 4))
468 " findfont 12 scalefont setfont} bind def \n"))
470 (define (font-switch i)
471 (string-append (font i) " "))
473 (define (generalmeter num den)
474 (string-append (number->string (inexact->exact num)) " " (number->string (inexact->exact den)) " generalmeter "))
476 (define (header-end) "")
477 (define (lily-def key val)
479 "/" key " {" val "} bind def\n"))
481 (define (header creator generate)
484 "%%Creator: " creator generate "\n"))
486 (define (invoke-char s i)
488 "(\\" (inexact->string i 8) ") " s " " ))
490 (define (invoke-dim1 s d)
492 (number->string (* d (/ 72.27 72))) " " s ))
494 (define (placebox x y s)
496 (number->string x) " " (number->string y) " {" s "} placebox "))
497 (define (pianobrace y)
501 (define (rulesym x y)
503 (number->string x) " "
504 (number->string y) " "
507 (define (bezier-sandwich l)
509 (apply string-append (map control->string l))
510 " draw_bezier_sandwich"))
512 (define (start-line height)
517 (define (stem breapth width depth height)
518 (string-append (numbers->string (list breapth width depth height))
525 (string-append "(" s ") show "))
528 (define (volta w thick last)
530 (numbers->string (list w thick (inexact->exact last)))
533 (define (tuplet ht dx dy thick dir)
535 (numbers->string (list ht dx dy thick (inexact->exact dir)))
544 (cond ((eq? action-name 'all-definitions)
547 (define tuplet ,tuplet)
548 (define bracket ,bracket)
550 (define crescendo ,crescendo)
551 (define volta ,volta)
552 (define bezier-sandwich ,bezier-sandwich)
553 (define dashed-slur ,dashed-slur)
554 (define decrescendo ,decrescendo)
555 (define end-output ,end-output)
556 (define experimental-on ,experimental-on)
557 (define filledbox ,filledbox)
558 (define font-def ,font-def)
559 (define font-switch ,font-switch)
560 (define generalmeter ,generalmeter)
561 (define pianobrace ,pianobrace)
562 (define header-end ,header-end)
563 (define lily-def ,lily-def)
564 (define header ,header)
565 (define invoke-char ,invoke-char)
566 (define invoke-dim1 ,invoke-dim1)
567 (define placebox ,placebox)
568 (define rulesym ,rulesym)
569 (define select-font ,select-font)
570 (define start-line ,start-line)
572 (define stop-line ,stop-line)
573 (define stop-last-line ,stop-line)
576 ((eq? action-name 'tuplet) tuplet)
577 ((eq? action-name 'beam) beam)
578 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
579 ((eq? action-name 'bracket) bracket)
580 ((eq? action-name 'char) char)
581 ((eq? action-name 'crescendo) crescendo)
582 ((eq? action-name 'dashed-slur) dashed-slur)
583 ((eq? action-name 'decrescendo) decrescendo)
584 ((eq? action-name 'experimental-on) experimental-on)
585 ((eq? action-name 'filledbox) filledbox)
586 ((eq? action-name 'select-font) select-font)
587 ((eq? action-name 'volta) volta)
588 (else (error "unknown tag -- PS-SCM " action-name))
593 ; Russ McManus, <mcmanus@IDT.NET>
595 ; I use the following, which should definitely be provided somewhere
596 ; in guile, but isn't, AFAIK:
600 (define (hash-table-for-each fn ht)
602 ((= i (vector-length ht)))
603 (do ((alist (vector-ref ht i) (cdr alist)))
605 (fn (car (car alist)) (cdr (car alist))))))
607 (define (hash-table-map fn ht)
610 ((= i (vector-length ht)) (reverse ret-ls))
611 (do ((alist (vector-ref ht i) (cdr alist)))
613 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))