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