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)))
19 (map (lambda (n) (string-append (number->string n) " ")) l)))
21 (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
23 (define (number->octal-string x)
24 (let* ((n (inexact->exact x))
26 (n8 (quotient (- n (* n64 64)) 8)))
30 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
32 (define (inexact->string x radix)
33 (let ((n (inexact->exact x)))
34 (number->string n radix)))
40 (string-append (number->string (car c)) " ")
41 (string-append (number->string (cadr c)) " ")))
48 (make-string 1 (integer->char (+ (char->integer #\A) i)))
53 (define (scm-scm action-name)
56 (define security-paranoia #f)
59 ;; See documentation of Item::visibility_lambda_
60 (define (postbreak_only_visibility d) (if (= d 1) '(#f . #f) '(#t . #t)))
61 (define (non_postbreak_visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
62 (define (spanbar_non_postbreak_visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
64 ;; Score_span_bars are only visible at start of line
65 ;; i.e. if break_dir == RIGHT == 1
66 (define Span_score_bar_engraver_visibility postbreak_only_visibility)
67 (define Span_bar_engraver_visibility non_postbreak_visibility)
68 (define Piano_bar_engraver_visibility postbreak_only_visibility)
69 (define Staff_group_bar_engraver_visibility postbreak_only_visibility)
78 ("dynamic" . "feta-din")
79 ("finger" . "feta-nummer")
80 ("typewriter" . "cmtt")
85 ("mark" . "feta-nummer")
86 ("number" . "feta-nummer")
87 ("volta" . "feta-nummer"))
91 ;; Map style names to TeX font names. Return false if
92 ;; no font name found.
93 (define (style-to-cmr s)
98 (define (tex-scm action-name)
103 (define font-alist '())
104 (define font-count 0)
105 (define current-font "")
106 (define (clear-fontcache)
108 (set! font-alist '())
110 (set! current-font "")))
112 (define (cached-fontname i)
115 (make-string 1 (integer->char (+ 65 i)))))
117 (define (select-font font-name)
118 (if (not (equal? font-name current-font))
120 (set! current-font font-name)
121 (define font-cmd (assoc font-name font-alist))
122 (if (eq? font-cmd #f)
124 (set! font-cmd (cached-fontname font-count))
125 (set! font-alist (acons font-name font-cmd font-alist))
126 (set! font-count (+ 1 font-count))
127 (string-append "\\font" font-cmd "=" font-name font-cmd))
132 (define (beam width slope thick)
133 (embedded-ps ((ps-scm 'beam) width slope thick)))
136 (embedded-ps ((ps-scm 'bracket) h)))
138 (define (dashed-slur thick dash l)
139 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
141 (define (crescendo w h cont)
142 (embedded-ps ((ps-scm 'crescendo) w h cont)))
145 (string-append "\\show{" (inexact->string i 10) "}"))
147 (define (decrescendo w h cont)
148 (embedded-ps ((ps-scm 'decrescendo) w h cont)))
150 (define (embedded-ps s)
151 (string-append "\\embeddedps{" s "}"))
154 "\n\\EndLilyPondOutput")
156 (define (experimental-on)
157 "\\turnOnExperimentalFeatures")
159 (define (font-switch i)
163 (define (font-def i s)
165 "\\font" (font-switch i) "=" s "\n"))
167 (define (generalmeter num den)
169 "\\generalmeter{" (number->string (inexact->exact num)) "}{" (number->string (inexact->exact den)) "}"))
171 (define (header-end) "\\turnOnPostScript")
173 (define (header creator generate)
175 "%created by: " creator generate "\n"))
177 (define (invoke-char s i)
179 "\n\\" s "{" (inexact->string i 10) "}" ))
181 (define (invoke-dim1 s d)
183 "\n\\" s "{" (number->dim d) "}"))
188 ;; need to do something to make this really safe.
190 (if security-paranoia
191 (define (output-tex-string s)
192 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post))
193 (define (output-tex-string s) s))
195 (define (lily-def key val)
197 "\\def\\" (output-tex-string key) "{" (output-tex-string val) "}\n"))
199 (define (number->dim x)
201 (number->string (chop-decimal x)) "pt "))
203 (define (placebox x y s)
206 (number->dim y) "}{" (number->dim x) "}{" s "}"))
208 (define (pianobrace y)
210 (define minht (* 2 mudelapaperstaffheight))
211 (define maxht (* 7 minht))
213 "{\\bracefont " (char (max
215 (/ (- (min y (- maxht step)) minht) step))) "}"))
219 (define (rulesym h w)
221 "\\vrule height " (number->dim (/ h 2))
222 " depth " (number->dim (/ h 2))
223 " width " (number->dim w)
227 (define (bezier-sandwich l)
228 (embedded-ps ((ps-scm 'bezier-sandwich) l)))
237 (define (filledbox breapth width depth height)
239 "\\kern" (number->dim (- breapth))
240 "\\vrule width " (number->dim (+ breapth width))
241 "depth " (number->dim depth)
242 "height " (number->dim height) " "))
249 (string-append "\\hbox{" (output-tex-string s) "}"))
251 (define (tuplet dx dy thick dir)
252 (embedded-ps ((ps-scm 'tuplet) dx dy thick dir)))
254 (define (volta w thick last)
255 (embedded-ps ((ps-scm 'volta) w thick last)))
258 ;; The procedures listed below form the public interface of TeX-scm.
259 ;; (should merge the 2 lists)
260 (cond ((eq? action-name 'all-definitions)
263 (define bezier-sandwich ,bezier-sandwich)
264 (define bracket ,bracket)
266 (define crescendo ,crescendo)
267 (define dashed-slur ,dashed-slur)
268 (define decrescendo ,decrescendo)
269 (define end-output ,end-output)
270 (define experimental-on ,experimental-on)
271 (define filledbox ,filledbox)
272 (define font-def ,font-def)
273 (define font-switch ,font-switch)
274 (define generalmeter ,generalmeter)
275 (define header-end ,header-end)
276 (define lily-def ,lily-def)
277 (define header ,header)
278 (define invoke-char ,invoke-char)
279 (define invoke-dim1 ,invoke-dim1)
280 (define pianobrace ,pianobrace)
281 (define placebox ,placebox)
282 (define rulesym ,rulesym)
283 (define select-font ,select-font)
284 (define start-line ,start-line)
285 (define stop-line ,stop-line)
287 (define tuplet ,tuplet)
288 (define volta ,volta)
291 ((eq? action-name 'beam) beam)
292 ((eq? action-name 'tuplet) tuplet)
293 ((eq? action-name 'bracket) bracket)
294 ((eq? action-name 'crescendo) crescendo)
295 ((eq? action-name 'dashed-slur) dashed-slur)
296 ((eq? action-name 'decrescendo) decrescendo)
297 ((eq? action-name 'end-output) end-output)
298 ((eq? action-name 'experimental-on) experimental-on)
299 ((eq? action-name 'font-def) font-def)
300 ((eq? action-name 'font-switch) font-switch)
301 ((eq? action-name 'generalmeter) generalmeter)
302 ((eq? action-name 'header-end) header-end)
303 ((eq? action-name 'lily-def) lily-def)
304 ((eq? action-name 'header) header)
305 ((eq? action-name 'invoke-char) invoke-char)
306 ((eq? action-name 'invoke-dim1) invoke-dim1)
307 ((eq? action-name 'placebox) placebox)
308 ((eq? action-name 'rulesym) rulesym)
309 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
310 ((eq? action-name 'start-line) start-line)
311 ((eq? action-name 'stem) stem)
312 ((eq? action-name 'stop-line) stop-line)
313 ((eq? action-name 'volta) volta)
314 (else (error "unknown tag -- PS-TEX " action-name))
319 (define (ps-scm action-name)
322 ;; alist containing fontname -> fontcommand assoc (both strings)
323 (define font-alist '())
324 (define font-count 0)
325 (define current-font "")
326 (define (clear-fontcache)
328 (set! font-alist '())
330 (set! current-font "")))
332 (define (cached-fontname i)
335 (make-string 1 (integer->char (+ 65 i)))))
337 (define (select-font font-name)
338 (if (not (equal? font-name current-font))
340 (set! current-font font-name)
341 (define font-cmd (assoc font-name font-alist))
342 (if (eq? font-cmd #f)
344 (set! font-cmd (cached-fontname font-count))
345 (set! font-alist (acons font-name font-cmd font-alist))
346 (set! font-count (+ 1 font-count))
347 (string-append "\n/" font-cmd " {/"
349 " findfont 12 scalefont setfont} bind def \n"
351 (string-append (cdr font-cmd) " ")))
352 ; font-name == current-font no switch needed
356 (define (beam width slope thick)
358 (numbers->string (list width slope thick)) " draw_beam " ))
361 (invoke-dim1 "draw_bracket" h))
364 (invoke-char "show" i))
366 (define (crescendo w h cont)
368 (numbers->string (list w h (inexact->exact cont)))
371 (define (dashed-slur thick dash l)
373 (apply string-append (map control->string l))
374 (number->string thick)
376 (if (> 1 dash) (number->string (- (* thick dash) thick)) "0") " "
377 (number->string (* 2 thick))
378 " ] 0 draw_dashed_slur"))
380 (define (decrescendo w h cont)
382 (numbers->string (list w h (inexact->exact cont)))
389 (define (experimental-on) "")
391 (define (filledbox kern width height depth)
392 (string-append (numbers->string (list kern width height depth))
396 (define (font-def i s)
399 (substring s 0 (- (string-length s) 4))
400 " findfont 12 scalefont setfont} bind def \n"))
402 (define (font-switch i)
403 (string-append (font i) " "))
405 (define (generalmeter num den)
406 (string-append (number->string (inexact->exact num)) " " (number->string (inexact->exact den)) " generalmeter "))
408 (define (header-end) "")
409 (define (lily-def key val)
411 "/" key " {" val "} bind def\n"))
413 (define (header creator generate)
416 "%%Creator: " creator generate "\n"))
418 (define (invoke-char s i)
420 "(\\" (inexact->string i 8) ") " s " " ))
422 (define (invoke-dim1 s d)
424 (number->string (* d (/ 72.27 72))) " " s ))
426 (define (placebox x y s)
428 (number->string x) " " (number->string y) " {" s "} placebox "))
429 (define (pianobrace y)
433 (define (rulesym x y)
435 (number->string x) " "
436 (number->string y) " "
439 (define (bezier-sandwich l)
441 (apply string-append (map control->string l))
442 " draw_bezier_sandwich"))
449 (define (stem kern width height depth)
450 (string-append (numbers->string (list kern width height depth))
457 (string-append "(" s ") show "))
460 (define (volta w thick last)
462 (numbers->string (list w thick (inexact->exact last)))
465 (define (tuplet dx dy thick dir)
467 (numbers->string (list dx dy thick (inexact->exact dir)))
476 (cond ((eq? action-name 'all-definitions)
479 (define tuplet ,tuplet)
480 (define bracket ,bracket)
482 (define crescendo ,crescendo)
483 (define volta ,volta)
484 (define bezier-sandwich ,bezier-sandwich)
485 (define dashed-slur ,dashed-slur)
486 (define decrescendo ,decrescendo)
487 (define end-output ,end-output)
488 (define experimental-on ,experimental-on)
489 (define filledbox ,filledbox)
490 (define font-def ,font-def)
491 (define font-switch ,font-switch)
492 (define generalmeter ,generalmeter)
493 (define pianobrace ,pianobrace)
494 (define header-end ,header-end)
495 (define lily-def ,lily-def)
496 (define header ,header)
497 (define invoke-char ,invoke-char)
498 (define invoke-dim1 ,invoke-dim1)
499 (define placebox ,placebox)
500 (define rulesym ,rulesym)
501 (define select-font ,select-font)
502 (define start-line ,start-line)
504 (define stop-line ,stop-line)
507 ((eq? action-name 'tuplet) tuplet)
508 ((eq? action-name 'beam) beam)
509 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
510 ((eq? action-name 'bracket) bracket)
511 ((eq? action-name 'char) char)
512 ((eq? action-name 'crescendo) crescendo)
513 ((eq? action-name 'dashed-slur) dashed-slur)
514 ((eq? action-name 'decrescendo) decrescendo)
515 ((eq? action-name 'experimental-on) experimental-on)
516 ((eq? action-name 'filledbox) filledbox)
517 ((eq? action-name 'select-font) select-font)
518 ((eq? action-name 'volta) volta)
519 (else (error "unknown tag -- PS-SCM " action-name))
525 ; Russ McManus, <mcmanus@IDT.NET>
527 ; I use the following, which should definitely be provided somewhere
528 ; in guile, but isn't, AFAIK:
532 (define (hash-table-for-each fn ht)
534 ((= i (vector-length ht)))
535 (do ((alist (vector-ref ht i) (cdr alist)))
537 (fn (car (car alist)) (cdr (car alist))))))
539 (define (hash-table-map fn ht)
542 ((= i (vector-length ht)) (reverse ret-ls))
543 (do ((alist (vector-ref ht i) (cdr alist)))
545 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))