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