]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
release: 1.3.53
[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     "\n\\EndLilyPondOutput")
314   
315   (define (experimental-on)
316     "")
317
318   (define (font-switch i)
319     (string-append
320      "\\" (font i) "\n"))
321
322   (define (font-def i s)
323     (string-append
324      "\\font" (font-switch i) "=" s "\n"))
325
326   (define (header-end)
327     (string-append
328      "\\special{! "
329
330      ;; URG: ly-gulp-file: now we can't use scm output without Lily
331      (if use-regex
332          ;; fixed in 1.3.4 for powerpc -- broken on Windows
333          (regexp-substitute/global #f "\n"
334                                    (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
335          (ly-gulp-file "lily.ps"))
336      "}"
337      "\\input lilyponddefs \\turnOnPostScript"))
338
339   (define (header creator generate) 
340     (string-append
341      "%created by: " creator generate "\n"))
342
343   (define (invoke-char s i)
344     (string-append 
345      "\n\\" s "{" (inexact->string i 10) "}" ))
346
347   (define (invoke-dim1 s d)
348     (string-append
349      "\n\\" s "{" (number->dim d) "}"))
350   (define (pt->sp x)
351     (* 65536 x))
352   
353   ;;
354   ;; need to do something to make this really safe.
355   ;;
356   (define (output-tex-string s)
357       (if security-paranoia
358           (if use-regex
359               (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
360               (begin (display "warning: not paranoid") (newline) s))
361           s))
362       
363   (define (lily-def key val)
364     (string-append
365      "\\def\\"
366      (if use-regex
367          ;; fixed in 1.3.4 for powerpc -- broken on Windows
368          (regexp-substitute/global #f "_"
369                                    (output-tex-string key) 'pre "X" 'post)
370          (output-tex-string key))
371      "{" (output-tex-string val) "}\n"))
372
373   (define (number->dim x)
374     (string-append 
375      (ly-number->string x) " pt "))
376
377   (define (placebox x y s) 
378     (string-append 
379      "\\placebox{"
380      (number->dim y) "}{" (number->dim x) "}{" s "}\n"))
381
382   (define (bezier-sandwich l thick)
383     (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
384
385   (define (start-line ht)
386       (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
387
388   (define (stop-line) 
389     "}\\vss}\\interscoreline")
390   (define (stop-last-line)
391     "}\\vss}")
392   (define (filledbox breapth width depth height) 
393     (string-append 
394      "\\kern" (number->dim (- breapth))
395      "\\vrule width " (number->dim (+ breapth width))
396      "depth " (number->dim depth)
397      "height " (number->dim height) " "))
398
399   (define (text s)
400     (string-append "\\hbox{" (output-tex-string s) "}"))
401   
402   (define (tuplet ht gapx dx dy thick dir)
403     (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
404
405   (define (volta h w thick vert_start vert_end)
406     (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
407
408   ;; TeX
409   ;; The procedures listed below form the public interface of TeX-scm.
410   ;; (should merge the 2 lists)
411   (cond ((eq? action-name 'all-definitions)
412          `(begin
413             (define font-load-command ,font-load-command)
414             (define beam ,beam)
415             (define bezier-sandwich ,bezier-sandwich)
416             (define bracket ,bracket)
417             (define char ,char)
418             (define crescendo ,crescendo)
419             (define dashed-line ,dashed-line) 
420             (define dashed-slur ,dashed-slur) 
421             (define decrescendo ,decrescendo) 
422             (define end-output ,end-output)
423             (define experimental-on ,experimental-on)
424             (define filledbox ,filledbox)
425             (define font-def ,font-def)
426             (define font-switch ,font-switch)
427             (define header-end ,header-end)
428             (define lily-def ,lily-def)
429             (define header ,header) 
430             (define invoke-char ,invoke-char) 
431             (define invoke-dim1 ,invoke-dim1)
432             (define placebox ,placebox)
433             (define select-font ,select-font)
434             (define start-line ,start-line)
435             (define stop-line ,stop-line)
436             (define stop-last-line ,stop-last-line)
437             (define text ,text)
438             (define tuplet ,tuplet)
439             (define volta ,volta)
440             ))
441
442         ((eq? action-name 'beam) beam)
443         ((eq? action-name 'tuplet) tuplet)
444         ((eq? action-name 'bracket) bracket)
445         ((eq? action-name 'crescendo) crescendo)
446         ((eq? action-name 'dashed-line) dashed-line) 
447         ((eq? action-name 'dashed-slur) dashed-slur) 
448         ((eq? action-name 'decrescendo) decrescendo) 
449         ((eq? action-name 'end-output) end-output)
450         ((eq? action-name 'experimental-on) experimental-on)
451         ((eq? action-name 'font-def) font-def)
452         ((eq? action-name 'font-switch) font-switch)
453         ((eq? action-name 'header-end) header-end)
454         ((eq? action-name 'lily-def) lily-def)
455         ((eq? action-name 'header) header) 
456         ((eq? action-name 'invoke-char) invoke-char) 
457         ((eq? action-name 'invoke-dim1) invoke-dim1)
458         ((eq? action-name 'placebox) placebox)
459         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
460         ((eq? action-name 'start-line) start-line)
461         ((eq? action-name 'stem) stem)
462         ((eq? action-name 'stop-line) stop-line)
463         ((eq? action-name 'stop-last-line) stop-last-line)
464         ((eq? action-name 'volta) volta)
465         (else (error "unknown tag -- PS-TEX " action-name))
466         )
467   )
468
469
470 ;;;;;;;;;;;; PS
471 (define (ps-scm action-name)
472
473   ;; alist containing fontname -> fontcommand assoc (both strings)
474   (define font-alist '())
475   (define font-count 0)
476   (define current-font "")
477
478   
479   (define (cached-fontname i)
480     (string-append
481      "lilyfont"
482      (make-string 1 (integer->char (+ 65 i)))))
483     
484   (define (mag-to-size m)
485     (number->string (case m 
486                       (0 12)
487                       (1 12)
488                       (2 14) ; really: 14.400
489                       (3 17) ; really: 17.280
490                       (4 21) ; really: 20.736
491                       (5 24) ; really: 24.888
492                       (6 30) ; really: 29.856
493                       )))
494   
495   
496   (define (select-font font-name-symbol)
497     (let*
498         (
499          (c (assoc font-name-symbol font-name-alist))
500          )
501
502       (if (eq? c #f)
503           (begin
504             (ly-warn (string-append
505                       "Programming error: No such font known " (car font-name-symbol)))
506             "")                         ; issue no command
507           (string-append " " (cdr c) " "))
508       
509       
510       ))
511
512     (define (font-load-command name-mag command)
513       (string-append
514        "/" command
515        " { /"
516        (symbol->string (car name-mag))
517        " findfont "
518        (number->string (magstep (cdr name-mag)))
519        " 1000 div 12 mul  scalefont setfont } bind def "
520        "\n"))
521
522
523   (define (beam width slope thick)
524     (string-append
525      (numbers->string (list width slope thick)) " draw_beam" ))
526
527   (define (comment s)
528     (string-append "% " s))
529
530   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
531     (string-append
532      (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
533
534   (define (char i)
535     (invoke-char " show" i))
536
537   (define (crescendo thick w h cont )
538     (string-append 
539      (numbers->string (list w h (inexact->exact cont) thick))
540      " draw_crescendo"))
541
542   ;; what the heck is this interface ?
543   (define (dashed-slur thick dash l)
544     (string-append 
545      (apply string-append (map control->string l)) 
546      (number->string thick) 
547      " [ "
548      (number->string dash)
549      " "
550      (number->string (* 10 thick))      ;UGH.  10 ?
551      " ] 0 draw_dashed_slur"))
552
553   (define (dashed-line thick dash width)
554     (string-append 
555      (number->string width) 
556      " "
557      (number->string thick) 
558      " [ "
559      (number->string dash)
560      " "
561      (number->string dash)
562      " ] 0 draw_dashed_line"))
563
564   (define (decrescendo thick w h cont)
565     (string-append 
566      (numbers->string (list w h (inexact->exact cont) thick))
567      " draw_decrescendo"))
568
569
570   (define (end-output)
571     "\nshowpage\n")
572   
573   (define (experimental-on) "")
574   
575   (define (filledbox breapth width depth height) 
576     (string-append (numbers->string (list breapth width depth height))
577                    " draw_box" ))
578
579   ;; obsolete?
580   (define (font-def i s)
581     (string-append
582      "\n/" (font i) " {/" 
583      (substring s 0 (- (string-length s) 4))
584      " findfont 12 scalefont setfont} bind def \n"))
585
586   (define (font-switch i)
587     (string-append (font i) " "))
588
589   (define (header-end)
590     (string-append
591      ;; URG: now we can't use scm output without Lily
592      (ly-gulp-file "lilyponddefs.ps")
593      " {exch pop //systemdict /run get exec} "
594      (ly-gulp-file "lily.ps")
595      "{ exch pop //systemdict /run get exec } "
596     ))
597   
598   (define (lily-def key val)
599
600      (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
601          (string-append "/" key " {" val "} bind def\n")
602          (string-append "/" key " (" val ") def\n")
603          )
604      )
605
606   (define (header creator generate) 
607     (string-append
608      "%!PS-Adobe-3.0\n"
609      "%%Creator: " creator generate "\n"))
610   
611   (define (invoke-char s i)
612     (string-append 
613      "(\\" (inexact->string i 8) ") " s " " ))
614   
615   (define (invoke-dim1 s d) 
616     (string-append
617      (number->string (* d  (/ 72.27 72))) " " s ))
618
619   (define (placebox x y s) 
620     (string-append 
621      (number->string x) " " (number->string y) " {" s "} placebox "))
622
623   (define (bezier-sandwich l thick)
624     (string-append 
625      (apply string-append (map control->string l))
626      (number->string  thick)
627      " draw_bezier_sandwich"))
628
629   (define (start-line height)
630           "\nstart_line {\n")
631   
632   (define (stem breapth width depth height) 
633     (string-append (numbers->string (list breapth width depth height))
634                    " draw_box" ))
635
636   (define (stop-line)
637       "}\nstop_line\n")
638
639   (define (text s)
640     (string-append "(" s ") show  "))
641
642
643   (define (volta h w thick vert_start vert_end)
644     (string-append 
645      (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
646      " draw_volta"))
647
648   (define (tuplet ht gap dx dy thick dir)
649     (string-append 
650      (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
651      " draw_tuplet"))
652
653
654   (define (unknown) 
655     "\n unknown\n")
656
657
658   ;; PS
659   (cond ((eq? action-name 'all-definitions)
660          `(begin
661             (define beam ,beam)
662             (define tuplet ,tuplet)
663             (define bracket ,bracket)
664             (define char ,char)
665             (define crescendo ,crescendo)
666             (define volta ,volta)
667             (define bezier-sandwich ,bezier-sandwich)
668             (define dashed-line ,dashed-line) 
669             (define dashed-slur ,dashed-slur) 
670             (define decrescendo ,decrescendo) 
671             (define end-output ,end-output)
672             (define experimental-on ,experimental-on)
673             (define filledbox ,filledbox)
674             (define font-def ,font-def)
675             (define font-switch ,font-switch)
676             (define header-end ,header-end)
677             (define lily-def ,lily-def)
678             (define font-load-command ,font-load-command)
679             (define header ,header) 
680             (define invoke-char ,invoke-char) 
681             (define invoke-dim1 ,invoke-dim1)
682             (define placebox ,placebox)
683             (define select-font ,select-font)
684             (define start-line ,start-line)
685             (define stem ,stem)
686             (define stop-line ,stop-line)
687             (define stop-last-line ,stop-line)
688             (define text ,text)
689             ))
690         ((eq? action-name 'tuplet) tuplet)
691         ((eq? action-name 'beam) beam)
692         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
693         ((eq? action-name 'bracket) bracket)
694         ((eq? action-name 'char) char)
695         ((eq? action-name 'crescendo) crescendo)
696         ((eq? action-name 'dashed-line) dashed-line) 
697         ((eq? action-name 'dashed-slur) dashed-slur) 
698         ((eq? action-name 'decrescendo) decrescendo)
699         ((eq? action-name 'experimental-on) experimental-on)
700         ((eq? action-name 'filledbox) filledbox)
701         ((eq? action-name 'select-font) select-font)
702         ((eq? action-name 'volta) volta)
703         (else (error "unknown tag -- PS-SCM " action-name))
704         )
705   )
706
707
708 (define (arg->string arg)
709   (cond ((number? arg) (inexact->string arg 10))
710         ((string? arg) (string-append "\"" arg "\""))
711         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
712
713 (define (func name . args)
714   (string-append 
715    "(" name 
716    (if (null? args) 
717        ""
718        (apply string-append 
719               (map (lambda (x) (string-append " " (arg->string x))) args)))
720    ")\n"))
721
722 (define (sign x)
723   (if (= x 0)
724       1
725       (inexact->exact (/ x (abs x)))))
726
727 ;;;; AsciiScript as
728 (define (as-scm action-name)
729
730   (define (beam width slope thick)
731           (string-append
732            (func "set-line-char" "#")
733            (func "rline-to" width (* width slope))
734            ))
735
736   ; simple flat slurs
737   (define (bezier-sandwich l thick)
738           (let (
739                 (c0 (cadddr l))
740                 (c1 (cadr l))
741                 (c3 (caddr l)))
742                (let* ((x (car c0))
743                       (dx (- (car c3) x))
744                       (dy (- (cdr c3) (cdr c0)))
745                       (rc (/ dy dx))
746                       (c1-dx (- (car c1) x))
747                       (c1-line-y (+ (cdr c0) (* c1-dx rc)))
748                       (dir (if (< c1-line-y (cdr c1)) 1 -1))
749                       (y (+ -1 (* dir (max (* dir (cdr c0)) (* dir (cdr c3)))))))
750                      (string-append
751                       (func "rmove-to" x y)
752                       (func "put" (if (< 0 dir) "/" "\\\\"))
753                       (func "rmove-to" 1 (if (< 0 dir) 1 0))
754                       (func "set-line-char" "_")
755                       (func "h-line" (- dx 1))
756                       (func "rmove-to" (- dx 1) (if (< 0 dir) -1 0))
757                       (func "put" (if (< 0 dir) "\\\\" "/"))))))
758
759   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
760           (string-append
761            (func "rmove-to" (+ width 1) (- (/ height -2) 1))
762            (func "put" "\\\\")
763            (func "set-line-char" "|")
764            (func "rmove-to" 0 1)
765            (func "v-line" (+ height 1))
766            (func "rmove-to" 0 (+ height 1))
767            (func "put" "/")
768            ))
769
770   (define (char i)
771     (func "char" i))
772
773   (define (end-output) 
774     (func "end-output"))
775   
776   (define (experimental-on)
777           "")
778
779   (define (filledbox breapth width depth height)
780           (let ((dx (+ width breapth))
781                 (dy (+ depth height)))
782                (string-append 
783                 (func "rmove-to" (* -1 breapth) (* -1 depth))
784                 (if (< dx dy)
785                     (string-append
786                      (func "set-line-char" 
787                            (if (<= dx 1) "|" "#"))
788                      (func "v-line" dy))
789                     (string-append
790                      (func "set-line-char" 
791                            (if (<= dy 1) "-" "="))
792                     (func "h-line" dx))))))
793
794   (define (font-load-command name-mag command)
795     (func "load-font" (car name-mag) (magstep (cdr name-mag))))
796
797   (define (header creator generate) 
798     (func "header" creator generate))
799
800   (define (header-end) 
801     (func "header-end"))
802
803   ;; urg: this is good for half of as2text's execution time
804   (define (xlily-def key val)
805           (string-append "(define " key " " (arg->string val) ")\n"))
806
807   (define (lily-def key val)
808           (if 
809            (or (equal? key "mudelapaperlinewidth")
810                (equal? key "mudelapaperstaffheight"))
811            (string-append "(define " key " " (arg->string val) ")\n")
812            ""))
813
814   (define (placebox x y s) 
815     (let ((ey (inexact->exact y)))
816           (string-append "(move-to " (number->string (inexact->exact x)) " "
817                          (if (= 0.5 (- (abs y) (abs ey)))
818                              (number->string y)
819                              (number->string ey))
820                          ")\n" s)))
821                        
822   (define (select-font font-name-symbol)
823     (let* ((c (assoc font-name-symbol font-name-alist)))
824       (if (eq? c #f)
825           (begin
826             (ly-warn 
827              (string-append 
828               "Programming error: No such font known " 
829               (car font-name-symbol)))
830             "")                         ; issue no command
831           (func "select-font" (car font-name-symbol)))))
832
833   (define (start-line height)
834           (func "start-line" height))
835
836   (define (stop-line)
837           (func "stop-line"))
838
839   (define (text s)
840           (func "text" s))
841
842   (define (volta h w thick vert-start vert-end)
843           ;; urg
844           (string-append
845            (func "set-line-char" "|")
846            (func "rmove-to" 0 -4)
847            ;; definition strange-way around
848            (if (= 0 vert-start)
849               (func "v-line" h)
850                "")
851            (func "rmove-to" 1 h)
852            (func "set-line-char" "_")
853            (func "h-line" (- w 1))
854            (func "set-line-char" "|")
855            (if (= 0 vert-end)
856                (string-append
857                 (func "rmove-to" (- w 1) (* -1 h))
858                 (func "v-line" (* -1 h)))
859                "")))
860
861   (cond ((eq? action-name 'all-definitions)
862          `(begin
863             (define beam ,beam)
864             (define bracket ,bracket)
865             (define char ,char)
866             ;;(define crescendo ,crescendo)
867             (define bezier-sandwich ,bezier-sandwich)
868             ;;(define dashed-slur ,dashed-slur) 
869             ;;(define decrescendo ,decrescendo) 
870             (define end-output ,end-output)
871             (define experimental-on ,experimental-on)
872             (define filledbox ,filledbox)
873             ;;(define font-def ,font-def)
874             (define font-load-command ,font-load-command)
875             ;;(define font-switch ,font-switch)
876             (define header ,header) 
877             (define header-end ,header-end)
878             (define lily-def ,lily-def)
879             ;;(define invoke-char ,invoke-char) 
880             ;;(define invoke-dim1 ,invoke-dim1)
881             (define placebox ,placebox)
882             (define select-font ,select-font)
883             (define start-line ,start-line)
884             ;;(define stem ,stem)
885             (define stop-line ,stop-line)
886             (define stop-last-line ,stop-line)
887             (define text ,text)
888             ;;(define tuplet ,tuplet)
889             (define volta ,volta)
890             ))
891         ;;((eq? action-name 'tuplet) tuplet)
892         ;;((eq? action-name 'beam) beam)
893         ;;((eq? action-name 'bezier-sandwich) bezier-sandwich)
894         ;;((eq? action-name 'bracket) bracket)
895         ((eq? action-name 'char) char)
896         ;;((eq? action-name 'crescendo) crescendo)
897         ;;((eq? action-name 'dashed-slur) dashed-slur) 
898         ;;((eq? action-name 'decrescendo) decrescendo)
899         ;;((eq? action-name 'experimental-on) experimental-on)
900         ((eq? action-name 'filledbox) filledbox)
901         ((eq? action-name 'select-font) select-font)
902         ;;((eq? action-name 'volta) volta)
903         (else (error "unknown tag -- MUSA-SCM " action-name))
904         )
905   )
906
907
908 (define (gulp-file name)
909   (let* ((port (open-file name "r"))
910          (content (let loop ((text ""))
911                        (let ((line (read-line port)))
912                             (if (or (eof-object? line)
913                                     (not line)) 
914                                 text
915                                 (loop (string-append text line "\n")))))))
916         (close port)
917         content))
918
919 ;; urg: Use when standalone, do:
920 ;; (define (ly-gulp-file name) (scm-gulp-file name))
921 (define (scm-gulp-file name)
922   (set! %load-path 
923         (cons (string-append (getenv 'LILYPONDPREFIX) "/ly")
924               (cons (string-append (getenv 'LILYPONDPREFIX) "/ps")
925                     %load-path)))
926   (let ((path (%search-load-path name)))
927        (if path
928            (gulp-file path)
929            (gulp-file name))))
930
931 (define (scm-tex-output)
932   (eval (tex-scm 'all-definitions)))
933                                 
934 (define (scm-ps-output)
935   (eval (ps-scm 'all-definitions)))
936
937 (define (scm-as-output)
938   (eval (as-scm 'all-definitions)))
939         
940 (define (index-cell cell dir)
941   (if (equal? dir 1)
942       (cdr cell)
943       (car cell)))
944
945 ;
946 ; How should a  bar line behave at a break? 
947 ;
948 (define (break-barline glyph dir)
949    (let ((result (assoc glyph 
950                         '((":|:" . (":|" . "|:"))
951                           ("|" . ("|" . ""))
952                           ("|s" . (nil . "|"))
953                           ("|:" . ("|" . "|:"))
954                           ("|." . ("|." . nil))
955                           (".|" . (nil . ".|"))
956                           (":|" . (":|" . nil))
957                           ("||" . ("||" . nil))
958                           (".|." . (".|." . nil))
959                           ("scorebar" . (nil . "scorepostbreak"))
960                           ("brace" . (nil . "brace"))
961                           ("bracket" . (nil . "bracket"))  
962                           )
963                         )))
964
965      (if (equal? result #f)
966          (ly-warn (string-append "Unknown bar glyph: `" glyph "'"))
967          (index-cell (cdr result) dir))
968      )
969    )
970