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