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)))
63 (define (spanbar_postbreak_only_visibility d) (if (= d 1) '(#f . #f) '(#t . #t)))
68 (define (tex-scm action-name)
73 (define font-alist '())
75 (define current-font "")
76 (define (clear-fontcache)
80 (set! current-font "")))
82 (define (cached-fontname i)
85 (make-string 1 (integer->char (+ 65 i)))))
87 (define (select-font font-name)
88 (if (not (equal? font-name current-font))
90 (set! current-font font-name)
91 (define font-cmd (assoc font-name font-alist))
94 (set! font-cmd (cached-fontname font-count))
95 (set! font-alist (acons font-name font-cmd font-alist))
96 (set! font-count (+ 1 font-count))
97 (string-append "\\font" font-cmd "=" font-name font-cmd))
102 (define (beam width slope thick)
103 (embedded-ps ((ps-scm 'beam) width slope thick)))
106 (embedded-ps ((ps-scm 'bracket) h)))
108 (define (dashed-slur thick dash l)
109 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
111 (define (crescendo w h cont)
112 (embedded-ps ((ps-scm 'crescendo) w h cont)))
115 (string-append "\\show{" (inexact->string i 10) "}"))
117 (define (decrescendo w h cont)
118 (embedded-ps ((ps-scm 'decrescendo) w h cont)))
120 (define (embedded-ps s)
121 (string-append "\\embeddedps{" s "}"))
124 "\n\\EndLilyPondOutput")
126 (define (experimental-on)
127 "\\turnOnExperimentalFeatures")
129 (define (font-switch i)
133 (define (font-def i s)
135 "\\font" (font-switch i) "=" s "\n"))
137 (define (generalmeter num den)
139 "\\generalmeter{" (number->string (inexact->exact num)) "}{" (number->string (inexact->exact den)) "}"))
141 (define (header-end) "\\turnOnPostScript")
143 (define (header creator generate)
145 "%created by: " creator generate "\n"))
147 (define (invoke-char s i)
149 "\n\\" s "{" (inexact->string i 10) "}" ))
151 (define (invoke-dim1 s d)
153 "\n\\" s "{" (number->dim d) "}"))
158 ;; need to do something to make this really safe.
160 (if security-paranoia
161 (define (output-tex-string s)
162 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post))
163 (define (output-tex-string s) s))
165 (define (lily-def key val)
167 "\\def\\" (output-tex-string key) "{" (output-tex-string val) "}\n"))
169 (define (number->dim x)
171 (number->string (chop-decimal x)) "pt "))
173 (define (placebox x y s)
176 (number->dim y) "}{" (number->dim x) "}{" s "}"))
178 (define (pianobrace y)
180 (define minht (* 2 mudelapaperstaffheight))
181 (define maxht (* 7 minht))
183 "{\\bracefont " (char (/ (- (min y (- maxht step)) minht) step)) "}"))
187 (define (rulesym h w)
189 "\\vrule height " (number->dim (/ h 2))
190 " depth " (number->dim (/ h 2))
191 " width " (number->dim w)
195 (define (bezier-sandwich l)
196 (embedded-ps ((ps-scm 'bezier-sandwich) l)))
205 (define (filledbox breapth width depth height)
207 "\\kern" (number->dim (- breapth))
208 "\\vrule width " (number->dim (+ breapth width))
209 "depth " (number->dim depth)
210 "height " (number->dim height) " "))
217 (string-append "\\hbox{" (output-tex-string s) "}"))
219 (define (tuplet dx dy thick dir)
220 (embedded-ps ((ps-scm 'tuplet) dx dy thick dir)))
222 (define (volta w thick last)
223 (embedded-ps ((ps-scm 'volta) w thick last)))
226 ;; The procedures listed below form the public interface of TeX-scm.
227 ;; (should merge the 2 lists)
228 (cond ((eq? action-name 'all-definitions)
231 (define bezier-sandwich ,bezier-sandwich)
232 (define bracket ,bracket)
234 (define crescendo ,crescendo)
235 (define dashed-slur ,dashed-slur)
236 (define decrescendo ,decrescendo)
237 (define end-output ,end-output)
238 (define experimental-on ,experimental-on)
239 (define filledbox ,filledbox)
240 (define font-def ,font-def)
241 (define font-switch ,font-switch)
242 (define generalmeter ,generalmeter)
243 (define header-end ,header-end)
244 (define lily-def ,lily-def)
245 (define header ,header)
246 (define invoke-char ,invoke-char)
247 (define invoke-dim1 ,invoke-dim1)
248 (define pianobrace ,pianobrace)
249 (define placebox ,placebox)
250 (define rulesym ,rulesym)
251 (define select-font ,select-font)
252 (define start-line ,start-line)
253 (define stop-line ,stop-line)
255 (define tuplet ,tuplet)
256 (define volta ,volta)
259 ((eq? action-name 'beam) beam)
260 ((eq? action-name 'tuplet) tuplet)
261 ((eq? action-name 'bracket) bracket)
262 ((eq? action-name 'crescendo) crescendo)
263 ((eq? action-name 'dashed-slur) dashed-slur)
264 ((eq? action-name 'decrescendo) decrescendo)
265 ((eq? action-name 'end-output) end-output)
266 ((eq? action-name 'experimental-on) experimental-on)
267 ((eq? action-name 'font-def) font-def)
268 ((eq? action-name 'font-switch) font-switch)
269 ((eq? action-name 'generalmeter) generalmeter)
270 ((eq? action-name 'header-end) header-end)
271 ((eq? action-name 'lily-def) lily-def)
272 ((eq? action-name 'header) header)
273 ((eq? action-name 'invoke-char) invoke-char)
274 ((eq? action-name 'invoke-dim1) invoke-dim1)
275 ((eq? action-name 'placebox) placebox)
276 ((eq? action-name 'rulesym) rulesym)
277 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
278 ((eq? action-name 'start-line) start-line)
279 ((eq? action-name 'stem) stem)
280 ((eq? action-name 'stop-line) stop-line)
281 ((eq? action-name 'volta) volta)
282 (else (error "unknown tag -- PS-TEX " action-name))
287 (define (ps-scm action-name)
289 (define font-alist '())
290 (define font-count 0)
291 (define current-font "")
292 (define (clear-fontcache)
294 (set! font-alist '())
296 (set! current-font "")))
298 (define (cached-fontname i)
301 (make-string 1 (integer->char (+ 65 i)))))
303 (define (select-font font-name)
304 (if (not (equal? font-name current-font))
306 (set! current-font font-name)
307 (define font-cmd (assoc font-name font-alist))
308 (if (eq? font-cmd #f)
310 (set! font-cmd (cached-fontname font-count))
311 (set! font-alist (acons font-name font-cmd font-alist))
312 (set! font-count (+ 1 font-count))
313 (string-append "\n/" font-cmd " {/"
315 " findfont 12 scalefont setfont} bind def\n"
321 (define (beam width slope thick)
323 (numbers->string (list width slope thick)) " draw_beam " ))
326 (invoke-dim1 "draw_bracket" h))
329 (invoke-char "show" i))
331 (define (crescendo w h cont)
333 (numbers->string (list w h (inexact->exact cont)))
336 (define (dashed-slur thick dash l)
338 (apply string-append (map control->string l))
339 (number->string thick)
341 (if (> 1 dash) (number->string (- (* thick dash) thick)) "0") " "
342 (number->string (* 2 thick))
343 " ] 0 draw_dashed_slur"))
345 (define (decrescendo w h cont)
347 (numbers->string (list w h (inexact->exact cont)))
354 (define (experimental-on) "")
356 (define (filledbox kern width height depth)
357 (string-append (numbers->string (list kern width height depth))
361 (define (font-def i s)
364 (substring s 0 (- (string-length s) 4))
365 " findfont 12 scalefont setfont} bind def\n"))
367 (define (font-switch i)
368 (string-append (font i) " "))
370 (define (generalmeter num den)
371 (string-append (number->string (inexact->exact num)) " " (number->string (inexact->exact den)) " generalmeter "))
373 (define (header-end) "")
374 (define (lily-def key val)
376 "/" key " {" val "} bind def\n"))
378 (define (header creator generate)
381 "%%Creator: " creator generate "\n"))
383 (define (invoke-char s i)
385 "(\\" (inexact->string i 8) ") " s " " ))
387 (define (invoke-dim1 s d)
389 (number->string (* d (/ 72.27 72))) " " s ))
391 (define (placebox x y s)
393 (number->string x) " " (number->string y) " {" s "} placebox "))
395 (define (rulesym x y)
397 (number->string x) " "
398 (number->string y) " "
401 (define (bezier-sandwich l)
403 (apply string-append (map control->string l))
404 " draw_bezier_sandwich"))
411 (define (stem kern width height depth)
412 (string-append (numbers->string (list kern width height depth))
419 (string-append "(" s ") set" f " "))
422 (define (volta w thick last)
424 (numbers->string (list w thick (inexact->exact last)))
427 (define (tuplet dx dy thick dir)
429 (numbers->string (list dx dy thick (inexact->exact dir)))
438 (cond ((eq? action-name 'all-definitions)
441 (define tuplet ,tuplet)
442 (define bracket ,bracket)
444 (define crescendo ,crescendo)
445 (define volta ,volta)
446 (define bezier-sandwich ,bezier-sandwich)
447 (define dashed-slur ,dashed-slur)
448 (define decrescendo ,decrescendo)
449 (define end-output ,end-output)
450 (define experimental-on ,experimental-on)
451 (define filledbox ,filledbox)
452 (define font-def ,font-def)
453 (define font-switch ,font-switch)
454 (define generalmeter ,generalmeter)
455 (define header-end ,header-end)
456 (define lily-def ,lily-def)
457 (define header ,header)
458 (define invoke-char ,invoke-char)
459 (define invoke-dim1 ,invoke-dim1)
460 (define placebox ,placebox)
461 (define rulesym ,rulesym)
462 (define select-font ,select-font)
463 (define start-line ,start-line)
465 (define stop-line ,stop-line)
468 ((eq? action-name 'tuplet) tuplet)
469 ((eq? action-name 'beam) beam)
470 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
471 ((eq? action-name 'bracket) bracket)
472 ((eq? action-name 'char) char)
473 ((eq? action-name 'crescendo) crescendo)
474 ((eq? action-name 'dashed-slur) dashed-slur)
475 ((eq? action-name 'decrescendo) decrescendo)
476 ((eq? action-name 'experimental-on) experimental-on)
477 ((eq? action-name 'filledbox) filledbox)
478 ((eq? action-name 'select-font) select-font)
479 ((eq? action-name 'volta) volta)
480 (else (error "unknown tag -- PS-SCM " action-name))
486 ; Russ McManus, <mcmanus@IDT.NET>
488 ; I use the following, which should definitely be provided somewhere
489 ; in guile, but isn't, AFAIK:
493 (define (hash-table-for-each fn ht)
495 ((= i (vector-length ht)))
496 (do ((alist (vector-ref ht i) (cdr alist)))
498 (fn (car (car alist)) (cdr (car alist))))))
500 (define (hash-table-map fn ht)
503 ((= i (vector-length ht)) (reverse ret-ls))
504 (do ((alist (vector-ref ht i) (cdr alist)))
506 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))