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