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