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