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)
90 ("dynamic" . "feta-din")
91 ("finger" . "feta-nummer")
92 ("typewriter" . "cmtt")
97 ("mark" . "feta-nummer")
98 ("number" . "feta-nummer")
99 ("volta" . "feta-nummer"))
102 (define script-alist '())
103 (define (articulation-to-scriptdef a)
104 (assoc a script-alist)
107 ;; Map style names to TeX font names. Return false if
108 ;; no font name found.
109 (define (style-to-cmr s)
114 (define (tex-scm action-name)
119 (define font-alist '())
120 (define font-count 0)
121 (define current-font "")
122 (define (clear-fontcache)
124 (set! font-alist '())
126 (set! current-font "")))
128 (define (cached-fontname i)
131 (make-string 1 (integer->char (+ 65 i)))))
133 (define (select-font font-name)
134 (if (not (equal? font-name current-font))
136 (set! current-font font-name)
137 (define font-cmd (assoc font-name font-alist))
138 (if (eq? font-cmd #f)
140 (set! font-cmd (cached-fontname font-count))
141 (set! font-alist (acons font-name font-cmd font-alist))
142 (set! font-count (+ 1 font-count))
143 (if (equal? font-name "")
144 (error "Empty fontname -- SELECT-FONT"))
145 (string-append "\\font" font-cmd "=" font-name font-cmd))
150 (define (beam width slope thick)
151 (embedded-ps ((ps-scm 'beam) width slope thick)))
154 (embedded-ps ((ps-scm 'bracket) h)))
156 (define (dashed-slur thick dash l)
157 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
159 (define (crescendo w h cont)
160 (embedded-ps ((ps-scm 'crescendo) w h cont)))
163 (string-append "\\show{" (inexact->string i 10) "}"))
165 (define (decrescendo w h cont)
166 (embedded-ps ((ps-scm 'decrescendo) w h cont)))
168 (define (embedded-ps s)
169 (string-append "\\embeddedps{" s "}"))
172 "\n\\EndLilyPondOutput")
174 (define (experimental-on)
175 "\\turnOnExperimentalFeatures")
177 (define (font-switch i)
181 (define (font-def i s)
183 "\\font" (font-switch i) "=" s "\n"))
185 (define (generalmeter num den)
187 "\\generalmeter{" (number->string (inexact->exact num)) "}{" (number->string (inexact->exact den)) "}"))
189 (define (header-end) "\\turnOnPostScript")
191 (define (header creator generate)
193 "%created by: " creator generate "\n"))
195 (define (invoke-char s i)
197 "\n\\" s "{" (inexact->string i 10) "}" ))
199 (define (invoke-dim1 s d)
201 "\n\\" s "{" (number->dim d) "}"))
206 ;; need to do something to make this really safe.
208 (if security-paranoia
209 (define (output-tex-string s)
210 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post))
211 (define (output-tex-string s) s))
213 (define (lily-def key val)
215 "\\def\\" (output-tex-string key) "{" (output-tex-string val) "}\n"))
217 (define (number->dim x)
219 (number->string (chop-decimal x)) " pt "))
221 (define (placebox x y s)
224 (number->dim y) "}{" (number->dim x) "}{" s "}"))
226 (define (pianobrace y)
228 (define minht (* 2 mudelapaperstaffheight))
229 (define maxht (* 7 minht))
231 "{\\bracefont " (char (max
233 (/ (- (min y (- maxht step)) minht) step))) "}"))
237 (define (rulesym h w)
239 "\\vrule height " (number->dim (/ h 2))
240 " depth " (number->dim (/ h 2))
241 " width " (number->dim w)
245 (define (bezier-sandwich l)
246 (embedded-ps ((ps-scm 'bezier-sandwich) l)))
249 (define (start-line ht)
252 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
255 "}\\vss}\\interscoreline")
257 (define (filledbox breapth width depth height)
259 "\\kern" (number->dim (- breapth))
260 "\\vrule width " (number->dim (+ breapth width))
261 "depth " (number->dim depth)
262 "height " (number->dim height) " "))
267 (string-append "\\hbox{" (output-tex-string s) "}"))
269 (define (tuplet ht dx dy thick dir)
270 (embedded-ps ((ps-scm 'tuplet) ht dx dy thick dir)))
272 (define (volta w thick last)
273 (embedded-ps ((ps-scm 'volta) w thick last)))
276 ;; The procedures listed below form the public interface of TeX-scm.
277 ;; (should merge the 2 lists)
278 (cond ((eq? action-name 'all-definitions)
281 (define bezier-sandwich ,bezier-sandwich)
282 (define bracket ,bracket)
284 (define crescendo ,crescendo)
285 (define dashed-slur ,dashed-slur)
286 (define decrescendo ,decrescendo)
287 (define end-output ,end-output)
288 (define experimental-on ,experimental-on)
289 (define filledbox ,filledbox)
290 (define font-def ,font-def)
291 (define font-switch ,font-switch)
292 (define generalmeter ,generalmeter)
293 (define header-end ,header-end)
294 (define lily-def ,lily-def)
295 (define header ,header)
296 (define invoke-char ,invoke-char)
297 (define invoke-dim1 ,invoke-dim1)
298 (define pianobrace ,pianobrace)
299 (define placebox ,placebox)
300 (define rulesym ,rulesym)
301 (define select-font ,select-font)
302 (define start-line ,start-line)
303 (define stop-line ,stop-line)
305 (define tuplet ,tuplet)
306 (define volta ,volta)
309 ((eq? action-name 'beam) beam)
310 ((eq? action-name 'tuplet) tuplet)
311 ((eq? action-name 'bracket) bracket)
312 ((eq? action-name 'crescendo) crescendo)
313 ((eq? action-name 'dashed-slur) dashed-slur)
314 ((eq? action-name 'decrescendo) decrescendo)
315 ((eq? action-name 'end-output) end-output)
316 ((eq? action-name 'experimental-on) experimental-on)
317 ((eq? action-name 'font-def) font-def)
318 ((eq? action-name 'font-switch) font-switch)
319 ((eq? action-name 'generalmeter) generalmeter)
320 ((eq? action-name 'header-end) header-end)
321 ((eq? action-name 'lily-def) lily-def)
322 ((eq? action-name 'header) header)
323 ((eq? action-name 'invoke-char) invoke-char)
324 ((eq? action-name 'invoke-dim1) invoke-dim1)
325 ((eq? action-name 'placebox) placebox)
326 ((eq? action-name 'rulesym) rulesym)
327 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
328 ((eq? action-name 'start-line) start-line)
329 ((eq? action-name 'stem) stem)
330 ((eq? action-name 'stop-line) stop-line)
331 ((eq? action-name 'volta) volta)
332 (else (error "unknown tag -- PS-TEX " action-name))
337 (define (ps-scm action-name)
340 ;; alist containing fontname -> fontcommand assoc (both strings)
341 (define font-alist '())
342 (define font-count 0)
343 (define current-font "")
344 (define (clear-fontcache)
346 (set! font-alist '())
348 (set! current-font "")))
350 (define (cached-fontname i)
353 (make-string 1 (integer->char (+ 65 i)))))
355 (define (select-font font-name)
356 (if (not (equal? font-name current-font))
358 (set! current-font font-name)
359 (define font-cmd (assoc font-name font-alist))
360 (if (eq? font-cmd #f)
362 (set! font-cmd (cached-fontname font-count))
363 (set! font-alist (acons font-name font-cmd font-alist))
364 (set! font-count (+ 1 font-count))
365 (string-append "\n/" font-cmd " {/"
367 " findfont 12 scalefont setfont} bind def \n"
369 (string-append (cdr font-cmd) " ")))
370 ; font-name == current-font no switch needed
374 (define (beam width slope thick)
376 (numbers->string (list width slope thick)) " draw_beam " ))
379 (invoke-dim1 " draw_bracket" h))
382 (invoke-char " show" i))
384 (define (crescendo w h cont)
386 (numbers->string (list w h (inexact->exact cont)))
389 (define (dashed-slur thick dash l)
391 (apply string-append (map control->string l))
392 (number->string thick)
394 (if (> 1 dash) (number->string (- (* thick dash) thick)) "0") " "
395 (number->string (* 2 thick))
396 " ] 0 draw_dashed_slur"))
398 (define (decrescendo w h cont)
400 (numbers->string (list w h (inexact->exact cont)))
401 " draw_decrescendo"))
407 (define (experimental-on) "")
409 (define (filledbox breapth width depth height)
410 (string-append (numbers->string (list breapth width depth height))
414 (define (font-def i s)
417 (substring s 0 (- (string-length s) 4))
418 " findfont 12 scalefont setfont} bind def \n"))
420 (define (font-switch i)
421 (string-append (font i) " "))
423 (define (generalmeter num den)
424 (string-append (number->string (inexact->exact num)) " " (number->string (inexact->exact den)) " generalmeter "))
426 (define (header-end) "")
427 (define (lily-def key val)
429 "/" key " {" val "} bind def\n"))
431 (define (header creator generate)
434 "%%Creator: " creator generate "\n"))
436 (define (invoke-char s i)
438 "(\\" (inexact->string i 8) ") " s " " ))
440 (define (invoke-dim1 s d)
442 (number->string (* d (/ 72.27 72))) " " s ))
444 (define (placebox x y s)
446 (number->string x) " " (number->string y) " {" s "} placebox "))
447 (define (pianobrace y)
451 (define (rulesym x y)
453 (number->string x) " "
454 (number->string y) " "
457 (define (bezier-sandwich l)
459 (apply string-append (map control->string l))
460 " draw_bezier_sandwich"))
462 (define (start-line height)
467 (define (stem breapth width depth height)
468 (string-append (numbers->string (list breapth width depth height))
475 (string-append "(" s ") show "))
478 (define (volta w thick last)
480 (numbers->string (list w thick (inexact->exact last)))
483 (define (tuplet ht dx dy thick dir)
485 (numbers->string (list ht dx dy thick (inexact->exact dir)))
494 (cond ((eq? action-name 'all-definitions)
497 (define tuplet ,tuplet)
498 (define bracket ,bracket)
500 (define crescendo ,crescendo)
501 (define volta ,volta)
502 (define bezier-sandwich ,bezier-sandwich)
503 (define dashed-slur ,dashed-slur)
504 (define decrescendo ,decrescendo)
505 (define end-output ,end-output)
506 (define experimental-on ,experimental-on)
507 (define filledbox ,filledbox)
508 (define font-def ,font-def)
509 (define font-switch ,font-switch)
510 (define generalmeter ,generalmeter)
511 (define pianobrace ,pianobrace)
512 (define header-end ,header-end)
513 (define lily-def ,lily-def)
514 (define header ,header)
515 (define invoke-char ,invoke-char)
516 (define invoke-dim1 ,invoke-dim1)
517 (define placebox ,placebox)
518 (define rulesym ,rulesym)
519 (define select-font ,select-font)
520 (define start-line ,start-line)
522 (define stop-line ,stop-line)
525 ((eq? action-name 'tuplet) tuplet)
526 ((eq? action-name 'beam) beam)
527 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
528 ((eq? action-name 'bracket) bracket)
529 ((eq? action-name 'char) char)
530 ((eq? action-name 'crescendo) crescendo)
531 ((eq? action-name 'dashed-slur) dashed-slur)
532 ((eq? action-name 'decrescendo) decrescendo)
533 ((eq? action-name 'experimental-on) experimental-on)
534 ((eq? action-name 'filledbox) filledbox)
535 ((eq? action-name 'select-font) select-font)
536 ((eq? action-name 'volta) volta)
537 (else (error "unknown tag -- PS-SCM " action-name))
542 ; Russ McManus, <mcmanus@IDT.NET>
544 ; I use the following, which should definitely be provided somewhere
545 ; in guile, but isn't, AFAIK:
549 (define (hash-table-for-each fn ht)
551 ((= i (vector-length ht)))
552 (do ((alist (vector-ref ht i) (cdr alist)))
554 (fn (car (car alist)) (cdr (car alist))))))
556 (define (hash-table-map fn ht)
559 ((= i (vector-length ht)) (reverse ret-ls))
560 (do ((alist (vector-ref ht i) (cdr alist)))
562 (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))