]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
2ac90b215594829be858ac75ba1eff069d6c6a37
[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 (define use-regex
22   (let ((os (string-downcase (vector-ref (uname) 0))))
23     (not (equal? "cygwin" (substring os 0 (min 6 (string-length os)))))))
24
25 ;; If you have trouble with regex, define #f
26 (define use-regex #t)
27 ;;(define use-regex #f)
28
29 ;; do nothing in .scm output
30 (define (comment s) "")
31
32 ;; URG guile-1.3/1.4 compatibility
33 (define (ly-eval x) (eval2 x #f))
34
35 (define (comment s) "")
36
37 (define (mm-to-pt x)
38   (* (/ 72.27 25.40) x)
39   )
40
41 (define (cons-map f x)
42   (cons (f (car x)) (f (cdr x))))
43
44 (define (reduce operator list)
45       (if (null? (cdr list)) (car list)
46           (operator (car list) (reduce operator (cdr list)))
47           )
48       )
49
50
51 (define (numbers->string l)
52   (apply string-append (map ly-number->string l)))
53
54 ; (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
55
56 (define (number->octal-string x)
57   (let* ((n (inexact->exact x))
58          (n64 (quotient n 64))
59          (n8 (quotient (- n (* n64 64)) 8)))
60     (string-append
61      (number->string n64)
62      (number->string n8)
63      (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
64
65 (define (inexact->string x radix)
66   (let ((n (inexact->exact x)))
67     (number->string n radix)))
68
69
70 (define (control->string c)
71   (string-append (number->string (car c)) " "
72                  (number->string (cdr c)) " "))
73
74
75 (define (font i)
76   (string-append
77    "font"
78    (make-string 1 (integer->char (+ (char->integer #\A) i)))
79    ))
80
81
82
83 (define (scm-scm action-name)
84   1)
85
86 (define security-paranoia #f)
87
88 ;; Spacing constants for prefatory matter.
89 ;;
90 ;; rules for this spacing are much more complicated than this. See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147
91 ;;
92 ;;
93
94 ;; (Measured in staff space)
95 (define space-alist
96  '(
97    ((none Instrument_name) . (extra-space 1.0))
98    ((Instrument_name Left_edge_item) . (extra-space 1.0))
99    ((Left_edge_item Clef_item) . (extra-space 1.0))
100    ((Left_edge_item Key_item) . (extra-space 0.0))   
101    ((Left_edge_item begin-of-note) . (extra-space 1.0))
102    ((none Left_edge_item) . (extra-space 0.0))
103    ((Left_edge_item Staff_bar) . (extra-space 0.0))
104 ;   ((none Left_edge_item) . (extra-space -15.0))
105 ;   ((none Left_edge_item) . (extra-space -15.0))
106    ((none Clef_item) . (minimum-space 1.0))
107    ((none Staff_bar) . (minimum-space 0.0))
108    ((none Clef_item) . (minimum-space 1.0))
109    ((none Key_item) . (minimum-space 0.5))
110    ((none Time_signature) . (extra-space 0.0))
111    ((none begin-of-note) . (minimum-space 1.5))
112    ((Clef_item Key_item) . (minimum-space 4.0))
113    ((Key_item Time_signature) . (extra-space 1.0))
114    ((Clef_item  Time_signature) . (minimum-space 3.5))
115    ((Staff_bar Clef_item) .   (minimum-space 1.0))
116    ((Clef_item  Staff_bar) .  (minimum-space 3.7))
117    ((Time_signature Staff_bar) .  (minimum-space 2.0))
118    ((Key_item  Staff_bar) .  (extra-space 1.0))
119    ((Staff_bar Time_signature) . (minimum-space 1.5)) ;double check this.
120    ((Time_signature begin-of-note) . (extra-space 2.0)) ;double check this.
121    ((Key_item begin-of-note) . (extra-space 2.5))
122    ((Staff_bar begin-of-note) . (extra-space 1.0))
123    ((Clef_item begin-of-note) . (minimum-space 5.0))
124    ((none Breathing_sign) . (minimum-space 0.0))
125    ((Breathing_sign Key_item) . (minimum-space 1.5))
126    ((Breathing_sign begin-of-note) . (minimum-space 1.0))
127    ((Breathing_sign Staff_bar) . (minimum-space 1.5))
128    ((Breathing_sign Clef_item) . (minimum-space 2.0))
129    )
130 )
131
132 ;; silly, use alist? 
133 (define (find-notehead-symbol duration style)
134   (case style
135    ((cross) "2cross")
136    ((harmonic) "0mensural")
137    ((baroque) 
138     (string-append (number->string duration)
139                    (if (< duration 0) "mensural" "")))
140    ((default) (number->string duration))
141    (else
142     (string-append (number->string duration) (symbol->string style)))))
143
144
145 ;;;;;;;; TeX
146
147 ;; this is silly, can't we use something like
148 ;; roman-0, roman-1 roman+1 ?
149 (define cmr-alist 
150   '(("bold" . "cmbx") 
151     ("brace" . "feta-braces")
152     ("default" . "cmr10")
153     ("dynamic" . "feta-din") 
154     ("feta" . "feta") 
155     ("feta-1" . "feta") 
156     ("feta-2" . "feta") 
157     ("typewriter" . "cmtt") 
158     ("italic" . "cmti") 
159     ("msam" . "msam") 
160     ("roman" . "cmr") 
161     ("script" . "cmr") 
162     ("large" . "cmbx") 
163     ("Large" . "cmbx") 
164     ("mark" . "feta-nummer") 
165     ("finger" . "feta-nummer")
166     ("timesig" . "feta-nummer")
167     ("number" . "feta-nummer") 
168     ("volta" . "feta-nummer"))
169 )
170
171 (define (string-encode-integer i)
172   (cond
173    ((= i  0) "o")
174    ((< i 0)   (string-append "n" (string-encode-integer (- i))))
175    (else (string-append
176           (make-string 1 (integer->char (+ 65 (modulo i 26))))
177           (string-encode-integer (quotient i 26))
178          )
179    )
180   )
181   )
182
183 (define (magstep i)
184   (cdr (assoc i '((-4 . 482)
185                   (-3 . 579)
186                   (-2 . 694)
187                   (-1 . 833)
188                   (0 . 1000)
189                   (1 . 1200) 
190                   (2 . 1440)
191                   (3 . 1728)
192                   (4 . 2074))
193               )
194        )
195   )
196              
197 (define script-alist '())
198
199 (define font-name-alist  '())
200 (define (font-command name-mag)
201     (cons name-mag
202           (string-append  "magfont"
203                           (string-encode-integer (hashq (car name-mag) 1000000))
204                           "m"
205                           (string-encode-integer (cdr name-mag)))
206
207           )
208     )
209 (define (define-fonts names)
210   (set! font-name-alist (map font-command names))
211   (apply string-append
212          (map (lambda (x)
213                 (font-load-command (car x) (cdr x))) font-name-alist)
214   ))
215
216 (define (fontify name exp)
217   (string-append (select-font name)
218                  exp)
219   )
220
221 ;;;;;;;;;;;;;;;;;;;;
222
223
224 ; Make a function that checks score element for being of a specific type. 
225 (define (make-type-checker symbol)
226   (lambda (elt)
227     (not (eq? #f (memq symbol (ly-get-elt-property elt 'interfaces))))
228     ))
229
230 ;;;;;;;;;;;;;;;;;;; TeX output
231 (define (tex-scm action-name)
232   (define (unknown) 
233     "%\n\\unknown%\n")
234
235
236   (define (select-font font-name-symbol)
237     (let*
238         (
239          (c (assoc font-name-symbol font-name-alist))
240          )
241
242       (if (eq? c #f)
243           (begin
244             (ly-warn (string-append
245                       "Programming error: No such font known " (car font-name-symbol)))
246             "")                         ; issue no command
247           (string-append "\\" (cdr c)))
248       
249       
250       ))
251   
252   (define (beam width slope thick)
253     (embedded-ps ((ps-scm 'beam) width slope thick)))
254
255   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
256     (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
257
258   (define (dashed-slur thick dash l)
259     (embedded-ps ((ps-scm 'dashed-slur)  thick dash l)))
260
261   (define (crescendo thick w h cont)
262     (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
263
264   (define (char i)
265     (string-append "\\char" (inexact->string i 10) " "))
266   
267   (define (dashed-line thick dash w)
268     (embedded-ps ((ps-scm 'dashed-line) thick dash w)))
269
270   (define (decrescendo thick w h cont)
271     (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
272
273   (define (font-load-command name-mag command)
274     (string-append
275      "\\font\\" command "="
276      (symbol->string (car name-mag))
277      " scaled "
278      (number->string (magstep (cdr name-mag)))
279      "\n"))
280
281   (define (embedded-ps s)
282     (string-append "\\embeddedps{" s "}"))
283
284   (define (comment s)
285     (string-append "% " s))
286   
287   (define (end-output) 
288         (begin
289 ; uncomment for some stats about lily memory      
290 ;               (display (gc-stats))
291     (string-append "\n\\EndLilyPondOutput"
292                    ; Put GC stats here.
293                    )))
294   
295   (define (experimental-on)
296     "")
297
298   (define (font-switch i)
299     (string-append
300      "\\" (font i) "\n"))
301
302   (define (font-def i s)
303     (string-append
304      "\\font" (font-switch i) "=" s "\n"))
305
306   (define (header-end)
307     (string-append
308      "\\special{! "
309
310      ;; URG: ly-gulp-file: now we can't use scm output without Lily
311      (if use-regex
312          ;; fixed in 1.3.4 for powerpc -- broken on Windows
313          (regexp-substitute/global #f "\n"
314                                    (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
315          (ly-gulp-file "lily.ps"))
316      "}"
317      "\\input lilyponddefs \\turnOnPostScript"))
318
319   (define (header creator generate) 
320     (string-append
321      "%created by: " creator generate "\n"))
322
323   (define (invoke-char s i)
324     (string-append 
325      "\n\\" s "{" (inexact->string i 10) "}" ))
326
327   (define (invoke-dim1 s d)
328     (string-append
329      "\n\\" s "{" (number->dim d) "}"))
330   (define (pt->sp x)
331     (* 65536 x))
332   
333   ;;
334   ;; need to do something to make this really safe.
335   ;;
336   (define (output-tex-string s)
337       (if security-paranoia
338           (if use-regex
339               (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
340               (begin (display "warning: not paranoid") (newline) s))
341           s))
342       
343   (define (lily-def key val)
344     (string-append
345      "\\def\\"
346      (if use-regex
347          ;; fixed in 1.3.4 for powerpc -- broken on Windows
348          (regexp-substitute/global #f "_"
349                                    (output-tex-string key) 'pre "X" 'post)
350          (output-tex-string key))
351      "{" (output-tex-string val) "}\n"))
352
353   (define (number->dim x)
354     (string-append 
355      (ly-number->string x) " pt "))
356
357   (define (placebox x y s) 
358     (string-append 
359      "\\placebox{"
360      (number->dim y) "}{" (number->dim x) "}{" s "}\n"))
361
362   (define (bezier-sandwich l thick)
363     (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
364
365   (define (start-line ht)
366       (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
367
368   (define (stop-line) 
369     "}\\vss}\\interscoreline\n")
370   (define (stop-last-line)
371     "}\\vss}")
372   (define (filledbox breapth width depth height) 
373     (string-append 
374      "\\kern" (number->dim (- breapth))
375      "\\vrule width " (number->dim (+ breapth width))
376      "depth " (number->dim depth)
377      "height " (number->dim height) " "))
378
379   (define (text s)
380     (string-append "\\hbox{" (output-tex-string s) "}"))
381   
382   (define (tuplet ht gapx dx dy thick dir)
383     (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
384
385   (define (volta h w thick vert_start vert_end)
386     (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
387
388   (define (define-origin file line col)
389     ; use this for column positions
390      (string-append "\\special{src:" (number->string line) ":"
391         (number->string col) " " file "}")
392
393      ; line numbers only:
394     ;(string-append "\\special{src:" (number->string line) " " file "}")
395 )
396   ; no-origin not yet supported by Xdvi
397   (define (no-origin) "")
398   
399   ;; TeX
400   ;; The procedures listed below form the public interface of TeX-scm.
401   ;; (should merge the 2 lists)
402   (cond ((eq? action-name 'all-definitions)
403          `(begin
404             (define font-load-command ,font-load-command)
405             (define beam ,beam)
406             (define bezier-sandwich ,bezier-sandwich)
407             (define bracket ,bracket)
408             (define char ,char)
409             (define crescendo ,crescendo)
410             (define dashed-line ,dashed-line) 
411             (define dashed-slur ,dashed-slur) 
412             (define decrescendo ,decrescendo) 
413             (define end-output ,end-output)
414             (define experimental-on ,experimental-on)
415             (define filledbox ,filledbox)
416             (define font-def ,font-def)
417             (define font-switch ,font-switch)
418             (define header-end ,header-end)
419             (define lily-def ,lily-def)
420             (define header ,header) 
421             (define invoke-char ,invoke-char) 
422             (define invoke-dim1 ,invoke-dim1)
423             (define placebox ,placebox)
424             (define select-font ,select-font)
425             (define start-line ,start-line)
426             (define stop-line ,stop-line)
427             (define stop-last-line ,stop-last-line)
428             (define text ,text)
429             (define tuplet ,tuplet)
430             (define volta ,volta)
431             (define define-origin ,define-origin)
432             (define no-origin ,no-origin)
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   (define (define-origin a b c ) "")
652   (define (no-origin) "")
653   
654   ;; PS
655   (cond ((eq? action-name 'all-definitions)
656          `(begin
657             (define beam ,beam)
658             (define tuplet ,tuplet)
659             (define bracket ,bracket)
660             (define char ,char)
661             (define crescendo ,crescendo)
662             (define volta ,volta)
663             (define bezier-sandwich ,bezier-sandwich)
664             (define dashed-line ,dashed-line) 
665             (define dashed-slur ,dashed-slur) 
666             (define decrescendo ,decrescendo) 
667             (define end-output ,end-output)
668             (define experimental-on ,experimental-on)
669             (define filledbox ,filledbox)
670             (define font-def ,font-def)
671             (define font-switch ,font-switch)
672             (define header-end ,header-end)
673             (define lily-def ,lily-def)
674             (define font-load-command ,font-load-command)
675             (define header ,header) 
676             (define invoke-char ,invoke-char) 
677             (define invoke-dim1 ,invoke-dim1)
678             (define placebox ,placebox)
679             (define select-font ,select-font)
680             (define start-line ,start-line)
681             (define stem ,stem)
682             (define stop-line ,stop-line)
683             (define stop-last-line ,stop-line)
684             (define text ,text)
685             (define no-origin ,no-origin)
686             (define define-origin ,define-origin)
687             ))
688         ((eq? action-name 'tuplet) tuplet)
689         ((eq? action-name 'beam) beam)
690         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
691         ((eq? action-name 'bracket) bracket)
692         ((eq? action-name 'char) char)
693         ((eq? action-name 'crescendo) crescendo)
694         ((eq? action-name 'dashed-line) dashed-line) 
695         ((eq? action-name 'dashed-slur) dashed-slur) 
696         ((eq? action-name 'decrescendo) decrescendo)
697         ((eq? action-name 'experimental-on) experimental-on)
698         ((eq? action-name 'filledbox) filledbox)
699         ((eq? action-name 'select-font) select-font)
700         ((eq? action-name 'volta) volta)
701         (else (error "unknown tag -- PS-SCM " action-name))
702         )
703   )
704
705
706 (define (arg->string arg)
707   (cond ((number? arg) (inexact->string arg 10))
708         ((string? arg) (string-append "\"" arg "\""))
709         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
710
711 (define (func name . args)
712   (string-append 
713    "(" name 
714    (if (null? args) 
715        ""
716        (apply string-append 
717               (map (lambda (x) (string-append " " (arg->string x))) args)))
718    ")\n"))
719
720 (define (sign x)
721   (if (= x 0)
722       1
723       (if (< x 0) -1 1)))
724
725 ;;;; AsciiScript as
726 (define (as-scm action-name)
727
728   (define (beam width slope thick)
729           (string-append
730            (func "set-line-char" "#")
731            (func "rline-to" width (* width slope))
732            ))
733
734   ; simple flat slurs
735   (define (bezier-sandwich l thick)
736           (let (
737                 (c0 (cadddr l))
738                 (c1 (cadr l))
739                 (c3 (caddr l)))
740                (let* ((x (car c0))
741                       (dx (- (car c3) x))
742                       (dy (- (cdr c3) (cdr c0)))
743                       (rc (/ dy dx))
744                       (c1-dx (- (car c1) x))
745                       (c1-line-y (+ (cdr c0) (* c1-dx rc)))
746                       (dir (if (< c1-line-y (cdr c1)) 1 -1))
747                       (y (+ -1 (* dir (max (* dir (cdr c0)) (* dir (cdr c3)))))))
748                      (string-append
749                       (func "rmove-to" x y)
750                       (func "put" (if (< 0 dir) "/" "\\\\"))
751                       (func "rmove-to" 1 (if (< 0 dir) 1 0))
752                       (func "set-line-char" "_")
753                       (func "h-line" (- dx 1))
754                       (func "rmove-to" (- dx 1) (if (< 0 dir) -1 0))
755                       (func "put" (if (< 0 dir) "\\\\" "/"))))))
756
757   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
758           (string-append
759            (func "rmove-to" (+ width 1) (- (/ height -2) 1))
760            (func "put" "\\\\")
761            (func "set-line-char" "|")
762            (func "rmove-to" 0 1)
763            (func "v-line" (+ height 1))
764            (func "rmove-to" 0 (+ height 1))
765            (func "put" "/")
766            ))
767
768   (define (char i)
769     (func "char" i))
770
771   (define (define-origin a b c ) "")
772
773   (define (end-output) 
774     (func "end-output"))
775   
776   (define (experimental-on)
777           "")
778
779   (define (filledbox breapth width depth height)
780           (let ((dx (+ width breapth))
781                 (dy (+ depth height)))
782                (string-append 
783                 (func "rmove-to" (* -1 breapth) (* -1 depth))
784                 (if (< dx dy)
785                     (string-append
786                      (func "set-line-char" 
787                            (if (<= dx 1) "|" "#"))
788                      (func "v-line" dy))
789                     (string-append
790                      (func "set-line-char" 
791                            (if (<= dy 1) "-" "="))
792                     (func "h-line" dx))))))
793
794   (define (font-load-command name-mag command)
795     (func "load-font" (car name-mag) (magstep (cdr name-mag))))
796
797   (define (header creator generate) 
798     (func "header" creator generate))
799
800   (define (header-end) 
801     (func "header-end"))
802
803   ;; urg: this is good for half of as2text's execution time
804   (define (xlily-def key val)
805           (string-append "(define " key " " (arg->string val) ")\n"))
806
807   (define (lily-def key val)
808           (if 
809            (or (equal? key "mudelapaperlinewidth")
810                (equal? key "mudelapaperstaffheight"))
811            (string-append "(define " key " " (arg->string val) ")\n")
812            ""))
813
814   (define (no-origin) "")
815   
816   (define (placebox x y s) 
817     (let ((ey (inexact->exact y)))
818           (string-append "(move-to " (number->string (inexact->exact x)) " "
819                          (if (= 0.5 (- (abs y) (abs ey)))
820                              (number->string y)
821                              (number->string ey))
822                          ")\n" s)))
823                        
824   (define (select-font font-name-symbol)
825     (let* ((c (assoc font-name-symbol font-name-alist)))
826       (if (eq? c #f)
827           (begin
828             (ly-warn 
829              (string-append 
830               "Programming error: No such font known " 
831               (car font-name-symbol)))
832             "")                         ; issue no command
833           (func "select-font" (car font-name-symbol)))))
834
835   (define (start-line height)
836           (func "start-line" height))
837
838   (define (stop-line)
839           (func "stop-line"))
840
841   (define (text s)
842           (func "text" s))
843
844   (define (tuplet ht gap dx dy thick dir) "")
845
846   (define (volta h w thick vert-start vert-end)
847           ;; urg
848           (string-append
849            (func "set-line-char" "|")
850            (func "rmove-to" 0 -4)
851            ;; definition strange-way around
852            (if (= 0 vert-start)
853               (func "v-line" h)
854                "")
855            (func "rmove-to" 1 h)
856            (func "set-line-char" "_")
857            (func "h-line" (- w 1))
858            (func "set-line-char" "|")
859            (if (= 0 vert-end)
860                (string-append
861                 (func "rmove-to" (- w 1) (* -1 h))
862                 (func "v-line" (* -1 h)))
863                "")))
864
865   (cond ((eq? action-name 'all-definitions)
866          `(begin
867             (define beam ,beam)
868             (define bracket ,bracket)
869             (define char ,char)
870             (define define-origin ,define-origin)
871             ;;(define crescendo ,crescendo)
872             (define bezier-sandwich ,bezier-sandwich)
873             ;;(define dashed-slur ,dashed-slur) 
874             ;;(define decrescendo ,decrescendo) 
875             (define end-output ,end-output)
876             (define experimental-on ,experimental-on)
877             (define filledbox ,filledbox)
878             ;;(define font-def ,font-def)
879             (define font-load-command ,font-load-command)
880             ;;(define font-switch ,font-switch)
881             (define header ,header) 
882             (define header-end ,header-end)
883             (define lily-def ,lily-def)
884             ;;(define invoke-char ,invoke-char) 
885             ;;(define invoke-dim1 ,invoke-dim1)
886             (define no-origin ,no-origin)
887             (define placebox ,placebox)
888             (define select-font ,select-font)
889             (define start-line ,start-line)
890             ;;(define stem ,stem)
891             (define stop-line ,stop-line)
892             (define stop-last-line ,stop-line)
893             (define text ,text)
894             (define tuplet ,tuplet)
895             (define volta ,volta)
896             ))
897         ((eq? action-name 'tuplet) tuplet)
898         ;;((eq? action-name 'beam) beam)
899         ;;((eq? action-name 'bezier-sandwich) bezier-sandwich)
900         ;;((eq? action-name 'bracket) bracket)
901         ((eq? action-name 'char) char)
902         ;;((eq? action-name 'crescendo) crescendo)
903         ;;((eq? action-name 'dashed-slur) dashed-slur) 
904         ;;((eq? action-name 'decrescendo) decrescendo)
905         ;;((eq? action-name 'experimental-on) experimental-on)
906         ((eq? action-name 'filledbox) filledbox)
907         ((eq? action-name 'select-font) select-font)
908         ;;((eq? action-name 'volta) volta)
909         (else (error "unknown tag -- MUSA-SCM " action-name))
910         )
911   )
912
913
914 (define (gulp-file name)
915   (let* ((port (open-file name "r"))
916          (content (let loop ((text ""))
917                        (let ((line (read-line port)))
918                             (if (or (eof-object? line)
919                                     (not line)) 
920                                 text
921                                 (loop (string-append text line "\n")))))))
922         (close port)
923         content))
924
925 ;; urg: Use when standalone, do:
926 ;; (define ly-gulp-file scm-gulp-file)
927 (define (scm-gulp-file name)
928   (set! %load-path 
929         (cons (string-append (getenv 'LILYPONDPREFIX) "/ly")
930               (cons (string-append (getenv 'LILYPONDPREFIX) "/ps")
931                     %load-path)))
932   (let ((path (%search-load-path name)))
933        (if path
934            (gulp-file path)
935            (gulp-file name))))
936
937 (define (scm-tex-output)
938   (ly-eval (tex-scm 'all-definitions)))
939                                 
940 (define (scm-ps-output)
941   (ly-eval (ps-scm 'all-definitions)))
942
943 (define (scm-as-output)
944   (ly-eval (as-scm 'all-definitions)))
945         
946 (define (index-cell cell dir)
947   (if (equal? dir 1)
948       (cdr cell)
949       (car cell)))
950
951 ;
952 ; How should a  bar line behave at a break? 
953 ;
954 (define (break-barline glyph dir)
955    (let ((result (assoc glyph 
956                         '((":|:" . (":|" . "|:"))
957                           ("|" . ("|" . ""))
958                           ("|s" . (nil . "|"))
959                           ("|:" . ("|" . "|:"))
960                           ("|." . ("|." . nil))
961                           (".|" . (nil . ".|"))
962                           (":|" . (":|" . nil))
963                           ("||" . ("||" . nil))
964                           (".|." . (".|." . nil))
965                           ("scorebar" . (nil . "scorepostbreak"))
966                           ("brace" . (nil . "brace"))
967                           ("bracket" . (nil . "bracket"))  
968                           )
969                         )))
970
971      (if (equal? result #f)
972          (ly-warn (string-append "Unknown bar glyph: `" glyph "'"))
973          (index-cell (cdr result) dir))
974      )
975    )
976      
977
978 (define major-scale
979   '(
980     (0 . 0)
981     (1 . 0)
982     (2 . 0)
983     (3 . 0)
984     (4 . 0)
985     (5 . 0)
986     (6 . 0)
987     )
988   )