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