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