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