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