]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
release: 1.3.50
[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 ;
9 ; This file contains various routines in Scheme that are easier to 
10 ; do here than in C++.  At present it is an unorganised mess. Sorry. 
11 ;
12
13
14 ;(debug-enable 'backtrace)
15
16 ;;; library funtions
17
18 (use-modules (ice-9 regex))
19
20 ;; The regex module may not be available, or may be broken.
21 ;; If you have trouble with regex, define #f
22 ;;(define use-regex #t)
23 ;;(define use-regex #f)
24
25 (define use-regex
26   (let ((os (string-downcase (vector-ref (uname) 0))))
27     (not (equal? "cygwin" (substring os 0 (min 6 (string-length os)))))))
28
29 ;; do nothing in .scm output
30 (define (comment s) "")
31
32 (define (mm-to-pt x)
33   (* (/ 72.27 25.40) x)
34   )
35
36 (define (cons-map f x)
37   (cons (f (car x)) (f (cdr x))))
38
39 (define (reduce operator list)
40       (if (null? (cdr list)) (car list)
41           (operator (car list) (reduce operator (cdr list)))
42           )
43       )
44
45
46 (define (numbers->string l)
47   (apply string-append (map ly-number->string l)))
48
49 ; (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
50
51 (define (number->octal-string x)
52   (let* ((n (inexact->exact x))
53          (n64 (quotient n 64))
54          (n8 (quotient (- n (* n64 64)) 8)))
55     (string-append
56      (number->string n64)
57      (number->string n8)
58      (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
59
60 (define (inexact->string x radix)
61   (let ((n (inexact->exact x)))
62     (number->string n radix)))
63
64
65 (define (control->string c)
66   (string-append (number->string (car c)) " "
67                  (number->string (cdr c)) " "))
68
69
70 (define (font i)
71   (string-append
72    "font"
73    (make-string 1 (integer->char (+ (char->integer #\A) i)))
74    ))
75
76
77
78 (define (scm-scm action-name)
79   1)
80
81 (define security-paranoia #f)
82
83
84 ;; See documentation of Item::visibility_lambda_
85 (define (begin-of-line-visible d) (if (= d 1) '(#f . #f) '(#t . #t)))
86 (define (spanbar-begin-of-line-invisible d) (if (= d -1) '(#t . #t) '(#f . #f)))
87 (define (all-visible d) '(#f . #f))
88 (define (all-invisible d) '(#t . #t))
89 (define (begin-of-line-invisible d) (if (= d 1) '(#t . #t) '(#f . #f)))
90 (define (end-of-line-invisible d) (if (= d -1) '(#t . #t) '(#f . #f)))
91
92
93 (define mark-visibility end-of-line-invisible)
94
95 ;; Spacing constants for prefatory matter.
96 ;;
97 ;; rules for this spacing are much more complicated than this. See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147
98 ;;
99 ;;
100
101 ;; (Measured in staff space)
102 (define space-alist
103  '(
104    ((none Instrument_name) . (extra-space 1.0))
105    ((Instrument_name Left_edge_item) . (extra-space 1.0))
106    ((Left_edge_item Clef_item) . (extra-space 1.0))
107    ((none Left_edge_item) . (extra-space 0.0))
108    ((Left_edge_item Staff_bar) . (extra-space 0.0))
109 ;   ((none Left_edge_item) . (extra-space -15.0))
110 ;   ((none Left_edge_item) . (extra-space -15.0))
111    ((none Clef_item) . (minimum-space 1.0))
112    ((none Staff_bar) . (minimum-space 0.0))
113    ((none Clef_item) . (minimum-space 1.0))
114    ((none Key_item) . (minimum-space 0.5))
115    ((none Time_signature) . (extra-space 0.0))
116    ((none begin-of-note) . (minimum-space 1.5))
117    ((Clef_item Key_item) . (minimum-space 4.0))
118    ((Key_item Time_signature) . (extra-space 1.0))
119    ((Clef_item  Time_signature) . (minimum-space 3.5))
120    ((Staff_bar Clef_item) .   (minimum-space 1.0))
121    ((Clef_item  Staff_bar) .  (minimum-space 3.7))
122    ((Time_signature Staff_bar) .  (minimum-space 2.0))
123    ((Key_item  Staff_bar) .  (extra-space 1.0))
124    ((Staff_bar Time_signature) . (minimum-space 1.5)) ;double check this.
125    ((Time_signature begin-of-note) . (extra-space 2.0)) ;double check this.
126    ((Key_item begin-of-note) . (extra-space 2.5))
127    ((Staff_bar begin-of-note) . (extra-space 1.0))
128    ((Clef_item begin-of-note) . (minimum-space 5.0))
129    ((none Breathing_sign) . (minimum-space 0.0))
130    ((Breathing_sign Key_item) . (minimum-space 1.5))
131    ((Breathing_sign begin-of-note) . (minimum-space 1.0))
132    ((Breathing_sign Staff_bar) . (minimum-space 1.5))
133    ((Breathing_sign Clef_item) . (minimum-space 2.0))
134    )
135 )
136
137 ;; (define (ly-warn s) (error s))
138 (define (break-align-spacer this next)
139   (let ((entry (assoc (list this next) space-alist)))
140     (if entry
141         (cdr entry)
142         (begin (ly-warn (string-append "Unknown spacing pair `" this "', `" next "'"))
143                '(minimum-space 0.0)))))
144   
145
146 (define (find-notehead-symbol duration style)
147   (case style
148    ((cross) "2cross")
149    ((harmonic) "0mensural")
150    ((baroque) 
151     (string-append (number->string duration)
152                    (if (< duration 0) "mensural" "")))
153    ((default) (number->string duration))
154    (else
155     (string-append (number->string duration) (symbol->string style)))))
156
157
158 ;;;;;;;; TeX
159
160 ;; this is silly, can't we use something like
161 ;; roman-0, roman-1 roman+1 ?
162 (define cmr-alist 
163   '(("bold" . "cmbx") 
164     ("brace" . "feta-braces")
165     ("default" . "cmr10")
166     ("dynamic" . "feta-din") 
167     ("feta" . "feta") 
168     ("feta-1" . "feta") 
169     ("feta-2" . "feta") 
170     ("typewriter" . "cmtt") 
171     ("italic" . "cmti") 
172     ("roman" . "cmr") 
173     ("script" . "cmr") 
174     ("large" . "cmbx") 
175     ("Large" . "cmbx") 
176     ("mark" . "feta-nummer") 
177     ("finger" . "feta-nummer")
178     ("timesig" . "feta-nummer")
179     ("number" . "feta-nummer") 
180     ("volta" . "feta-nummer"))
181 )
182
183 (define (string-encode-integer i)
184   (cond
185    ((= i  0) "o")
186    ((< i 0)   (string-append "n" (string-encode-integer (- i))))
187    (else (string-append
188           (make-string 1 (integer->char (+ 65 (modulo i 26))))
189           (string-encode-integer (quotient i 26))
190          )
191    )
192   )
193   )
194
195 (define (magstep i)
196   (cdr (assoc i '((-4 . 482)
197                   (-3 . 579)
198                   (-2 . 694)
199                   (-1 . 833)
200                   (0 . 1000)
201                   (1 . 1200) 
202                   (2 . 1440)
203                   (3 . 1728)
204                   (4 . 2074))
205               )
206        )
207   )
208              
209 (define script-alist '())
210 (define (articulation-to-scriptdef a)
211   (assoc a script-alist)
212   )
213
214 ;; Map style names to TeX font names.  Return false if 
215 ;; no font name found. 
216 (define (style-to-cmr s)
217   (assoc s cmr-alist )
218   )
219             
220
221
222 (define font-name-alist  '())
223 (define (font-command name-mag)
224     (cons name-mag
225           (string-append  "magfont"
226                           (string-encode-integer (hashq (car name-mag) 1000000))
227                           "m"
228                           (string-encode-integer (cdr name-mag)))
229
230           )
231     )
232 (define (define-fonts names)
233   (set! font-name-alist (map font-command names))
234   (apply string-append
235          (map (lambda (x)
236                 (font-load-command (car x) (cdr x))) font-name-alist)
237   ))
238
239 (define (fontify name exp)
240   (string-append (select-font name)
241                  exp)
242   )
243
244 ;;;;;;;;;;;;;;;;;;;;
245
246
247 ; Make a function that checks score element for being of a specific type. 
248 (define (make-type-checker name)
249   (lambda (elt)
250     (not (not (memq name (ly-get-elt-property elt 'interfaces))))))
251
252         
253 ;;;;;;;;;;;;;;;;;;; TeX output
254 (define (tex-scm action-name)
255   (define (unknown) 
256     "%\n\\unknown%\n")
257
258
259   (define (select-font font-name-symbol)
260     (let*
261         (
262          (c (assoc font-name-symbol font-name-alist))
263          )
264
265       (if (eq? c #f)
266           (begin
267             (ly-warn (string-append
268                       "Programming error: No such font known " (car font-name-symbol)))
269             "")                         ; issue no command
270           (string-append "\\" (cdr c)))
271       
272       
273       ))
274   
275   (define (beam width slope thick)
276     (embedded-ps ((ps-scm 'beam) width slope thick)))
277
278   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
279     (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
280
281   (define (dashed-slur thick dash l)
282     (embedded-ps ((ps-scm 'dashed-slur)  thick dash l)))
283
284   (define (crescendo thick w h cont)
285     (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
286
287   (define (char i)
288     (string-append "\\char" (inexact->string i 10) " "))
289   
290   (define (dashed-line thick dash w)
291     (embedded-ps ((ps-scm 'dashed-line) thick dash w)))
292
293   (define (decrescendo thick w h cont)
294     (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
295
296   (define (font-load-command name-mag command)
297     (string-append
298      "\\font\\" command "="
299      (symbol->string (car name-mag))
300      " scaled "
301      (number->string (magstep (cdr name-mag)))
302      "\n"))
303
304   (define (embedded-ps s)
305     (string-append "\\embeddedps{" s "}"))
306
307   (define (comment s)
308     (string-append "% " s))
309   
310   (define (end-output) 
311     "\n\\EndLilyPondOutput")
312   
313   (define (experimental-on)
314     "")
315
316   (define (font-switch i)
317     (string-append
318      "\\" (font i) "\n"))
319
320   (define (font-def i s)
321     (string-append
322      "\\font" (font-switch i) "=" s "\n"))
323
324   (define (header-end)
325     (string-append
326      "\\special{! "
327
328      ;; URG: ly-gulp-file: now we can't use scm output without Lily
329      (if use-regex
330          ;; fixed in 1.3.4 for powerpc -- broken on Windows
331          (regexp-substitute/global #f "\n"
332                                    (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
333          (ly-gulp-file "lily.ps"))
334      "}"
335      "\\input lilyponddefs \\turnOnPostScript"))
336
337   (define (header creator generate) 
338     (string-append
339      "%created by: " creator generate "\n"))
340
341   (define (invoke-char s i)
342     (string-append 
343      "\n\\" s "{" (inexact->string i 10) "}" ))
344
345   (define (invoke-dim1 s d)
346     (string-append
347      "\n\\" s "{" (number->dim d) "}"))
348   (define (pt->sp x)
349     (* 65536 x))
350   
351   ;;
352   ;; need to do something to make this really safe.
353   ;;
354   (define (output-tex-string s)
355       (if security-paranoia
356           (if use-regex
357               (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
358               (begin (display "warning: not paranoid") (newline) s))
359           s))
360       
361   (define (lily-def key val)
362     (string-append
363      "\\def\\"
364      (if use-regex
365          ;; fixed in 1.3.4 for powerpc -- broken on Windows
366          (regexp-substitute/global #f "_"
367                                    (output-tex-string key) 'pre "X" 'post)
368          (output-tex-string key))
369      "{" (output-tex-string val) "}\n"))
370
371   (define (number->dim x)
372     (string-append 
373      (ly-number->string x) " pt "))
374
375   (define (placebox x y s) 
376     (string-append 
377      "\\placebox{"
378      (number->dim y) "}{" (number->dim x) "}{" s "}\n"))
379
380   (define (bezier-sandwich l thick)
381     (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
382
383   (define (start-line ht)
384       (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
385
386   (define (stop-line) 
387     "}\\vss}\\interscoreline")
388   (define (stop-last-line)
389     "}\\vss}")
390   (define (filledbox breapth width depth height) 
391     (string-append 
392      "\\kern" (number->dim (- breapth))
393      "\\vrule width " (number->dim (+ breapth width))
394      "depth " (number->dim depth)
395      "height " (number->dim height) " "))
396
397   (define (text s)
398     (string-append "\\hbox{" (output-tex-string s) "}"))
399   
400   (define (tuplet ht gapx dx dy thick dir)
401     (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
402
403   (define (volta h w thick vert_start vert_end)
404     (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
405
406   ;; TeX
407   ;; The procedures listed below form the public interface of TeX-scm.
408   ;; (should merge the 2 lists)
409   (cond ((eq? action-name 'all-definitions)
410          `(begin
411             (define font-load-command ,font-load-command)
412             (define beam ,beam)
413             (define bezier-sandwich ,bezier-sandwich)
414             (define bracket ,bracket)
415             (define char ,char)
416             (define crescendo ,crescendo)
417             (define dashed-line ,dashed-line) 
418             (define dashed-slur ,dashed-slur) 
419             (define decrescendo ,decrescendo) 
420             (define end-output ,end-output)
421             (define experimental-on ,experimental-on)
422             (define filledbox ,filledbox)
423             (define font-def ,font-def)
424             (define font-switch ,font-switch)
425             (define header-end ,header-end)
426             (define lily-def ,lily-def)
427             (define header ,header) 
428             (define invoke-char ,invoke-char) 
429             (define invoke-dim1 ,invoke-dim1)
430             (define placebox ,placebox)
431             (define select-font ,select-font)
432             (define start-line ,start-line)
433             (define stop-line ,stop-line)
434             (define stop-last-line ,stop-last-line)
435             (define text ,text)
436             (define tuplet ,tuplet)
437             (define volta ,volta)
438             ))
439
440         ((eq? action-name 'beam) beam)
441         ((eq? action-name 'tuplet) tuplet)
442         ((eq? action-name 'bracket) bracket)
443         ((eq? action-name 'crescendo) crescendo)
444         ((eq? action-name 'dashed-line) dashed-line) 
445         ((eq? action-name 'dashed-slur) dashed-slur) 
446         ((eq? action-name 'decrescendo) decrescendo) 
447         ((eq? action-name 'end-output) end-output)
448         ((eq? action-name 'experimental-on) experimental-on)
449         ((eq? action-name 'font-def) font-def)
450         ((eq? action-name 'font-switch) font-switch)
451         ((eq? action-name 'header-end) header-end)
452         ((eq? action-name 'lily-def) lily-def)
453         ((eq? action-name 'header) header) 
454         ((eq? action-name 'invoke-char) invoke-char) 
455         ((eq? action-name 'invoke-dim1) invoke-dim1)
456         ((eq? action-name 'placebox) placebox)
457         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
458         ((eq? action-name 'start-line) start-line)
459         ((eq? action-name 'stem) stem)
460         ((eq? action-name 'stop-line) stop-line)
461         ((eq? action-name 'stop-last-line) stop-last-line)
462         ((eq? action-name 'volta) volta)
463         (else (error "unknown tag -- PS-TEX " action-name))
464         )
465   )
466
467
468 ;;;;;;;;;;;; PS
469 (define (ps-scm action-name)
470
471   ;; alist containing fontname -> fontcommand assoc (both strings)
472   (define font-alist '())
473   (define font-count 0)
474   (define current-font "")
475
476   
477   (define (cached-fontname i)
478     (string-append
479      "lilyfont"
480      (make-string 1 (integer->char (+ 65 i)))))
481     
482   (define (mag-to-size m)
483     (number->string (case m 
484                       (0 12)
485                       (1 12)
486                       (2 14) ; really: 14.400
487                       (3 17) ; really: 17.280
488                       (4 21) ; really: 20.736
489                       (5 24) ; really: 24.888
490                       (6 30) ; really: 29.856
491                       )))
492   
493   
494   (define (select-font font-name-symbol)
495     (let*
496         (
497          (c (assoc font-name-symbol font-name-alist))
498          )
499
500       (if (eq? c #f)
501           (begin
502             (ly-warn (string-append
503                       "Programming error: No such font known " (car font-name-symbol)))
504             "")                         ; issue no command
505           (string-append " " (cdr c) " "))
506       
507       
508       ))
509
510     (define (font-load-command name-mag command)
511       (string-append
512        "/" command
513        " { /"
514        (symbol->string (car name-mag))
515        " findfont "
516        (number->string (magstep (cdr name-mag)))
517        " 1000 div 12 mul  scalefont setfont } bind def "
518        "\n"))
519
520
521   (define (beam width slope thick)
522     (string-append
523      (numbers->string (list width slope thick)) " draw_beam" ))
524
525   (define (comment s)
526     (string-append "% " s))
527
528   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
529     (string-append
530      (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
531
532   (define (char i)
533     (invoke-char " show" i))
534
535   (define (crescendo thick w h cont )
536     (string-append 
537      (numbers->string (list w h (inexact->exact cont) thick))
538      " draw_crescendo"))
539
540   ;; what the heck is this interface ?
541   (define (dashed-slur thick dash l)
542     (string-append 
543      (apply string-append (map control->string l)) 
544      (number->string thick) 
545      " [ "
546      (number->string dash)
547      " "
548      (number->string (* 10 thick))      ;UGH.  10 ?
549      " ] 0 draw_dashed_slur"))
550
551   (define (dashed-line thick dash width)
552     (string-append 
553      (number->string width) 
554      " "
555      (number->string thick) 
556      " [ "
557      (number->string dash)
558      " "
559      (number->string dash)
560      " ] 0 draw_dashed_line"))
561
562   (define (decrescendo thick w h cont)
563     (string-append 
564      (numbers->string (list w h (inexact->exact cont) thick))
565      " draw_decrescendo"))
566
567
568   (define (end-output)
569     "\nshowpage\n")
570   
571   (define (experimental-on) "")
572   
573   (define (filledbox breapth width depth height) 
574     (string-append (numbers->string (list breapth width depth height))
575                    " draw_box" ))
576
577   ;; obsolete?
578   (define (font-def i s)
579     (string-append
580      "\n/" (font i) " {/" 
581      (substring s 0 (- (string-length s) 4))
582      " findfont 12 scalefont setfont} bind def \n"))
583
584   (define (font-switch i)
585     (string-append (font i) " "))
586
587   (define (header-end)
588     (string-append
589      ;; URG: now we can't use scm output without Lily
590      (ly-gulp-file "lilyponddefs.ps")
591      " {exch pop //systemdict /run get exec} "
592      (ly-gulp-file "lily.ps")
593      "{ exch pop //systemdict /run get exec } "
594     ))
595   
596   (define (lily-def key val)
597
598      (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
599          (string-append "/" key " {" val "} bind def\n")
600          (string-append "/" key " (" val ") def\n")
601          )
602      )
603
604   (define (header creator generate) 
605     (string-append
606      "%!PS-Adobe-3.0\n"
607      "%%Creator: " creator generate "\n"))
608   
609   (define (invoke-char s i)
610     (string-append 
611      "(\\" (inexact->string i 8) ") " s " " ))
612   
613   (define (invoke-dim1 s d) 
614     (string-append
615      (number->string (* d  (/ 72.27 72))) " " s ))
616
617   (define (placebox x y s) 
618     (string-append 
619      (number->string x) " " (number->string y) " {" s "} placebox "))
620
621   (define (bezier-sandwich l thick)
622     (string-append 
623      (apply string-append (map control->string l))
624      (number->string  thick)
625      " draw_bezier_sandwich"))
626
627   (define (start-line height)
628           "\nstart_line {\n")
629   
630   (define (stem breapth width depth height) 
631     (string-append (numbers->string (list breapth width depth height))
632                    " draw_box" ))
633
634   (define (stop-line)
635       "}\nstop_line\n")
636
637   (define (text s)
638     (string-append "(" s ") show  "))
639
640
641   (define (volta h w thick vert_start vert_end)
642     (string-append 
643      (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
644      " draw_volta"))
645
646   (define (tuplet ht gap dx dy thick dir)
647     (string-append 
648      (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
649      " draw_tuplet"))
650
651
652   (define (unknown) 
653     "\n unknown\n")
654
655
656   ;; PS
657   (cond ((eq? action-name 'all-definitions)
658          `(begin
659             (define beam ,beam)
660             (define tuplet ,tuplet)
661             (define bracket ,bracket)
662             (define char ,char)
663             (define crescendo ,crescendo)
664             (define volta ,volta)
665             (define bezier-sandwich ,bezier-sandwich)
666             (define dashed-line ,dashed-line) 
667             (define dashed-slur ,dashed-slur) 
668             (define decrescendo ,decrescendo) 
669             (define end-output ,end-output)
670             (define experimental-on ,experimental-on)
671             (define filledbox ,filledbox)
672             (define font-def ,font-def)
673             (define font-switch ,font-switch)
674             (define header-end ,header-end)
675             (define lily-def ,lily-def)
676             (define font-load-command ,font-load-command)
677             (define header ,header) 
678             (define invoke-char ,invoke-char) 
679             (define invoke-dim1 ,invoke-dim1)
680             (define placebox ,placebox)
681             (define select-font ,select-font)
682             (define start-line ,start-line)
683             (define stem ,stem)
684             (define stop-line ,stop-line)
685             (define stop-last-line ,stop-line)
686             (define text ,text)
687             ))
688         ((eq? action-name 'tuplet) tuplet)
689         ((eq? action-name 'beam) beam)
690         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
691         ((eq? action-name 'bracket) bracket)
692         ((eq? action-name 'char) char)
693         ((eq? action-name 'crescendo) crescendo)
694         ((eq? action-name 'dashed-line) dashed-line) 
695         ((eq? action-name 'dashed-slur) dashed-slur) 
696         ((eq? action-name 'decrescendo) decrescendo)
697         ((eq? action-name 'experimental-on) experimental-on)
698         ((eq? action-name 'filledbox) filledbox)
699         ((eq? action-name 'select-font) select-font)
700         ((eq? action-name 'volta) volta)
701         (else (error "unknown tag -- PS-SCM " action-name))
702         )
703   )
704
705
706 (define (arg->string arg)
707   (cond ((number? arg) (inexact->string arg 10))
708         ((string? arg) (string-append "\"" arg "\""))
709         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
710
711 (define (func name . args)
712   (string-append 
713    "(" name 
714    (if (null? args) 
715        ""
716        (apply string-append 
717               (map (lambda (x) (string-append " " (arg->string x))) args)))
718    ")\n"))
719
720 (define (sign x)
721   (if (= x 0)
722       1
723       (inexact->exact (/ x (abs x)))))
724
725 ;;;; AsciiScript as
726 (define (as-scm action-name)
727
728   (define (beam width slope thick)
729           (string-append
730            (func "set-line-char" "#")
731            (func "rline-to" width (* width slope))
732            ))
733
734   ; simple flat slurs
735   (define (bezier-sandwich l thick)
736           (let (
737                 (c0 (cadddr l))
738                 (c1 (cadr l))
739                 (c3 (caddr l)))
740                (let* ((x (car c0))
741                       (dx (- (car c3) x))
742                       (dy (- (cdr c3) (cdr c0)))
743                       (rc (/ dy dx))
744                       (c1-dx (- (car c1) x))
745                       (c1-line-y (+ (cdr c0) (* c1-dx rc)))
746                       (dir (if (< c1-line-y (cdr c1)) 1 -1))
747                       (y (+ -1 (* dir (max (* dir (cdr c0)) (* dir (cdr c3)))))))
748                      (string-append
749                       (func "rmove-to" x y)
750                       (func "put" (if (< 0 dir) "/" "\\\\"))
751                       (func "rmove-to" 1 (if (< 0 dir) 1 0))
752                       (func "set-line-char" "_")
753                       (func "h-line" (- dx 1))
754                       (func "rmove-to" (- dx 1) (if (< 0 dir) -1 0))
755                       (func "put" (if (< 0 dir) "\\\\" "/"))))))
756
757   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
758           (string-append
759            (func "rmove-to" (+ width 1) (- (/ height -2) 1))
760            (func "put" "\\\\")
761            (func "set-line-char" "|")
762            (func "rmove-to" 0 1)
763            (func "v-line" (+ height 1))
764            (func "rmove-to" 0 (+ height 1))
765            (func "put" "/")
766            ))
767
768   (define (char i)
769     (func "char" i))
770
771   (define (end-output) 
772     (func "end-output"))
773   
774   (define (experimental-on)
775           "")
776
777   (define (filledbox breapth width depth height)
778           (let ((dx (+ width breapth))
779                 (dy (+ depth height)))
780                (string-append 
781                 (func "rmove-to" (* -1 breapth) (* -1 depth))
782                 (if (< dx dy)
783                     (string-append
784                      (func "set-line-char" 
785                            (if (<= dx 1) "|" "#"))
786                      (func "v-line" dy))
787                     (string-append
788                      (func "set-line-char" 
789                            (if (<= dy 1) "-" "="))
790                     (func "h-line" dx))))))
791
792   (define (font-load-command name-mag command)
793     (func "load-font" (car name-mag) (magstep (cdr name-mag))))
794
795   (define (header creator generate) 
796     (func "header" creator generate))
797
798   (define (header-end) 
799     (func "header-end"))
800
801   ;; urg: this is good for half of as2text's execution time
802   (define (xlily-def key val)
803           (string-append "(define " key " " (arg->string val) ")\n"))
804
805   (define (lily-def key val)
806           (if 
807            (or (equal? key "mudelapaperlinewidth")
808                (equal? key "mudelapaperstaffheight"))
809            (string-append "(define " key " " (arg->string val) ")\n")
810            ""))
811
812   (define (placebox x y s) 
813     (let ((ey (inexact->exact y)))
814           (string-append "(move-to " (number->string (inexact->exact x)) " "
815                          (if (= 0.5 (- (abs y) (abs ey)))
816                              (number->string y)
817                              (number->string ey))
818                          ")\n" s)))
819                        
820   (define (select-font font-name-symbol)
821     (let* ((c (assoc font-name-symbol font-name-alist)))
822       (if (eq? c #f)
823           (begin
824             (ly-warn 
825              (string-append 
826               "Programming error: No such font known " 
827               (car font-name-symbol)))
828             "")                         ; issue no command
829           (func "select-font" (car font-name-symbol)))))
830
831   (define (start-line height)
832           (func "start-line" height))
833
834   (define (stop-line)
835           (func "stop-line"))
836
837   (define (text s)
838           (func "text" s))
839
840   (define (volta h w thick vert-start vert-end)
841           ;; urg
842           (string-append
843            (func "set-line-char" "|")
844            (func "rmove-to" 0 -4)
845            ;; definition strange-way around
846            (if (= 0 vert-start)
847               (func "v-line" h)
848                "")
849            (func "rmove-to" 1 h)
850            (func "set-line-char" "_")
851            (func "h-line" (- w 1))
852            (func "set-line-char" "|")
853            (if (= 0 vert-end)
854                (string-append
855                 (func "rmove-to" (- w 1) (* -1 h))
856                 (func "v-line" (* -1 h)))
857                "")))
858
859   (cond ((eq? action-name 'all-definitions)
860          `(begin
861             (define beam ,beam)
862             (define bracket ,bracket)
863             (define char ,char)
864             ;;(define crescendo ,crescendo)
865             (define bezier-sandwich ,bezier-sandwich)
866             ;;(define dashed-slur ,dashed-slur) 
867             ;;(define decrescendo ,decrescendo) 
868             (define end-output ,end-output)
869             (define experimental-on ,experimental-on)
870             (define filledbox ,filledbox)
871             ;;(define font-def ,font-def)
872             (define font-load-command ,font-load-command)
873             ;;(define font-switch ,font-switch)
874             (define header ,header) 
875             (define header-end ,header-end)
876             (define lily-def ,lily-def)
877             ;;(define invoke-char ,invoke-char) 
878             ;;(define invoke-dim1 ,invoke-dim1)
879             (define placebox ,placebox)
880             (define select-font ,select-font)
881             (define start-line ,start-line)
882             ;;(define stem ,stem)
883             (define stop-line ,stop-line)
884             (define stop-last-line ,stop-line)
885             (define text ,text)
886             ;;(define tuplet ,tuplet)
887             (define volta ,volta)
888             ))
889         ;;((eq? action-name 'tuplet) tuplet)
890         ;;((eq? action-name 'beam) beam)
891         ;;((eq? action-name 'bezier-sandwich) bezier-sandwich)
892         ;;((eq? action-name 'bracket) bracket)
893         ((eq? action-name 'char) char)
894         ;;((eq? action-name 'crescendo) crescendo)
895         ;;((eq? action-name 'dashed-slur) dashed-slur) 
896         ;;((eq? action-name 'decrescendo) decrescendo)
897         ;;((eq? action-name 'experimental-on) experimental-on)
898         ((eq? action-name 'filledbox) filledbox)
899         ((eq? action-name 'select-font) select-font)
900         ;;((eq? action-name 'volta) volta)
901         (else (error "unknown tag -- MUSA-SCM " action-name))
902         )
903   )
904
905
906 (define (gulp-file name)
907   (let* ((port (open-file name "r"))
908          (content (let loop ((text ""))
909                        (let ((line (read-line port)))
910                             (if (or (eof-object? line)
911                                     (not line)) 
912                                 text
913                                 (loop (string-append text line "\n")))))))
914         (close port)
915         content))
916
917 ;; urg: Use when standalone, do:
918 ;; (define (ly-gulp-file name) (scm-gulp-file name))
919 (define (scm-gulp-file name)
920   (set! %load-path 
921         (cons (string-append (getenv 'LILYPONDPREFIX) "/ly")
922               (cons (string-append (getenv 'LILYPONDPREFIX) "/ps")
923                     %load-path)))
924   (let ((path (%search-load-path name)))
925        (if path
926            (gulp-file path)
927            (gulp-file name))))
928
929 (define (scm-tex-output)
930   (eval (tex-scm 'all-definitions)))
931                                 
932 (define (scm-ps-output)
933   (eval (ps-scm 'all-definitions)))
934
935 (define (scm-as-output)
936   (eval (as-scm 'all-definitions)))
937         
938 (define (index-cell cell dir)
939   (if (equal? dir 1)
940       (cdr cell)
941       (car cell)))
942
943 ;
944 ; How should a  bar line behave at a break? 
945 ;
946 (define (break-barline glyph dir)
947    (let ((result (assoc glyph 
948                         '((":|:" . (":|" . "|:"))
949                           ("|" . ("|" . ""))
950                           ("|s" . (nil . "|"))
951                           ("|:" . ("|" . "|:"))
952                           ("|." . ("|." . nil))
953                           (":|" . (":|" . nil))
954                           ("||" . ("||" . nil))
955                           (".|." . (".|." . nil))
956                           ("scorebar" . (nil . "scorepostbreak"))
957                           ("brace" . (nil . "brace"))
958                           ("bracket" . (nil . "bracket"))  
959                           )
960                         )))
961
962      (if (equal? result #f)
963          (ly-warn (string-append "Unknown bar glyph: `" glyph "'"))
964          (index-cell (cdr result) dir))
965      )
966    )
967