]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-sketch.scm
* scm/framework-svg.scm (output-framework): put scaling in
[lilypond.git] / scm / output-sketch.scm
1 ;;;; sketch.scm -- implement Scheme output routines for Sketch
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 1998--2005 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;;                 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8
9 ;; def dispats (out,x,y,expr):
10 ;;     (symbol, rest) = expr
11 ;;     if symbol == 'placebox':
12 ;;      (dx,dy,expr) = rest
13 ;;      dispats (out, x + dx, y + dy, expr)
14 ;;         # hier wordt (X+DX) dus eerder gedaan dan dispats van EXPR.
15 ;;         # er zijn geen "globale" variabelen.
16 ;;     elif symbol == 'char':
17 ;;         out.write ('moveto( %f %f); char(%d)' % (x,y,rest))
18
19
20
21
22 ;;
23 ;; All functions have the signature 
24 ;;
25 ;;  NAME X Y ARGUMENTS-PASSED-BY-LILYPOND
26 ;;
27
28 (define-module (scm output-sketch))
29 (debug-enable 'backtrace)
30
31 (define this-module (current-module))
32
33 (define-public (sketch-output-expression expr port)
34   (display (dispatch expr) port))
35
36 (use-modules (ice-9 format) (guile) (lily))
37
38 ;; hmm
39 ; (define (dispatch x y expr)
40 ;  (let ((keyword (car expr))) 
41 ;    (cond
42 ; ((eq? keyword 'beam x y width slope thick)
43 ; ((eq? keyword 'bezier-sandwich x y lst thick)
44 ; ((eq? keyword 'bracket arch_angle arch_width arch_height  height arch_thick thick)
45 ; ((eq? keyword 'char x y i)
46 ; ((eq? keyword 'comment s)
47 ; ((eq? keyword 'dashed-line thick on off dx dy)
48 ; ((eq? keyword 'dashed-slur thick dash lst)
49 ; ((eq? keyword 'define-origin a b c ) "")
50 ; ((eq? keyword 'experimental-on) "")
51 ; ((eq? keyword 'ez-ball ch letter-col ball-col)
52 ; ((eq? keyword 'filledbox x y breapth width depth height)
53 ; ((eq? keyword 'font-load-command name-mag command)
54 ; ((eq? keyword 'font-switch i)
55 ; ((eq? keyword 'header creator generate)
56 ; ((eq? keyword 'header-end)
57 ; ((eq? keyword 'invoke-char s i)
58 ; ((eq? keyword 'lily-def key val)
59 ; ((eq? keyword 'no-origin) "")
60 ; ((eq? keyword 'output-scale 1)
61 ; ((eq? keyword 'placebox)
62 ;  (dispatch (+ x (cadr expr)) (+ y (caddr expr) (cadddr expr))))
63 ; ((eq? keyword 'repeat-slash wid slope thick)
64 ; ((eq? keyword 'round-filled-box x y dx dy w h b)
65 ; ((eq? keyword 'select-font name-mag-pair)
66 ; ((eq? keyword 'start-system width height)
67 ; ((eq? keyword 'stem x y z w) (filledbox x y z w))
68 ; ((eq? keyword 'stop-last-system)
69 ; ((eq? keyword 'stop-system)
70 ; ((eq? keyword 'text x y s)
71 ; ((eq? keyword 'unknown)
72
73 ;     )))
74
75
76 (define current-y 150)
77
78 (define (dispatch expr)
79   (let ((keyword (car expr))) 
80     (cond
81      ((eq? keyword 'placebox)
82       (dispatch-x-y (cadr expr) (+ current-y (caddr expr)) (cadddr expr)))
83      (else
84       (apply (eval keyword this-module) (cdr expr))))))
85
86 (define (dispatch-x-y x y expr)
87   (apply (eval (car expr) this-module) (append (list x y) (cdr expr))))
88       
89 (define (ascii->string i) (make-string 1 (integer->char i)))
90
91 (define (control->list x y c)
92   (list (+ x (car c)) (+ y (cdr c))))
93
94 (define (control-flip-y c)
95   (cons (car c) (* -1 (cdr c))))
96
97 ;;; urg.
98 (define (sketch-numbers->string lst)
99   (string-append
100    (ly:number->string (car lst))
101    (if (null? (cdr lst))
102        ""
103        (string-append ","  (sketch-numbers->string (cdr lst))))))
104
105 ;;;\def\scaletounit{ 2.83464566929134 mul }%
106
107 ;;(define output-scale 2.83464566929134)
108
109
110 (define (mul-scale x) (* scale-to-unit output-scale x))
111
112 (define (sketch-filled-rectangle width dy dx height x y)
113   (string-append
114    "fp((0,0,0))\n"
115    "lw(0.1)\n"
116    "r("
117    (sketch-numbers->string (map mul-scale (list width dy dx height x y)))
118    ")\n"))
119
120
121 (define (sketch-bezier x y lst)
122   (let* ((c0 (car (list-tail lst 3)))
123          (c123 (list-head lst 3))
124          (start (control->list x y c0))
125          (control (apply append
126                          (map (lambda (c) (control->list x y c)) c123))))
127     (string-append
128      "bs(" (sketch-numbers->string (map mul-scale start)) ",0)\n"
129      "bc(" (sketch-numbers->string (map mul-scale control)) ",2)\n")))
130   
131
132
133 (define (sketch-beziers x y lst thick)
134   (let* ((first (list-tail lst 4))
135          (second (list-head lst 4)))
136     (string-append
137      "fp((0,0,0))\n"
138      "lw(0.1)\n"
139      "b()\n"
140      (sketch-bezier x y first)
141      (sketch-bezier x y second))))
142          
143
144 ;; alist containing fontname -> fontcommand assoc (both strings)
145 ;; old scheme
146 ;;(define font-alist '(("feta13" . ("feta13" . "13"))
147 ;;                   ("feta20" . ("feta20" . "20"))))
148 (define font-alist '(("feta13" . ("LilyPond-Feta13" . "13"))
149 ;;                   ("feta20" . ("LilyPond-Feta-20" . "20")
150                      ("feta20" . ("GNU-LilyPond-feta-20" . "20")
151                       )))
152
153 ;;(define font "")
154 (define font (cdar font-alist))
155
156 (define font-count 0)
157 (define current-font "")
158
159 (define (define-fonts x) "")
160
161 (define (font-def x)
162 "")
163
164
165 (define (cached-fontname i)
166   "")
167
168
169 (define (round-filled-box x y dx dy w h b)
170   (sketch-filled-rectangle w 0 0 h x y))
171
172 (define (polygon points blotdiameter) "") ;; TODO
173
174 (define (select-font name-mag-pair)
175   ;; name-mag-pair: (quote ("feta20" . 0.569055118110236))"feta20"(quote ("feta20" . 0.569055118110236))
176   (let ((f (assoc (caadr name-mag-pair) font-alist)))
177     (if (pair? f)
178         (set! font (cdr f))
179         (format #t "font not found: ~s\n" (caadr name-mag-pair))))
180   ;;(write font)
181   "")
182
183 (define (font-load-command name-mag command)
184   "")
185
186 (define (beam x y width slope thick blot)
187   (apply sketch-filled-rectangle
188          (list width (* slope width) 0 thick x y)))
189
190 (define (comment s)
191   (string-append "# " s "\n"))
192
193 (define (bracket arch_angle arch_width arch_height  height arch_thick thick)
194   (string-append
195    (ly:numbers->string (list arch_angle arch_width arch_height height arch_thick thick)) " draw_bracket" ))
196
197 (define (char x y i)
198   (string-append
199    "fp((0,0,0))\n"
200    "le()\n"
201    "lw(0.1)\n"
202    "Fn('" (car font) "')\n"
203    "Fs(" (cdr font) ")\n"
204    ;; how to get zero-left padding with ``Guile's fprintf'' ?
205    ;;(format #f "txt('\\x~2x',(" i)
206    ;;(format #f "txt('\\x~02x',(" i)
207    ;; ugh: python's '%02x' % i
208    (format #f "&#x~2,'0x;" i)
209    (sketch-numbers->string (map mul-scale (list x y)))
210    "))\n"))
211
212
213 ;; what the heck is this interface ?
214 (define (dashed-slur thick on off l)
215   "")
216
217 (define (dashed-line thick on off dx dy)
218   (string-append 
219    (ly:number->string dx)
220    " "
221    (ly:number->string dy)
222    " "
223    (ly:number->string thick) 
224    " [ "
225    (ly:number->string on)
226    " "
227    (ly:number->string off)
228    " ] 0 draw_dashed_line"))
229
230 (define (repeat-slash wid slope thick)
231  (string-append (ly:numbers->string (list wid slope thick))
232   " draw_repeat_slash"))
233
234 (define (end-output)
235   "guidelayer('Guide Lines',1,0,0,1,(0,0,1))
236 grid((0,0,20,20),0,(0,0,1),'Grid')\n")
237
238 (define (experimental-on) "")
239
240 (define (font-switch i)
241   "")
242
243 (define (header-end)
244   "")
245
246 (define output-scale 1)
247
248 (define (lily-def key val)
249   (if (equal? key "lilypondpaperoutputscale")
250       ;; ugr
251       (set! output-scale (string->number val))
252       )
253   "")
254
255
256 (define (header creator generate)
257   (string-append
258    "##Sketch 1 2
259 document()
260 layout('A4',0)
261 layer('Layer 1',1,1,0,0,(0,0,0))
262 "))
263
264 (define (invoke-char s i)
265   "")
266
267 (define (bezier-sandwich x y lst thick)
268   (apply
269    sketch-beziers (list x y (primitive-eval lst) thick)))
270
271 (define (start-system width height)
272   (set! current-y (- current-y height))
273   "G()\n")
274
275 ;;  r((520.305,0,0,98.0075,51.8863,10.089))
276 ;;  width, 0, 0, height, x, y
277 (define (filledbox x y breapth width depth height)
278   (apply sketch-filled-rectangle
279          (list
280           (+ breapth width) 0 0 (+ depth height) (- x breapth) (- y depth))))
281
282 (define (stem x y z w) (filledbox x y z w))
283
284
285 (define (stop-system)
286     "G_()\n")
287
288 ;; huh?
289 (define (stop-last-system)
290    (stop-system))
291
292 (define (text x y s)
293   (string-append
294    "fp((0,0,0))\n"
295    "le()\n"
296    "lw(0.1)\n"
297    "Fn('" (car font) "')\n"
298    "Fs(" (cdr font) ")\n"
299    ;; Hmm
300    "txt('" s "',(" (sketch-numbers->string
301                                   (map mul-scale (list x y))) "))\n"))
302
303 (define (unknown) 
304   "\n unknown\n")
305
306 (define (ez-ball ch letter-col ball-col)
307   (string-append
308    " (" ch ") "
309    (ly:numbers->string (list letter-col ball-col))
310    " /Helvetica-Bold " ;; ugh
311    " draw_ez_ball"))
312
313 (define (define-origin a b c ) "")
314 (define (no-origin) "")
315
316
317
318 ;;;;;;;;;;;;;;;;;;;;
319 ;;;;;;;;;;;;;;;;;;;;
320
321