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