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