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