]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
release: 1.3.68
[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 an 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 (define mark-visibility end-of-line-invisible)
94
95 ;; Spacing constants for prefatory matter.
96 ;;
97 ;; rules for this spacing are much more complicated than this. See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147
98 ;;
99 ;;
100
101 ;; (Measured in staff space)
102 (define space-alist
103  '(
104    ((none Instrument_name) . (extra-space 1.0))
105    ((Instrument_name Left_edge_item) . (extra-space 1.0))
106    ((Left_edge_item Clef_item) . (extra-space 1.0))
107    ((Left_edge_item Key_item) . (extra-space 0.0))   
108    ((Left_edge_item begin-of-note) . (extra-space 1.0))
109    ((none Left_edge_item) . (extra-space 0.0))
110    ((Left_edge_item Staff_bar) . (extra-space 0.0))
111 ;   ((none Left_edge_item) . (extra-space -15.0))
112 ;   ((none Left_edge_item) . (extra-space -15.0))
113    ((none Clef_item) . (minimum-space 1.0))
114    ((none Staff_bar) . (minimum-space 0.0))
115    ((none Clef_item) . (minimum-space 1.0))
116    ((none Key_item) . (minimum-space 0.5))
117    ((none Time_signature) . (extra-space 0.0))
118    ((none begin-of-note) . (minimum-space 1.5))
119    ((Clef_item Key_item) . (minimum-space 4.0))
120    ((Key_item Time_signature) . (extra-space 1.0))
121    ((Clef_item  Time_signature) . (minimum-space 3.5))
122    ((Staff_bar Clef_item) .   (minimum-space 1.0))
123    ((Clef_item  Staff_bar) .  (minimum-space 3.7))
124    ((Time_signature Staff_bar) .  (minimum-space 2.0))
125    ((Key_item  Staff_bar) .  (extra-space 1.0))
126    ((Staff_bar Time_signature) . (minimum-space 1.5)) ;double check this.
127    ((Time_signature begin-of-note) . (extra-space 2.0)) ;double check this.
128    ((Key_item begin-of-note) . (extra-space 2.5))
129    ((Staff_bar begin-of-note) . (extra-space 1.0))
130    ((Clef_item begin-of-note) . (minimum-space 5.0))
131    ((none Breathing_sign) . (minimum-space 0.0))
132    ((Breathing_sign Key_item) . (minimum-space 1.5))
133    ((Breathing_sign begin-of-note) . (minimum-space 1.0))
134    ((Breathing_sign Staff_bar) . (minimum-space 1.5))
135    ((Breathing_sign Clef_item) . (minimum-space 2.0))
136    )
137 )
138
139 ;; silly, use alist? 
140 (define (find-notehead-symbol duration style)
141   (case style
142    ((cross) "2cross")
143    ((harmonic) "0mensural")
144    ((baroque) 
145     (string-append (number->string duration)
146                    (if (< duration 0) "mensural" "")))
147    ((default) (number->string duration))
148    (else
149     (string-append (number->string duration) (symbol->string style)))))
150
151
152 ;;;;;;;; TeX
153
154 ;; this is silly, can't we use something like
155 ;; roman-0, roman-1 roman+1 ?
156 (define cmr-alist 
157   '(("bold" . "cmbx") 
158     ("brace" . "feta-braces")
159     ("default" . "cmr10")
160     ("dynamic" . "feta-din") 
161     ("feta" . "feta") 
162     ("feta-1" . "feta") 
163     ("feta-2" . "feta") 
164     ("typewriter" . "cmtt") 
165     ("italic" . "cmti") 
166     ("msam" . "msam") 
167     ("roman" . "cmr") 
168     ("script" . "cmr") 
169     ("large" . "cmbx") 
170     ("Large" . "cmbx") 
171     ("mark" . "feta-nummer") 
172     ("finger" . "feta-nummer")
173     ("timesig" . "feta-nummer")
174     ("number" . "feta-nummer") 
175     ("volta" . "feta-nummer"))
176 )
177
178 (define (string-encode-integer i)
179   (cond
180    ((= i  0) "o")
181    ((< i 0)   (string-append "n" (string-encode-integer (- i))))
182    (else (string-append
183           (make-string 1 (integer->char (+ 65 (modulo i 26))))
184           (string-encode-integer (quotient i 26))
185          )
186    )
187   )
188   )
189
190 (define (magstep i)
191   (cdr (assoc i '((-4 . 482)
192                   (-3 . 579)
193                   (-2 . 694)
194                   (-1 . 833)
195                   (0 . 1000)
196                   (1 . 1200) 
197                   (2 . 1440)
198                   (3 . 1728)
199                   (4 . 2074))
200               )
201        )
202   )
203              
204 (define script-alist '())
205
206 (define font-name-alist  '())
207 (define (font-command name-mag)
208     (cons name-mag
209           (string-append  "magfont"
210                           (string-encode-integer (hashq (car name-mag) 1000000))
211                           "m"
212                           (string-encode-integer (cdr name-mag)))
213
214           )
215     )
216 (define (define-fonts names)
217   (set! font-name-alist (map font-command names))
218   (apply string-append
219          (map (lambda (x)
220                 (font-load-command (car x) (cdr x))) font-name-alist)
221   ))
222
223 (define (fontify name exp)
224   (string-append (select-font name)
225                  exp)
226   )
227
228 ;;;;;;;;;;;;;;;;;;;;
229
230
231 ; Make a function that checks score element for being of a specific type. 
232 (define (make-type-checker symbol)
233   (lambda (elt)
234     (display  symbol)
235     (if (eq? #t (ly-get-elt-property elt symbol))
236                #t
237         #f)
238     ))
239
240   
241
242         
243 ;;;;;;;;;;;;;;;;;;; TeX output
244 (define (tex-scm action-name)
245   (define (unknown) 
246     "%\n\\unknown%\n")
247
248
249   (define (select-font font-name-symbol)
250     (let*
251         (
252          (c (assoc font-name-symbol font-name-alist))
253          )
254
255       (if (eq? c #f)
256           (begin
257             (ly-warn (string-append
258                       "Programming error: No such font known " (car font-name-symbol)))
259             "")                         ; issue no command
260           (string-append "\\" (cdr c)))
261       
262       
263       ))
264   
265   (define (beam width slope thick)
266     (embedded-ps ((ps-scm 'beam) width slope thick)))
267
268   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
269     (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
270
271   (define (dashed-slur thick dash l)
272     (embedded-ps ((ps-scm 'dashed-slur)  thick dash l)))
273
274   (define (crescendo thick w h cont)
275     (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
276
277   (define (char i)
278     (string-append "\\char" (inexact->string i 10) " "))
279   
280   (define (dashed-line thick dash w)
281     (embedded-ps ((ps-scm 'dashed-line) thick dash w)))
282
283   (define (decrescendo thick w h cont)
284     (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
285
286   (define (font-load-command name-mag command)
287     (string-append
288      "\\font\\" command "="
289      (symbol->string (car name-mag))
290      " scaled "
291      (number->string (magstep (cdr name-mag)))
292      "\n"))
293
294   (define (embedded-ps s)
295     (string-append "\\embeddedps{" s "}"))
296
297   (define (comment s)
298     (string-append "% " s))
299   
300   (define (end-output) 
301         (begin
302 ; uncomment for some stats about lily memory      
303 ;               (display (gc-stats))
304     (string-append "\n\\EndLilyPondOutput"
305                    ; Put GC stats here.
306                    )))
307   
308   (define (experimental-on)
309     "")
310
311   (define (font-switch i)
312     (string-append
313      "\\" (font i) "\n"))
314
315   (define (font-def i s)
316     (string-append
317      "\\font" (font-switch i) "=" s "\n"))
318
319   (define (header-end)
320     (string-append
321      "\\special{! "
322
323      ;; URG: ly-gulp-file: now we can't use scm output without Lily
324      (if use-regex
325          ;; fixed in 1.3.4 for powerpc -- broken on Windows
326          (regexp-substitute/global #f "\n"
327                                    (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
328          (ly-gulp-file "lily.ps"))
329      "}"
330      "\\input lilyponddefs \\turnOnPostScript"))
331
332   (define (header creator generate) 
333     (string-append
334      "%created by: " creator generate "\n"))
335
336   (define (invoke-char s i)
337     (string-append 
338      "\n\\" s "{" (inexact->string i 10) "}" ))
339
340   (define (invoke-dim1 s d)
341     (string-append
342      "\n\\" s "{" (number->dim d) "}"))
343   (define (pt->sp x)
344     (* 65536 x))
345   
346   ;;
347   ;; need to do something to make this really safe.
348   ;;
349   (define (output-tex-string s)
350       (if security-paranoia
351           (if use-regex
352               (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
353               (begin (display "warning: not paranoid") (newline) s))
354           s))
355       
356   (define (lily-def key val)
357     (string-append
358      "\\def\\"
359      (if use-regex
360          ;; fixed in 1.3.4 for powerpc -- broken on Windows
361          (regexp-substitute/global #f "_"
362                                    (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      (ly-number->string 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\n")
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   ;; 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      ;; URG: now we can't use scm output without Lily
585      (ly-gulp-file "lilyponddefs.ps")
586      " {exch pop //systemdict /run get exec} "
587      (ly-gulp-file "lily.ps")
588      "{ exch pop //systemdict /run get exec } "
589     ))
590   
591   (define (lily-def key val)
592
593      (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
594          (string-append "/" key " {" val "} bind def\n")
595          (string-append "/" key " (" val ") def\n")
596          )
597      )
598
599   (define (header creator generate) 
600     (string-append
601      "%!PS-Adobe-3.0\n"
602      "%%Creator: " creator generate "\n"))
603   
604   (define (invoke-char s i)
605     (string-append 
606      "(\\" (inexact->string i 8) ") " s " " ))
607   
608   (define (invoke-dim1 s d) 
609     (string-append
610      (number->string (* d  (/ 72.27 72))) " " s ))
611
612   (define (placebox x y s) 
613     (string-append 
614      (number->string x) " " (number->string y) " {" s "} placebox "))
615
616   (define (bezier-sandwich l thick)
617     (string-append 
618      (apply string-append (map control->string l))
619      (number->string  thick)
620      " draw_bezier_sandwich"))
621
622   (define (start-line height)
623           "\nstart_line {\n")
624   
625   (define (stem breapth width depth height) 
626     (string-append (numbers->string (list breapth width depth height))
627                    " draw_box" ))
628
629   (define (stop-line)
630       "}\nstop_line\n")
631
632   (define (text s)
633     (string-append "(" s ") show  "))
634
635
636   (define (volta h w thick vert_start vert_end)
637     (string-append 
638      (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
639      " draw_volta"))
640
641   (define (tuplet ht gap dx dy thick dir)
642     (string-append 
643      (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
644      " draw_tuplet"))
645
646
647   (define (unknown) 
648     "\n unknown\n")
649
650
651   ;; PS
652   (cond ((eq? action-name 'all-definitions)
653          `(begin
654             (define beam ,beam)
655             (define tuplet ,tuplet)
656             (define bracket ,bracket)
657             (define char ,char)
658             (define crescendo ,crescendo)
659             (define volta ,volta)
660             (define bezier-sandwich ,bezier-sandwich)
661             (define dashed-line ,dashed-line) 
662             (define dashed-slur ,dashed-slur) 
663             (define decrescendo ,decrescendo) 
664             (define end-output ,end-output)
665             (define experimental-on ,experimental-on)
666             (define filledbox ,filledbox)
667             (define font-def ,font-def)
668             (define font-switch ,font-switch)
669             (define header-end ,header-end)
670             (define lily-def ,lily-def)
671             (define font-load-command ,font-load-command)
672             (define header ,header) 
673             (define invoke-char ,invoke-char) 
674             (define invoke-dim1 ,invoke-dim1)
675             (define placebox ,placebox)
676             (define select-font ,select-font)
677             (define start-line ,start-line)
678             (define stem ,stem)
679             (define stop-line ,stop-line)
680             (define stop-last-line ,stop-line)
681             (define text ,text)
682             ))
683         ((eq? action-name 'tuplet) tuplet)
684         ((eq? action-name 'beam) beam)
685         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
686         ((eq? action-name 'bracket) bracket)
687         ((eq? action-name 'char) char)
688         ((eq? action-name 'crescendo) crescendo)
689         ((eq? action-name 'dashed-line) dashed-line) 
690         ((eq? action-name 'dashed-slur) dashed-slur) 
691         ((eq? action-name 'decrescendo) decrescendo)
692         ((eq? action-name 'experimental-on) experimental-on)
693         ((eq? action-name 'filledbox) filledbox)
694         ((eq? action-name 'select-font) select-font)
695         ((eq? action-name 'volta) volta)
696         (else (error "unknown tag -- PS-SCM " action-name))
697         )
698   )
699
700
701 (define (arg->string arg)
702   (cond ((number? arg) (inexact->string arg 10))
703         ((string? arg) (string-append "\"" arg "\""))
704         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
705
706 (define (func name . args)
707   (string-append 
708    "(" name 
709    (if (null? args) 
710        ""
711        (apply string-append 
712               (map (lambda (x) (string-append " " (arg->string x))) args)))
713    ")\n"))
714
715 (define (sign x)
716   (if (= x 0)
717       1
718       (inexact->exact (/ x (abs x)))))
719
720 ;;;; AsciiScript as
721 (define (as-scm action-name)
722
723   (define (beam width slope thick)
724           (string-append
725            (func "set-line-char" "#")
726            (func "rline-to" width (* width slope))
727            ))
728
729   ; simple flat slurs
730   (define (bezier-sandwich l thick)
731           (let (
732                 (c0 (cadddr l))
733                 (c1 (cadr l))
734                 (c3 (caddr l)))
735                (let* ((x (car c0))
736                       (dx (- (car c3) x))
737                       (dy (- (cdr c3) (cdr c0)))
738                       (rc (/ dy dx))
739                       (c1-dx (- (car c1) x))
740                       (c1-line-y (+ (cdr c0) (* c1-dx rc)))
741                       (dir (if (< c1-line-y (cdr c1)) 1 -1))
742                       (y (+ -1 (* dir (max (* dir (cdr c0)) (* dir (cdr c3)))))))
743                      (string-append
744                       (func "rmove-to" x y)
745                       (func "put" (if (< 0 dir) "/" "\\\\"))
746                       (func "rmove-to" 1 (if (< 0 dir) 1 0))
747                       (func "set-line-char" "_")
748                       (func "h-line" (- dx 1))
749                       (func "rmove-to" (- dx 1) (if (< 0 dir) -1 0))
750                       (func "put" (if (< 0 dir) "\\\\" "/"))))))
751
752   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
753           (string-append
754            (func "rmove-to" (+ width 1) (- (/ height -2) 1))
755            (func "put" "\\\\")
756            (func "set-line-char" "|")
757            (func "rmove-to" 0 1)
758            (func "v-line" (+ height 1))
759            (func "rmove-to" 0 (+ height 1))
760            (func "put" "/")
761            ))
762
763   (define (char i)
764     (func "char" i))
765
766   (define (end-output) 
767     (func "end-output"))
768   
769   (define (experimental-on)
770           "")
771
772   (define (filledbox breapth width depth height)
773           (let ((dx (+ width breapth))
774                 (dy (+ depth height)))
775                (string-append 
776                 (func "rmove-to" (* -1 breapth) (* -1 depth))
777                 (if (< dx dy)
778                     (string-append
779                      (func "set-line-char" 
780                            (if (<= dx 1) "|" "#"))
781                      (func "v-line" dy))
782                     (string-append
783                      (func "set-line-char" 
784                            (if (<= dy 1) "-" "="))
785                     (func "h-line" dx))))))
786
787   (define (font-load-command name-mag command)
788     (func "load-font" (car name-mag) (magstep (cdr name-mag))))
789
790   (define (header creator generate) 
791     (func "header" creator generate))
792
793   (define (header-end) 
794     (func "header-end"))
795
796   ;; urg: this is good for half of as2text's execution time
797   (define (xlily-def key val)
798           (string-append "(define " key " " (arg->string val) ")\n"))
799
800   (define (lily-def key val)
801           (if 
802            (or (equal? key "mudelapaperlinewidth")
803                (equal? key "mudelapaperstaffheight"))
804            (string-append "(define " key " " (arg->string val) ")\n")
805            ""))
806
807   (define (placebox x y s) 
808     (let ((ey (inexact->exact y)))
809           (string-append "(move-to " (number->string (inexact->exact x)) " "
810                          (if (= 0.5 (- (abs y) (abs ey)))
811                              (number->string y)
812                              (number->string ey))
813                          ")\n" s)))
814                        
815   (define (select-font font-name-symbol)
816     (let* ((c (assoc font-name-symbol font-name-alist)))
817       (if (eq? c #f)
818           (begin
819             (ly-warn 
820              (string-append 
821               "Programming error: No such font known " 
822               (car font-name-symbol)))
823             "")                         ; issue no command
824           (func "select-font" (car font-name-symbol)))))
825
826   (define (start-line height)
827           (func "start-line" height))
828
829   (define (stop-line)
830           (func "stop-line"))
831
832   (define (text s)
833           (func "text" s))
834
835   (define (volta h w thick vert-start vert-end)
836           ;; urg
837           (string-append
838            (func "set-line-char" "|")
839            (func "rmove-to" 0 -4)
840            ;; definition strange-way around
841            (if (= 0 vert-start)
842               (func "v-line" h)
843                "")
844            (func "rmove-to" 1 h)
845            (func "set-line-char" "_")
846            (func "h-line" (- w 1))
847            (func "set-line-char" "|")
848            (if (= 0 vert-end)
849                (string-append
850                 (func "rmove-to" (- w 1) (* -1 h))
851                 (func "v-line" (* -1 h)))
852                "")))
853
854   (cond ((eq? action-name 'all-definitions)
855          `(begin
856             (define beam ,beam)
857             (define bracket ,bracket)
858             (define char ,char)
859             ;;(define crescendo ,crescendo)
860             (define bezier-sandwich ,bezier-sandwich)
861             ;;(define dashed-slur ,dashed-slur) 
862             ;;(define decrescendo ,decrescendo) 
863             (define end-output ,end-output)
864             (define experimental-on ,experimental-on)
865             (define filledbox ,filledbox)
866             ;;(define font-def ,font-def)
867             (define font-load-command ,font-load-command)
868             ;;(define font-switch ,font-switch)
869             (define header ,header) 
870             (define header-end ,header-end)
871             (define lily-def ,lily-def)
872             ;;(define invoke-char ,invoke-char) 
873             ;;(define invoke-dim1 ,invoke-dim1)
874             (define placebox ,placebox)
875             (define select-font ,select-font)
876             (define start-line ,start-line)
877             ;;(define stem ,stem)
878             (define stop-line ,stop-line)
879             (define stop-last-line ,stop-line)
880             (define text ,text)
881             ;;(define tuplet ,tuplet)
882             (define volta ,volta)
883             ))
884         ;;((eq? action-name 'tuplet) tuplet)
885         ;;((eq? action-name 'beam) beam)
886         ;;((eq? action-name 'bezier-sandwich) bezier-sandwich)
887         ;;((eq? action-name 'bracket) bracket)
888         ((eq? action-name 'char) char)
889         ;;((eq? action-name 'crescendo) crescendo)
890         ;;((eq? action-name 'dashed-slur) dashed-slur) 
891         ;;((eq? action-name 'decrescendo) decrescendo)
892         ;;((eq? action-name 'experimental-on) experimental-on)
893         ((eq? action-name 'filledbox) filledbox)
894         ((eq? action-name 'select-font) select-font)
895         ;;((eq? action-name 'volta) volta)
896         (else (error "unknown tag -- MUSA-SCM " action-name))
897         )
898   )
899
900
901 (define (gulp-file name)
902   (let* ((port (open-file name "r"))
903          (content (let loop ((text ""))
904                        (let ((line (read-line port)))
905                             (if (or (eof-object? line)
906                                     (not line)) 
907                                 text
908                                 (loop (string-append text line "\n")))))))
909         (close port)
910         content))
911
912 ;; urg: Use when standalone, do:
913 ;; (define ly-gulp-file scm-gulp-file)
914 (define (scm-gulp-file name)
915   (set! %load-path 
916         (cons (string-append (getenv 'LILYPONDPREFIX) "/ly")
917               (cons (string-append (getenv 'LILYPONDPREFIX) "/ps")
918                     %load-path)))
919   (let ((path (%search-load-path name)))
920        (if path
921            (gulp-file path)
922            (gulp-file name))))
923
924 (define (scm-tex-output)
925   (eval (tex-scm 'all-definitions)))
926                                 
927 (define (scm-ps-output)
928   (eval (ps-scm 'all-definitions)))
929
930 (define (scm-as-output)
931   (eval (as-scm 'all-definitions)))
932         
933 (define (index-cell cell dir)
934   (if (equal? dir 1)
935       (cdr cell)
936       (car cell)))
937
938 ;
939 ; How should a  bar line behave at a break? 
940 ;
941 (define (break-barline glyph dir)
942    (let ((result (assoc glyph 
943                         '((":|:" . (":|" . "|:"))
944                           ("|" . ("|" . ""))
945                           ("|s" . (nil . "|"))
946                           ("|:" . ("|" . "|:"))
947                           ("|." . ("|." . nil))
948                           (".|" . (nil . ".|"))
949                           (":|" . (":|" . nil))
950                           ("||" . ("||" . nil))
951                           (".|." . (".|." . nil))
952                           ("scorebar" . (nil . "scorepostbreak"))
953                           ("brace" . (nil . "brace"))
954                           ("bracket" . (nil . "bracket"))  
955                           )
956                         )))
957
958      (if (equal? result #f)
959          (ly-warn (string-append "Unknown bar glyph: `" glyph "'"))
960          (index-cell (cdr result) dir))
961      )
962    )
963      
964
965 (define major-scale
966   '(
967     (0 . 0)
968     (1 . 0)
969     (2 . 0)
970     (3 . 0)
971     (4 . 0)
972     (5 . 0)
973     (6 . 0)
974     )
975   )