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