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