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