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