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 (spanbar_non_postbreak_visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
63 (define (non_postbreak_visibility d) (if (= d 1) '(#t . #t) '(#f . #f)))
66 ;; Score_span_bars are only visible at start of line
67 ;; i.e. if break_dir == RIGHT == 1
68 (define Span_bar_engraver_visibility non_postbreak_visibility)
70 (define Span_score_bar_engraver_visibility postbreak_only_visibility)
71 (define Piano_bar_engraver_visibility postbreak_only_visibility)
72 (define Staff_group_bar_engraver_visibility postbreak_only_visibility)
81 ("dynamic" . "feta-din")
82 ("finger" . "feta-nummer")
83 ("typewriter" . "cmtt")
88 ("mark" . "feta-nummer")
89 ("number" . "feta-nummer")
90 ("volta" . "feta-nummer"))
94 ;; Map style names to TeX font names. Return false if
95 ;; no font name found.
96 (define (style-to-cmr s)
101 (define (tex-scm action-name)
106 (define font-alist '())
107 (define font-count 0)
108 (define current-font "")
109 (define (clear-fontcache)
111 (set! font-alist '())
113 (set! current-font "")))
115 (define (cached-fontname i)
118 (make-string 1 (integer->char (+ 65 i)))))
120 (define (select-font font-name)
121 (if (not (equal? font-name current-font))
123 (set! current-font font-name)
124 (define font-cmd (assoc font-name font-alist))
125 (if (eq? font-cmd #f)
127 (set! font-cmd (cached-fontname font-count))
128 (set! font-alist (acons font-name font-cmd font-alist))
129 (set! font-count (+ 1 font-count))
130 (string-append "\\font" font-cmd "=" font-name font-cmd))
135 (define (beam width slope thick)
136 (embedded-ps ((ps-scm 'beam) width slope thick)))
139 (embedded-ps ((ps-scm 'bracket) h)))
141 (define (dashed-slur thick dash l)
142 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
144 (define (crescendo w h cont)
145 (embedded-ps ((ps-scm 'crescendo) w h cont)))
148 (string-append "\\show{" (inexact->string i 10) "}"))
150 (define (decrescendo w h cont)
151 (embedded-ps ((ps-scm 'decrescendo) w h cont)))
153 (define (embedded-ps s)
154 (string-append "\\embeddedps{" s "}"))
157 "\n\\EndLilyPondOutput")
159 (define (experimental-on)
160 "\\turnOnExperimentalFeatures")
162 (define (font-switch i)
166 (define (font-def i s)
168 "\\font" (font-switch i) "=" s "\n"))
170 (define (generalmeter num den)
172 "\\generalmeter{" (number->string (inexact->exact num)) "}{" (number->string (inexact->exact den)) "}"))
174 (define (header-end) "\\turnOnPostScript")
176 (define (header creator generate)
178 "%created by: " creator generate "\n"))
180 (define (invoke-char s i)
182 "\n\\" s "{" (inexact->string i 10) "}" ))
184 (define (invoke-dim1 s d)
186 "\n\\" s "{" (number->dim d) "}"))
191 ;; need to do something to make this really safe.
193 (if security-paranoia
194 (define (output-tex-string s)
195 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post))
196 (define (output-tex-string s) s))
198 (define (lily-def key val)
200 "\\def\\" (output-tex-string key) "{" (output-tex-string val) "}\n"))
202 (define (number->dim x)
204 (number->string (chop-decimal x)) "pt "))
206 (define (placebox x y s)
209 (number->dim y) "}{" (number->dim x) "}{" s "}"))
211 (define (pianobrace y)
213 (define minht (* 2 mudelapaperstaffheight))
214 (define maxht (* 7 minht))
216 "{\\bracefont " (char (max
218 (/ (- (min y (- maxht step)) minht) step))) "}"))
222 (define (rulesym h w)
224 "\\vrule height " (number->dim (/ h 2))
225 " depth " (number->dim (/ h 2))
226 " width " (number->dim w)
230 (define (bezier-sandwich l)
231 (embedded-ps ((ps-scm 'bezier-sandwich) l)))
240 (define (filledbox breapth width depth height)
242 "\\kern" (number->dim (- breapth))
243 "\\vrule width " (number->dim (+ breapth width))
244 "depth " (number->dim depth)
245 "height " (number->dim height) " "))
252 (string-append "\\hbox{" (output-tex-string s) "}"))
254 (define (tuplet dx dy thick dir)
255 (embedded-ps ((ps-scm 'tuplet) dx dy thick dir)))
257 (define (volta w thick last)
258 (embedded-ps ((ps-scm 'volta) w thick last)))
261 ;; The procedures listed below form the public interface of TeX-scm.
262 ;; (should merge the 2 lists)
263 (cond ((eq? action-name 'all-definitions)
266 (define bezier-sandwich ,bezier-sandwich)
267 (define bracket ,bracket)
269 (define crescendo ,crescendo)
270 (define dashed-slur ,dashed-slur)
271 (define decrescendo ,decrescendo)
272 (define end-output ,end-output)
273 (define experimental-on ,experimental-on)
274 (define filledbox ,filledbox)
275 (define font-def ,font-def)
276 (define font-switch ,font-switch)
277 (define generalmeter ,generalmeter)
278 (define header-end ,header-end)
279 (define lily-def ,lily-def)
280 (define header ,header)
281 (define invoke-char ,invoke-char)
282 (define invoke-dim1 ,invoke-dim1)
283 (define pianobrace ,pianobrace)
284 (define placebox ,placebox)
285 (define rulesym ,rulesym)
286 (define select-font ,select-font)
287 (define start-line ,start-line)
288 (define stop-line ,stop-line)
290 (define tuplet ,tuplet)
291 (define volta ,volta)
294 ((eq? action-name 'beam) beam)
295 ((eq? action-name 'tuplet) tuplet)
296 ((eq? action-name 'bracket) bracket)
297 ((eq? action-name 'crescendo) crescendo)
298 ((eq? action-name 'dashed-slur) dashed-slur)
299 ((eq? action-name 'decrescendo) decrescendo)
300 ((eq? action-name 'end-output) end-output)
301 ((eq? action-name 'experimental-on) experimental-on)
302 ((eq? action-name 'font-def) font-def)
303 ((eq? action-name 'font-switch) font-switch)
304 ((eq? action-name 'generalmeter) generalmeter)
305 ((eq? action-name 'header-end) header-end)
306 ((eq? action-name 'lily-def) lily-def)
307 ((eq? action-name 'header) header)
308 ((eq? action-name 'invoke-char) invoke-char)
309 ((eq? action-name 'invoke-dim1) invoke-dim1)
310 ((eq? action-name 'placebox) placebox)
311 ((eq? action-name 'rulesym) rulesym)
312 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
313 ((eq? action-name 'start-line) start-line)
314 ((eq? action-name 'stem) stem)
315 ((eq? action-name 'stop-line) stop-line)
316 ((eq? action-name 'volta) volta)
317 (else (error "unknown tag -- PS-TEX " action-name))
322 (define (ps-scm action-name)
325 ;; alist containing fontname -> fontcommand assoc (both strings)
326 (define font-alist '())
327 (define font-count 0)
328 (define current-font "")
329 (define (clear-fontcache)
331 (set! font-alist '())
333 (set! current-font "")))
335 (define (cached-fontname i)
338 (make-string 1 (integer->char (+ 65 i)))))
340 (define (select-font font-name)
341 (if (not (equal? font-name current-font))
343 (set! current-font font-name)
344 (define font-cmd (assoc font-name font-alist))
345 (if (eq? font-cmd #f)
347 (set! font-cmd (cached-fontname font-count))
348 (set! font-alist (acons font-name font-cmd font-alist))
349 (set! font-count (+ 1 font-count))
350 (string-append "\n/" font-cmd " {/"
352 " findfont 12 scalefont setfont} bind def \n"
354 (string-append (cdr font-cmd) " ")))
355 ; font-name == current-font no switch needed
359 (define (beam width slope thick)
361 (numbers->string (list width slope thick)) " draw_beam " ))
364 (invoke-dim1 "draw_bracket" h))
367 (invoke-char "show" i))
369 (define (crescendo w h cont)
371 (numbers->string (list w h (inexact->exact cont)))
374 (define (dashed-slur thick dash l)
376 (apply string-append (map control->string l))
377 (number->string thick)
379 (if (> 1 dash) (number->string (- (* thick dash) thick)) "0") " "
380 (number->string (* 2 thick))
381 " ] 0 draw_dashed_slur"))
383 (define (decrescendo w h cont)
385 (numbers->string (list w h (inexact->exact cont)))
392 (define (experimental-on) "")
394 (define (filledbox breapth width depth height)
395 (string-append (numbers->string (list breapth width depth height))
399 (define (font-def i s)
402 (substring s 0 (- (string-length s) 4))
403 " findfont 12 scalefont setfont} bind def \n"))
405 (define (font-switch i)
406 (string-append (font i) " "))
408 (define (generalmeter num den)
409 (string-append (number->string (inexact->exact num)) " " (number->string (inexact->exact den)) " generalmeter "))
411 (define (header-end) "")
412 (define (lily-def key val)
414 "/" key " {" val "} bind def\n"))
416 (define (header creator generate)
419 "%%Creator: " creator generate "\n"))
421 (define (invoke-char s i)
423 "(\\" (inexact->string i 8) ") " s " " ))
425 (define (invoke-dim1 s d)
427 (number->string (* d (/ 72.27 72))) " " s ))
429 (define (placebox x y s)
431 (number->string x) " " (number->string y) " {" s "} placebox "))
432 (define (pianobrace y)
436 (define (rulesym x y)
438 (number->string x) " "
439 (number->string y) " "
442 (define (bezier-sandwich l)
444 (apply string-append (map control->string l))
445 " draw_bezier_sandwich"))
452 (define (stem breapth width depth height)
453 (string-append (numbers->string (list breapth width depth height))
460 (string-append "(" s ") show "))
463 (define (volta w thick last)
465 (numbers->string (list w thick (inexact->exact last)))
468 (define (tuplet dx dy thick dir)
470 (numbers->string (list dx dy thick (inexact->exact dir)))
479 (cond ((eq? action-name 'all-definitions)
482 (define tuplet ,tuplet)
483 (define bracket ,bracket)
485 (define crescendo ,crescendo)
486 (define volta ,volta)
487 (define bezier-sandwich ,bezier-sandwich)
488 (define dashed-slur ,dashed-slur)
489 (define decrescendo ,decrescendo)
490 (define end-output ,end-output)
491 (define experimental-on ,experimental-on)
492 (define filledbox ,filledbox)
493 (define font-def ,font-def)
494 (define font-switch ,font-switch)
495 (define generalmeter ,generalmeter)
496 (define pianobrace ,pianobrace)
497 (define header-end ,header-end)
498 (define lily-def ,lily-def)
499 (define header ,header)
500 (define invoke-char ,invoke-char)
501 (define invoke-dim1 ,invoke-dim1)
502 (define placebox ,placebox)
503 (define rulesym ,rulesym)
504 (define select-font ,select-font)
505 (define start-line ,start-line)
507 (define stop-line ,stop-line)
510 ((eq? action-name 'tuplet) tuplet)
511 ((eq? action-name 'beam) beam)
512 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
513 ((eq? action-name 'bracket) bracket)
514 ((eq? action-name 'char) char)
515 ((eq? action-name 'crescendo) crescendo)
516 ((eq? action-name 'dashed-slur) dashed-slur)
517 ((eq? action-name 'decrescendo) decrescendo)
518 ((eq? action-name 'experimental-on) experimental-on)
519 ((eq? action-name 'filledbox) filledbox)
520 ((eq? action-name 'select-font) select-font)
521 ((eq? action-name 'volta) volta)
522 (else (error "unknown tag -- PS-SCM " action-name))
528 ; Russ McManus, <mcmanus@IDT.NET>
530 ; I use the following, which should definitely be provided somewhere
531 ; in guile, but isn't, AFAIK:
535 (define (hash-table-for-each fn ht)
537 ((= i (vector-length ht)))
538 (do ((alist (vector-ref ht i) (cdr alist)))
540 (fn (car (car alist)) (cdr (car alist))))))
542 (define (hash-table-map fn ht)
545 ((= i (vector-length ht)) (reverse ret-ls))
546 (do ((alist (vector-ref ht i) (cdr alist)))
548 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))