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