]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-sketch.scm
add TODO comment.
[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--2003 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 'end-output)
52 ; ((eq? keyword 'experimental-on) "")
53 ; ((eq? keyword 'ez-ball ch letter-col ball-col)
54 ; ((eq? keyword 'filledbox x y breapth width depth height)
55 ; ((eq? keyword 'font-load-command name-mag command)
56 ; ((eq? keyword 'font-switch i)
57 ; ((eq? keyword 'header creator generate)
58 ; ((eq? keyword 'header-end)
59 ; ((eq? keyword 'invoke-char s i)
60 ; ((eq? keyword 'lily-def key val)
61 ; ((eq? keyword 'no-origin) "")
62 ; ((eq? keyword 'output-scale 1)
63 ; ((eq? keyword 'placebox)
64 ;  (dispatch (+ x (cadr expr)) (+ y (caddr expr) (cadddr expr))))
65 ; ((eq? keyword 'repeat-slash wid slope thick)
66 ; ((eq? keyword 'roundfilledbox x y dx dy w h b)
67 ; ((eq? keyword 'select-font name-mag-pair)
68 ; ((eq? keyword 'start-system width height)
69 ; ((eq? keyword 'stem x y z w) (filledbox x y z w))
70 ; ((eq? keyword 'stop-last-system)
71 ; ((eq? keyword 'stop-system)
72 ; ((eq? keyword 'text x y s)
73 ; ((eq? keyword 'unknown)
74
75 ;     )))
76
77
78 (define current-y 150)
79
80 (define (dispatch expr)
81   (let ((keyword (car expr))) 
82     (cond
83      ((eq? keyword 'placebox)
84       (dispatch-x-y (cadr expr) (+ current-y (caddr expr)) (cadddr expr)))
85      (else
86       (apply (eval keyword this-module) (cdr expr))))))
87
88 (define (dispatch-x-y x y expr)
89   (apply (eval (car expr) this-module) (append (list x y) (cdr expr))))
90       
91 (define (ascii->string i) (make-string 1 (integer->char i)))
92
93 (define (control->list x y c)
94   (list (+ x (car c)) (+ y (cdr c))))
95
96 (define (control-flip-y c)
97   (cons (car c) (* -1 (cdr c))))
98
99 ;;; urg.
100 (define (sketch-numbers->string l)
101   (string-append
102    (number->string (car l))
103    (if (null? (cdr l))
104        ""
105        (string-append ","  (sketch-numbers->string (cdr l))))))
106
107 ;;;\def\scaletounit{ 2.83464566929134 mul }%
108
109 ;;(define output-scale 2.83464566929134)
110
111 (define scale-to-unit
112   (cond
113    ((equal? (ly:unit) "mm") (/ 72.0  25.4))
114    ((equal? (ly:unit) "pt") (/ 72.0  72.27))
115    (else (error "unknown unit" (ly:unit)))
116    ))
117
118 (define (mul-scale x) (* scale-to-unit output-scale x))
119
120 (define (sketch-filled-rectangle width dy dx height x y)
121   (string-append
122    "fp((0,0,0))\n"
123    "lw(0.1)\n"
124    "r("
125    (sketch-numbers->string (map mul-scale (list width dy dx height x y)))
126    ")\n"))
127
128
129 (define (sketch-bezier x y l)
130   (let* ((c0 (car (list-tail l 3)))
131          (c123 (list-head l 3))
132          (start (control->list x y c0))
133          (control (apply append
134                          (map (lambda (c) (control->list x y c)) c123))))
135     (string-append
136      "bs(" (sketch-numbers->string (map mul-scale start)) ",0)\n"
137      "bc(" (sketch-numbers->string (map mul-scale control)) ",2)\n")))
138   
139
140
141 (define (sketch-beziers x y l thick)
142   (let* ((first (list-tail l 4))
143          (second (list-head l 4)))
144     (string-append
145      "fp((0,0,0))\n"
146      "lw(0.1)\n"
147      "b()\n"
148      (sketch-bezier x y first)
149      (sketch-bezier x y second))))
150          
151
152 ;; alist containing fontname -> fontcommand assoc (both strings)
153 ;; old scheme
154 ;;(define font-alist '(("feta13" . ("feta13" . "13"))
155 ;;                   ("feta20" . ("feta20" . "20"))))
156 (define font-alist '(("feta13" . ("LilyPond-Feta13" . "13"))
157 ;;                   ("feta20" . ("LilyPond-Feta-20" . "20")
158                      ("feta20" . ("GNU-LilyPond-feta-20" . "20")
159                       )))
160
161 ;;(define font "")
162 (define font (cdar font-alist))
163
164 (define font-count 0)
165 (define current-font "")
166
167 (define (fontify x y name-mag-pair exp)
168   (string-append (select-font name-mag-pair)
169                  (apply (eval (car exp) this-module)
170                         (append (list x y) (cdr exp)))))
171 ;;               (if (string? exp) exp "")))
172
173 (define (define-fonts x) "")
174
175 (define (font-def x)
176 "")
177
178
179 (define (cached-fontname i)
180   "")
181
182
183 (define (roundfilledbox x y dx dy w h b)
184   (sketch-filled-rectangle w 0 0 h x y))
185
186 (define (polygon points blotdiameter) "") ;; TODO
187
188 (define (select-font name-mag-pair)
189   ;; name-mag-pair: (quote ("feta20" . 0.569055118110236))"feta20"(quote ("feta20" . 0.569055118110236))
190   (let ((f (assoc (caadr name-mag-pair) font-alist)))
191     (if (pair? f)
192         (set! font (cdr f))
193         (format #t "font not found: ~s\n" (caadr name-mag-pair))))
194   ;;(write font)
195   "")
196
197 (define (font-load-command name-mag command)
198   "")
199
200 (define (beam x y width slope thick)
201   (apply sketch-filled-rectangle
202          (list width (* slope width) 0 thick x y)))
203
204 (define (comment s)
205   (string-append "# " s "\n"))
206
207 (define (bracket arch_angle arch_width arch_height  height arch_thick thick)
208   (string-append
209    (numbers->string (list arch_angle arch_width arch_height height arch_thick thick)) " draw_bracket" ))
210
211 (define (char x y i)
212   (string-append
213    "fp((0,0,0))\n"
214    "le()\n"
215    "lw(0.1)\n"
216    "Fn('" (car font) "')\n"
217    "Fs(" (cdr font) ")\n"
218    ;; how to get zero-left padding with ``Guile's fprintf'' ?
219    ;;(format #f "txt('\\x~2x',(" i)
220    ;;(format #f "txt('\\x~02x',(" i)
221    ;; ugh: python's '%02x' % i
222    (format #f "&#x~2,'0x;" i)
223    (sketch-numbers->string (map mul-scale (list x y)))
224    "))\n"))
225
226
227 ;; what the heck is this interface ?
228 (define (dashed-slur thick dash l)
229   (string-append 
230    (apply string-append (map number-pair->string l)) 
231    (ly:number->string thick) 
232    " [ "
233    (ly:number->string dash)
234    " "
235    (ly:number->string (* 10 thick))     ;UGH.  10 ?
236    " ] 0 draw_dashed_slur"))
237
238 (define (dashed-line thick on off dx dy)
239   (string-append 
240    (ly:number->string dx)
241    " "
242    (ly:number->string dy)
243    " "
244    (ly:number->string thick) 
245    " [ "
246    (ly:number->string on)
247    " "
248    (ly:number->string off)
249    " ] 0 draw_dashed_line"))
250
251 (define (repeat-slash wid slope thick)
252  (string-append (numbers->string (list wid slope thick))
253   " draw_repeat_slash"))
254
255 (define (end-output)
256   "guidelayer('Guide Lines',1,0,0,1,(0,0,1))
257 grid((0,0,20,20),0,(0,0,1),'Grid')\n")
258
259 (define (experimental-on) "")
260
261 (define (font-switch i)
262   "")
263
264 (define (header-end)
265   "")
266
267 (define output-scale 1)
268
269 (define (lily-def key val)
270   (if (equal? key "lilypondpaperoutputscale")
271       ;; ugr
272       (set! output-scale (string->number val))
273       )
274   "")
275
276
277 (define (header creator generate)
278   (string-append
279    "##Sketch 1 2
280 document()
281 layout('A4',0)
282 layer('Layer 1',1,1,0,0,(0,0,0))
283 "))
284
285 (define (invoke-char s i)
286   "")
287
288 (define (bezier-sandwich x y l thick)
289   (apply
290    sketch-beziers (list x y (primitive-eval l) thick)))
291
292 (define (start-system width height)
293   (set! current-y (- current-y height))
294   "G()\n")
295
296 ;;  r((520.305,0,0,98.0075,51.8863,10.089))
297 ;;  width, 0, 0, height, x, y
298 (define (filledbox x y breapth width depth height)
299   (apply sketch-filled-rectangle
300          (list
301           (+ breapth width) 0 0 (+ depth height) (- x breapth) (- y depth))))
302
303 (define (stem x y z w) (filledbox x y z w))
304
305
306 (define (stop-system)
307     "G_()\n")
308
309 ;; huh?
310 (define (stop-last-system)
311    (stop-system))
312
313 (define (text x y s)
314   (string-append
315    "fp((0,0,0))\n"
316    "le()\n"
317    "lw(0.1)\n"
318    "Fn('" (car font) "')\n"
319    "Fs(" (cdr font) ")\n"
320    ;; Hmm
321    "txt('" s "',(" (sketch-numbers->string
322                                   (map mul-scale (list x y))) "))\n"))
323
324 (define (unknown) 
325   "\n unknown\n")
326
327 (define (ez-ball ch letter-col ball-col)
328   (string-append
329    " (" ch ") "
330    (numbers->string (list letter-col ball-col))
331    " /Helvetica-Bold " ;; ugh
332    " draw_ez_ball"))
333
334 (define (define-origin a b c ) "")
335 (define (no-origin) "")
336
337
338
339 ;;;;;;;;;;;;;;;;;;;;
340 ;;;;;;;;;;;;;;;;;;;;
341
342