]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
patch::: 1.3.2.jcn3
[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 ;; this is silly, can't we use something like
136 ;; roman-0, roman-1 roman+1 ?
137 (define cmr-alist 
138   '(("bold" . "cmbx") 
139     ("dynamic" . "feta-din") 
140     ("feta" . "feta") 
141     ("feta-1" . "feta") 
142     ("feta-2" . "feta") 
143     ("finger" . "feta-nummer") 
144     ("typewriter" . "cmtt") 
145     ("italic" . "cmti") 
146     ("roman" . "cmr") 
147     ("script" . "cmr") 
148     ("large" . "cmbx") 
149     ("Large" . "cmbx") 
150     ("mark" . "feta-nummer") 
151     ("number" . "feta-nummer") 
152     ("volta" . "feta-nummer"))
153 )
154
155 (define script-alist '())
156 (define (articulation-to-scriptdef a)
157   (assoc a script-alist)
158   )
159
160 ;; Map style names to TeX font names.  Return false if 
161 ;; no font name found. 
162 (define (style-to-cmr s)
163   (assoc s cmr-alist )
164   )
165
166
167 (define (tex-scm action-name)
168
169   (define (unknown) 
170     "%\n\\unknown%\n")
171
172   (define font-alist '())
173   (define font-count 0)
174   (define current-font "")
175   (define (clear-fontcache)
176     (begin
177       (set! font-alist '())
178       (set! font-count 0)
179       (set! current-font "")))
180   
181   (define (cached-fontname i)
182     (string-append
183      "\\lilyfont"
184      (make-string 1 (integer->char (+ 65 i)))))
185
186   (define (select-font font-name magnification)
187       (if (not (equal? font-name current-font))
188           (let* ((font-cmd (assoc font-name font-alist)))
189             (set! current-font font-name)
190             (if (eq? font-cmd #f)
191                 (begin
192                   (set! font-cmd (cached-fontname font-count))
193                   (set! font-alist (acons font-name font-cmd font-alist))
194                   (set! font-count (+ 1 font-count))
195                   (if (equal? font-name "")
196                       (error "Empty fontname -- SELECT-FONT"))
197                   (if (> magnification 0)
198                       (string-append "\\font" font-cmd "=" font-name 
199                                      " scaled \\magstep " 
200                                      (number->string magnification) font-cmd)
201                       (string-append "\\font" font-cmd "=" font-name font-cmd)))
202                 
203                 (cdr font-cmd)))
204           ""                            ;no switch needed
205           ))
206   
207   (define (beam width slope thick)
208     (embedded-ps ((ps-scm 'beam) width slope thick)))
209
210   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
211     (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
212
213   (define (dashed-slur thick dash l)
214     (embedded-ps ((ps-scm 'dashed-slur)  thick dash l)))
215
216   (define (crescendo thick w h cont)
217     (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
218
219   (define (char i)
220     (string-append "\\char" (inexact->string i 10) " "))
221   
222   (define (decrescendo thick w h cont)
223     (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
224
225    ;This sets CTM so that you get to the currentpoint
226   ; by executing a 0 0 moveto
227        
228   (define (embedded-ps s)
229     (string-append "\\embeddedps{" s "}"))
230
231   (define (end-output) 
232     "\n\\EndLilyPondOutput")
233   
234   (define (experimental-on)
235     "")
236
237   (define (font-switch i)
238     (string-append
239      "\\" (font i) "\n"))
240
241   (define (font-def i s)
242     (string-append
243      "\\font" (font-switch i) "=" s "\n"))
244
245   (define (header-end)
246     (string-append
247      "\\special{! "
248      (ly-gulp-file "lily.ps")
249      ;; breaks on ppc
250 ;;     (regexp-substitute/global #f "\n" (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
251      "}"
252      "\\input lilyponddefs \\turnOnPostScript"))
253
254   (define (header creator generate) 
255     (string-append
256      "%created by: " creator generate "\n"))
257
258   (define (invoke-char s i)
259     (string-append 
260      "\n\\" s "{" (inexact->string i 10) "}" ))
261
262   (define (invoke-dim1 s d)
263     (string-append
264      "\n\\" s "{" (number->dim d) "}"))
265   (define (pt->sp x)
266     (* 65536 x))
267   
268   ;;
269   ;; need to do something to make this really safe.
270   ;;
271   (define (output-tex-string s)
272       (if security-paranoia
273           (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
274           s))
275       
276   (define (lily-def key val)
277     (string-append
278      "\\def\\"
279 ;     (regexp-substitute/global #f "_" (output-tex-string key) 'pre "X" 'post)
280      (output-tex-string key)
281      "{" (output-tex-string val) "}\n"))
282
283   (define (number->dim x)
284     (string-append 
285      (number->string  (chop-decimal x)) " pt "))
286
287   (define (placebox x y s) 
288     (string-append 
289      "\\placebox{"
290      (number->dim y) "}{" (number->dim x) "}{" s "}"))
291
292   ;;;;
293   (define (pianobrace y staffht)
294     (let* ((step 1.0)
295            (minht (* 2 staffht))
296            (maxht (* 7 minht))
297            )
298       (string-append
299        (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
300        (char (max 0 (/  (- (min y (- maxht step)) minht) step))))
301       )
302     )
303
304
305   (define (bezier-sandwich l thick)
306     (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
307
308
309   (define (start-line ht)
310     (begin
311       (clear-fontcache)
312       (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
313     )
314   (define (stop-line) 
315     "}\\vss}\\interscoreline")
316   (define (stop-last-line)
317     "}\\vss}")
318   (define (filledbox breapth width depth height) 
319     (string-append 
320      "\\kern" (number->dim (- breapth))
321      "\\vrule width " (number->dim (+ breapth width))
322      "depth " (number->dim depth)
323      "height " (number->dim height) " "))
324
325   (define (text s)
326     (string-append "\\hbox{" (output-tex-string s) "}"))
327   
328   (define (tuplet ht gapx dx dy thick dir)
329     (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
330
331   (define (volta h w thick vert_start vert_end)
332     (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
333
334   ;; TeX
335   ;; The procedures listed below form the public interface of TeX-scm.
336   ;; (should merge the 2 lists)
337   (cond ((eq? action-name 'all-definitions)
338          `(begin
339             (define beam ,beam)
340             (define bezier-sandwich ,bezier-sandwich)
341             (define bracket ,bracket)
342             (define char ,char)
343             (define crescendo ,crescendo)
344             (define dashed-slur ,dashed-slur) 
345             (define decrescendo ,decrescendo) 
346             (define end-output ,end-output)
347             (define experimental-on ,experimental-on)
348             (define filledbox ,filledbox)
349             (define font-def ,font-def)
350             (define font-switch ,font-switch)
351             (define header-end ,header-end)
352             (define lily-def ,lily-def)
353             (define header ,header) 
354             (define invoke-char ,invoke-char) 
355             (define invoke-dim1 ,invoke-dim1)
356             (define pianobrace ,pianobrace)
357             (define placebox ,placebox)
358             (define select-font ,select-font)
359             (define start-line ,start-line)
360             (define stop-line ,stop-line)
361             (define stop-last-line ,stop-last-line)
362             (define text ,text)
363             (define tuplet ,tuplet)
364             (define volta ,volta)
365             ))
366
367         ((eq? action-name 'beam) beam)
368         ((eq? action-name 'tuplet) tuplet)
369         ((eq? action-name 'bracket) bracket)
370         ((eq? action-name 'crescendo) crescendo)
371         ((eq? action-name 'dashed-slur) dashed-slur) 
372         ((eq? action-name 'decrescendo) decrescendo) 
373         ((eq? action-name 'end-output) end-output)
374         ((eq? action-name 'experimental-on) experimental-on)
375         ((eq? action-name 'font-def) font-def)
376         ((eq? action-name 'font-switch) font-switch)
377         ((eq? action-name 'header-end) header-end)
378         ((eq? action-name 'lily-def) lily-def)
379         ((eq? action-name 'header) header) 
380         ((eq? action-name 'invoke-char) invoke-char) 
381         ((eq? action-name 'invoke-dim1) invoke-dim1)
382         ((eq? action-name 'placebox) placebox)
383         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
384         ((eq? action-name 'start-line) start-line)
385         ((eq? action-name 'stem) stem)
386         ((eq? action-name 'stop-line) stop-line)
387         ((eq? action-name 'stop-last-line) stop-last-line)
388         ((eq? action-name 'volta) volta)
389         (else (error "unknown tag -- PS-TEX " action-name))
390         )
391   )
392
393 ;;;;;;;;;;;; PS
394 (define (ps-scm action-name)
395
396
397   ;; alist containing fontname -> fontcommand assoc (both strings)
398   (define font-alist '())
399   (define font-count 0)
400   (define current-font "")
401   (define (clear-fontcache)
402     (begin
403       (set! font-alist '())
404       (set! font-count 0)
405       (set! current-font "")))
406   
407   (define (cached-fontname i)
408     (string-append
409      "lilyfont"
410      (make-string 1 (integer->char (+ 65 i)))))
411     
412   (define (mag-to-size m)
413     (number->string (case m 
414                       ('0 12)
415                       ('1 12)
416                       ('2 14) ; really: 14.400
417                       ('3 17) ; really: 17.280
418                       ('4 21) ; really: 20.736
419                       ('5 24) ; really: 24.888
420                       ('6 30) ; really: 29.856
421                       )))
422   
423   (define (select-font font-name magnification)
424     (define font-cmd (assoc font-name font-alist))
425     (if (not (equal? font-name current-font))
426         (begin
427           (set! current-font font-name)
428           (if (eq? font-cmd #f)
429               (begin
430                 (set! font-cmd (cached-fontname font-count))
431                 (set! font-alist (acons font-name font-cmd font-alist))
432                 (set! font-count (+ 1 font-count))
433                 (string-append "\n/" font-cmd " {/"
434                                font-name " findfont " 
435                                (mag-to-size magnification)
436                                " scalefont setfont} bind def \n"
437                                font-cmd " \n"))
438                 (string-append (cdr font-cmd) " ")))
439           ; font-name == current-font no switch needed
440           ""                            
441           ))
442                   
443   (define (beam width slope thick)
444     (string-append
445      (numbers->string (list width slope thick)) " draw_beam" ))
446
447   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
448     (string-append
449      (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
450
451   (define (char i)
452     (invoke-char " show" i))
453
454   (define (crescendo thick w h cont )
455     (string-append 
456      (numbers->string (list w h (inexact->exact cont) thick))
457      " draw_crescendo"))
458
459   (define (dashed-slur thick dash l)
460     (string-append 
461      (apply string-append (map control->string l)) 
462      (number->string thick) 
463      " [ "
464      (if (> 1 dash)
465          (number->string (- (* thick dash) thick))
466          "0")
467      " "
468      (number->string (* 2 thick))
469      " ] 0 draw_dashed_slur"))
470
471   (define (decrescendo thick w h cont)
472     (string-append 
473      (numbers->string (list w h (inexact->exact cont) thick))
474      " draw_decrescendo"))
475
476
477   (define (end-output)
478     "\nshowpage\n")
479   
480   (define (experimental-on) "")
481   
482   (define (filledbox breapth width depth height) 
483     (string-append (numbers->string (list breapth width depth height))
484                    " draw_box" ))
485
486   ;; obsolete?
487   (define (font-def i s)
488     (string-append
489      "\n/" (font i) " {/" 
490      (substring s 0 (- (string-length s) 4))
491      " findfont 12 scalefont setfont} bind def \n"))
492
493   (define (font-switch i)
494     (string-append (font i) " "))
495
496   (define (header-end)
497     (string-append
498      (ly-gulp-file "lilyponddefs.ps")
499      " {exch pop //systemdict /run get exec} "
500      (ly-gulp-file "lily.ps")
501      "{ exch pop //systemdict /run get exec } "
502     ))
503   
504   (define (lily-def key val)
505
506      (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
507          (string-append "/" key " {" val "} bind def\n")
508          (string-append "/" key " (" val ") def\n")
509          )
510      )
511
512   (define (header creator generate) 
513     (string-append
514      "%!PS-Adobe-3.0\n"
515      "%%Creator: " creator generate "\n"))
516   
517   (define (invoke-char s i)
518     (string-append 
519      "(\\" (inexact->string i 8) ") " s " " ))
520   
521   (define (invoke-dim1 s d) 
522     (string-append
523      (number->string (* d  (/ 72.27 72))) " " s ))
524
525   (define (placebox x y s) 
526     (string-append 
527      (number->string x) " " (number->string y) " {" s "} placebox "))
528
529   (define (pianobrace y staffht)
530     (let* ((step 1.0)
531            (minht (* 2 staffht))
532            (maxht (* 7 minht))
533            )
534       (string-append
535        (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
536        (char (max 0 (/  (- (min y (- maxht step)) minht) step))))
537       )
538     )
539
540
541   (define (bezier-sandwich l thick)
542     (string-append 
543      (apply string-append (map control->string l))
544      (number->string  thick)
545      " draw_bezier_sandwich"))
546
547   (define (start-line height)
548     (begin
549       (clear-fontcache)
550       "\nstart_line {\n"))
551   
552   (define (stem breapth width depth height) 
553     (string-append (numbers->string (list breapth width depth height))
554                    " draw_box" ))
555
556   (define (stop-line)
557       "}\nstop_line\n")
558
559   (define (text s)
560     (string-append "(" s ") show  "))
561
562
563   (define (volta h w thick vert_start vert_end)
564     (string-append 
565      (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
566      " draw_volta"))
567
568   (define (tuplet ht gap dx dy thick dir)
569     (string-append 
570      (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
571      " draw_tuplet"))
572
573
574   (define (unknown) 
575     "\n unknown\n")
576
577
578   ;; PS
579   (cond ((eq? action-name 'all-definitions)
580          `(begin
581             (define beam ,beam)
582             (define tuplet ,tuplet)
583             (define bracket ,bracket)
584             (define char ,char)
585             (define crescendo ,crescendo)
586             (define volta ,volta)
587             (define bezier-sandwich ,bezier-sandwich)
588             (define dashed-slur ,dashed-slur) 
589             (define decrescendo ,decrescendo) 
590             (define end-output ,end-output)
591             (define experimental-on ,experimental-on)
592             (define filledbox ,filledbox)
593             (define font-def ,font-def)
594             (define font-switch ,font-switch)
595             (define pianobrace ,pianobrace)
596             (define header-end ,header-end)
597             (define lily-def ,lily-def)
598             (define header ,header) 
599             (define invoke-char ,invoke-char) 
600             (define invoke-dim1 ,invoke-dim1)
601             (define placebox ,placebox)
602             (define select-font ,select-font)
603             (define start-line ,start-line)
604             (define stem ,stem)
605             (define stop-line ,stop-line)
606             (define stop-last-line ,stop-line)
607             (define text ,text)
608             ))
609         ((eq? action-name 'tuplet) tuplet)
610         ((eq? action-name 'beam) beam)
611         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
612         ((eq? action-name 'bracket) bracket)
613         ((eq? action-name 'char) char)
614         ((eq? action-name 'crescendo) crescendo)
615         ((eq? action-name 'dashed-slur) dashed-slur) 
616         ((eq? action-name 'decrescendo) decrescendo)
617         ((eq? action-name 'experimental-on) experimental-on)
618         ((eq? action-name 'filledbox) filledbox)
619         ((eq? action-name 'select-font) select-font)
620         ((eq? action-name 'volta) volta)
621         (else (error "unknown tag -- PS-SCM " action-name))
622         )
623   )
624
625                                         ;
626 ; Russ McManus, <mcmanus@IDT.NET>  
627
628 ; I use the following, which should definitely be provided somewhere
629 ; in guile, but isn't, AFAIK:
630
631
632
633 (define (hash-table-for-each fn ht)
634   (do ((i 0 (+ 1 i)))
635       ((= i (vector-length ht)))
636     (do ((alist (vector-ref ht i) (cdr alist)))
637         ((null? alist) #t)
638       (fn (car (car alist)) (cdr (car alist))))))
639
640 (define (hash-table-map fn ht)
641   (do ((i 0 (+ 1 i))
642        (ret-ls '()))
643       ((= i (vector-length ht)) (reverse ret-ls))
644     (do ((alist (vector-ref ht i) (cdr alist)))
645         ((null? alist) #t)
646       (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
647
648
649 ;;;; print a SCM expression.  Isn't this part of the std lib?
650
651 (define (scmlist->string exp)
652   (cond
653    ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
654    ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
655    (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
656    ))
657
658 (define (scm->string exp)
659   (cond
660    ((pair? exp) (string-append "(" (scmlist->string exp)))
661    ((number? exp) (number->string exp))
662    ((symbol? exp) (symbol->string exp))
663    ((string? exp) (string-append "\"" exp "\""))
664    ))
665
666 (define (test-scm->string)
667 (list (scmlist->string '(a))
668 (scmlist->string '(a b))
669 (scmlist->string '(a b . c))
670
671
672 (scm->string '(a))
673 (scm->string '(a b ))
674 (scm->string '(a b . c))
675 (scm->string '(a b (c . d)))
676 (scm->string '(a "bla" (c . 1.5)))
677 )
678 )