]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
release: 1.1.53
[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
274   ;;;; UGH! Junk \\bracefont !
275   (define (pianobrace y)
276     (define step 1.0)
277     (define minht (* 2 mudelapaperstaffheight))
278     (define maxht (* 7 minht))
279     (string-append
280      "{\\bracefont " (char (max
281                             0
282                             (/  (- (min y (- maxht step)) minht) step))) "}"))
283
284
285
286   (define (rulesym h w) 
287     (string-append 
288      "\\vrule height " (number->dim (/ h 2))
289      " depth " (number->dim (/ h 2))
290      " width " (number->dim w)
291      )
292     )
293
294   (define (bezier-sandwich l)
295     (embedded-ps ((ps-scm 'bezier-sandwich) l)))
296
297
298   (define (start-line ht)
299     (begin
300       (clear-fontcache)
301       (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
302     )
303   (define (stop-line) 
304     "}\\vss}\\interscoreline")
305   (define (stop-last-line)
306     "}\\vss}")
307   (define (filledbox breapth width depth height) 
308     (string-append 
309      "\\kern" (number->dim (- breapth))
310      "\\vrule width " (number->dim (+ breapth width))
311      "depth " (number->dim depth)
312      "height " (number->dim height) " "))
313
314
315
316   (define (text s)
317     (string-append "\\hbox{" (output-tex-string s) "}"))
318   
319   (define (tuplet ht dx dy thick dir)
320     (embedded-ps ((ps-scm 'tuplet) ht dx dy thick dir)))
321
322   (define (volta w thick last)
323     (embedded-ps ((ps-scm 'volta) w thick last)))
324
325   ;; TeX
326   ;; The procedures listed below form the public interface of TeX-scm.
327   ;; (should merge the 2 lists)
328   (cond ((eq? action-name 'all-definitions)
329          `(begin
330             (define beam ,beam)
331             (define bezier-sandwich ,bezier-sandwich)
332             (define bracket ,bracket)
333             (define char ,char)
334             (define crescendo ,crescendo)
335             (define dashed-slur ,dashed-slur) 
336             (define decrescendo ,decrescendo) 
337             (define end-output ,end-output)
338             (define experimental-on ,experimental-on)
339             (define filledbox ,filledbox)
340             (define font-def ,font-def)
341             (define font-switch ,font-switch)
342             (define generalmeter ,generalmeter)
343             (define header-end ,header-end)
344             (define lily-def ,lily-def)
345             (define header ,header) 
346             (define invoke-char ,invoke-char) 
347             (define invoke-dim1 ,invoke-dim1)
348             (define pianobrace ,pianobrace)
349             (define placebox ,placebox)
350             (define rulesym ,rulesym)
351             (define select-font ,select-font)
352             (define start-line ,start-line)
353             (define stop-line ,stop-line)
354             (define stop-last-line ,stop-last-line)
355             (define text ,text)
356             (define tuplet ,tuplet)
357             (define volta ,volta)
358             ))
359
360         ((eq? action-name 'beam) beam)
361         ((eq? action-name 'tuplet) tuplet)
362         ((eq? action-name 'bracket) bracket)
363         ((eq? action-name 'crescendo) crescendo)
364         ((eq? action-name 'dashed-slur) dashed-slur) 
365         ((eq? action-name 'decrescendo) decrescendo) 
366         ((eq? action-name 'end-output) end-output)
367         ((eq? action-name 'experimental-on) experimental-on)
368         ((eq? action-name 'font-def) font-def)
369         ((eq? action-name 'font-switch) font-switch)
370         ((eq? action-name 'generalmeter) generalmeter)
371         ((eq? action-name 'header-end) header-end)
372         ((eq? action-name 'lily-def) lily-def)
373         ((eq? action-name 'header) header) 
374         ((eq? action-name 'invoke-char) invoke-char) 
375         ((eq? action-name 'invoke-dim1) invoke-dim1)
376         ((eq? action-name 'placebox) placebox)
377         ((eq? action-name 'rulesym) rulesym)
378         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
379         ((eq? action-name 'start-line) start-line)
380         ((eq? action-name 'stem) stem)
381         ((eq? action-name 'stop-line) stop-line)
382         ((eq? action-name 'stop-last-line) stop-last-line)
383         ((eq? action-name 'volta) volta)
384         (else (error "unknown tag -- PS-TEX " action-name))
385         )
386   )
387
388 ;;;;;;;;;;;; PS
389 (define (ps-scm action-name)
390
391
392   ;; alist containing fontname -> fontcommand assoc (both strings)
393   (define font-alist '())
394   (define font-count 0)
395   (define current-font "")
396   (define (clear-fontcache)
397     (begin
398       (set! font-alist '())
399       (set! font-count 0)
400       (set! current-font "")))
401   
402   (define (cached-fontname i)
403     (string-append
404      " lilyfont"
405      (make-string 1 (integer->char (+ 65 i)))))
406
407   (define (select-font font-name)
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
419                                  " findfont 12 scalefont setfont} bind def \n"
420                                  font-cmd " \n"))
421                 (string-append (cdr font-cmd) " ")))
422           ; font-name == current-font no switch needed
423           ""                            
424           ))
425                   
426   (define (beam width slope thick)
427     (string-append
428      (numbers->string (list width slope thick)) " draw_beam " ))
429
430   (define (bracket h)
431     (invoke-dim1 " draw_bracket" h))
432
433   (define (char i)
434     (invoke-char " show" i))
435
436   (define (crescendo w h cont)
437     (string-append 
438      (numbers->string (list w h (inexact->exact cont)))
439      " draw_crescendo"))
440
441   (define (dashed-slur thick dash l)
442     (string-append 
443      (apply string-append (map control->string l)) 
444      (number->string thick) 
445      " [ "
446      (if (> 1 dash) (number->string (- (* thick dash) thick)) "0") " "
447      (number->string (* 2 thick))
448      " ] 0 draw_dashed_slur"))
449
450   (define (decrescendo w h cont)
451     (string-append 
452      (numbers->string (list w h (inexact->exact cont)))
453      " draw_decrescendo"))
454
455
456   (define (end-output)
457     "\nshowpage\n")
458   
459   (define (experimental-on) "")
460   
461   (define (filledbox breapth width depth height) 
462     (string-append (numbers->string (list breapth width depth height))
463                    " draw_box" ))
464
465   ;; obsolete?
466   (define (font-def i s)
467     (string-append
468      "\n/" (font i) " {/" 
469      (substring s 0 (- (string-length s) 4))
470      " findfont 12 scalefont setfont} bind def \n"))
471
472   (define (font-switch i)
473     (string-append (font i) " "))
474
475   (define (generalmeter num den)
476     (string-append (number->string (inexact->exact num)) " " (number->string (inexact->exact den)) " generalmeter "))
477
478   (define (header-end) "")
479   (define (lily-def key val)
480     (string-append
481      "/" key " {" val "} bind def\n"))
482
483   (define (header creator generate) 
484     (string-append
485      "%!PS-Adobe-3.0\n"
486      "%%Creator: " creator generate "\n"))
487   
488   (define (invoke-char s i)
489     (string-append 
490      "(\\" (inexact->string i 8) ") " s " " ))
491   
492   (define (invoke-dim1 s d) 
493     (string-append
494      (number->string (* d  (/ 72.27 72))) " " s ))
495
496   (define (placebox x y s) 
497     (string-append 
498      (number->string x) " " (number->string y) " {" s "} placebox "))
499   (define (pianobrace y)
500     ""
501     )
502
503   (define (rulesym x y) 
504     (string-append 
505      (number->string x) " "
506      (number->string y) " "
507      " rulesym"))
508
509   (define (bezier-sandwich l)
510     (string-append 
511      (apply string-append (map control->string l)) 
512      " draw_bezier_sandwich"))
513
514   (define (start-line height)
515     (begin
516       (clear-fontcache)
517       "\nstart_line {\n"))
518   
519   (define (stem breapth width depth height) 
520     (string-append (numbers->string (list breapth width depth height))
521                    " draw_box" ))
522
523   (define (stop-line)
524       "}\nstop_line\n")
525
526   (define (text s)
527     (string-append "(" s ") show  "))
528
529
530   (define (volta w thick last)
531     (string-append 
532      (numbers->string (list w thick (inexact->exact last)))
533      " draw_volta"))
534
535   (define (tuplet ht dx dy thick dir)
536     (string-append 
537      (numbers->string (list ht dx dy thick (inexact->exact dir)))
538      " draw_tuplet"))
539
540
541   (define (unknown) 
542     "\n unknown\n")
543
544
545   ;; PS
546   (cond ((eq? action-name 'all-definitions)
547          `(begin
548             (define beam ,beam)
549             (define tuplet ,tuplet)
550             (define bracket ,bracket)
551             (define char ,char)
552             (define crescendo ,crescendo)
553             (define volta ,volta)
554             (define bezier-sandwich ,bezier-sandwich)
555             (define dashed-slur ,dashed-slur) 
556             (define decrescendo ,decrescendo) 
557             (define end-output ,end-output)
558             (define experimental-on ,experimental-on)
559             (define filledbox ,filledbox)
560             (define font-def ,font-def)
561             (define font-switch ,font-switch)
562             (define generalmeter ,generalmeter)
563             (define pianobrace ,pianobrace)
564             (define header-end ,header-end)
565             (define lily-def ,lily-def)
566             (define header ,header) 
567             (define invoke-char ,invoke-char) 
568             (define invoke-dim1 ,invoke-dim1)
569             (define placebox ,placebox)
570             (define rulesym ,rulesym)
571             (define select-font ,select-font)
572             (define start-line ,start-line)
573             (define stem ,stem)
574             (define stop-line ,stop-line)
575             (define stop-last-line ,stop-line)
576             (define text ,text)
577             ))
578         ((eq? action-name 'tuplet) tuplet)
579         ((eq? action-name 'beam) beam)
580         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
581         ((eq? action-name 'bracket) bracket)
582         ((eq? action-name 'char) char)
583         ((eq? action-name 'crescendo) crescendo)
584         ((eq? action-name 'dashed-slur) dashed-slur) 
585         ((eq? action-name 'decrescendo) decrescendo)
586         ((eq? action-name 'experimental-on) experimental-on)
587         ((eq? action-name 'filledbox) filledbox)
588         ((eq? action-name 'select-font) select-font)
589         ((eq? action-name 'volta) volta)
590         (else (error "unknown tag -- PS-SCM " action-name))
591         )
592   )
593
594                                         ;
595 ; Russ McManus, <mcmanus@IDT.NET>  
596
597 ; I use the following, which should definitely be provided somewhere
598 ; in guile, but isn't, AFAIK:
599
600
601
602 (define (hash-table-for-each fn ht)
603   (do ((i 0 (+ 1 i)))
604       ((= i (vector-length ht)))
605     (do ((alist (vector-ref ht i) (cdr alist)))
606         ((null? alist) #t)
607       (fn (car (car alist)) (cdr (car alist))))))
608
609 (define (hash-table-map fn ht)
610   (do ((i 0 (+ 1 i))
611        (ret-ls '()))
612       ((= i (vector-length ht)) (reverse ret-ls))
613     (do ((alist (vector-ref ht i) (cdr alist)))
614         ((null? alist) #t)
615       (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
616
617
618 ;;;; print a SCM expression.  Isn't this part of the std lib?
619
620 (define (scmlist->string exp)
621   (cond
622    ((pair? (cdr exp)) (string-append (scm->string (car exp)) " " (scmlist->string (cdr exp))))
623    ((eq? '() (cdr exp)) (string-append (scm->string (car exp)) ")"))
624    (else (string-append (scm->string (car exp)) " . " (scm->string (cdr exp)) ")"))
625    ))
626
627 (define (scm->string exp)
628   (cond
629    ((pair? exp) (string-append "(" (scmlist->string exp)))
630    ((number? exp) (number->string exp))
631    ((symbol? exp) (symbol->string exp))
632    ((string? exp) (string-append "\"" exp "\""))
633    ))
634
635 (define (test-scm->string)
636 (list (scmlist->string '(a))
637 (scmlist->string '(a b))
638 (scmlist->string '(a b . c))
639
640
641 (scm->string '(a))
642 (scm->string '(a b ))
643 (scm->string '(a b . c))
644 (scm->string '(a b (c . d)))
645 (scm->string '(a "bla" (c . 1.5)))
646 )
647 )