]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
e7da9d43a9e12134c1cdeedbc5eb4fe49a637e26
[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 a 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 ;; Score_span_bars are only visible at start of line
94 ;; i.e. if break_dir == RIGHT == 1
95 (define Span_bar_engraver-visibility begin-of-line-invisible)
96 (define mark-visibility end-of-line-invisible)
97
98 ;; Spacing constants for prefatory matter.
99 ;;
100 ;; rules for this spacing are much more complicated than this. See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147
101 ;;
102 ;;
103
104 ;; (Measured in staff space)
105 (define space-alist
106  '(
107    ((none Instrument_name) . (extra-space 1.0))
108    ((Instrument_name Left_edge_item) . (extra-space 1.0))
109    ((Left_edge_item Clef_item) . (extra-space 1.0))
110    ((none Left_edge_item) . (extra-space 0.0))
111    ((Left_edge_item Staff_bar) . (extra-space 0.0))         
112 ;   ((none Left_edge_item) . (extra-space -15.0))
113 ;   ((none Left_edge_item) . (extra-space -15.0))
114    ((none Clef_item) . (minimum-space 1.0))
115    ((none Staff_bar) . (minimum-space 0.0))
116    ((none Clef_item) . (minimum-space 1.0))
117    ((none Key_item) . (minimum-space 0.5))
118    ((none Time_signature) . (extra-space 0.0))
119    ((none begin-of-note) . (minimum-space 1.5))
120    ((Clef_item Key_item) . (minimum-space 4.0))
121    ((Key_item Time_signature) . (extra-space 1.0))
122    ((Clef_item  Time_signature) . (minimum-space 3.5))
123    ((Staff_bar Clef_item) .   (minimum-space 1.0))
124    ((Clef_item  Staff_bar) .  (minimum-space 3.7))
125    ((Time_signature Staff_bar) .  (minimum-space 2.0))
126    ((Key_item  Staff_bar) .  (extra-space 1.0))
127    ((Staff_bar Time_signature) . (minimum-space 1.5)) ;double check this.
128    ((Time_signature begin-of-note) . (extra-space 2.0)) ;double check this.
129    ((Key_item begin-of-note) . (extra-space 2.5))
130    ((Staff_bar begin-of-note) . (extra-space 1.0))
131    ((Clef_item begin-of-note) . (minimum-space 5.0))
132    ((none Breathing_sign) . (minimum-space 0.0))
133    ((Breathing_sign Key_item) . (minimum-space 1.5))
134    ((Breathing_sign begin-of-note) . (minimum-space 1.0))
135    ((Breathing_sign Staff_bar) . (minimum-space 1.5))
136    ((Breathing_sign Clef_item) . (minimum-space 2.0))
137    )
138 )
139
140 ;; (define (ly-warn s) (error s))
141 (define (break-align-spacer this next)
142   (let ((entry (assoc (list this next) space-alist)))
143     (if entry
144         (cdr entry)
145         (begin (ly-warn (string-append "Unknown spacing pair `" this "', `" next "'"))
146                '(minimum-space 0.0)))))
147   
148
149 (define (find-notehead-symbol duration style)
150   (case style
151    ((cross) "2cross")
152    ((harmonic) "0mensural")
153    ((baroque) 
154     (string-append (number->string duration)
155                    (if (< duration 0) "mensural" "")))
156    ((default) (number->string duration))
157    (else
158     (string-append (number->string duration) (symbol->string style)))))
159
160
161 ;;;;;;;; TeX
162
163 ;; this is silly, can't we use something like
164 ;; roman-0, roman-1 roman+1 ?
165 (define cmr-alist 
166   '(("bold" . "cmbx") 
167     ("brace" . "feta-braces")
168     ("default" . "cmr10")
169     ("dynamic" . "feta-din") 
170     ("feta" . "feta") 
171     ("feta-1" . "feta") 
172     ("feta-2" . "feta") 
173     ("typewriter" . "cmtt") 
174     ("italic" . "cmti") 
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     "\n\\EndLilyPondOutput")
315   
316   (define (experimental-on)
317     "")
318
319   (define (font-switch i)
320     (string-append
321      "\\" (font i) "\n"))
322
323   (define (font-def i s)
324     (string-append
325      "\\font" (font-switch i) "=" s "\n"))
326
327   (define (header-end)
328     (string-append
329      "\\special{! "
330
331      ;; URG: ly-gulp-file: now we can't use scm output without Lily
332      (if use-regex
333          ;; fixed in 1.3.4 for powerpc -- broken on Windows
334          (regexp-substitute/global #f "\n"
335                                    (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
336          (ly-gulp-file "lily.ps"))
337      "}"
338      "\\input lilyponddefs \\turnOnPostScript"))
339
340   (define (header creator generate) 
341     (string-append
342      "%created by: " creator generate "\n"))
343
344   (define (invoke-char s i)
345     (string-append 
346      "\n\\" s "{" (inexact->string i 10) "}" ))
347
348   (define (invoke-dim1 s d)
349     (string-append
350      "\n\\" s "{" (number->dim d) "}"))
351   (define (pt->sp x)
352     (* 65536 x))
353   
354   ;;
355   ;; need to do something to make this really safe.
356   ;;
357   (define (output-tex-string s)
358       (if security-paranoia
359           (if use-regex
360               (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
361               (begin (display "warning: not paranoid") (newline) s))
362           s))
363       
364   (define (lily-def key val)
365     (string-append
366      "\\def\\"
367      (if use-regex
368          ;; fixed in 1.3.4 for powerpc -- broken on Windows
369          (regexp-substitute/global #f "_"
370                                    (output-tex-string key) 'pre "X" 'post)
371          (output-tex-string key))
372      "{" (output-tex-string val) "}\n"))
373
374   (define (number->dim x)
375     (string-append 
376      (ly-number->string x) " pt "))
377
378   (define (placebox x y s) 
379     (string-append 
380      "\\placebox{"
381      (number->dim y) "}{" (number->dim x) "}{" s "}\n"))
382
383   (define (bezier-sandwich l thick)
384     (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
385
386   (define (start-line ht)
387       (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
388
389   (define (stop-line) 
390     "}\\vss}\\interscoreline")
391   (define (stop-last-line)
392     "}\\vss}")
393   (define (filledbox breapth width depth height) 
394     (string-append 
395      "\\kern" (number->dim (- breapth))
396      "\\vrule width " (number->dim (+ breapth width))
397      "depth " (number->dim depth)
398      "height " (number->dim height) " "))
399
400   (define (text s)
401     (string-append "\\hbox{" (output-tex-string s) "}"))
402   
403   (define (tuplet ht gapx dx dy thick dir)
404     (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
405
406   (define (volta h w thick vert_start vert_end)
407     (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
408
409   ;; TeX
410   ;; The procedures listed below form the public interface of TeX-scm.
411   ;; (should merge the 2 lists)
412   (cond ((eq? action-name 'all-definitions)
413          `(begin
414             (define font-load-command ,font-load-command)
415             (define beam ,beam)
416             (define bezier-sandwich ,bezier-sandwich)
417             (define bracket ,bracket)
418             (define char ,char)
419             (define crescendo ,crescendo)
420             (define dashed-line ,dashed-line) 
421             (define dashed-slur ,dashed-slur) 
422             (define decrescendo ,decrescendo) 
423             (define end-output ,end-output)
424             (define experimental-on ,experimental-on)
425             (define filledbox ,filledbox)
426             (define font-def ,font-def)
427             (define font-switch ,font-switch)
428             (define header-end ,header-end)
429             (define lily-def ,lily-def)
430             (define header ,header) 
431             (define invoke-char ,invoke-char) 
432             (define invoke-dim1 ,invoke-dim1)
433             (define placebox ,placebox)
434             (define select-font ,select-font)
435             (define start-line ,start-line)
436             (define stop-line ,stop-line)
437             (define stop-last-line ,stop-last-line)
438             (define text ,text)
439             (define tuplet ,tuplet)
440             (define volta ,volta)
441             ))
442
443         ((eq? action-name 'beam) beam)
444         ((eq? action-name 'tuplet) tuplet)
445         ((eq? action-name 'bracket) bracket)
446         ((eq? action-name 'crescendo) crescendo)
447         ((eq? action-name 'dashed-line) dashed-line) 
448         ((eq? action-name 'dashed-slur) dashed-slur) 
449         ((eq? action-name 'decrescendo) decrescendo) 
450         ((eq? action-name 'end-output) end-output)
451         ((eq? action-name 'experimental-on) experimental-on)
452         ((eq? action-name 'font-def) font-def)
453         ((eq? action-name 'font-switch) font-switch)
454         ((eq? action-name 'header-end) header-end)
455         ((eq? action-name 'lily-def) lily-def)
456         ((eq? action-name 'header) header) 
457         ((eq? action-name 'invoke-char) invoke-char) 
458         ((eq? action-name 'invoke-dim1) invoke-dim1)
459         ((eq? action-name 'placebox) placebox)
460         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
461         ((eq? action-name 'start-line) start-line)
462         ((eq? action-name 'stem) stem)
463         ((eq? action-name 'stop-line) stop-line)
464         ((eq? action-name 'stop-last-line) stop-last-line)
465         ((eq? action-name 'volta) volta)
466         (else (error "unknown tag -- PS-TEX " action-name))
467         )
468   )
469
470
471 ;;;;;;;;;;;; PS
472 (define (ps-scm action-name)
473
474   ;; alist containing fontname -> fontcommand assoc (both strings)
475   (define font-alist '())
476   (define font-count 0)
477   (define current-font "")
478
479   
480   (define (cached-fontname i)
481     (string-append
482      "lilyfont"
483      (make-string 1 (integer->char (+ 65 i)))))
484     
485   (define (mag-to-size m)
486     (number->string (case m 
487                       (0 12)
488                       (1 12)
489                       (2 14) ; really: 14.400
490                       (3 17) ; really: 17.280
491                       (4 21) ; really: 20.736
492                       (5 24) ; really: 24.888
493                       (6 30) ; really: 29.856
494                       )))
495   
496   
497   (define (select-font font-name-symbol)
498     (let*
499         (
500          (c (assoc font-name-symbol font-name-alist))
501          )
502
503       (if (eq? c #f)
504           (begin
505             (ly-warn (string-append
506                       "Programming error: No such font known " (car font-name-symbol)))
507             "")                         ; issue no command
508           (string-append " " (cdr c) " "))
509       
510       
511       ))
512
513     (define (font-load-command name-mag command)
514       (string-append
515        "/" command
516        " { /"
517        (symbol->string (car name-mag))
518        " findfont "
519        (number->string (magstep (cdr name-mag)))
520        " 1000 div 12 mul  scalefont setfont } bind def "
521        "\n"))
522
523
524   (define (beam width slope thick)
525     (string-append
526      (numbers->string (list width slope thick)) " draw_beam" ))
527
528   (define (comment s)
529     (string-append "% " s))
530
531   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
532     (string-append
533      (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
534
535   (define (char i)
536     (invoke-char " show" i))
537
538   (define (crescendo thick w h cont )
539     (string-append 
540      (numbers->string (list w h (inexact->exact cont) thick))
541      " draw_crescendo"))
542
543   ;; what the heck is this interface ?
544   (define (dashed-slur thick dash l)
545     (string-append 
546      (apply string-append (map control->string l)) 
547      (number->string thick) 
548      " [ "
549      (number->string dash)
550      " "
551      (number->string (* 10 thick))      ;UGH.  10 ?
552      " ] 0 draw_dashed_slur"))
553
554   (define (dashed-line thick dash width)
555     (string-append 
556      (number->string width) 
557      " "
558      (number->string thick) 
559      " [ "
560      (number->string dash)
561      " "
562      (number->string dash)
563      " ] 0 draw_dashed_line"))
564
565   (define (decrescendo thick w h cont)
566     (string-append 
567      (numbers->string (list w h (inexact->exact cont) thick))
568      " draw_decrescendo"))
569
570
571   (define (end-output)
572     "\nshowpage\n")
573   
574   (define (experimental-on) "")
575   
576   (define (filledbox breapth width depth height) 
577     (string-append (numbers->string (list breapth width depth height))
578                    " draw_box" ))
579
580   ;; obsolete?
581   (define (font-def i s)
582     (string-append
583      "\n/" (font i) " {/" 
584      (substring s 0 (- (string-length s) 4))
585      " findfont 12 scalefont setfont} bind def \n"))
586
587   (define (font-switch i)
588     (string-append (font i) " "))
589
590   (define (header-end)
591     (string-append
592      ;; URG: now we can't use scm output without Lily
593      (ly-gulp-file "lilyponddefs.ps")
594      " {exch pop //systemdict /run get exec} "
595      (ly-gulp-file "lily.ps")
596      "{ exch pop //systemdict /run get exec } "
597     ))
598   
599   (define (lily-def key val)
600
601      (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
602          (string-append "/" key " {" val "} bind def\n")
603          (string-append "/" key " (" val ") def\n")
604          )
605      )
606
607   (define (header creator generate) 
608     (string-append
609      "%!PS-Adobe-3.0\n"
610      "%%Creator: " creator generate "\n"))
611   
612   (define (invoke-char s i)
613     (string-append 
614      "(\\" (inexact->string i 8) ") " s " " ))
615   
616   (define (invoke-dim1 s d) 
617     (string-append
618      (number->string (* d  (/ 72.27 72))) " " s ))
619
620   (define (placebox x y s) 
621     (string-append 
622      (number->string x) " " (number->string y) " {" s "} placebox "))
623
624   (define (bezier-sandwich l thick)
625     (string-append 
626      (apply string-append (map control->string l))
627      (number->string  thick)
628      " draw_bezier_sandwich"))
629
630   (define (start-line height)
631           "\nstart_line {\n")
632   
633   (define (stem breapth width depth height) 
634     (string-append (numbers->string (list breapth width depth height))
635                    " draw_box" ))
636
637   (define (stop-line)
638       "}\nstop_line\n")
639
640   (define (text s)
641     (string-append "(" s ") show  "))
642
643
644   (define (volta h w thick vert_start vert_end)
645     (string-append 
646      (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
647      " draw_volta"))
648
649   (define (tuplet ht gap dx dy thick dir)
650     (string-append 
651      (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
652      " draw_tuplet"))
653
654
655   (define (unknown) 
656     "\n unknown\n")
657
658
659   ;; PS
660   (cond ((eq? action-name 'all-definitions)
661          `(begin
662             (define beam ,beam)
663             (define tuplet ,tuplet)
664             (define bracket ,bracket)
665             (define char ,char)
666             (define crescendo ,crescendo)
667             (define volta ,volta)
668             (define bezier-sandwich ,bezier-sandwich)
669             (define dashed-line ,dashed-line) 
670             (define dashed-slur ,dashed-slur) 
671             (define decrescendo ,decrescendo) 
672             (define end-output ,end-output)
673             (define experimental-on ,experimental-on)
674             (define filledbox ,filledbox)
675             (define font-def ,font-def)
676             (define font-switch ,font-switch)
677             (define header-end ,header-end)
678             (define lily-def ,lily-def)
679             (define font-load-command ,font-load-command)
680             (define header ,header) 
681             (define invoke-char ,invoke-char) 
682             (define invoke-dim1 ,invoke-dim1)
683             (define placebox ,placebox)
684             (define select-font ,select-font)
685             (define start-line ,start-line)
686             (define stem ,stem)
687             (define stop-line ,stop-line)
688             (define stop-last-line ,stop-line)
689             (define text ,text)
690             ))
691         ((eq? action-name 'tuplet) tuplet)
692         ((eq? action-name 'beam) beam)
693         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
694         ((eq? action-name 'bracket) bracket)
695         ((eq? action-name 'char) char)
696         ((eq? action-name 'crescendo) crescendo)
697         ((eq? action-name 'dashed-line) dashed-line) 
698         ((eq? action-name 'dashed-slur) dashed-slur) 
699         ((eq? action-name 'decrescendo) decrescendo)
700         ((eq? action-name 'experimental-on) experimental-on)
701         ((eq? action-name 'filledbox) filledbox)
702         ((eq? action-name 'select-font) select-font)
703         ((eq? action-name 'volta) volta)
704         (else (error "unknown tag -- PS-SCM " action-name))
705         )
706   )
707
708
709 (define (arg->string arg)
710   (cond ((number? arg) (inexact->string arg 10))
711         ((string? arg) (string-append "\"" arg "\""))
712         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
713
714 (define (func name . args)
715   (string-append 
716    "(" name 
717    (if (null? args) 
718        ""
719        (apply string-append 
720               (map (lambda (x) (string-append " " (arg->string x))) args)))
721    ")\n"))
722
723 (define (sign x)
724   (if (= x 0)
725       1
726       (inexact->exact (/ x (abs x)))))
727
728 ;;;; AsciiScript as
729 (define (as-scm action-name)
730
731   (define (beam width slope thick)
732           (string-append
733            (func "set-line-char" "#")
734            (func "rline-to" width (* width slope))
735            ))
736
737   ; simple flat slurs
738   (define (bezier-sandwich l thick)
739           (let (
740                 (c0 (cadddr l))
741                 (c1 (cadr l))
742                 (c3 (caddr l)))
743                (let* ((x (car c0))
744                       (dx (- (car c3) x))
745                       (dy (- (cdr c3) (cdr c0)))
746                       (rc (/ dy dx))
747                       (c1-dx (- (car c1) x))
748                       (c1-line-y (+ (cdr c0) (* c1-dx rc)))
749                       (dir (if (< c1-line-y (cdr c1)) 1 -1))
750                       (y (+ -1 (* dir (max (* dir (cdr c0)) (* dir (cdr c3)))))))
751                      (string-append
752                       (func "rmove-to" x y)
753                       (func "put" (if (< 0 dir) "/" "\\\\"))
754                       (func "rmove-to" 1 (if (< 0 dir) 1 0))
755                       (func "set-line-char" "_")
756                       (func "h-line" (- dx 1))
757                       (func "rmove-to" (- dx 1) (if (< 0 dir) -1 0))
758                       (func "put" (if (< 0 dir) "\\\\" "/"))))))
759
760   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
761           (string-append
762            (func "rmove-to" (+ width 1) (- (/ height -2) 1))
763            (func "put" "\\\\")
764            (func "set-line-char" "|")
765            (func "rmove-to" 0 1)
766            (func "v-line" (+ height 1))
767            (func "rmove-to" 0 (+ height 1))
768            (func "put" "/")
769            ))
770
771   (define (char i)
772     (func "char" i))
773
774   (define (end-output) 
775     (func "end-output"))
776   
777   (define (experimental-on)
778           "")
779
780   (define (filledbox breapth width depth height)
781           (let ((dx (+ width breapth))
782                 (dy (+ depth height)))
783                (string-append 
784                 (func "rmove-to" (* -1 breapth) (* -1 depth))
785                 (if (< dx dy)
786                     (string-append
787                      (func "set-line-char" 
788                            (if (<= dx 1) "|" "#"))
789                      (func "v-line" dy))
790                     (string-append
791                      (func "set-line-char" 
792                            (if (<= dy 1) "-" "="))
793                     (func "h-line" dx))))))
794
795   (define (font-load-command name-mag command)
796     (func "load-font" (car name-mag) (magstep (cdr name-mag))))
797
798   (define (header creator generate) 
799     (func "header" creator generate))
800
801   (define (header-end) 
802     (func "header-end"))
803
804   ;; urg: this is good for half of as2text's execution time
805   (define (xlily-def key val)
806           (string-append "(define " key " " (arg->string val) ")\n"))
807
808   (define (lily-def key val)
809           (if 
810            (or (equal? key "mudelapaperlinewidth")
811                (equal? key "mudelapaperstaffheight"))
812            (string-append "(define " key " " (arg->string val) ")\n")
813            ""))
814
815   (define (placebox x y s) 
816     (let ((ey (inexact->exact y)))
817           (string-append "(move-to " (number->string (inexact->exact x)) " "
818                          (if (= 0.5 (- (abs y) (abs ey)))
819                              (number->string y)
820                              (number->string ey))
821                          ")\n" s)))
822                        
823   (define (select-font font-name-symbol)
824     (let* ((c (assoc font-name-symbol font-name-alist)))
825       (if (eq? c #f)
826           (begin
827             (ly-warn 
828              (string-append 
829               "Programming error: No such font known " 
830               (car font-name-symbol)))
831             "")                         ; issue no command
832           (func "select-font" (car font-name-symbol)))))
833
834   (define (start-line height)
835           (func "start-line" height))
836
837   (define (stop-line)
838           (func "stop-line"))
839
840   (define (text s)
841           (func "text" s))
842
843   (define (volta h w thick vert-start vert-end)
844           ;; urg
845           (string-append
846            (func "set-line-char" "|")
847            (func "rmove-to" 0 -4)
848            ;; definition strange-way around
849            (if (= 0 vert-start)
850               (func "v-line" h)
851                "")
852            (func "rmove-to" 1 h)
853            (func "set-line-char" "_")
854            (func "h-line" (- w 1))
855            (func "set-line-char" "|")
856            (if (= 0 vert-end)
857                (string-append
858                 (func "rmove-to" (- w 1) (* -1 h))
859                 (func "v-line" (* -1 h)))
860                "")))
861
862   (cond ((eq? action-name 'all-definitions)
863          `(begin
864             (define beam ,beam)
865             (define bracket ,bracket)
866             (define char ,char)
867             ;;(define crescendo ,crescendo)
868             (define bezier-sandwich ,bezier-sandwich)
869             ;;(define dashed-slur ,dashed-slur) 
870             ;;(define decrescendo ,decrescendo) 
871             (define end-output ,end-output)
872             (define experimental-on ,experimental-on)
873             (define filledbox ,filledbox)
874             ;;(define font-def ,font-def)
875             (define font-load-command ,font-load-command)
876             ;;(define font-switch ,font-switch)
877             (define header ,header) 
878             (define header-end ,header-end)
879             (define lily-def ,lily-def)
880             ;;(define invoke-char ,invoke-char) 
881             ;;(define invoke-dim1 ,invoke-dim1)
882             (define placebox ,placebox)
883             (define select-font ,select-font)
884             (define start-line ,start-line)
885             ;;(define stem ,stem)
886             (define stop-line ,stop-line)
887             (define stop-last-line ,stop-line)
888             (define text ,text)
889             ;;(define tuplet ,tuplet)
890             (define volta ,volta)
891             ))
892         ;;((eq? action-name 'tuplet) tuplet)
893         ;;((eq? action-name 'beam) beam)
894         ;;((eq? action-name 'bezier-sandwich) bezier-sandwich)
895         ;;((eq? action-name 'bracket) bracket)
896         ((eq? action-name 'char) char)
897         ;;((eq? action-name 'crescendo) crescendo)
898         ;;((eq? action-name 'dashed-slur) dashed-slur) 
899         ;;((eq? action-name 'decrescendo) decrescendo)
900         ;;((eq? action-name 'experimental-on) experimental-on)
901         ((eq? action-name 'filledbox) filledbox)
902         ((eq? action-name 'select-font) select-font)
903         ;;((eq? action-name 'volta) volta)
904         (else (error "unknown tag -- MUSA-SCM " action-name))
905         )
906   )
907
908
909 (define (gulp-file name)
910   (let* ((port (open-file name "r"))
911          (content (let loop ((text ""))
912                        (let ((line (read-line port)))
913                             (if (or (eof-object? line)
914                                     (not line)) 
915                                 text
916                                 (loop (string-append text line "\n")))))))
917         (close port)
918         content))
919
920 ;; urg: Use when standalone, do:
921 ;; (define (ly-gulp-file name) (scm-gulp-file name))
922 (define (scm-gulp-file name)
923   (set! %load-path 
924         (cons (string-append (getenv 'LILYPONDPREFIX) "/ly")
925               (cons (string-append (getenv 'LILYPONDPREFIX) "/ps")
926                     %load-path)))
927   (let ((path (%search-load-path name)))
928        (if path
929            (gulp-file path)
930            (gulp-file name))))
931
932 (define (scm-tex-output)
933   (eval (tex-scm 'all-definitions)))
934                                 
935 (define (scm-ps-output)
936   (eval (ps-scm 'all-definitions)))
937
938 (define (scm-as-output)
939   (eval (as-scm 'all-definitions)))
940                                 
941 ; Russ McManus, <mcmanus@IDT.NET>  
942
943 ; I use the following, which should definitely be provided somewhere
944 ; in guile, but isn't, AFAIK:
945
946
947
948 (define (hash-table-for-each fn ht)
949   (do ((i 0 (+ 1 i)))
950       ((= i (vector-length ht)))
951     (do ((alist (vector-ref ht i) (cdr alist)))
952         ((null? alist) #t)
953       (fn (car (car alist)) (cdr (car alist))))))
954
955 (define (hash-table-map fn ht)
956   (do ((i 0 (+ 1 i))
957        (ret-ls '()))
958       ((= i (vector-length ht)) (reverse ret-ls))
959     (do ((alist (vector-ref ht i) (cdr alist)))
960         ((null? alist) #t)
961       (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
962
963
964
965 (define (index-cell cell dir)
966   (if (equal? dir 1)
967       (cdr cell)
968       (car cell)))
969
970 ;
971 ; How should a  bar line behave at a break? 
972 ;
973 (define (break-barline glyph dir)
974    (let ((result (assoc glyph 
975                         '((":|:" . (":|" . "|:"))
976                           ("|" . ("|" . ""))
977                           ("|s" . (nil . "|"))
978                           ("|:" . ("|" . "|:"))
979                           ("|." . ("|." . nil))
980                           (":|" . (":|" . nil))
981                           ("||" . ("||" . nil))
982                           (".|." . (".|." . nil))
983                           ("scorebar" . (nil . "scorepostbreak"))
984                           ("brace" . (nil . "brace"))
985                           ("bracket" . (nil . "bracket"))  
986                           )
987                         )))
988
989      (if (equal? result #f)
990          (ly-warn (string-append "Unknown bar glyph: `" glyph "'"))
991          (index-cell (cdr result) dir))
992      )
993    )
994      
995
996 (define (slur-ugly ind ht)
997   (if (and
998 ;       (< ht 4.0)
999        (< ht (* 4 ind))
1000        (> ht (* 0.4 ind))
1001        (> ht (+ (* 2 ind) -4))
1002        (< ht (+ (* -2 ind) 8)))
1003       #f
1004       (cons ind  ht)
1005   ))