]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
release: 1.3.28
[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 (define (fontify name exp)
220   (string-append (select-font name)
221                  exp)
222   )
223
224 ;;;;;;;;;;;;;;;;;;;;
225
226
227 ; Make a function that checks score element for being of a specific type. 
228 (define (make-type-checker name)
229   (lambda (elt)
230     (not (not (memq name (ly-get-elt-property elt 'interfaces))))))
231
232         
233
234
235
236
237
238
239
240
241 ;;;;;;;;;;;;;;;;;;; generic output
242
243 (define (translate-atom offset exp)
244   exp)
245
246
247 ;;;;;;;;;;;;;;;;;;; TeX output
248 (define (tex-scm action-name)
249   (define (unknown) 
250     "%\n\\unknown%\n")
251
252
253   (define (select-font font-name-symbol)
254     (let*
255         (
256          (c (assoc font-name-symbol font-name-alist))
257          )
258
259       (if (eq? c #f)
260           (begin
261             (ly-warn (string-append
262                       "Programming error: No such font known " (car font-name-symbol)))
263             "")                         ; issue no command
264           (string-append "\\" (cdr c)))
265       
266       
267       ))
268   
269   (define (beam width slope thick)
270     (embedded-ps ((ps-scm 'beam) width slope thick)))
271
272   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
273     (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
274
275   (define (dashed-slur thick dash l)
276     (embedded-ps ((ps-scm 'dashed-slur)  thick dash l)))
277
278   (define (crescendo thick w h cont)
279     (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
280
281   (define (char i)
282     (string-append "\\char" (inexact->string i 10) " "))
283   
284   (define (decrescendo thick w h cont)
285     (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
286
287    ;This sets CTM so that you get to the currentpoint
288   ; by executing a 0 0 moveto
289
290   
291  
292
293   (define (font-load-command name-mag command)
294     (string-append
295      "\\font\\" command "="
296      (symbol->string (car name-mag))
297      " scaled "
298      (number->string (magstep (cdr name-mag)))
299      "\n"))
300
301
302   (define (embedded-ps s)
303     (string-append "\\embeddedps{" s "}"))
304
305   (define (comment s)
306     (string-append "% " s))
307   
308   (define (end-output) 
309     "\n\\EndLilyPondOutput")
310   
311   (define (experimental-on)
312     "")
313
314   (define (font-switch i)
315     (string-append
316      "\\" (font i) "\n"))
317
318   (define (font-def i s)
319     (string-append
320      "\\font" (font-switch i) "=" s "\n"))
321
322   (define (header-end)
323     (string-append
324      "\\special{! "
325      ; fixed in 1.3.4
326      ;(ly-gulp-file "lily.ps")
327
328      (regexp-substitute/global #f "\n" (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
329      "}"
330      "\\input lilyponddefs \\turnOnPostScript"))
331
332   (define (header creator generate) 
333     (string-append
334      "%created by: " creator generate "\n"))
335
336   (define (invoke-char s i)
337     (string-append 
338      "\n\\" s "{" (inexact->string i 10) "}" ))
339
340   (define (invoke-dim1 s d)
341     (string-append
342      "\n\\" s "{" (number->dim d) "}"))
343   (define (pt->sp x)
344     (* 65536 x))
345   
346   ;;
347   ;; need to do something to make this really safe.
348   ;;
349   (define (output-tex-string s)
350       (if security-paranoia
351           (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
352           s))
353       
354   (define (lily-def key val)
355     (string-append
356      "\\def\\"
357      ; fixed in 1.3.4
358      (regexp-substitute/global #f "_" (output-tex-string key) 'pre "X" 'post)
359      ;(output-tex-string key)
360      "{" (output-tex-string val) "}\n"))
361
362   (define (number->dim x)
363     (string-append 
364      (number->string  (chop-decimal x)) " pt "))
365
366   (define (placebox x y s) 
367     (string-append 
368      "\\placebox{"
369      (number->dim y) "}{" (number->dim x) "}{" s "}\n"))
370
371   (define (bezier-sandwich l thick)
372     (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
373
374   (define (start-line ht)
375       (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
376
377   (define (stop-line) 
378     "}\\vss}\\interscoreline")
379   (define (stop-last-line)
380     "}\\vss}")
381   (define (filledbox breapth width depth height) 
382     (string-append 
383      "\\kern" (number->dim (- breapth))
384      "\\vrule width " (number->dim (+ breapth width))
385      "depth " (number->dim depth)
386      "height " (number->dim height) " "))
387
388   (define (text s)
389     (string-append "\\hbox{" (output-tex-string s) "}"))
390   
391   (define (tuplet ht gapx dx dy thick dir)
392     (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
393
394   (define (volta h w thick vert_start vert_end)
395     (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
396
397   ;; TeX
398   ;; The procedures listed below form the public interface of TeX-scm.
399   ;; (should merge the 2 lists)
400   (cond ((eq? action-name 'all-definitions)
401          `(begin
402             (define font-load-command ,font-load-command)
403             (define beam ,beam)
404             (define bezier-sandwich ,bezier-sandwich)
405             (define bracket ,bracket)
406             (define char ,char)
407             (define crescendo ,crescendo)
408             (define dashed-slur ,dashed-slur) 
409             (define decrescendo ,decrescendo) 
410             (define end-output ,end-output)
411             (define experimental-on ,experimental-on)
412             (define filledbox ,filledbox)
413             (define font-def ,font-def)
414             (define font-switch ,font-switch)
415             (define header-end ,header-end)
416             (define lily-def ,lily-def)
417             (define header ,header) 
418             (define invoke-char ,invoke-char) 
419             (define invoke-dim1 ,invoke-dim1)
420             (define placebox ,placebox)
421             (define select-font ,select-font)
422             (define start-line ,start-line)
423             (define stop-line ,stop-line)
424             (define stop-last-line ,stop-last-line)
425             (define text ,text)
426             (define tuplet ,tuplet)
427             (define volta ,volta)
428             ))
429
430         ((eq? action-name 'beam) beam)
431         ((eq? action-name 'tuplet) tuplet)
432         ((eq? action-name 'bracket) bracket)
433         ((eq? action-name 'crescendo) crescendo)
434         ((eq? action-name 'dashed-slur) dashed-slur) 
435         ((eq? action-name 'decrescendo) decrescendo) 
436         ((eq? action-name 'end-output) end-output)
437         ((eq? action-name 'experimental-on) experimental-on)
438         ((eq? action-name 'font-def) font-def)
439         ((eq? action-name 'font-switch) font-switch)
440         ((eq? action-name 'header-end) header-end)
441         ((eq? action-name 'lily-def) lily-def)
442         ((eq? action-name 'header) header) 
443         ((eq? action-name 'invoke-char) invoke-char) 
444         ((eq? action-name 'invoke-dim1) invoke-dim1)
445         ((eq? action-name 'placebox) placebox)
446         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
447         ((eq? action-name 'start-line) start-line)
448         ((eq? action-name 'stem) stem)
449         ((eq? action-name 'stop-line) stop-line)
450         ((eq? action-name 'stop-last-line) stop-last-line)
451         ((eq? action-name 'volta) volta)
452         (else (error "unknown tag -- PS-TEX " action-name))
453         )
454   )
455
456
457 ;;;;;;;;;;;; PS
458 (define (ps-scm action-name)
459
460
461   ;; alist containing fontname -> fontcommand assoc (both strings)
462   (define font-alist '())
463   (define font-count 0)
464   (define current-font "")
465
466   
467   (define (cached-fontname i)
468     (string-append
469      "lilyfont"
470      (make-string 1 (integer->char (+ 65 i)))))
471     
472   (define (mag-to-size m)
473     (number->string (case m 
474                       (0 12)
475                       (1 12)
476                       (2 14) ; really: 14.400
477                       (3 17) ; really: 17.280
478                       (4 21) ; really: 20.736
479                       (5 24) ; really: 24.888
480                       (6 30) ; really: 29.856
481                       )))
482   
483   
484   (define (select-font font-name-symbol)
485     (let*
486         (
487          (c (assoc font-name-symbol font-name-alist))
488          )
489
490       (if (eq? c #f)
491           (begin
492             (ly-warn (string-append
493                       "Programming error: No such font known " (car font-name-symbol)))
494             "")                         ; issue no command
495           (string-append " " (cdr c) " "))
496       
497       
498       ))
499
500     (define (font-load-command name-mag command)
501       (string-append
502        "/" command
503        " { /"
504        (symbol->string (car name-mag))
505        " findfont "
506        (number->string (magstep (cdr name-mag)))
507        " 1000 div 12 mul  scalefont setfont } bind def "
508        "\n"))
509
510
511   (define (beam width slope thick)
512     (string-append
513      (numbers->string (list width slope thick)) " draw_beam" ))
514
515   (define (comment s)
516     (string-append "% " s))
517
518   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
519     (string-append
520      (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
521
522   (define (char i)
523     (invoke-char " show" i))
524
525   (define (crescendo thick w h cont )
526     (string-append 
527      (numbers->string (list w h (inexact->exact cont) thick))
528      " draw_crescendo"))
529
530   ;; what the heck is this interface ?
531   (define (dashed-slur thick dash l)
532     (string-append 
533      (apply string-append (map control->string l)) 
534      (number->string thick) 
535      " [ "
536      (number->string dash)
537      " "
538      (number->string (* 10 thick))      ;UGH.  10 ?
539      " ] 0 draw_dashed_slur"))
540
541   (define (decrescendo thick w h cont)
542     (string-append 
543      (numbers->string (list w h (inexact->exact cont) thick))
544      " draw_decrescendo"))
545
546
547   (define (end-output)
548     "\nshowpage\n")
549   
550   (define (experimental-on) "")
551   
552   (define (filledbox breapth width depth height) 
553     (string-append (numbers->string (list breapth width depth height))
554                    " draw_box" ))
555
556   ;; obsolete?
557   (define (font-def i s)
558     (string-append
559      "\n/" (font i) " {/" 
560      (substring s 0 (- (string-length s) 4))
561      " findfont 12 scalefont setfont} bind def \n"))
562
563   (define (font-switch i)
564     (string-append (font i) " "))
565
566   (define (header-end)
567     (string-append
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-slur ,dashed-slur) 
645             (define decrescendo ,decrescendo) 
646             (define end-output ,end-output)
647             (define experimental-on ,experimental-on)
648             (define filledbox ,filledbox)
649             (define font-def ,font-def)
650             (define font-switch ,font-switch)
651             (define header-end ,header-end)
652             (define lily-def ,lily-def)
653             (define font-load-command ,font-load-command)
654             (define header ,header) 
655             (define invoke-char ,invoke-char) 
656             (define invoke-dim1 ,invoke-dim1)
657             (define placebox ,placebox)
658             (define select-font ,select-font)
659             (define start-line ,start-line)
660             (define stem ,stem)
661             (define stop-line ,stop-line)
662             (define stop-last-line ,stop-line)
663             (define text ,text)
664             ))
665         ((eq? action-name 'tuplet) tuplet)
666         ((eq? action-name 'beam) beam)
667         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
668         ((eq? action-name 'bracket) bracket)
669         ((eq? action-name 'char) char)
670         ((eq? action-name 'crescendo) crescendo)
671         ((eq? action-name 'dashed-slur) dashed-slur) 
672         ((eq? action-name 'decrescendo) decrescendo)
673         ((eq? action-name 'experimental-on) experimental-on)
674         ((eq? action-name 'filledbox) filledbox)
675         ((eq? action-name 'select-font) select-font)
676         ((eq? action-name 'volta) volta)
677         (else (error "unknown tag -- PS-SCM " action-name))
678         )
679   )
680
681
682 (define (arg->string arg)
683   (cond ((number? arg) (inexact->string arg 10))
684         ((string? arg) (string-append "\"" arg "\""))
685         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
686
687 (define (func name . args)
688   (string-append 
689    "(" name 
690    (if (null? args) 
691        ""
692        (apply string-append 
693               (map (lambda (x) (string-append " " (arg->string x))) args)))
694    ")\n"))
695
696 (define (sign x)
697   (if (= x 0)
698       1
699       (inexact->exact (/ x (abs x)))))
700
701 ;;;; AsciiScript as
702 (define (as-scm action-name)
703
704   (define (beam width slope thick)
705           (string-append
706            (func "set-line-char" "#")
707            (func "rline-to" width (* width slope))
708            ))
709
710   ; simple flat slurs
711   (define (bezier-sandwich l thick)
712           (let (
713                 (c0 (cadddr l))
714                 (c1 (cadr l))
715                 (c3 (caddr l)))
716                (let* ((x (car c0))
717                       (dx (- (car c3) x))
718                       (dy (- (cdr c3) (cdr c0)))
719                       (rc (/ dy dx))
720                       (c1-dx (- (car c1) x))
721                       (c1-line-y (+ (cdr c0) (* c1-dx rc)))
722                       (dir (if (< c1-line-y (cdr c1)) 1 -1))
723                       (y (+ -1 (* dir (max (* dir (cdr c0)) (* dir (cdr c3)))))))
724                      (string-append
725                       (func "rmove-to" x y)
726                       (func "put" (if (< 0 dir) "/" "\\\\"))
727                       (func "rmove-to" 1 (if (< 0 dir) 1 0))
728                       (func "set-line-char" "_")
729                       (func "h-line" (- dx 1))
730                       (func "rmove-to" (- dx 1) (if (< 0 dir) -1 0))
731                       (func "put" (if (< 0 dir) "\\\\" "/"))))))
732
733   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
734           (string-append
735            (func "rmove-to" (+ width 1) (- (/ height -2) 1))
736            (func "put" "\\\\")
737            (func "set-line-char" "|")
738            (func "rmove-to" 0 1)
739            (func "v-line" (+ height 1))
740            (func "rmove-to" 0 (+ height 1))
741            (func "put" "/")
742            ))
743
744   (define (char i)
745     (func "char" i))
746
747   (define (end-output) 
748     (func "end-output"))
749   
750   (define (experimental-on)
751           "")
752
753   (define (filledbox breapth width depth height)
754           (let ((dx (+ width breapth))
755                 (dy (+ depth height)))
756                (string-append 
757                 (func "rmove-to" (* -1 breapth) (* -1 depth))
758                 (if (< dx dy)
759                     (string-append
760                      (func "set-line-char" 
761                            (if (<= dx 1) "|" "#"))
762                      (func "v-line" dy))
763                     (string-append
764                      (func "set-line-char" 
765                            (if (<= dy 1) "-" "="))
766                     (func "h-line" dx))))))
767
768   (define (font-load-command name-mag command)
769     (func "load-font" (car name-mag) (magstep (cdr name-mag))))
770
771   (define (header creator generate) 
772     (func "header" creator generate))
773
774   (define (header-end) 
775     (func "header-end"))
776
777   ;; urg: this is good for half of as2text's execution time
778   (define (xlily-def key val)
779           (string-append "(define " key " " (arg->string val) ")\n"))
780
781   (define (lily-def key val)
782           (if 
783            (or (equal? key "mudelapaperlinewidth")
784                (equal? key "mudelapaperstaffheight"))
785            (string-append "(define " key " " (arg->string val) ")\n")
786            ""))
787
788   (define (placebox x y s) 
789     (let ((ey (inexact->exact y)))
790           (string-append "(move-to " (number->string (inexact->exact x)) " "
791                          (if (= 0.5 (- (abs y) (abs ey)))
792                              (number->string y)
793                              (number->string ey))
794                          ")\n" s)))
795                        
796   (define (select-font font-name-symbol)
797     (let* ((c (assoc font-name-symbol font-name-alist)))
798       (if (eq? c #f)
799           (begin
800             (ly-warn 
801              (string-append 
802               "Programming error: No such font known " 
803               (car font-name-symbol)))
804             "")                         ; issue no command
805           (func "select-font" (car font-name-symbol)))))
806
807   (define (start-line height)
808           (func "start-line" height))
809
810   (define (stop-line)
811           (func "stop-line"))
812
813   (define (text s)
814           (func "text" s))
815
816   (define (volta h w thick vert-start vert-end)
817           ;; urg
818           (string-append
819            (func "set-line-char" "|")
820            (func "rmove-to" 0 -4)
821            ;; definition strange-way around
822            (if (= 0 vert-start)
823               (func "v-line" h)
824                "")
825            (func "rmove-to" 1 h)
826            (func "set-line-char" "_")
827            (func "h-line" (- w 1))
828            (func "set-line-char" "|")
829            (if (= 0 vert-end)
830                (string-append
831                 (func "rmove-to" (- w 1) (* -1 h))
832                 (func "v-line" (* -1 h)))
833                "")))
834
835   (cond ((eq? action-name 'all-definitions)
836          `(begin
837             (define beam ,beam)
838             (define bracket ,bracket)
839             (define char ,char)
840             ;;(define crescendo ,crescendo)
841             (define bezier-sandwich ,bezier-sandwich)
842             ;;(define dashed-slur ,dashed-slur) 
843             ;;(define decrescendo ,decrescendo) 
844             (define end-output ,end-output)
845             (define experimental-on ,experimental-on)
846             (define filledbox ,filledbox)
847             ;;(define font-def ,font-def)
848             (define font-load-command ,font-load-command)
849             ;;(define font-switch ,font-switch)
850             (define header ,header) 
851             (define header-end ,header-end)
852             (define lily-def ,lily-def)
853             ;;(define invoke-char ,invoke-char) 
854             ;;(define invoke-dim1 ,invoke-dim1)
855             (define placebox ,placebox)
856             (define select-font ,select-font)
857             (define start-line ,start-line)
858             ;;(define stem ,stem)
859             (define stop-line ,stop-line)
860             (define stop-last-line ,stop-line)
861             (define text ,text)
862             ;;(define tuplet ,tuplet)
863             (define volta ,volta)
864             ))
865         ;;((eq? action-name 'tuplet) tuplet)
866         ;;((eq? action-name 'beam) beam)
867         ;;((eq? action-name 'bezier-sandwich) bezier-sandwich)
868         ;;((eq? action-name 'bracket) bracket)
869         ((eq? action-name 'char) char)
870         ;;((eq? action-name 'crescendo) crescendo)
871         ;;((eq? action-name 'dashed-slur) dashed-slur) 
872         ;;((eq? action-name 'decrescendo) decrescendo)
873         ;;((eq? action-name 'experimental-on) experimental-on)
874         ((eq? action-name 'filledbox) filledbox)
875         ((eq? action-name 'select-font) select-font)
876         ;;((eq? action-name 'volta) volta)
877         (else (error "unknown tag -- MUSA-SCM " action-name))
878         )
879   )
880
881
882 (define (gulp-file name)
883   (let* ((port (open-file name "r"))
884          (content (let loop ((text ""))
885                        (let ((line (read-line port)))
886                             (if (or (eof-object? line)
887                                     (not line)) 
888                                 text
889                                 (loop (string-append text line "\n")))))))
890         (close port)
891         content))
892
893 (define (scm-gulp-file name)
894   (set! %load-path 
895         (cons (string-append 
896                (getenv 'LILYPONDPREFIX) "/ps") %load-path))
897   (let ((path (%search-load-path name)))
898        (if path
899            (gulp-file path)
900            (gulp-file name))))
901
902 (define (scm-tex-output)
903   (eval (tex-scm 'all-definitions)))
904                                 
905 (define (scm-ps-output)
906   (eval (ps-scm 'all-definitions)))
907
908 (define (scm-as-output)
909   (eval (as-scm 'all-definitions)))
910                                 
911 ; Russ McManus, <mcmanus@IDT.NET>  
912
913 ; I use the following, which should definitely be provided somewhere
914 ; in guile, but isn't, AFAIK:
915
916
917
918 (define (hash-table-for-each fn ht)
919   (do ((i 0 (+ 1 i)))
920       ((= i (vector-length ht)))
921     (do ((alist (vector-ref ht i) (cdr alist)))
922         ((null? alist) #t)
923       (fn (car (car alist)) (cdr (car alist))))))
924
925 (define (hash-table-map fn ht)
926   (do ((i 0 (+ 1 i))
927        (ret-ls '()))
928       ((= i (vector-length ht)) (reverse ret-ls))
929     (do ((alist (vector-ref ht i) (cdr alist)))
930         ((null? alist) #t)
931       (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
932
933
934
935 (define (index-cell cell dir)
936   (if (equal? dir 1)
937       (cdr cell)
938       (car cell)))
939
940 ;
941 ; How should a  bar line behave at a break? 
942 ;
943 (define (break-barline glyph dir)
944    (let ((result (assoc glyph 
945                         '((":|:" . (":|" . "|:"))
946                           ("|" . ("|" . ""))
947                           ("|s" . (nil . "|"))
948                           ("|:" . ("|" . "|:"))
949                           ("|." . ("|." . nil))
950                           (":|" . (":|" . nil))
951                           ("||" . ("||" . nil))
952                           (".|." . (".|." . nil))
953                           ("scorebar" . (nil . "scorepostbreak"))
954                           ("brace" . (nil . "brace"))
955                           ("bracket" . (nil . "bracket"))  
956                           )
957                         )))
958
959      (if (equal? result #f)
960          (ly-warn (string-append "Unknown bar glyph: `" glyph "'"))
961          (index-cell (cdr result) dir))
962      )
963    )
964      
965
966 (define (slur-ugly ind ht)
967   (if (and
968 ;       (< ht 4.0)
969        (< ht (* 4 ind))
970        (> ht (* 0.4 ind))
971        (> ht (+ (* 2 ind) -4))
972        (< ht (+ (* -2 ind) 8)))
973       #f
974       (cons ind  ht)
975   ))