]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
release: 1.3.6
[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 ;; do nothing in .scm output
15 (define (comment s)
16   ""
17   )
18
19
20 (define
21   (xnumbers->string l)
22   (string-append 
23    (map (lambda (n) (string-append (number->string n ) " ")) l)))
24
25 (define (mm-to-pt x)
26   (* (/ 72.27 25.40) x)
27   )
28
29 (define (cons-map f x)
30   (cons (f (car x)) (f (cdr x))))
31
32 (define (reduce operator list)
33       (if (null? (cdr list)) (car list)
34           (operator (car list) (reduce operator (cdr list)))
35           )
36       )
37
38
39 (define (glue-2-strings a b) (string-append a " " b))
40
41 (define
42   (numbers->string l)
43   (reduce glue-2-strings (map number->string l)))
44
45 (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
46
47 (define (number->octal-string x)
48   (let* ((n (inexact->exact x))
49          (n64 (quotient n 64))
50          (n8 (quotient (- n (* n64 64)) 8)))
51     (string-append
52      (number->string n64)
53      (number->string n8)
54      (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
55
56 (define (inexact->string x radix)
57   (let ((n (inexact->exact x)))
58     (number->string n radix)))
59
60
61 (define
62   (control->string c)
63   (string-append
64    (string-append (number->string (car c)) " ")
65    (string-append (number->string (cadr c)) " ")))
66
67
68 (define (font i)
69   (string-append
70    "font"
71    (make-string 1 (integer->char (+ (char->integer #\A) i)))
72    ))
73
74
75
76 (define (scm-scm action-name)
77   1)
78
79 (define security-paranoia #f)
80
81
82 ;; See documentation of Item::visibility_lambda_
83 (define (postbreak-only-visibility d) (if (= d 1) '(#f . #f) '(#t . #t)))
84 (define (spanbar-non-postbreak-visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
85 (define (all-visibility d) '(#f . #f))
86 (define (non-postbreak-visibility d) (if (= d 1) '(#t . #t) '(#f . #f)))
87 (define (non-prebreak-visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
88
89
90 ;; Score_span_bars are only visible at start of line
91 ;; i.e. if break_dir == RIGHT == 1
92 (define Span_bar_engraver-visibility non-postbreak-visibility)
93 (define mark-visibility non-prebreak-visibility)
94 (define Span_score_bar_engraver-visibility postbreak-only-visibility)
95 (define Piano_bar_engraver-visibility postbreak-only-visibility)
96 (define Staff_group_bar_engraver-visibility postbreak-only-visibility)
97
98 ;; Spacing constants for prefatory matter.
99 ;;
100 ;; rules for this spacing are much more complicated than this. See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147
101 ;;
102 ;;
103
104 (define space-alist
105  '(
106    (("" "Clef_item") . (minimum-space 1.0))
107    (("" "Staff_bar") . (minimum-space 0.0))
108    (("" "Clef_item") . (minimum-space 1.0))
109    (("" "Key_item") . (minimum-space 0.5))
110    (("" "Span_bar") . (extra-space 0.0))
111    (("" "Time_signature") . (extra-space 0.0))
112    (("" "begin-of-note") . (minimum-space 1.5))
113    (("Clef_item" "Key_item") . (minimum-space 4.0))
114    (("Key_item" "Time_signature") . (extra-space 1.0))
115    (("Clef_item"  "Time_signature") . (minimum-space 3.5))
116    (("Staff_bar" "Clef_item") .   (minimum-space 1.0))
117    (("Clef_item"  "Staff_bar") .  (minimum-space 3.7))
118    (("Time_signature" "Staff_bar") .  (minimum-space 2.0))
119    (("Key_item"  "Staff_bar") .  (extra-space 1.0))
120    (("Span_bar" "Clef_item") .   (extra-space 1.0))
121    (("Clef_item"  "Span_bar") . (minimum-space 3.7))
122    (("Time_signature" "Span_bar") . (minimum-space 2.0))
123    (("Key_item"  "Span_bar") . (minimum-space 2.5))
124    (("Staff_bar" "Time_signature") . (minimum-space 1.5)) ;double check this.
125    (("Time_signature" "begin-of-note") . (extra-space 2.0)) ;double check this.
126    (("Key_item" "begin-of-note") . (extra-space 2.5))
127    (("Staff_bar" "begin-of-note") . (extra-space 1.0))
128    (("Clef_item" "begin-of-note") . (minimum-space 5.0))
129    (("" "Breathing_sign") . (minimum-space 0.0))
130    (("Breathing_sign" "Key_item") . (minimum-space 1.5))
131    (("Breathing_sign" "begin-of-note") . (minimum-space 1.0))
132    (("Breathing_sign" "Staff_bar") . (minimum-space 1.5))
133    (("Breathing_sign" "Clef_item") . (minimum-space 2.0))
134    )
135 )
136  
137 (define (break-align-spacer this next)
138   (let ((entry (assoc `(,this ,next) space-alist)))
139     (if entry
140         (cdr entry)
141         (begin (ly-warn (string-append "Unknown spacing pair `" this "', `" next "'"))
142                '(minimum-space 0.0)))))
143   
144         
145
146 ;;;;;;;; TeX
147
148 ;; this is silly, can't we use something like
149 ;; roman-0, roman-1 roman+1 ?
150 (define cmr-alist 
151   '(("bold" . "cmbx") 
152     ("dynamic" . "feta-din") 
153     ("feta" . "feta") 
154     ("feta-1" . "feta") 
155     ("feta-2" . "feta") 
156     ("finger" . "feta-nummer") 
157     ("typewriter" . "cmtt") 
158     ("italic" . "cmti") 
159     ("roman" . "cmr") 
160     ("script" . "cmr") 
161     ("large" . "cmbx") 
162     ("Large" . "cmbx") 
163     ("mark" . "feta-nummer") 
164     ("number" . "feta-nummer") 
165     ("volta" . "feta-nummer"))
166 )
167
168 (define (string-encode-integer i)
169   (cond
170    ((= i  0) "o")
171    ((< i 0)   (string-append "n" (string-encode-integer (- i))))
172    (else (string-append
173           (make-string 1 (integer->char (+ 65 (modulo i 26))))
174           (string-encode-integer (quotient i 26))
175          )
176    )
177   )
178   )
179
180 (define (magstep i)
181   (cdr (assoc i '((-4 . 482)
182                   (-3 . 579)
183                   (-2 . 694)
184                   (-1 . 833)
185                   (0 . 1000)
186                   (1 . 1200) 
187                   (2 . 1440)
188                   (3 . 1728)
189                   (4 . 2074))
190               )
191        )
192   )
193              
194 (define script-alist '())
195 (define (articulation-to-scriptdef a)
196   (assoc a script-alist)
197   )
198
199 ;; Map style names to TeX font names.  Return false if 
200 ;; no font name found. 
201 (define (style-to-cmr s)
202   (assoc s cmr-alist )
203   )
204
205 (define (define-font name-mag)
206   (cons name-mag
207         (string-append  "\\magfont"
208                         (string-encode-integer (hash (car name-mag) 1000000))
209                         "m"
210                         (string-encode-integer (cdr name-mag)))
211
212         )
213   )
214
215 (define font-name-alist  '())
216 (define (define-fonts names)
217   (set! font-name-alist (map define-font names))
218   (apply string-append (map (lambda (x)
219                         (string-append "\\font" (cdr x) "="
220                                        (symbol->string (caar x))
221                                        " scaled "
222                                        (number->string (magstep (cdar x)))
223                                        "\n"))
224                       font-name-alist
225                       )
226          )
227   )
228   
229
230
231 (define (tex-scm action-name)
232   (define (unknown) 
233     "%\n\\unknown%\n")
234
235
236   (define (select-font font-name-symbol)
237     (let*
238         (
239          (c (assoc font-name-symbol font-name-alist))
240          )
241
242       (if (eq? c #f)
243           (begin
244             (ly-warn (string-append
245                       "Programming error: No such font known " (car font-name-symbol)))
246             "")                         ; issue no command
247           (cdr c))
248       
249       
250       ))
251   
252   (define (beam width slope thick)
253     (embedded-ps ((ps-scm 'beam) width slope thick)))
254
255   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
256     (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
257
258   (define (dashed-slur thick dash l)
259     (embedded-ps ((ps-scm 'dashed-slur)  thick dash l)))
260
261   (define (crescendo thick w h cont)
262     (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
263
264   (define (char i)
265     (string-append "\\char" (inexact->string i 10) " "))
266   
267   (define (decrescendo thick w h cont)
268     (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
269
270    ;This sets CTM so that you get to the currentpoint
271   ; by executing a 0 0 moveto
272        
273   (define (embedded-ps s)
274     (string-append "\\embeddedps{" s "}"))
275
276   (define (comment s)
277     (string-append "% " s))
278   
279   (define (end-output) 
280     "\n\\EndLilyPondOutput")
281   
282   (define (experimental-on)
283     "")
284
285   (define (font-switch i)
286     (string-append
287      "\\" (font i) "\n"))
288
289   (define (font-def i s)
290     (string-append
291      "\\font" (font-switch i) "=" s "\n"))
292
293   (define (header-end)
294     (string-append
295      "\\special{! "
296      (ly-gulp-file "lily.ps")
297      ;; breaks on ppc
298 ;;     (regexp-substitute/global #f "\n" (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
299      "}"
300      "\\input lilyponddefs \\turnOnPostScript"))
301
302   (define (header creator generate) 
303     (string-append
304      "%created by: " creator generate "\n"))
305
306   (define (invoke-char s i)
307     (string-append 
308      "\n\\" s "{" (inexact->string i 10) "}" ))
309
310   (define (invoke-dim1 s d)
311     (string-append
312      "\n\\" s "{" (number->dim d) "}"))
313   (define (pt->sp x)
314     (* 65536 x))
315   
316   ;;
317   ;; need to do something to make this really safe.
318   ;;
319   (define (output-tex-string s)
320       (if security-paranoia
321           (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
322           s))
323       
324   (define (lily-def key val)
325     (string-append
326      "\\def\\"
327 ;     (regexp-substitute/global #f "_" (output-tex-string key) 'pre "X" 'post)
328      (output-tex-string key)
329      "{" (output-tex-string val) "}\n"))
330
331   (define (number->dim x)
332     (string-append 
333      (number->string  (chop-decimal x)) " pt "))
334
335   (define (placebox x y s) 
336     (string-append 
337      "\\placebox{"
338      (number->dim y) "}{" (number->dim x) "}{" s "}"))
339
340   ;;;;
341   (define (pianobrace y staffht)
342     (let* ((step 1.0)
343            (minht (* 2 staffht))
344            (maxht (* 7 minht))
345            )
346       (string-append
347        (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
348        (char (max 0 (/  (- (min y (- maxht step)) minht) step))))
349       )
350     )
351
352
353   (define (bezier-sandwich l thick)
354     (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
355
356
357   (define (start-line ht)
358     (begin
359       (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
360     )
361   (define (stop-line) 
362     "}\\vss}\\interscoreline")
363   (define (stop-last-line)
364     "}\\vss}")
365   (define (filledbox breapth width depth height) 
366     (string-append 
367      "\\kern" (number->dim (- breapth))
368      "\\vrule width " (number->dim (+ breapth width))
369      "depth " (number->dim depth)
370      "height " (number->dim height) " "))
371
372   (define (text s)
373     (string-append "\\hbox{" (output-tex-string s) "}"))
374   
375   (define (tuplet ht gapx dx dy thick dir)
376     (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
377
378   (define (volta h w thick vert_start vert_end)
379     (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
380
381   ;; TeX
382   ;; The procedures listed below form the public interface of TeX-scm.
383   ;; (should merge the 2 lists)
384   (cond ((eq? action-name 'all-definitions)
385          `(begin
386             (define beam ,beam)
387             (define bezier-sandwich ,bezier-sandwich)
388             (define bracket ,bracket)
389             (define char ,char)
390             (define crescendo ,crescendo)
391             (define dashed-slur ,dashed-slur) 
392             (define decrescendo ,decrescendo) 
393             (define end-output ,end-output)
394             (define experimental-on ,experimental-on)
395             (define filledbox ,filledbox)
396             (define font-def ,font-def)
397             (define font-switch ,font-switch)
398             (define header-end ,header-end)
399             (define lily-def ,lily-def)
400             (define header ,header) 
401             (define invoke-char ,invoke-char) 
402             (define invoke-dim1 ,invoke-dim1)
403             (define pianobrace ,pianobrace)
404             (define placebox ,placebox)
405             (define select-font ,select-font)
406             (define start-line ,start-line)
407             (define stop-line ,stop-line)
408             (define stop-last-line ,stop-last-line)
409             (define text ,text)
410             (define tuplet ,tuplet)
411             (define volta ,volta)
412             ))
413
414         ((eq? action-name 'beam) beam)
415         ((eq? action-name 'tuplet) tuplet)
416         ((eq? action-name 'bracket) bracket)
417         ((eq? action-name 'crescendo) crescendo)
418         ((eq? action-name 'dashed-slur) dashed-slur) 
419         ((eq? action-name 'decrescendo) decrescendo) 
420         ((eq? action-name 'end-output) end-output)
421         ((eq? action-name 'experimental-on) experimental-on)
422         ((eq? action-name 'font-def) font-def)
423         ((eq? action-name 'font-switch) font-switch)
424         ((eq? action-name 'header-end) header-end)
425         ((eq? action-name 'lily-def) lily-def)
426         ((eq? action-name 'header) header) 
427         ((eq? action-name 'invoke-char) invoke-char) 
428         ((eq? action-name 'invoke-dim1) invoke-dim1)
429         ((eq? action-name 'placebox) placebox)
430         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
431         ((eq? action-name 'start-line) start-line)
432         ((eq? action-name 'stem) stem)
433         ((eq? action-name 'stop-line) stop-line)
434         ((eq? action-name 'stop-last-line) stop-last-line)
435         ((eq? action-name 'volta) volta)
436         (else (error "unknown tag -- PS-TEX " action-name))
437         )
438   )
439
440 ;;;;;;;;;;;; PS
441 (define (ps-scm action-name)
442
443
444   ;; alist containing fontname -> fontcommand assoc (both strings)
445   (define font-alist '())
446   (define font-count 0)
447   (define current-font "")
448
449   
450   (define (cached-fontname i)
451     (string-append
452      "lilyfont"
453      (make-string 1 (integer->char (+ 65 i)))))
454     
455   (define (mag-to-size m)
456     (number->string (case m 
457                       (0 12)
458                       (1 12)
459                       (2 14) ; really: 14.400
460                       (3 17) ; really: 17.280
461                       (4 21) ; really: 20.736
462                       (5 24) ; really: 24.888
463                       (6 30) ; really: 29.856
464                       )))
465   
466   (define (select-font font-name magnification)
467     (define font-cmd (assoc font-name font-alist))
468     (if (not (equal? font-name current-font))
469         (begin
470           (set! current-font font-name)
471           (if (eq? font-cmd #f)
472               (begin
473                 (set! font-cmd (cached-fontname font-count))
474                 (set! font-alist (acons font-name font-cmd font-alist))
475                 (set! font-count (+ 1 font-count))
476                 (string-append "\n/" font-cmd " {/"
477                                font-name " findfont " 
478                                (mag-to-size magnification)
479                                " scalefont setfont} bind def \n"
480                                font-cmd " \n"))
481                 (string-append (cdr font-cmd) " ")))
482           ; font-name == current-font no switch needed
483           ""                            
484           ))
485                   
486   (define (beam width slope thick)
487     (string-append
488      (numbers->string (list width slope thick)) " draw_beam" ))
489
490   (define (comment s)
491     (string-append "% " s))
492
493   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
494     (string-append
495      (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
496
497   (define (char i)
498     (invoke-char " show" i))
499
500   (define (crescendo thick w h cont )
501     (string-append 
502      (numbers->string (list w h (inexact->exact cont) thick))
503      " draw_crescendo"))
504
505   (define (dashed-slur thick dash l)
506     (string-append 
507      (apply string-append (map control->string l)) 
508      (number->string thick) 
509      " [ "
510      (if (> 1 dash)
511          (number->string (- (* thick dash) thick))
512          "0")
513      " "
514      (number->string (* 2 thick))
515      " ] 0 draw_dashed_slur"))
516
517   (define (decrescendo thick w h cont)
518     (string-append 
519      (numbers->string (list w h (inexact->exact cont) thick))
520      " draw_decrescendo"))
521
522
523   (define (end-output)
524     "\nshowpage\n")
525   
526   (define (experimental-on) "")
527   
528   (define (filledbox breapth width depth height) 
529     (string-append (numbers->string (list breapth width depth height))
530                    " draw_box" ))
531
532   ;; obsolete?
533   (define (font-def i s)
534     (string-append
535      "\n/" (font i) " {/" 
536      (substring s 0 (- (string-length s) 4))
537      " findfont 12 scalefont setfont} bind def \n"))
538
539   (define (font-switch i)
540     (string-append (font i) " "))
541
542   (define (header-end)
543     (string-append
544      (ly-gulp-file "lilyponddefs.ps")
545      " {exch pop //systemdict /run get exec} "
546      (ly-gulp-file "lily.ps")
547      "{ exch pop //systemdict /run get exec } "
548     ))
549   
550   (define (lily-def key val)
551
552      (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
553          (string-append "/" key " {" val "} bind def\n")
554          (string-append "/" key " (" val ") def\n")
555          )
556      )
557
558   (define (header creator generate) 
559     (string-append
560      "%!PS-Adobe-3.0\n"
561      "%%Creator: " creator generate "\n"))
562   
563   (define (invoke-char s i)
564     (string-append 
565      "(\\" (inexact->string i 8) ") " s " " ))
566   
567   (define (invoke-dim1 s d) 
568     (string-append
569      (number->string (* d  (/ 72.27 72))) " " s ))
570
571   (define (placebox x y s) 
572     (string-append 
573      (number->string x) " " (number->string y) " {" s "} placebox "))
574
575   (define (pianobrace y staffht)
576     (let* ((step 1.0)
577            (minht (* 2 staffht))
578            (maxht (* 7 minht))
579            )
580       (string-append
581        (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
582        (char (max 0 (/  (- (min y (- maxht step)) minht) step))))
583       )
584     )
585
586
587   (define (bezier-sandwich l thick)
588     (string-append 
589      (apply string-append (map control->string l))
590      (number->string  thick)
591      " draw_bezier_sandwich"))
592
593   (define (start-line height)
594     (begin
595       "\nstart_line {\n"))
596   
597   (define (stem breapth width depth height) 
598     (string-append (numbers->string (list breapth width depth height))
599                    " draw_box" ))
600
601   (define (stop-line)
602       "}\nstop_line\n")
603
604   (define (text s)
605     (string-append "(" s ") show  "))
606
607
608   (define (volta h w thick vert_start vert_end)
609     (string-append 
610      (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
611      " draw_volta"))
612
613   (define (tuplet ht gap dx dy thick dir)
614     (string-append 
615      (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
616      " draw_tuplet"))
617
618
619   (define (unknown) 
620     "\n unknown\n")
621
622
623   ;; PS
624   (cond ((eq? action-name 'all-definitions)
625          `(begin
626             (define beam ,beam)
627             (define tuplet ,tuplet)
628             (define bracket ,bracket)
629             (define char ,char)
630             (define crescendo ,crescendo)
631             (define volta ,volta)
632             (define bezier-sandwich ,bezier-sandwich)
633             (define dashed-slur ,dashed-slur) 
634             (define decrescendo ,decrescendo) 
635             (define end-output ,end-output)
636             (define experimental-on ,experimental-on)
637             (define filledbox ,filledbox)
638             (define font-def ,font-def)
639             (define font-switch ,font-switch)
640             (define pianobrace ,pianobrace)
641             (define header-end ,header-end)
642             (define lily-def ,lily-def)
643             (define header ,header) 
644             (define invoke-char ,invoke-char) 
645             (define invoke-dim1 ,invoke-dim1)
646             (define placebox ,placebox)
647             (define select-font ,select-font)
648             (define start-line ,start-line)
649             (define stem ,stem)
650             (define stop-line ,stop-line)
651             (define stop-last-line ,stop-line)
652             (define text ,text)
653             ))
654         ((eq? action-name 'tuplet) tuplet)
655         ((eq? action-name 'beam) beam)
656         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
657         ((eq? action-name 'bracket) bracket)
658         ((eq? action-name 'char) char)
659         ((eq? action-name 'crescendo) crescendo)
660         ((eq? action-name 'dashed-slur) dashed-slur) 
661         ((eq? action-name 'decrescendo) decrescendo)
662         ((eq? action-name 'experimental-on) experimental-on)
663         ((eq? action-name 'filledbox) filledbox)
664         ((eq? action-name 'select-font) select-font)
665         ((eq? action-name 'volta) volta)
666         (else (error "unknown tag -- PS-SCM " action-name))
667         )
668   )
669
670                                         ;
671 ; Russ McManus, <mcmanus@IDT.NET>  
672
673 ; I use the following, which should definitely be provided somewhere
674 ; in guile, but isn't, AFAIK:
675
676
677
678 (define (hash-table-for-each fn ht)
679   (do ((i 0 (+ 1 i)))
680       ((= i (vector-length ht)))
681     (do ((alist (vector-ref ht i) (cdr alist)))
682         ((null? alist) #t)
683       (fn (car (car alist)) (cdr (car alist))))))
684
685 (define (hash-table-map fn ht)
686   (do ((i 0 (+ 1 i))
687        (ret-ls '()))
688       ((= i (vector-length ht)) (reverse ret-ls))
689     (do ((alist (vector-ref ht i) (cdr alist)))
690         ((null? alist) #t)
691       (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
692
693
694 ;;;; print a SCM expression.  Isn't this part of the std lib?
695
696 (define (scmlist->string exp)
697   (cond
698    ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
699    ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
700    (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
701    ))
702
703 (define (scm->string exp)
704   (cond
705    ((pair? exp) (string-append "(" (scmlist->string exp)))
706    ((number? exp) (number->string exp))
707    ((symbol? exp) (symbol->string exp))
708    ((string? exp) (string-append "\"" exp "\""))
709    ))
710