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