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