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