]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
003a92a63935a3311bb0d529fa007658f4eec2d5
[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 "\\char" (inexact->string i 10) " "))
210   
211   (define (decrescendo w h cont)
212     (embedded-ps ((ps-scm 'decrescendo) w h cont)))
213
214    ;This sets CTM so that you get to the currentpoint
215   ; by executing a 0 0 moveto
216        
217   (define (embedded-ps s)
218     (string-append "\\special{ps: @beginspecial @setspecial " s " @endspecial}"))
219
220   (define (end-output) 
221     "\n\\EndLilyPondOutput")
222   
223   (define (experimental-on)
224     "")
225
226   (define (font-switch i)
227     (string-append
228      "\\" (font i) "\n"))
229
230   (define (font-def i s)
231     (string-append
232      "\\font" (font-switch i) "=" s "\n"))
233
234   ;; UGH
235    
236   (define (header-end) (string-append
237 "\\special{!"
238 "/stafflinethickness \\mudelapaperstaffline0 def " ;may we burn in hell.
239 "/interline \\mudelapaperinterline0 %\n def "
240 "interline 3 div /bracket_b exch def "
241 "interline 2 mul /bracket_w exch def "
242 "stafflinethickness 2 mul /bracket_t exch def "
243 "interline 1.5 mul /bracket_v exch def "
244 "bracket_v /bracket_u exch def "
245 "50 /bracket_alpha exch def "
246 "1 setlinecap}"))
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   (if security-paranoia
266       (define (output-tex-string s)
267         (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post))
268       (define (output-tex-string s)    s))
269
270   (define (lily-def key val)
271     (string-append
272      "\\def\\" (output-tex-string key) "{" (output-tex-string val) "}\n"))
273
274   (define (number->dim x)
275     (string-append 
276      (number->string  (chop-decimal x)) " pt "))
277
278   (define (placebox x y s) 
279     (string-append 
280      "\\placebox{"
281      (number->dim y) "}{" (number->dim x) "}{" s "}"))
282
283   ;;;;
284   (define (pianobrace y staffht)
285     (let* ((step 1.0)
286            (minht (* 2 staffht))
287            (maxht (* 7 minht))
288            )
289       (string-append
290        (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))))
291        (char (max 0 (/  (- (min y (- maxht step)) minht) step))))
292       )
293     )
294
295
296   (define (rulesym h w) 
297     (string-append 
298      "\\vrule height " (number->dim (/ h 2))
299      " depth " (number->dim (/ h 2))
300      " width " (number->dim w)
301      )
302     )
303
304   (define (bezier-sandwich l thick)
305     (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
306
307
308   (define (start-line ht)
309     (begin
310       (clear-fontcache)
311       (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
312     )
313   (define (stop-line) 
314     "}\\vss}\\interscoreline")
315   (define (stop-last-line)
316     "}\\vss}")
317   (define (filledbox breapth width depth height) 
318     (string-append 
319      "\\kern" (number->dim (- breapth))
320      "\\vrule width " (number->dim (+ breapth width))
321      "depth " (number->dim depth)
322      "height " (number->dim height) " "))
323
324   (define (text s)
325     (string-append "\\hbox{" (output-tex-string s) "}"))
326   
327   (define (tuplet ht gapx dx dy thick dir)
328     (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
329
330   (define (volta h w thick last)
331     (embedded-ps ((ps-scm 'volta) h w thick last)))
332
333   ;; TeX
334   ;; The procedures listed below form the public interface of TeX-scm.
335   ;; (should merge the 2 lists)
336   (cond ((eq? action-name 'all-definitions)
337          `(begin
338             (define beam ,beam)
339             (define bezier-sandwich ,bezier-sandwich)
340             (define bracket ,bracket)
341             (define char ,char)
342             (define crescendo ,crescendo)
343             (define dashed-slur ,dashed-slur) 
344             (define decrescendo ,decrescendo) 
345             (define end-output ,end-output)
346             (define experimental-on ,experimental-on)
347             (define filledbox ,filledbox)
348             (define font-def ,font-def)
349             (define font-switch ,font-switch)
350             (define header-end ,header-end)
351             (define lily-def ,lily-def)
352             (define header ,header) 
353             (define invoke-char ,invoke-char) 
354             (define invoke-dim1 ,invoke-dim1)
355             (define pianobrace ,pianobrace)
356             (define placebox ,placebox)
357             (define rulesym ,rulesym)
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 'rulesym) rulesym)
384         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
385         ((eq? action-name 'start-line) start-line)
386         ((eq? action-name 'stem) stem)
387         ((eq? action-name 'stop-line) stop-line)
388         ((eq? action-name 'stop-last-line) stop-last-line)
389         ((eq? action-name 'volta) volta)
390         (else (error "unknown tag -- PS-TEX " action-name))
391         )
392   )
393
394 ;;;;;;;;;;;; PS
395 (define (ps-scm action-name)
396
397
398   ;; alist containing fontname -> fontcommand assoc (both strings)
399   (define font-alist '())
400   (define font-count 0)
401   (define current-font "")
402   (define (clear-fontcache)
403     (begin
404       (set! font-alist '())
405       (set! font-count 0)
406       (set! current-font "")))
407   
408   (define (cached-fontname i)
409     (string-append
410      "lilyfont"
411      (make-string 1 (integer->char (+ 65 i)))))
412
413   (define (select-font font-name)
414       (if (not (equal? font-name current-font))
415           (begin
416             (set! current-font font-name)
417             (define font-cmd (assoc font-name font-alist))
418             (if (eq? font-cmd #f)
419                 (begin
420                   (set! font-cmd (cached-fontname font-count))
421                   (set! font-alist (acons font-name font-cmd font-alist))
422                   (set! font-count (+ 1 font-count))
423                   (string-append "\n/" font-cmd " {/"
424                                  font-name
425                                  " findfont 12 scalefont setfont} bind def \n"
426                                  font-cmd " \n"))
427                 (string-append (cdr font-cmd) " ")))
428           ; font-name == current-font no switch needed
429           ""                            
430           ))
431                   
432   (define (beam width slope thick)
433     (string-append
434      (numbers->string (list width slope thick)) " draw_beam " ))
435
436   (define (bracket h)
437     (invoke-dim1 " draw_bracket" h))
438
439   (define (char i)
440     (invoke-char " show" i))
441
442   (define (crescendo w h cont thick)
443     (string-append 
444      (numbers->string (list w h (inexact->exact cont) thick))
445      " draw_crescendo"))
446
447   (define (dashed-slur thick dash l)
448     (string-append 
449      (apply string-append (map control->string l)) 
450      (number->string thick) 
451      " [ "
452      (if (> 1 dash) (number->string (- (* thick dash) thick)) "0") " "
453      (number->string (* 2 thick))
454      " ] 0 draw_dashed_slur"))
455
456   (define (decrescendo thick w h cont)
457     (string-append 
458      (numbers->string (list w h (inexact->exact cont) thick))
459      " draw_decrescendo"))
460
461
462   (define (end-output)
463     "\nshowpage\n")
464   
465   (define (experimental-on) "")
466   
467   (define (filledbox breapth width depth height) 
468     (string-append (numbers->string (list breapth width depth height))
469                    " draw_box" ))
470
471   ;; obsolete?
472   (define (font-def i s)
473     (string-append
474      "\n/" (font i) " {/" 
475      (substring s 0 (- (string-length s) 4))
476      " findfont 12 scalefont setfont} bind def \n"))
477
478   (define (font-switch i)
479     (string-append (font i) " "))
480
481   (define (header-end) "")
482   (define (lily-def key val)
483
484      (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
485          (string-append "/" key " {" val "} bind def\n")
486          (string-append "/" key " (" val ") def\n")
487          )
488      )
489
490   (define (header creator generate) 
491     (string-append
492      "%!PS-Adobe-3.0\n"
493      "%%Creator: " creator generate "\n"))
494   
495   (define (invoke-char s i)
496     (string-append 
497      "(\\" (inexact->string i 8) ") " s " " ))
498   
499   (define (invoke-dim1 s d) 
500     (string-append
501      (number->string (* d  (/ 72.27 72))) " " s ))
502
503   (define (placebox x y s) 
504     (string-append 
505      (number->string x) " " (number->string y) " {" s "} placebox "))
506
507   (define (pianobrace y staffht)
508     (let* ((step 1.0)
509            (minht (* 2 staffht))
510            (maxht (* 7 minht))
511            )
512       (string-append
513        (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))))
514        (char (max 0 (/  (- (min y (- maxht step)) minht) step))))
515       )
516     )
517
518
519   (define (rulesym x y) 
520     (string-append 
521      (number->string x) " "
522      (number->string y) " "
523      " rulesym"))
524
525   (define (bezier-sandwich l thick)
526     (string-append 
527      (apply string-append (map control->string l))
528      (number->string  thick)
529      " draw_bezier_sandwich"))
530
531   (define (start-line height)
532     (begin
533       (clear-fontcache)
534       "\nstart_line {\n"))
535   
536   (define (stem breapth width depth height) 
537     (string-append (numbers->string (list breapth width depth height))
538                    " draw_box" ))
539
540   (define (stop-line)
541       "}\nstop_line\n")
542
543   (define (text s)
544     (string-append "(" s ") show  "))
545
546
547   (define (volta h w thick last)
548     (string-append 
549      (numbers->string (list h w thick (inexact->exact last)))
550      " draw_volta"))
551
552   (define (tuplet ht gap dx dy thick dir)
553     (string-append 
554      (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
555      " draw_tuplet"))
556
557
558   (define (unknown) 
559     "\n unknown\n")
560
561
562   ;; PS
563   (cond ((eq? action-name 'all-definitions)
564          `(begin
565             (define beam ,beam)
566             (define tuplet ,tuplet)
567             (define bracket ,bracket)
568             (define char ,char)
569             (define crescendo ,crescendo)
570             (define volta ,volta)
571             (define bezier-sandwich ,bezier-sandwich)
572             (define dashed-slur ,dashed-slur) 
573             (define decrescendo ,decrescendo) 
574             (define end-output ,end-output)
575             (define experimental-on ,experimental-on)
576             (define filledbox ,filledbox)
577             (define font-def ,font-def)
578             (define font-switch ,font-switch)
579             (define pianobrace ,pianobrace)
580             (define header-end ,header-end)
581             (define lily-def ,lily-def)
582             (define header ,header) 
583             (define invoke-char ,invoke-char) 
584             (define invoke-dim1 ,invoke-dim1)
585             (define placebox ,placebox)
586             (define rulesym ,rulesym)
587             (define select-font ,select-font)
588             (define start-line ,start-line)
589             (define stem ,stem)
590             (define stop-line ,stop-line)
591             (define stop-last-line ,stop-line)
592             (define text ,text)
593             ))
594         ((eq? action-name 'tuplet) tuplet)
595         ((eq? action-name 'beam) beam)
596         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
597         ((eq? action-name 'bracket) bracket)
598         ((eq? action-name 'char) char)
599         ((eq? action-name 'crescendo) crescendo)
600         ((eq? action-name 'dashed-slur) dashed-slur) 
601         ((eq? action-name 'decrescendo) decrescendo)
602         ((eq? action-name 'experimental-on) experimental-on)
603         ((eq? action-name 'filledbox) filledbox)
604         ((eq? action-name 'select-font) select-font)
605         ((eq? action-name 'volta) volta)
606         (else (error "unknown tag -- PS-SCM " action-name))
607         )
608   )
609
610                                         ;
611 ; Russ McManus, <mcmanus@IDT.NET>  
612
613 ; I use the following, which should definitely be provided somewhere
614 ; in guile, but isn't, AFAIK:
615
616
617
618 (define (hash-table-for-each fn ht)
619   (do ((i 0 (+ 1 i)))
620       ((= i (vector-length ht)))
621     (do ((alist (vector-ref ht i) (cdr alist)))
622         ((null? alist) #t)
623       (fn (car (car alist)) (cdr (car alist))))))
624
625 (define (hash-table-map fn ht)
626   (do ((i 0 (+ 1 i))
627        (ret-ls '()))
628       ((= i (vector-length ht)) (reverse ret-ls))
629     (do ((alist (vector-ref ht i) (cdr alist)))
630         ((null? alist) #t)
631       (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
632
633
634 ;;;; print a SCM expression.  Isn't this part of the std lib?
635
636 (define (scmlist->string exp)
637   (cond
638    ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
639    ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
640    (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
641    ))
642
643 (define (scm->string exp)
644   (cond
645    ((pair? exp) (string-append "(" (scmlist->string exp)))
646    ((number? exp) (number->string exp))
647    ((symbol? exp) (symbol->string exp))
648    ((string? exp) (string-append "\"" exp "\""))
649    ))
650
651 (define (test-scm->string)
652 (list (scmlist->string '(a))
653 (scmlist->string '(a b))
654 (scmlist->string '(a b . c))
655
656
657 (scm->string '(a))
658 (scm->string '(a b ))
659 (scm->string '(a b . c))
660 (scm->string '(a b (c . d)))
661 (scm->string '(a "bla" (c . 1.5)))
662 )
663 )