]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
release: 1.1.39
[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
257   (define (filledbox breapth width depth height) 
258     (string-append 
259      "\\kern" (number->dim (- breapth))
260      "\\vrule width " (number->dim (+ breapth width))
261      "depth " (number->dim depth)
262      "height " (number->dim height) " "))
263
264
265
266   (define (text s)
267     (string-append "\\hbox{" (output-tex-string s) "}"))
268   
269   (define (tuplet ht dx dy thick dir)
270     (embedded-ps ((ps-scm 'tuplet) ht dx dy thick dir)))
271
272   (define (volta w thick last)
273     (embedded-ps ((ps-scm 'volta) w thick last)))
274
275   ;; TeX
276   ;; The procedures listed below form the public interface of TeX-scm.
277   ;; (should merge the 2 lists)
278   (cond ((eq? action-name 'all-definitions)
279          `(begin
280             (define beam ,beam)
281             (define bezier-sandwich ,bezier-sandwich)
282             (define bracket ,bracket)
283             (define char ,char)
284             (define crescendo ,crescendo)
285             (define dashed-slur ,dashed-slur) 
286             (define decrescendo ,decrescendo) 
287             (define end-output ,end-output)
288             (define experimental-on ,experimental-on)
289             (define filledbox ,filledbox)
290             (define font-def ,font-def)
291             (define font-switch ,font-switch)
292             (define generalmeter ,generalmeter)
293             (define header-end ,header-end)
294             (define lily-def ,lily-def)
295             (define header ,header) 
296             (define invoke-char ,invoke-char) 
297             (define invoke-dim1 ,invoke-dim1)
298             (define pianobrace ,pianobrace)
299             (define placebox ,placebox)
300             (define rulesym ,rulesym)
301             (define select-font ,select-font)
302             (define start-line ,start-line)
303             (define stop-line ,stop-line)
304             (define text ,text)
305             (define tuplet ,tuplet)
306             (define volta ,volta)
307             ))
308
309         ((eq? action-name 'beam) beam)
310         ((eq? action-name 'tuplet) tuplet)
311         ((eq? action-name 'bracket) bracket)
312         ((eq? action-name 'crescendo) crescendo)
313         ((eq? action-name 'dashed-slur) dashed-slur) 
314         ((eq? action-name 'decrescendo) decrescendo) 
315         ((eq? action-name 'end-output) end-output)
316         ((eq? action-name 'experimental-on) experimental-on)
317         ((eq? action-name 'font-def) font-def)
318         ((eq? action-name 'font-switch) font-switch)
319         ((eq? action-name 'generalmeter) generalmeter)
320         ((eq? action-name 'header-end) header-end)
321         ((eq? action-name 'lily-def) lily-def)
322         ((eq? action-name 'header) header) 
323         ((eq? action-name 'invoke-char) invoke-char) 
324         ((eq? action-name 'invoke-dim1) invoke-dim1)
325         ((eq? action-name 'placebox) placebox)
326         ((eq? action-name 'rulesym) rulesym)
327         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
328         ((eq? action-name 'start-line) start-line)
329         ((eq? action-name 'stem) stem)
330         ((eq? action-name 'stop-line) stop-line)
331         ((eq? action-name 'volta) volta)
332         (else (error "unknown tag -- PS-TEX " action-name))
333         )
334   )
335
336 ;;;;;;;;;;;; PS
337 (define (ps-scm action-name)
338
339
340   ;; alist containing fontname -> fontcommand assoc (both strings)
341   (define font-alist '())
342   (define font-count 0)
343   (define current-font "")
344   (define (clear-fontcache)
345     (begin
346       (set! font-alist '())
347       (set! font-count 0)
348       (set! current-font "")))
349   
350   (define (cached-fontname i)
351     (string-append
352      " lilyfont"
353      (make-string 1 (integer->char (+ 65 i)))))
354
355   (define (select-font font-name)
356       (if (not (equal? font-name current-font))
357           (begin
358             (set! current-font font-name)
359             (define font-cmd (assoc font-name font-alist))
360             (if (eq? font-cmd #f)
361                 (begin
362                   (set! font-cmd (cached-fontname font-count))
363                   (set! font-alist (acons font-name font-cmd font-alist))
364                   (set! font-count (+ 1 font-count))
365                   (string-append "\n/" font-cmd " {/"
366                                  font-name
367                                  " findfont 12 scalefont setfont} bind def \n"
368                                  font-cmd " \n"))
369                 (string-append (cdr font-cmd) " ")))
370           ; font-name == current-font no switch needed
371           ""                            
372           ))
373                   
374   (define (beam width slope thick)
375     (string-append
376      (numbers->string (list width slope thick)) " draw_beam " ))
377
378   (define (bracket h)
379     (invoke-dim1 " draw_bracket" h))
380
381   (define (char i)
382     (invoke-char " show" i))
383
384   (define (crescendo w h cont)
385     (string-append 
386      (numbers->string (list w h (inexact->exact cont)))
387      " draw_crescendo"))
388
389   (define (dashed-slur thick dash l)
390     (string-append 
391      (apply string-append (map control->string l)) 
392      (number->string thick) 
393      " [ "
394      (if (> 1 dash) (number->string (- (* thick dash) thick)) "0") " "
395      (number->string (* 2 thick))
396      " ] 0 draw_dashed_slur"))
397
398   (define (decrescendo w h cont)
399     (string-append 
400      (numbers->string (list w h (inexact->exact cont)))
401      " draw_decrescendo"))
402
403
404   (define (end-output)
405     "\nshowpage\n")
406   
407   (define (experimental-on) "")
408   
409   (define (filledbox breapth width depth height) 
410     (string-append (numbers->string (list breapth width depth height))
411                    " draw_box" ))
412
413   ;; obsolete?
414   (define (font-def i s)
415     (string-append
416      "\n/" (font i) " {/" 
417      (substring s 0 (- (string-length s) 4))
418      " findfont 12 scalefont setfont} bind def \n"))
419
420   (define (font-switch i)
421     (string-append (font i) " "))
422
423   (define (generalmeter num den)
424     (string-append (number->string (inexact->exact num)) " " (number->string (inexact->exact den)) " generalmeter "))
425
426   (define (header-end) "")
427   (define (lily-def key val)
428     (string-append
429      "/" key " {" val "} bind def\n"))
430
431   (define (header creator generate) 
432     (string-append
433      "%!PS-Adobe-3.0\n"
434      "%%Creator: " creator generate "\n"))
435   
436   (define (invoke-char s i)
437     (string-append 
438      "(\\" (inexact->string i 8) ") " s " " ))
439   
440   (define (invoke-dim1 s d) 
441     (string-append
442      (number->string (* d  (/ 72.27 72))) " " s ))
443
444   (define (placebox x y s) 
445     (string-append 
446      (number->string x) " " (number->string y) " {" s "} placebox "))
447   (define (pianobrace y)
448     ""
449     )
450
451   (define (rulesym x y) 
452     (string-append 
453      (number->string x) " "
454      (number->string y) " "
455      " rulesym"))
456
457   (define (bezier-sandwich l)
458     (string-append 
459      (apply string-append (map control->string l)) 
460      " draw_bezier_sandwich"))
461
462   (define (start-line height)
463     (begin
464       (clear-fontcache)
465       "\nstart_line {\n"))
466   
467   (define (stem breapth width depth height) 
468     (string-append (numbers->string (list breapth width depth height))
469                    " draw_box" ))
470
471   (define (stop-line)
472       "}\nstop_line\n")
473
474   (define (text s)
475     (string-append "(" s ") show  "))
476
477
478   (define (volta w thick last)
479     (string-append 
480      (numbers->string (list w thick (inexact->exact last)))
481      " draw_volta"))
482
483   (define (tuplet ht dx dy thick dir)
484     (string-append 
485      (numbers->string (list ht dx dy thick (inexact->exact dir)))
486      " draw_tuplet"))
487
488
489   (define (unknown) 
490     "\n unknown\n")
491
492
493   ;; PS
494   (cond ((eq? action-name 'all-definitions)
495          `(begin
496             (define beam ,beam)
497             (define tuplet ,tuplet)
498             (define bracket ,bracket)
499             (define char ,char)
500             (define crescendo ,crescendo)
501             (define volta ,volta)
502             (define bezier-sandwich ,bezier-sandwich)
503             (define dashed-slur ,dashed-slur) 
504             (define decrescendo ,decrescendo) 
505             (define end-output ,end-output)
506             (define experimental-on ,experimental-on)
507             (define filledbox ,filledbox)
508             (define font-def ,font-def)
509             (define font-switch ,font-switch)
510             (define generalmeter ,generalmeter)
511             (define pianobrace ,pianobrace)
512             (define header-end ,header-end)
513             (define lily-def ,lily-def)
514             (define header ,header) 
515             (define invoke-char ,invoke-char) 
516             (define invoke-dim1 ,invoke-dim1)
517             (define placebox ,placebox)
518             (define rulesym ,rulesym)
519             (define select-font ,select-font)
520             (define start-line ,start-line)
521             (define stem ,stem)
522             (define stop-line ,stop-line)
523             (define text ,text)
524             ))
525         ((eq? action-name 'tuplet) tuplet)
526         ((eq? action-name 'beam) beam)
527         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
528         ((eq? action-name 'bracket) bracket)
529         ((eq? action-name 'char) char)
530         ((eq? action-name 'crescendo) crescendo)
531         ((eq? action-name 'dashed-slur) dashed-slur) 
532         ((eq? action-name 'decrescendo) decrescendo)
533         ((eq? action-name 'experimental-on) experimental-on)
534         ((eq? action-name 'filledbox) filledbox)
535         ((eq? action-name 'select-font) select-font)
536         ((eq? action-name 'volta) volta)
537         (else (error "unknown tag -- PS-SCM " action-name))
538         )
539   )
540
541                                         ;
542 ; Russ McManus, <mcmanus@IDT.NET>  
543
544 ; I use the following, which should definitely be provided somewhere
545 ; in guile, but isn't, AFAIK:
546
547
548
549 (define (hash-table-for-each fn ht)
550   (do ((i 0 (+ 1 i)))
551       ((= i (vector-length ht)))
552     (do ((alist (vector-ref ht i) (cdr alist)))
553         ((null? alist) #t)
554       (fn (car (car alist)) (cdr (car alist))))))
555
556 (define (hash-table-map fn ht)
557   (do ((i 0 (+ 1 i))
558        (ret-ls '()))
559       ((= i (vector-length ht)) (reverse ret-ls))
560     (do ((alist (vector-ref ht i) (cdr alist)))
561         ((null? alist) #t)
562       (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
563