]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
release: 1.1.51
[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
53   (font i)
54   (string-append
55    "font"
56    (make-string 1 (integer->char (+ (char->integer #\A) i)))
57    ))
58
59
60
61 (define (scm-scm action-name)
62   1)
63
64 (define security-paranoia #f)
65
66
67 ;; See documentation of Item::visibility_lambda_
68 (define (postbreak_only_visibility d) (if (= d 1) '(#f . #f) '(#t . #t)))
69 (define (spanbar_non_postbreak_visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
70
71 (define (non_postbreak_visibility d) (if (= d 1) '(#t . #t) '(#f . #f)))
72 (define (non_prebreak_visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
73
74
75 ;; Score_span_bars are only visible at start of line
76 ;; i.e. if break_dir == RIGHT == 1
77 (define Span_bar_engraver_visibility non_postbreak_visibility)
78 (define mark-visibility non_prebreak_visibility)
79 (define Span_score_bar_engraver_visibility postbreak_only_visibility)
80 (define Piano_bar_engraver_visibility postbreak_only_visibility)
81 (define Staff_group_bar_engraver_visibility postbreak_only_visibility)
82
83 ;; Spacing constants for prefatory matter.
84 ;;
85 ;; rules for this spacing are much more complicated than this. See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147
86 ;;
87 ;;
88
89 (define space-alist
90  '(
91    (("" "Clef_item") . (minimum_space 1.0))
92    (("" "Staff_bar") . (minimum_space 0.0))
93    (("" "Clef_item") . (minimum_space 1.0))
94    (("" "Key_item") . (minimum_space 0.5))
95    (("" "Span_bar") . (extra_space 0.0))
96    (("" "Time_signature") . (extra_space 0.0))
97    (("" "begin-of-note") . (minimum_space 1.5))
98    (("Clef_item" "Key_item") . (minimum_space 3.5))
99    (("Key_item" "Time_signature") . (extra_space 2.0))
100    (("Clef_item"  "Time_signature") . (minimum_space 3.5))
101    (("Staff_bar" "Clef_item") .   (minimum_space 1.0))
102    (("Clef_item"  "Staff_bar") .  (minimum_space 3.7))
103    (("Time_signature" "Staff_bar") .  (minimum_space 2.0))
104    (("Key_item"  "Staff_bar") .  (extra_space 1.0))
105    (("Span_bar" "Clef_item") .   (extra_space 1.0))
106    (("Clef_item"  "Span_bar") . (minimum_space 3.7))
107    (("Time_signature" "Span_bar") . (minimum_space 2.0))
108    (("Key_item"  "Span_bar") . (minimum_space 2.5))
109    (("Staff_bar" "Time_signature") . (minimum_space 2.0))
110    (("Time_signature" "begin-of-note") . (extra_space 1.0))     ; Huh? see twinkle.ly
111    (("Key_item" "begin-of-note") . (extra_space 2.5))
112    (("Staff_bar" "begin-of-note") . (extra_space 1.0))
113    (("Clef_item" "begin-of-note") . (minimum_space 5.0))
114    (("" "Breathing_sign") . (minimum_space 0.0))
115    (("Breathing_sign" "Key_item") . (minimum_space 1.5))
116    (("Breathing_sign" "begin-of-note") . (minimum_space 1.0))
117    (("Breathing_sign" "Staff_bar") . (minimum_space 1.5))
118    (("Breathing_sign" "Clef_item") . (minimum_space 2.0))
119    )
120 )
121  
122  
123
124 (define (break-align-spacer this next)
125   (let ((entry (assoc `(,this ,next) space-alist)))
126     (if entry
127         (cdr entry)
128         (begin (ly-warn (string-append "Unknown spacing pair `" this "', `" next "'"))
129                '(minimum_space 0.0)))))
130   
131         
132
133 ;;;;;;;; TeX
134
135 (define cmr-alist 
136   '(("bold" . "cmbx") 
137     ("dynamic" . "feta-din") 
138     ("finger" . "feta-nummer") 
139     ("typewriter" . "cmtt") 
140     ("italic" . "cmti") 
141     ("roman" . "cmr") 
142     ("large" . "cmbx") 
143     ("Large" . "cmbx") 
144     ("mark" . "feta-nummer") 
145     ("number" . "feta-nummer") 
146     ("volta" . "feta-nummer"))
147 )
148
149 (define script-alist '())
150 (define (articulation-to-scriptdef a)
151   (assoc a script-alist)
152   )
153
154 ;; Map style names to TeX font names.  Return false if 
155 ;; no font name found. 
156 (define (style-to-cmr s)
157   (assoc s cmr-alist )
158   )
159
160
161 (define (tex-scm action-name)
162
163   (define (unknown) 
164     "%\n\\unknown%\n")
165
166   (define font-alist '())
167   (define font-count 0)
168   (define current-font "")
169   (define (clear-fontcache)
170     (begin
171       (set! font-alist '())
172       (set! font-count 0)
173       (set! current-font "")))
174   
175   (define (cached-fontname i)
176     (string-append
177      "\\lilyfont"
178      (make-string 1 (integer->char (+ 65 i)))))
179     
180   (define (select-font font-name)
181       (if (not (equal? font-name current-font))
182           (begin
183             (set! current-font font-name)
184             (define font-cmd (assoc font-name font-alist))
185             (if (eq? font-cmd #f)
186                 (begin
187                   (set! font-cmd (cached-fontname font-count))
188                   (set! font-alist (acons font-name font-cmd font-alist))
189                   (set! font-count (+ 1 font-count))
190                   (if (equal? font-name "")
191                       (error "Empty fontname -- SELECT-FONT"))
192                   (string-append "\\font" font-cmd "=" font-name font-cmd))
193                 (cdr font-cmd)))
194           ""                            ;no switch needed
195           ))
196   
197   (define (beam width slope thick)
198     (embedded-ps ((ps-scm 'beam) width slope thick)))
199
200   (define (bracket h)
201     (embedded-ps ((ps-scm 'bracket) h)))
202
203   (define (dashed-slur thick dash l)
204     (embedded-ps ((ps-scm 'dashed-slur)  thick dash l)))
205
206   (define (crescendo w h cont)
207     (embedded-ps ((ps-scm 'crescendo) w h cont)))
208
209   (define (char i)
210     (string-append "\\show{" (inexact->string i 10) "}"))
211   
212   (define (decrescendo w h cont)
213     (embedded-ps ((ps-scm 'decrescendo) w h cont)))
214
215   (define (embedded-ps s)
216     (string-append "\\embeddedps{" s "}"))
217
218   (define (end-output) 
219     "\n\\EndLilyPondOutput")
220   
221   (define (experimental-on)
222     "\\turnOnExperimentalFeatures")
223
224   (define (font-switch i)
225     (string-append
226      "\\" (font i) "\n"))
227
228   (define (font-def i s)
229     (string-append
230      "\\font" (font-switch i) "=" s "\n"))
231
232   (define (generalmeter num den)
233     (string-append 
234      "\\generalmeter{" (number->string (inexact->exact num)) "}{" (number->string (inexact->exact den)) "}"))
235
236   (define (header-end) "\\turnOnPostScript")
237
238   (define (header creator generate) 
239     (string-append
240      "%created by: " creator generate "\n"))
241
242   (define (invoke-char s i)
243     (string-append 
244      "\n\\" s "{" (inexact->string i 10) "}" ))
245
246   (define (invoke-dim1 s d)
247     (string-append
248      "\n\\" s "{" (number->dim d) "}"))
249   (define (pt->sp x)
250     (* 65536 x))
251   
252   ;;
253   ;; need to do something to make this really safe.
254   ;;
255   (if security-paranoia
256       (define (output-tex-string s)
257         (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post))
258       (define (output-tex-string s)    s))
259
260   (define (lily-def key val)
261     (string-append
262      "\\def\\" (output-tex-string key) "{" (output-tex-string val) "}\n"))
263
264   (define (number->dim x)
265     (string-append 
266      (number->string  (chop-decimal x)) " pt "))
267
268   (define (placebox x y s) 
269     (string-append 
270      "\\placebox{"
271      (number->dim y) "}{" (number->dim x) "}{" s "}"))
272
273   (define (pianobrace y)
274     (define step 1.0)
275     (define minht (* 2 mudelapaperstaffheight))
276     (define maxht (* 7 minht))
277     (string-append
278      "{\\bracefont " (char (max
279                             0
280                             (/  (- (min y (- maxht step)) minht) step))) "}"))
281
282
283
284   (define (rulesym h w) 
285     (string-append 
286      "\\vrule height " (number->dim (/ h 2))
287      " depth " (number->dim (/ h 2))
288      " width " (number->dim w)
289      )
290     )
291
292   (define (bezier-sandwich l)
293     (embedded-ps ((ps-scm 'bezier-sandwich) l)))
294
295
296   (define (start-line ht)
297     (begin
298       (clear-fontcache)
299       (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
300     )
301   (define (stop-line) 
302     "}\\vss}\\interscoreline")
303   (define (stop-last-line)
304     "}\\vss}")
305   (define (filledbox breapth width depth height) 
306     (string-append 
307      "\\kern" (number->dim (- breapth))
308      "\\vrule width " (number->dim (+ breapth width))
309      "depth " (number->dim depth)
310      "height " (number->dim height) " "))
311
312
313
314   (define (text s)
315     (string-append "\\hbox{" (output-tex-string s) "}"))
316   
317   (define (tuplet ht dx dy thick dir)
318     (embedded-ps ((ps-scm 'tuplet) ht dx dy thick dir)))
319
320   (define (volta w thick last)
321     (embedded-ps ((ps-scm 'volta) w thick last)))
322
323   ;; TeX
324   ;; The procedures listed below form the public interface of TeX-scm.
325   ;; (should merge the 2 lists)
326   (cond ((eq? action-name 'all-definitions)
327          `(begin
328             (define beam ,beam)
329             (define bezier-sandwich ,bezier-sandwich)
330             (define bracket ,bracket)
331             (define char ,char)
332             (define crescendo ,crescendo)
333             (define dashed-slur ,dashed-slur) 
334             (define decrescendo ,decrescendo) 
335             (define end-output ,end-output)
336             (define experimental-on ,experimental-on)
337             (define filledbox ,filledbox)
338             (define font-def ,font-def)
339             (define font-switch ,font-switch)
340             (define generalmeter ,generalmeter)
341             (define header-end ,header-end)
342             (define lily-def ,lily-def)
343             (define header ,header) 
344             (define invoke-char ,invoke-char) 
345             (define invoke-dim1 ,invoke-dim1)
346             (define pianobrace ,pianobrace)
347             (define placebox ,placebox)
348             (define rulesym ,rulesym)
349             (define select-font ,select-font)
350             (define start-line ,start-line)
351             (define stop-line ,stop-line)
352             (define stop-last-line ,stop-last-line)
353             (define text ,text)
354             (define tuplet ,tuplet)
355             (define volta ,volta)
356             ))
357
358         ((eq? action-name 'beam) beam)
359         ((eq? action-name 'tuplet) tuplet)
360         ((eq? action-name 'bracket) bracket)
361         ((eq? action-name 'crescendo) crescendo)
362         ((eq? action-name 'dashed-slur) dashed-slur) 
363         ((eq? action-name 'decrescendo) decrescendo) 
364         ((eq? action-name 'end-output) end-output)
365         ((eq? action-name 'experimental-on) experimental-on)
366         ((eq? action-name 'font-def) font-def)
367         ((eq? action-name 'font-switch) font-switch)
368         ((eq? action-name 'generalmeter) generalmeter)
369         ((eq? action-name 'header-end) header-end)
370         ((eq? action-name 'lily-def) lily-def)
371         ((eq? action-name 'header) header) 
372         ((eq? action-name 'invoke-char) invoke-char) 
373         ((eq? action-name 'invoke-dim1) invoke-dim1)
374         ((eq? action-name 'placebox) placebox)
375         ((eq? action-name 'rulesym) rulesym)
376         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
377         ((eq? action-name 'start-line) start-line)
378         ((eq? action-name 'stem) stem)
379         ((eq? action-name 'stop-line) stop-line)
380         ((eq? action-name 'stop-last-line) stop-last-line)
381         ((eq? action-name 'volta) volta)
382         (else (error "unknown tag -- PS-TEX " action-name))
383         )
384   )
385
386 ;;;;;;;;;;;; PS
387 (define (ps-scm action-name)
388
389
390   ;; alist containing fontname -> fontcommand assoc (both strings)
391   (define font-alist '())
392   (define font-count 0)
393   (define current-font "")
394   (define (clear-fontcache)
395     (begin
396       (set! font-alist '())
397       (set! font-count 0)
398       (set! current-font "")))
399   
400   (define (cached-fontname i)
401     (string-append
402      " lilyfont"
403      (make-string 1 (integer->char (+ 65 i)))))
404
405   (define (select-font font-name)
406       (if (not (equal? font-name current-font))
407           (begin
408             (set! current-font font-name)
409             (define font-cmd (assoc font-name font-alist))
410             (if (eq? font-cmd #f)
411                 (begin
412                   (set! font-cmd (cached-fontname font-count))
413                   (set! font-alist (acons font-name font-cmd font-alist))
414                   (set! font-count (+ 1 font-count))
415                   (string-append "\n/" font-cmd " {/"
416                                  font-name
417                                  " findfont 12 scalefont setfont} bind def \n"
418                                  font-cmd " \n"))
419                 (string-append (cdr font-cmd) " ")))
420           ; font-name == current-font no switch needed
421           ""                            
422           ))
423                   
424   (define (beam width slope thick)
425     (string-append
426      (numbers->string (list width slope thick)) " draw_beam " ))
427
428   (define (bracket h)
429     (invoke-dim1 " draw_bracket" h))
430
431   (define (char i)
432     (invoke-char " show" i))
433
434   (define (crescendo w h cont)
435     (string-append 
436      (numbers->string (list w h (inexact->exact cont)))
437      " draw_crescendo"))
438
439   (define (dashed-slur thick dash l)
440     (string-append 
441      (apply string-append (map control->string l)) 
442      (number->string thick) 
443      " [ "
444      (if (> 1 dash) (number->string (- (* thick dash) thick)) "0") " "
445      (number->string (* 2 thick))
446      " ] 0 draw_dashed_slur"))
447
448   (define (decrescendo w h cont)
449     (string-append 
450      (numbers->string (list w h (inexact->exact cont)))
451      " draw_decrescendo"))
452
453
454   (define (end-output)
455     "\nshowpage\n")
456   
457   (define (experimental-on) "")
458   
459   (define (filledbox breapth width depth height) 
460     (string-append (numbers->string (list breapth width depth height))
461                    " draw_box" ))
462
463   ;; obsolete?
464   (define (font-def i s)
465     (string-append
466      "\n/" (font i) " {/" 
467      (substring s 0 (- (string-length s) 4))
468      " findfont 12 scalefont setfont} bind def \n"))
469
470   (define (font-switch i)
471     (string-append (font i) " "))
472
473   (define (generalmeter num den)
474     (string-append (number->string (inexact->exact num)) " " (number->string (inexact->exact den)) " generalmeter "))
475
476   (define (header-end) "")
477   (define (lily-def key val)
478     (string-append
479      "/" key " {" val "} bind def\n"))
480
481   (define (header creator generate) 
482     (string-append
483      "%!PS-Adobe-3.0\n"
484      "%%Creator: " creator generate "\n"))
485   
486   (define (invoke-char s i)
487     (string-append 
488      "(\\" (inexact->string i 8) ") " s " " ))
489   
490   (define (invoke-dim1 s d) 
491     (string-append
492      (number->string (* d  (/ 72.27 72))) " " s ))
493
494   (define (placebox x y s) 
495     (string-append 
496      (number->string x) " " (number->string y) " {" s "} placebox "))
497   (define (pianobrace y)
498     ""
499     )
500
501   (define (rulesym x y) 
502     (string-append 
503      (number->string x) " "
504      (number->string y) " "
505      " rulesym"))
506
507   (define (bezier-sandwich l)
508     (string-append 
509      (apply string-append (map control->string l)) 
510      " draw_bezier_sandwich"))
511
512   (define (start-line height)
513     (begin
514       (clear-fontcache)
515       "\nstart_line {\n"))
516   
517   (define (stem breapth width depth height) 
518     (string-append (numbers->string (list breapth width depth height))
519                    " draw_box" ))
520
521   (define (stop-line)
522       "}\nstop_line\n")
523
524   (define (text s)
525     (string-append "(" s ") show  "))
526
527
528   (define (volta w thick last)
529     (string-append 
530      (numbers->string (list w thick (inexact->exact last)))
531      " draw_volta"))
532
533   (define (tuplet ht dx dy thick dir)
534     (string-append 
535      (numbers->string (list ht dx dy thick (inexact->exact dir)))
536      " draw_tuplet"))
537
538
539   (define (unknown) 
540     "\n unknown\n")
541
542
543   ;; PS
544   (cond ((eq? action-name 'all-definitions)
545          `(begin
546             (define beam ,beam)
547             (define tuplet ,tuplet)
548             (define bracket ,bracket)
549             (define char ,char)
550             (define crescendo ,crescendo)
551             (define volta ,volta)
552             (define bezier-sandwich ,bezier-sandwich)
553             (define dashed-slur ,dashed-slur) 
554             (define decrescendo ,decrescendo) 
555             (define end-output ,end-output)
556             (define experimental-on ,experimental-on)
557             (define filledbox ,filledbox)
558             (define font-def ,font-def)
559             (define font-switch ,font-switch)
560             (define generalmeter ,generalmeter)
561             (define pianobrace ,pianobrace)
562             (define header-end ,header-end)
563             (define lily-def ,lily-def)
564             (define header ,header) 
565             (define invoke-char ,invoke-char) 
566             (define invoke-dim1 ,invoke-dim1)
567             (define placebox ,placebox)
568             (define rulesym ,rulesym)
569             (define select-font ,select-font)
570             (define start-line ,start-line)
571             (define stem ,stem)
572             (define stop-line ,stop-line)
573             (define stop-last-line ,stop-line)
574             (define text ,text)
575             ))
576         ((eq? action-name 'tuplet) tuplet)
577         ((eq? action-name 'beam) beam)
578         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
579         ((eq? action-name 'bracket) bracket)
580         ((eq? action-name 'char) char)
581         ((eq? action-name 'crescendo) crescendo)
582         ((eq? action-name 'dashed-slur) dashed-slur) 
583         ((eq? action-name 'decrescendo) decrescendo)
584         ((eq? action-name 'experimental-on) experimental-on)
585         ((eq? action-name 'filledbox) filledbox)
586         ((eq? action-name 'select-font) select-font)
587         ((eq? action-name 'volta) volta)
588         (else (error "unknown tag -- PS-SCM " action-name))
589         )
590   )
591
592                                         ;
593 ; Russ McManus, <mcmanus@IDT.NET>  
594
595 ; I use the following, which should definitely be provided somewhere
596 ; in guile, but isn't, AFAIK:
597
598
599
600 (define (hash-table-for-each fn ht)
601   (do ((i 0 (+ 1 i)))
602       ((= i (vector-length ht)))
603     (do ((alist (vector-ref ht i) (cdr alist)))
604         ((null? alist) #t)
605       (fn (car (car alist)) (cdr (car alist))))))
606
607 (define (hash-table-map fn ht)
608   (do ((i 0 (+ 1 i))
609        (ret-ls '()))
610       ((= i (vector-length ht)) (reverse ret-ls))
611     (do ((alist (vector-ref ht i) (cdr alist)))
612         ((null? alist) #t)
613       (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
614
615
616 ;;;; print a SCM expression.  Isn't this part of the std lib?
617
618 (define (scmlist->string exp)
619   (cond
620    ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
621    ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
622    (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
623    ))
624
625 (define (scm->string exp)
626   (cond
627    ((pair? exp) (string-append "(" (scmlist->string exp)))
628    ((number? exp) (number->string exp))
629    ((symbol? exp) (symbol->string exp))
630    ((string? exp) (string-append "\"" exp "\""))
631    ))
632
633 (define (test-scm->string)
634 (list (scmlist->string '(a))
635 (scmlist->string '(a b))
636 (scmlist->string '(a b . c))
637
638
639 (scm->string '(a))
640 (scm->string '(a b ))
641 (scm->string '(a b . c))
642 (scm->string '(a b (c . d)))
643 (scm->string '(a "bla" (c . 1.5)))
644 )
645 )