]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-sketch.scm
afe39c407605954d5d8b46a966b3f2a5d689d4a2
[lilypond.git] / scm / output-sketch.scm
1
2 ;;; sketch.scm -- implement Scheme output routines for Sketch
3 ;;;
4 ;;;  source file of the GNU LilyPond music typesetter
5 ;;; 
6 ;;; (c)  1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
8
9
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))
19
20
21
22
23 ;;
24 ;; All functions have the signature 
25 ;;
26 ;;  NAME X Y ARGUMENTS-PASSED-BY-LILYPOND
27 ;;
28
29 (define-module (scm output-sketch))
30 (debug-enable 'backtrace)
31
32 (define this-module (current-module))
33
34 (define-public (sketch-output-expression expr port)
35   (display (dispatch expr) port))
36
37 (use-modules (ice-9 format) (guile) (lily))
38
39 ;; hmm
40 ; (define (dispatch x y expr)
41 ;  (let ((keyword (car expr))) 
42 ;    (cond
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)
73
74 ;     )))
75
76
77 (define current-y 150)
78
79 (define (dispatch expr)
80   (let ((keyword (car expr))) 
81     (cond
82      ((eq? keyword 'placebox)
83       (dispatch-x-y (cadr expr) (+ current-y (caddr expr)) (cadddr expr)))
84      (else
85       (apply (eval keyword this-module) (cdr expr))))))
86
87 (define (dispatch-x-y x y expr)
88   (apply (eval (car expr) this-module) (append (list x y) (cdr expr))))
89       
90 (define (ascii->string i) (make-string 1 (integer->char i)))
91
92 (define (control->list x y c)
93   (list (+ x (car c)) (+ y (cdr c))))
94
95 (define (control-flip-y c)
96   (cons (car c) (* -1 (cdr c))))
97
98 ;;; urg.
99 (define (sketch-numbers->string l)
100   (string-append
101    (ly:number->string (car l))
102    (if (null? (cdr l))
103        ""
104        (string-append ","  (sketch-numbers->string (cdr l))))))
105
106 ;;;\def\scaletounit{ 2.83464566929134 mul }%
107
108 ;;(define output-scale 2.83464566929134)
109
110 (define scale-to-unit
111   (cond
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)))
115    ))
116
117 (define (mul-scale x) (* scale-to-unit output-scale x))
118
119 (define (sketch-filled-rectangle width dy dx height x y)
120   (string-append
121    "fp((0,0,0))\n"
122    "lw(0.1)\n"
123    "r("
124    (sketch-numbers->string (map mul-scale (list width dy dx height x y)))
125    ")\n"))
126
127
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))))
134     (string-append
135      "bs(" (sketch-numbers->string (map mul-scale start)) ",0)\n"
136      "bc(" (sketch-numbers->string (map mul-scale control)) ",2)\n")))
137   
138
139
140 (define (sketch-beziers x y l thick)
141   (let* ((first (list-tail l 4))
142          (second (list-head l 4)))
143     (string-append
144      "fp((0,0,0))\n"
145      "lw(0.1)\n"
146      "b()\n"
147      (sketch-bezier x y first)
148      (sketch-bezier x y second))))
149          
150
151 ;; alist containing fontname -> fontcommand assoc (both strings)
152 ;; old scheme
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")
158                       )))
159
160 ;;(define font "")
161 (define font (cdar font-alist))
162
163 (define font-count 0)
164 (define current-font "")
165
166 (define (define-fonts x) "")
167
168 (define (font-def x)
169 "")
170
171
172 (define (cached-fontname i)
173   "")
174
175
176 (define (round-filled-box x y dx dy w h b)
177   (sketch-filled-rectangle w 0 0 h x y))
178
179 (define (polygon points blotdiameter) "") ;; TODO
180
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)))
184     (if (pair? f)
185         (set! font (cdr f))
186         (format #t "font not found: ~s\n" (caadr name-mag-pair))))
187   ;;(write font)
188   "")
189
190 (define (font-load-command name-mag command)
191   "")
192
193 (define (beam x y width slope thick blot)
194   (apply sketch-filled-rectangle
195          (list width (* slope width) 0 thick x y)))
196
197 (define (comment s)
198   (string-append "# " s "\n"))
199
200 (define (bracket arch_angle arch_width arch_height  height arch_thick thick)
201   (string-append
202    (ly:numbers->string (list arch_angle arch_width arch_height height arch_thick thick)) " draw_bracket" ))
203
204 (define (char x y i)
205   (string-append
206    "fp((0,0,0))\n"
207    "le()\n"
208    "lw(0.1)\n"
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)))
217    "))\n"))
218
219
220 ;; what the heck is this interface ?
221 (define (dashed-slur thick dash l)
222   (string-append 
223    (string-join (map ly:number-pair->string l) " ")
224    " "
225    (ly:number->string thick) 
226    " [ "
227    (ly:number->string dash)
228    " "
229    (ly:number->string (* 10 thick))     ;UGH.  10 ?
230    " ] 0 draw_dashed_slur"))
231
232 (define (dashed-line thick on off dx dy)
233   (string-append 
234    (ly:number->string dx)
235    " "
236    (ly:number->string dy)
237    " "
238    (ly:number->string thick) 
239    " [ "
240    (ly:number->string on)
241    " "
242    (ly:number->string off)
243    " ] 0 draw_dashed_line"))
244
245 (define (repeat-slash wid slope thick)
246  (string-append (ly:numbers->string (list wid slope thick))
247   " draw_repeat_slash"))
248
249 (define (end-output)
250   "guidelayer('Guide Lines',1,0,0,1,(0,0,1))
251 grid((0,0,20,20),0,(0,0,1),'Grid')\n")
252
253 (define (experimental-on) "")
254
255 (define (font-switch i)
256   "")
257
258 (define (header-end)
259   "")
260
261 (define output-scale 1)
262
263 (define (lily-def key val)
264   (if (equal? key "lilypondpaperoutputscale")
265       ;; ugr
266       (set! output-scale (string->number val))
267       )
268   "")
269
270
271 (define (header creator generate)
272   (string-append
273    "##Sketch 1 2
274 document()
275 layout('A4',0)
276 layer('Layer 1',1,1,0,0,(0,0,0))
277 "))
278
279 (define (invoke-char s i)
280   "")
281
282 (define (bezier-sandwich x y l thick)
283   (apply
284    sketch-beziers (list x y (primitive-eval l) thick)))
285
286 (define (start-system width height)
287   (set! current-y (- current-y height))
288   "G()\n")
289
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
294          (list
295           (+ breapth width) 0 0 (+ depth height) (- x breapth) (- y depth))))
296
297 (define (stem x y z w) (filledbox x y z w))
298
299
300 (define (stop-system)
301     "G_()\n")
302
303 ;; huh?
304 (define (stop-last-system)
305    (stop-system))
306
307 (define (text x y s)
308   (string-append
309    "fp((0,0,0))\n"
310    "le()\n"
311    "lw(0.1)\n"
312    "Fn('" (car font) "')\n"
313    "Fs(" (cdr font) ")\n"
314    ;; Hmm
315    "txt('" s "',(" (sketch-numbers->string
316                                   (map mul-scale (list x y))) "))\n"))
317
318 (define (unknown) 
319   "\n unknown\n")
320
321 (define (ez-ball ch letter-col ball-col)
322   (string-append
323    " (" ch ") "
324    (ly:numbers->string (list letter-col ball-col))
325    " /Helvetica-Bold " ;; ugh
326    " draw_ez_ball"))
327
328 (define (define-origin a b c ) "")
329 (define (no-origin) "")
330
331
332
333 ;;;;;;;;;;;;;;;;;;;;
334 ;;;;;;;;;;;;;;;;;;;;
335
336