]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
release: 1.1.48
[lilypond.git] / scm / lily.scm
1 ; lily.scm -- implement Scheme output routines for TeX and PostScript
2 ;
3 ;  source file of the GNU LilyPond music typesetter
4
5 ; (c) 1998 Jan Nieuwenhuizen <janneke@gnu.org>
6
7
8 ;(debug-enable 'backtrace)
9
10 ;;; library funtions
11 (define
12   (xnumbers->string l)
13   (string-append 
14    (map (lambda (n) (string-append (number->string n ) " ")) l)))
15
16 (define (reduce operator list)
17       (if (null? (cdr list)) (car list)
18           (operator (car list) (reduce operator (cdr list)))
19           )
20       )
21
22
23 (define (glue-2-strings a b) (string-append a " " b))
24
25 (define
26   (numbers->string l)
27   (reduce glue-2-strings (map number->string l)))
28
29 (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
30
31 (define (number->octal-string x)
32   (let* ((n (inexact->exact x))
33          (n64 (quotient n 64))
34          (n8 (quotient (- n (* n64 64)) 8)))
35     (string-append
36      (number->string n64)
37      (number->string n8)
38      (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
39
40 (define (inexact->string x radix)
41   (let ((n (inexact->exact x)))
42     (number->string n radix)))
43
44
45 (define
46   (control->string c)
47   (string-append
48    (string-append (number->string (car c)) " ")
49    (string-append (number->string (cadr c)) " ")))
50
51
52 (define
53   (font i)
54   (string-append
55    "font"
56    (make-string 1 (integer->char (+ (char->integer #\A) i)))
57    ))
58
59
60
61 (define (scm-scm action-name)
62   1)
63
64 (define security-paranoia #f)
65
66
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)))
70
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)))
73
74
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)
82
83 ;; Spacing constants for prefatory matter.
84 ;;
85 ;; rules for this spacing are much more complicated than this. See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147
86 ;;
87 ;;
88
89 (define space-alist
90  '(
91    (("" "Clef_item") . (minimum_space 1.0))
92    (("" "Staff_bar") . (minimum_space 0.0))
93    (("" "Clef_item") . (minimum_space 1.0))
94    (("" "Key_item") . (minimum_space 0.5))
95    (("" "Span_bar") . (extra_space 0.0))
96    (("" "Time_signature") . (extra_space 0.0))
97    (("" "begin-of-note") . (minimum_space 1.5))
98    (("Clef_item" "Key_item") . (minimum_space 3.5))
99    (("Key_item" "Time_signature") . (extra_space 2.0))
100    (("Clef_item"  "Time_signature") . (minimum_space 3.5))
101    (("Staff_bar" "Clef_item") .   (minimum_space 1.0))
102    (("Clef_item"  "Staff_bar") .  (minimum_space 3.7))
103    (("Time_signature" "Staff_bar") .  (minimum_space 2.0))
104    (("Key_item"  "Staff_bar") .  (extra_space 1.0))
105    (("Span_bar" "Clef_item") .   (extra_space 1.0))
106    (("Clef_item"  "Span_bar") . (minimum_space 3.7))
107    (("Time_signature" "Span_bar") . (minimum_space 2.0))
108    (("Key_item"  "Span_bar") . (minimum_space 2.5))
109    (("Staff_bar" "Time_signature") . (minimum_space 1.0))
110    (("Time_signature" "begin-of-note") . (extra_space 1.0))     ; Huh? see twinkle.ly
111    (("Key_item" "begin-of-note") . (extra_space 3.0))
112    (("Staff_bar" "begin-of-note") . (extra_space 1.0))
113    (("Clef_item" "begin-of-note") . (minimum_space 5.0))
114    )
115 )
116  
117  
118
119 (define (break-align-spacer this next)
120   (let ((entry (assoc `(,this ,next) space-alist)))
121     (if entry
122         (cdr entry)
123         (begin (ly-warn (string-append "Unknown spacing pair `" this "', `" next "'"))
124                '(minimum_space 0.0)))))
125   
126         
127
128 ;;;;;;;; TeX
129
130 (define cmr-alist 
131   '(("bold" . "cmbx") 
132     ("dynamic" . "feta-din") 
133     ("finger" . "feta-nummer") 
134     ("typewriter" . "cmtt") 
135     ("italic" . "cmti") 
136     ("roman" . "cmr") 
137     ("large" . "cmbx") 
138     ("Large" . "cmbx") 
139     ("mark" . "feta-nummer") 
140     ("number" . "feta-nummer") 
141     ("volta" . "feta-nummer"))
142 )
143
144 (define script-alist '())
145 (define (articulation-to-scriptdef a)
146   (assoc a script-alist)
147   )
148
149 ;; Map style names to TeX font names.  Return false if 
150 ;; no font name found. 
151 (define (style-to-cmr s)
152   (assoc s cmr-alist )
153   )
154
155
156 (define (tex-scm action-name)
157
158   (define (unknown) 
159     "%\n\\unknown%\n")
160
161   (define font-alist '())
162   (define font-count 0)
163   (define current-font "")
164   (define (clear-fontcache)
165     (begin
166       (set! font-alist '())
167       (set! font-count 0)
168       (set! current-font "")))
169   
170   (define (cached-fontname i)
171     (string-append
172      "\\lilyfont"
173      (make-string 1 (integer->char (+ 65 i)))))
174     
175   (define (select-font font-name)
176       (if (not (equal? font-name current-font))
177           (begin
178             (set! current-font font-name)
179             (define font-cmd (assoc font-name font-alist))
180             (if (eq? font-cmd #f)
181                 (begin
182                   (set! font-cmd (cached-fontname font-count))
183                   (set! font-alist (acons font-name font-cmd font-alist))
184                   (set! font-count (+ 1 font-count))
185                   (if (equal? font-name "")
186                       (error "Empty fontname -- SELECT-FONT"))
187                   (string-append "\\font" font-cmd "=" font-name font-cmd))
188                 (cdr font-cmd)))
189           ""                            ;no switch needed
190           ))
191   
192   (define (beam width slope thick)
193     (embedded-ps ((ps-scm 'beam) width slope thick)))
194
195   (define (bracket h)
196     (embedded-ps ((ps-scm 'bracket) h)))
197
198   (define (dashed-slur thick dash l)
199     (embedded-ps ((ps-scm 'dashed-slur)  thick dash l)))
200
201   (define (crescendo w h cont)
202     (embedded-ps ((ps-scm 'crescendo) w h cont)))
203
204   (define (char i)
205     (string-append "\\show{" (inexact->string i 10) "}"))
206   
207   (define (decrescendo w h cont)
208     (embedded-ps ((ps-scm 'decrescendo) w h cont)))
209
210   (define (embedded-ps s)
211     (string-append "\\embeddedps{" s "}"))
212
213   (define (end-output) 
214     "\n\\EndLilyPondOutput")
215   
216   (define (experimental-on)
217     "\\turnOnExperimentalFeatures")
218
219   (define (font-switch i)
220     (string-append
221      "\\" (font i) "\n"))
222
223   (define (font-def i s)
224     (string-append
225      "\\font" (font-switch i) "=" s "\n"))
226
227   (define (generalmeter num den)
228     (string-append 
229      "\\generalmeter{" (number->string (inexact->exact num)) "}{" (number->string (inexact->exact den)) "}"))
230
231   (define (header-end) "\\turnOnPostScript")
232
233   (define (header creator generate) 
234     (string-append
235      "%created by: " creator generate "\n"))
236
237   (define (invoke-char s i)
238     (string-append 
239      "\n\\" s "{" (inexact->string i 10) "}" ))
240
241   (define (invoke-dim1 s d)
242     (string-append
243      "\n\\" s "{" (number->dim d) "}"))
244   (define (pt->sp x)
245     (* 65536 x))
246   
247   ;;
248   ;; need to do something to make this really safe.
249   ;;
250   (if security-paranoia
251       (define (output-tex-string s)
252         (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post))
253       (define (output-tex-string s)    s))
254
255   (define (lily-def key val)
256     (string-append
257      "\\def\\" (output-tex-string key) "{" (output-tex-string val) "}\n"))
258
259   (define (number->dim x)
260     (string-append 
261      (number->string  (chop-decimal x)) " pt "))
262
263   (define (placebox x y s) 
264     (string-append 
265      "\\placebox{"
266      (number->dim y) "}{" (number->dim x) "}{" s "}"))
267
268   (define (pianobrace y)
269     (define step 1.0)
270     (define minht (* 2 mudelapaperstaffheight))
271     (define maxht (* 7 minht))
272     (string-append
273      "{\\bracefont " (char (max
274                             0
275                             (/  (- (min y (- maxht step)) minht) step))) "}"))
276
277
278
279   (define (rulesym h w) 
280     (string-append 
281      "\\vrule height " (number->dim (/ h 2))
282      " depth " (number->dim (/ h 2))
283      " width " (number->dim w)
284      )
285     )
286
287   (define (bezier-sandwich l)
288     (embedded-ps ((ps-scm 'bezier-sandwich) l)))
289
290
291   (define (start-line ht)
292     (begin
293       (clear-fontcache)
294       (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
295     )
296   (define (stop-line) 
297     "}\\vss}\\interscoreline")
298   (define (stop-last-line)
299     "}\\vss}")
300   (define (filledbox breapth width depth height) 
301     (string-append 
302      "\\kern" (number->dim (- breapth))
303      "\\vrule width " (number->dim (+ breapth width))
304      "depth " (number->dim depth)
305      "height " (number->dim height) " "))
306
307
308
309   (define (text s)
310     (string-append "\\hbox{" (output-tex-string s) "}"))
311   
312   (define (tuplet ht dx dy thick dir)
313     (embedded-ps ((ps-scm 'tuplet) ht dx dy thick dir)))
314
315   (define (volta w thick last)
316     (embedded-ps ((ps-scm 'volta) w thick last)))
317
318   ;; TeX
319   ;; The procedures listed below form the public interface of TeX-scm.
320   ;; (should merge the 2 lists)
321   (cond ((eq? action-name 'all-definitions)
322          `(begin
323             (define beam ,beam)
324             (define bezier-sandwich ,bezier-sandwich)
325             (define bracket ,bracket)
326             (define char ,char)
327             (define crescendo ,crescendo)
328             (define dashed-slur ,dashed-slur) 
329             (define decrescendo ,decrescendo) 
330             (define end-output ,end-output)
331             (define experimental-on ,experimental-on)
332             (define filledbox ,filledbox)
333             (define font-def ,font-def)
334             (define font-switch ,font-switch)
335             (define generalmeter ,generalmeter)
336             (define header-end ,header-end)
337             (define lily-def ,lily-def)
338             (define header ,header) 
339             (define invoke-char ,invoke-char) 
340             (define invoke-dim1 ,invoke-dim1)
341             (define pianobrace ,pianobrace)
342             (define placebox ,placebox)
343             (define rulesym ,rulesym)
344             (define select-font ,select-font)
345             (define start-line ,start-line)
346             (define stop-line ,stop-line)
347             (define stop-last-line ,stop-last-line)
348             (define text ,text)
349             (define tuplet ,tuplet)
350             (define volta ,volta)
351             ))
352
353         ((eq? action-name 'beam) beam)
354         ((eq? action-name 'tuplet) tuplet)
355         ((eq? action-name 'bracket) bracket)
356         ((eq? action-name 'crescendo) crescendo)
357         ((eq? action-name 'dashed-slur) dashed-slur) 
358         ((eq? action-name 'decrescendo) decrescendo) 
359         ((eq? action-name 'end-output) end-output)
360         ((eq? action-name 'experimental-on) experimental-on)
361         ((eq? action-name 'font-def) font-def)
362         ((eq? action-name 'font-switch) font-switch)
363         ((eq? action-name 'generalmeter) generalmeter)
364         ((eq? action-name 'header-end) header-end)
365         ((eq? action-name 'lily-def) lily-def)
366         ((eq? action-name 'header) header) 
367         ((eq? action-name 'invoke-char) invoke-char) 
368         ((eq? action-name 'invoke-dim1) invoke-dim1)
369         ((eq? action-name 'placebox) placebox)
370         ((eq? action-name 'rulesym) rulesym)
371         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
372         ((eq? action-name 'start-line) start-line)
373         ((eq? action-name 'stem) stem)
374         ((eq? action-name 'stop-line) stop-line)
375         ((eq? action-name 'stop-last-line) stop-last-line)
376         ((eq? action-name 'volta) volta)
377         (else (error "unknown tag -- PS-TEX " action-name))
378         )
379   )
380
381 ;;;;;;;;;;;; PS
382 (define (ps-scm action-name)
383
384
385   ;; alist containing fontname -> fontcommand assoc (both strings)
386   (define font-alist '())
387   (define font-count 0)
388   (define current-font "")
389   (define (clear-fontcache)
390     (begin
391       (set! font-alist '())
392       (set! font-count 0)
393       (set! current-font "")))
394   
395   (define (cached-fontname i)
396     (string-append
397      " lilyfont"
398      (make-string 1 (integer->char (+ 65 i)))))
399
400   (define (select-font font-name)
401       (if (not (equal? font-name current-font))
402           (begin
403             (set! current-font font-name)
404             (define font-cmd (assoc font-name font-alist))
405             (if (eq? font-cmd #f)
406                 (begin
407                   (set! font-cmd (cached-fontname font-count))
408                   (set! font-alist (acons font-name font-cmd font-alist))
409                   (set! font-count (+ 1 font-count))
410                   (string-append "\n/" font-cmd " {/"
411                                  font-name
412                                  " findfont 12 scalefont setfont} bind def \n"
413                                  font-cmd " \n"))
414                 (string-append (cdr font-cmd) " ")))
415           ; font-name == current-font no switch needed
416           ""                            
417           ))
418                   
419   (define (beam width slope thick)
420     (string-append
421      (numbers->string (list width slope thick)) " draw_beam " ))
422
423   (define (bracket h)
424     (invoke-dim1 " draw_bracket" h))
425
426   (define (char i)
427     (invoke-char " show" i))
428
429   (define (crescendo w h cont)
430     (string-append 
431      (numbers->string (list w h (inexact->exact cont)))
432      " draw_crescendo"))
433
434   (define (dashed-slur thick dash l)
435     (string-append 
436      (apply string-append (map control->string l)) 
437      (number->string thick) 
438      " [ "
439      (if (> 1 dash) (number->string (- (* thick dash) thick)) "0") " "
440      (number->string (* 2 thick))
441      " ] 0 draw_dashed_slur"))
442
443   (define (decrescendo w h cont)
444     (string-append 
445      (numbers->string (list w h (inexact->exact cont)))
446      " draw_decrescendo"))
447
448
449   (define (end-output)
450     "\nshowpage\n")
451   
452   (define (experimental-on) "")
453   
454   (define (filledbox breapth width depth height) 
455     (string-append (numbers->string (list breapth width depth height))
456                    " draw_box" ))
457
458   ;; obsolete?
459   (define (font-def i s)
460     (string-append
461      "\n/" (font i) " {/" 
462      (substring s 0 (- (string-length s) 4))
463      " findfont 12 scalefont setfont} bind def \n"))
464
465   (define (font-switch i)
466     (string-append (font i) " "))
467
468   (define (generalmeter num den)
469     (string-append (number->string (inexact->exact num)) " " (number->string (inexact->exact den)) " generalmeter "))
470
471   (define (header-end) "")
472   (define (lily-def key val)
473     (string-append
474      "/" key " {" val "} bind def\n"))
475
476   (define (header creator generate) 
477     (string-append
478      "%!PS-Adobe-3.0\n"
479      "%%Creator: " creator generate "\n"))
480   
481   (define (invoke-char s i)
482     (string-append 
483      "(\\" (inexact->string i 8) ") " s " " ))
484   
485   (define (invoke-dim1 s d) 
486     (string-append
487      (number->string (* d  (/ 72.27 72))) " " s ))
488
489   (define (placebox x y s) 
490     (string-append 
491      (number->string x) " " (number->string y) " {" s "} placebox "))
492   (define (pianobrace y)
493     ""
494     )
495
496   (define (rulesym x y) 
497     (string-append 
498      (number->string x) " "
499      (number->string y) " "
500      " rulesym"))
501
502   (define (bezier-sandwich l)
503     (string-append 
504      (apply string-append (map control->string l)) 
505      " draw_bezier_sandwich"))
506
507   (define (start-line height)
508     (begin
509       (clear-fontcache)
510       "\nstart_line {\n"))
511   
512   (define (stem breapth width depth height) 
513     (string-append (numbers->string (list breapth width depth height))
514                    " draw_box" ))
515
516   (define (stop-line)
517       "}\nstop_line\n")
518
519   (define (text s)
520     (string-append "(" s ") show  "))
521
522
523   (define (volta w thick last)
524     (string-append 
525      (numbers->string (list w thick (inexact->exact last)))
526      " draw_volta"))
527
528   (define (tuplet ht dx dy thick dir)
529     (string-append 
530      (numbers->string (list ht dx dy thick (inexact->exact dir)))
531      " draw_tuplet"))
532
533
534   (define (unknown) 
535     "\n unknown\n")
536
537
538   ;; PS
539   (cond ((eq? action-name 'all-definitions)
540          `(begin
541             (define beam ,beam)
542             (define tuplet ,tuplet)
543             (define bracket ,bracket)
544             (define char ,char)
545             (define crescendo ,crescendo)
546             (define volta ,volta)
547             (define bezier-sandwich ,bezier-sandwich)
548             (define dashed-slur ,dashed-slur) 
549             (define decrescendo ,decrescendo) 
550             (define end-output ,end-output)
551             (define experimental-on ,experimental-on)
552             (define filledbox ,filledbox)
553             (define font-def ,font-def)
554             (define font-switch ,font-switch)
555             (define generalmeter ,generalmeter)
556             (define pianobrace ,pianobrace)
557             (define header-end ,header-end)
558             (define lily-def ,lily-def)
559             (define header ,header) 
560             (define invoke-char ,invoke-char) 
561             (define invoke-dim1 ,invoke-dim1)
562             (define placebox ,placebox)
563             (define rulesym ,rulesym)
564             (define select-font ,select-font)
565             (define start-line ,start-line)
566             (define stem ,stem)
567             (define stop-line ,stop-line)
568             (define stop-last-line ,stop-line)
569             (define text ,text)
570             ))
571         ((eq? action-name 'tuplet) tuplet)
572         ((eq? action-name 'beam) beam)
573         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
574         ((eq? action-name 'bracket) bracket)
575         ((eq? action-name 'char) char)
576         ((eq? action-name 'crescendo) crescendo)
577         ((eq? action-name 'dashed-slur) dashed-slur) 
578         ((eq? action-name 'decrescendo) decrescendo)
579         ((eq? action-name 'experimental-on) experimental-on)
580         ((eq? action-name 'filledbox) filledbox)
581         ((eq? action-name 'select-font) select-font)
582         ((eq? action-name 'volta) volta)
583         (else (error "unknown tag -- PS-SCM " action-name))
584         )
585   )
586
587                                         ;
588 ; Russ McManus, <mcmanus@IDT.NET>  
589
590 ; I use the following, which should definitely be provided somewhere
591 ; in guile, but isn't, AFAIK:
592
593
594
595 (define (hash-table-for-each fn ht)
596   (do ((i 0 (+ 1 i)))
597       ((= i (vector-length ht)))
598     (do ((alist (vector-ref ht i) (cdr alist)))
599         ((null? alist) #t)
600       (fn (car (car alist)) (cdr (car alist))))))
601
602 (define (hash-table-map fn ht)
603   (do ((i 0 (+ 1 i))
604        (ret-ls '()))
605       ((= i (vector-length ht)) (reverse ret-ls))
606     (do ((alist (vector-ref ht i) (cdr alist)))
607         ((null? alist) #t)
608       (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
609