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