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