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