2 ;;; sketch.scm -- implement Scheme output routines for Sketch
4 ;;; source file of the GNU LilyPond music typesetter
6 ;;; (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 ;; def dispats (out,x,y,expr):
11 ;; (symbol, rest) = expr
12 ;; if symbol == 'placebox':
13 ;; (dx,dy,expr) = rest
14 ;; dispats (out, x + dx, y + dy, expr)
15 ;; # hier wordt (X+DX) dus eerder gedaan dan dispats van EXPR.
16 ;; # er zijn geen "globale" variabelen.
17 ;; elif symbol == 'char':
18 ;; out.write ('moveto( %f %f); char(%d)' % (x,y,rest))
24 ;; All functions have the signature
26 ;; NAME X Y ARGUMENTS-PASSED-BY-LILYPOND
29 (define-module (scm output-sketch))
30 (debug-enable 'backtrace)
32 (define this-module (current-module))
34 (define-public (sketch-output-expression expr port)
35 (display (dispatch expr) port))
37 (use-modules (ice-9 format) (guile) (lily))
40 ; (define (dispatch x y expr)
41 ; (let ((keyword (car expr)))
43 ; ((eq? keyword 'beam x y width slope thick)
44 ; ((eq? keyword 'bezier-sandwich x y l thick)
45 ; ((eq? keyword 'bracket arch_angle arch_width arch_height height arch_thick thick)
46 ; ((eq? keyword 'char x y i)
47 ; ((eq? keyword 'comment s)
48 ; ((eq? keyword 'dashed-line thick on off dx dy)
49 ; ((eq? keyword 'dashed-slur thick dash l)
50 ; ((eq? keyword 'define-origin a b c ) "")
51 ; ((eq? keyword 'experimental-on) "")
52 ; ((eq? keyword 'ez-ball ch letter-col ball-col)
53 ; ((eq? keyword 'filledbox x y breapth width depth height)
54 ; ((eq? keyword 'font-load-command name-mag command)
55 ; ((eq? keyword 'font-switch i)
56 ; ((eq? keyword 'header creator generate)
57 ; ((eq? keyword 'header-end)
58 ; ((eq? keyword 'invoke-char s i)
59 ; ((eq? keyword 'lily-def key val)
60 ; ((eq? keyword 'no-origin) "")
61 ; ((eq? keyword 'output-scale 1)
62 ; ((eq? keyword 'placebox)
63 ; (dispatch (+ x (cadr expr)) (+ y (caddr expr) (cadddr expr))))
64 ; ((eq? keyword 'repeat-slash wid slope thick)
65 ; ((eq? keyword 'round-filled-box x y dx dy w h b)
66 ; ((eq? keyword 'select-font name-mag-pair)
67 ; ((eq? keyword 'start-system width height)
68 ; ((eq? keyword 'stem x y z w) (filledbox x y z w))
69 ; ((eq? keyword 'stop-last-system)
70 ; ((eq? keyword 'stop-system)
71 ; ((eq? keyword 'text x y s)
72 ; ((eq? keyword 'unknown)
77 (define current-y 150)
79 (define (dispatch expr)
80 (let ((keyword (car expr)))
82 ((eq? keyword 'placebox)
83 (dispatch-x-y (cadr expr) (+ current-y (caddr expr)) (cadddr expr)))
85 (apply (eval keyword this-module) (cdr expr))))))
87 (define (dispatch-x-y x y expr)
88 (apply (eval (car expr) this-module) (append (list x y) (cdr expr))))
90 (define (ascii->string i) (make-string 1 (integer->char i)))
92 (define (control->list x y c)
93 (list (+ x (car c)) (+ y (cdr c))))
95 (define (control-flip-y c)
96 (cons (car c) (* -1 (cdr c))))
99 (define (sketch-numbers->string l)
101 (ly:number->string (car l))
104 (string-append "," (sketch-numbers->string (cdr l))))))
106 ;;;\def\scaletounit{ 2.83464566929134 mul }%
108 ;;(define output-scale 2.83464566929134)
110 (define scale-to-unit
112 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
113 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
114 (else (error "unknown unit" (ly:unit)))
117 (define (mul-scale x) (* scale-to-unit output-scale x))
119 (define (sketch-filled-rectangle width dy dx height x y)
124 (sketch-numbers->string (map mul-scale (list width dy dx height x y)))
128 (define (sketch-bezier x y l)
129 (let* ((c0 (car (list-tail l 3)))
130 (c123 (list-head l 3))
131 (start (control->list x y c0))
132 (control (apply append
133 (map (lambda (c) (control->list x y c)) c123))))
135 "bs(" (sketch-numbers->string (map mul-scale start)) ",0)\n"
136 "bc(" (sketch-numbers->string (map mul-scale control)) ",2)\n")))
140 (define (sketch-beziers x y l thick)
141 (let* ((first (list-tail l 4))
142 (second (list-head l 4)))
147 (sketch-bezier x y first)
148 (sketch-bezier x y second))))
151 ;; alist containing fontname -> fontcommand assoc (both strings)
153 ;;(define font-alist '(("feta13" . ("feta13" . "13"))
154 ;; ("feta20" . ("feta20" . "20"))))
155 (define font-alist '(("feta13" . ("LilyPond-Feta13" . "13"))
156 ;; ("feta20" . ("LilyPond-Feta-20" . "20")
157 ("feta20" . ("GNU-LilyPond-feta-20" . "20")
161 (define font (cdar font-alist))
163 (define font-count 0)
164 (define current-font "")
166 (define (define-fonts x) "")
172 (define (cached-fontname i)
176 (define (round-filled-box x y dx dy w h b)
177 (sketch-filled-rectangle w 0 0 h x y))
179 (define (polygon points blotdiameter) "") ;; TODO
181 (define (select-font name-mag-pair)
182 ;; name-mag-pair: (quote ("feta20" . 0.569055118110236))"feta20"(quote ("feta20" . 0.569055118110236))
183 (let ((f (assoc (caadr name-mag-pair) font-alist)))
186 (format #t "font not found: ~s\n" (caadr name-mag-pair))))
190 (define (font-load-command name-mag command)
193 (define (beam x y width slope thick blot)
194 (apply sketch-filled-rectangle
195 (list width (* slope width) 0 thick x y)))
198 (string-append "# " s "\n"))
200 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
202 (ly:numbers->string (list arch_angle arch_width arch_height height arch_thick thick)) " draw_bracket" ))
209 "Fn('" (car font) "')\n"
210 "Fs(" (cdr font) ")\n"
211 ;; how to get zero-left padding with ``Guile's fprintf'' ?
212 ;;(format #f "txt('\\x~2x',(" i)
213 ;;(format #f "txt('\\x~02x',(" i)
214 ;; ugh: python's '%02x' % i
215 (format #f "&#x~2,'0x;" i)
216 (sketch-numbers->string (map mul-scale (list x y)))
220 ;; what the heck is this interface ?
221 (define (dashed-slur thick dash l)
223 (string-join (map ly:number-pair->string l) " ")
225 (ly:number->string thick)
227 (ly:number->string dash)
229 (ly:number->string (* 10 thick)) ;UGH. 10 ?
230 " ] 0 draw_dashed_slur"))
232 (define (dashed-line thick on off dx dy)
234 (ly:number->string dx)
236 (ly:number->string dy)
238 (ly:number->string thick)
240 (ly:number->string on)
242 (ly:number->string off)
243 " ] 0 draw_dashed_line"))
245 (define (repeat-slash wid slope thick)
246 (string-append (ly:numbers->string (list wid slope thick))
247 " draw_repeat_slash"))
250 "guidelayer('Guide Lines',1,0,0,1,(0,0,1))
251 grid((0,0,20,20),0,(0,0,1),'Grid')\n")
253 (define (experimental-on) "")
255 (define (font-switch i)
261 (define output-scale 1)
263 (define (lily-def key val)
264 (if (equal? key "lilypondpaperoutputscale")
266 (set! output-scale (string->number val))
271 (define (header creator generate)
276 layer('Layer 1',1,1,0,0,(0,0,0))
279 (define (invoke-char s i)
282 (define (bezier-sandwich x y l thick)
284 sketch-beziers (list x y (primitive-eval l) thick)))
286 (define (start-system width height)
287 (set! current-y (- current-y height))
290 ;; r((520.305,0,0,98.0075,51.8863,10.089))
291 ;; width, 0, 0, height, x, y
292 (define (filledbox x y breapth width depth height)
293 (apply sketch-filled-rectangle
295 (+ breapth width) 0 0 (+ depth height) (- x breapth) (- y depth))))
297 (define (stem x y z w) (filledbox x y z w))
300 (define (stop-system)
304 (define (stop-last-system)
312 "Fn('" (car font) "')\n"
313 "Fs(" (cdr font) ")\n"
315 "txt('" s "',(" (sketch-numbers->string
316 (map mul-scale (list x y))) "))\n"))
321 (define (ez-ball ch letter-col ball-col)
324 (ly:numbers->string (list letter-col ball-col))
325 " /Helvetica-Bold " ;; ugh
328 (define (define-origin a b c ) "")
329 (define (no-origin) "")