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