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