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