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)))
64 (define (non_prebreak_visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
67 ;; Score_span_bars are only visible at start of line
68 ;; i.e. if break_dir == RIGHT == 1
69 (define Span_bar_engraver_visibility non_postbreak_visibility)
70 (define mark-visibility non_prebreak_visibility)
71 (define Span_score_bar_engraver_visibility postbreak_only_visibility)
72 (define Piano_bar_engraver_visibility postbreak_only_visibility)
73 (define Staff_group_bar_engraver_visibility postbreak_only_visibility)
82 ("dynamic" . "feta-din")
83 ("finger" . "feta-nummer")
84 ("typewriter" . "cmtt")
89 ("mark" . "feta-nummer")
90 ("number" . "feta-nummer")
91 ("volta" . "feta-nummer"))
94 (define script-alist '())
95 (define (articulation-to-scriptdef a)
96 (assoc a script-alist)
99 ;; Map style names to TeX font names. Return false if
100 ;; no font name found.
101 (define (style-to-cmr s)
106 (define (tex-scm action-name)
111 (define font-alist '())
112 (define font-count 0)
113 (define current-font "")
114 (define (clear-fontcache)
116 (set! font-alist '())
118 (set! current-font "")))
120 (define (cached-fontname i)
123 (make-string 1 (integer->char (+ 65 i)))))
125 (define (select-font font-name)
126 (if (not (equal? font-name current-font))
128 (set! current-font font-name)
129 (define font-cmd (assoc font-name font-alist))
130 (if (eq? font-cmd #f)
132 (set! font-cmd (cached-fontname font-count))
133 (set! font-alist (acons font-name font-cmd font-alist))
134 (set! font-count (+ 1 font-count))
135 (if (equal? font-name "")
136 (error "Empty fontname -- SELECT-FONT"))
137 (string-append "\\font" font-cmd "=" font-name font-cmd))
142 (define (beam width slope thick)
143 (embedded-ps ((ps-scm 'beam) width slope thick)))
146 (embedded-ps ((ps-scm 'bracket) h)))
148 (define (dashed-slur thick dash l)
149 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
151 (define (crescendo w h cont)
152 (embedded-ps ((ps-scm 'crescendo) w h cont)))
155 (string-append "\\show{" (inexact->string i 10) "}"))
157 (define (decrescendo w h cont)
158 (embedded-ps ((ps-scm 'decrescendo) w h cont)))
160 (define (embedded-ps s)
161 (string-append "\\embeddedps{" s "}"))
164 "\n\\EndLilyPondOutput")
166 (define (experimental-on)
167 "\\turnOnExperimentalFeatures")
169 (define (font-switch i)
173 (define (font-def i s)
175 "\\font" (font-switch i) "=" s "\n"))
177 (define (generalmeter num den)
179 "\\generalmeter{" (number->string (inexact->exact num)) "}{" (number->string (inexact->exact den)) "}"))
181 (define (header-end) "\\turnOnPostScript")
183 (define (header creator generate)
185 "%created by: " creator generate "\n"))
187 (define (invoke-char s i)
189 "\n\\" s "{" (inexact->string i 10) "}" ))
191 (define (invoke-dim1 s d)
193 "\n\\" s "{" (number->dim d) "}"))
198 ;; need to do something to make this really safe.
200 (if security-paranoia
201 (define (output-tex-string s)
202 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post))
203 (define (output-tex-string s) s))
205 (define (lily-def key val)
207 "\\def\\" (output-tex-string key) "{" (output-tex-string val) "}\n"))
209 (define (number->dim x)
211 (number->string (chop-decimal x)) "pt "))
213 (define (placebox x y s)
216 (number->dim y) "}{" (number->dim x) "}{" s "}"))
218 (define (pianobrace y)
220 (define minht (* 2 mudelapaperstaffheight))
221 (define maxht (* 7 minht))
223 "{\\bracefont " (char (max
225 (/ (- (min y (- maxht step)) minht) step))) "}"))
229 (define (rulesym h w)
231 "\\vrule height " (number->dim (/ h 2))
232 " depth " (number->dim (/ h 2))
233 " width " (number->dim w)
237 (define (bezier-sandwich l)
238 (embedded-ps ((ps-scm 'bezier-sandwich) l)))
241 (define (start-line ht)
244 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
247 "}\\vss}\\interscoreline")
249 (define (filledbox breapth width depth height)
251 "\\kern" (number->dim (- breapth))
252 "\\vrule width " (number->dim (+ breapth width))
253 "depth " (number->dim depth)
254 "height " (number->dim height) " "))
259 (string-append "\\hbox{" (output-tex-string s) "}"))
261 (define (tuplet dx dy thick dir)
262 (embedded-ps ((ps-scm 'tuplet) dx dy thick dir)))
264 (define (volta w thick last)
265 (embedded-ps ((ps-scm 'volta) w thick last)))
268 ;; The procedures listed below form the public interface of TeX-scm.
269 ;; (should merge the 2 lists)
270 (cond ((eq? action-name 'all-definitions)
273 (define bezier-sandwich ,bezier-sandwich)
274 (define bracket ,bracket)
276 (define crescendo ,crescendo)
277 (define dashed-slur ,dashed-slur)
278 (define decrescendo ,decrescendo)
279 (define end-output ,end-output)
280 (define experimental-on ,experimental-on)
281 (define filledbox ,filledbox)
282 (define font-def ,font-def)
283 (define font-switch ,font-switch)
284 (define generalmeter ,generalmeter)
285 (define header-end ,header-end)
286 (define lily-def ,lily-def)
287 (define header ,header)
288 (define invoke-char ,invoke-char)
289 (define invoke-dim1 ,invoke-dim1)
290 (define pianobrace ,pianobrace)
291 (define placebox ,placebox)
292 (define rulesym ,rulesym)
293 (define select-font ,select-font)
294 (define start-line ,start-line)
295 (define stop-line ,stop-line)
297 (define tuplet ,tuplet)
298 (define volta ,volta)
301 ((eq? action-name 'beam) beam)
302 ((eq? action-name 'tuplet) tuplet)
303 ((eq? action-name 'bracket) bracket)
304 ((eq? action-name 'crescendo) crescendo)
305 ((eq? action-name 'dashed-slur) dashed-slur)
306 ((eq? action-name 'decrescendo) decrescendo)
307 ((eq? action-name 'end-output) end-output)
308 ((eq? action-name 'experimental-on) experimental-on)
309 ((eq? action-name 'font-def) font-def)
310 ((eq? action-name 'font-switch) font-switch)
311 ((eq? action-name 'generalmeter) generalmeter)
312 ((eq? action-name 'header-end) header-end)
313 ((eq? action-name 'lily-def) lily-def)
314 ((eq? action-name 'header) header)
315 ((eq? action-name 'invoke-char) invoke-char)
316 ((eq? action-name 'invoke-dim1) invoke-dim1)
317 ((eq? action-name 'placebox) placebox)
318 ((eq? action-name 'rulesym) rulesym)
319 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
320 ((eq? action-name 'start-line) start-line)
321 ((eq? action-name 'stem) stem)
322 ((eq? action-name 'stop-line) stop-line)
323 ((eq? action-name 'volta) volta)
324 (else (error "unknown tag -- PS-TEX " action-name))
329 (define (ps-scm action-name)
332 ;; alist containing fontname -> fontcommand assoc (both strings)
333 (define font-alist '())
334 (define font-count 0)
335 (define current-font "")
336 (define (clear-fontcache)
338 (set! font-alist '())
340 (set! current-font "")))
342 (define (cached-fontname i)
345 (make-string 1 (integer->char (+ 65 i)))))
347 (define (select-font font-name)
348 (if (not (equal? font-name current-font))
350 (set! current-font font-name)
351 (define font-cmd (assoc font-name font-alist))
352 (if (eq? font-cmd #f)
354 (set! font-cmd (cached-fontname font-count))
355 (set! font-alist (acons font-name font-cmd font-alist))
356 (set! font-count (+ 1 font-count))
357 (string-append "\n/" font-cmd " {/"
359 " findfont 12 scalefont setfont} bind def \n"
361 (string-append (cdr font-cmd) " ")))
362 ; font-name == current-font no switch needed
366 (define (beam width slope thick)
368 (numbers->string (list width slope thick)) " draw_beam " ))
371 (invoke-dim1 "draw_bracket" h))
374 (invoke-char "show" i))
376 (define (crescendo w h cont)
378 (numbers->string (list w h (inexact->exact cont)))
381 (define (dashed-slur thick dash l)
383 (apply string-append (map control->string l))
384 (number->string thick)
386 (if (> 1 dash) (number->string (- (* thick dash) thick)) "0") " "
387 (number->string (* 2 thick))
388 " ] 0 draw_dashed_slur"))
390 (define (decrescendo w h cont)
392 (numbers->string (list w h (inexact->exact cont)))
399 (define (experimental-on) "")
401 (define (filledbox breapth width depth height)
402 (string-append (numbers->string (list breapth width depth height))
406 (define (font-def i s)
409 (substring s 0 (- (string-length s) 4))
410 " findfont 12 scalefont setfont} bind def \n"))
412 (define (font-switch i)
413 (string-append (font i) " "))
415 (define (generalmeter num den)
416 (string-append (number->string (inexact->exact num)) " " (number->string (inexact->exact den)) " generalmeter "))
418 (define (header-end) "")
419 (define (lily-def key val)
421 "/" key " {" val "} bind def\n"))
423 (define (header creator generate)
426 "%%Creator: " creator generate "\n"))
428 (define (invoke-char s i)
430 "(\\" (inexact->string i 8) ") " s " " ))
432 (define (invoke-dim1 s d)
434 (number->string (* d (/ 72.27 72))) " " s ))
436 (define (placebox x y s)
438 (number->string x) " " (number->string y) " {" s "} placebox "))
439 (define (pianobrace y)
443 (define (rulesym x y)
445 (number->string x) " "
446 (number->string y) " "
449 (define (bezier-sandwich l)
451 (apply string-append (map control->string l))
452 " draw_bezier_sandwich"))
459 (define (stem breapth width depth height)
460 (string-append (numbers->string (list breapth width depth height))
467 (string-append "(" s ") show "))
470 (define (volta w thick last)
472 (numbers->string (list w thick (inexact->exact last)))
475 (define (tuplet dx dy thick dir)
477 (numbers->string (list dx dy thick (inexact->exact dir)))
486 (cond ((eq? action-name 'all-definitions)
489 (define tuplet ,tuplet)
490 (define bracket ,bracket)
492 (define crescendo ,crescendo)
493 (define volta ,volta)
494 (define bezier-sandwich ,bezier-sandwich)
495 (define dashed-slur ,dashed-slur)
496 (define decrescendo ,decrescendo)
497 (define end-output ,end-output)
498 (define experimental-on ,experimental-on)
499 (define filledbox ,filledbox)
500 (define font-def ,font-def)
501 (define font-switch ,font-switch)
502 (define generalmeter ,generalmeter)
503 (define pianobrace ,pianobrace)
504 (define header-end ,header-end)
505 (define lily-def ,lily-def)
506 (define header ,header)
507 (define invoke-char ,invoke-char)
508 (define invoke-dim1 ,invoke-dim1)
509 (define placebox ,placebox)
510 (define rulesym ,rulesym)
511 (define select-font ,select-font)
512 (define start-line ,start-line)
514 (define stop-line ,stop-line)
517 ((eq? action-name 'tuplet) tuplet)
518 ((eq? action-name 'beam) beam)
519 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
520 ((eq? action-name 'bracket) bracket)
521 ((eq? action-name 'char) char)
522 ((eq? action-name 'crescendo) crescendo)
523 ((eq? action-name 'dashed-slur) dashed-slur)
524 ((eq? action-name 'decrescendo) decrescendo)
525 ((eq? action-name 'experimental-on) experimental-on)
526 ((eq? action-name 'filledbox) filledbox)
527 ((eq? action-name 'select-font) select-font)
528 ((eq? action-name 'volta) volta)
529 (else (error "unknown tag -- PS-SCM " action-name))
534 ; Russ McManus, <mcmanus@IDT.NET>
536 ; I use the following, which should definitely be provided somewhere
537 ; in guile, but isn't, AFAIK:
541 (define (hash-table-for-each fn ht)
543 ((= i (vector-length ht)))
544 (do ((alist (vector-ref ht i) (cdr alist)))
546 (fn (car (car alist)) (cdr (car alist))))))
548 (define (hash-table-map fn ht)
551 ((= i (vector-length ht)) (reverse ret-ls))
552 (do ((alist (vector-ref ht i) (cdr alist)))
554 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))