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