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