-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathuser-drawing.lisp
351 lines (275 loc) · 11.1 KB
/
user-drawing.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
;;; Copyright (c) 2007 Zachary Beane, All Rights Reserved
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;;
;;; * Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;;
;;; * Redistributions in binary form must reproduce the above
;;; copyright notice, this list of conditions and the following
;;; disclaimer in the documentation and/or other materials
;;; provided with the distribution.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;;
;;; $Id: user-drawing.lisp,v 1.21 2007/10/01 14:12:55 xach Exp $
(in-package #:vecto)
(defvar *graphics-state*)
(setf (documentation '*graphics-state* 'variable)
"The currently active graphics state. Bound for the
duration of WITH-GRAPICS-STATE.")
;;; Low-level path construction
(defun %move-to (state x y)
(let ((path (paths:create-path :open-polyline)))
(push (setf (path state) path) (paths state))
(paths:path-reset path (paths:make-point x y))))
(defun %line-to (state x y)
(paths:path-extend (path state) (paths:make-straight-line)
(paths:make-point x y)))
(defun %curve-to (state cx1 cy1 cx2 cy2 x y)
"Draw a cubic Bezier curve from the current point to (x,y)
through two control points."
(let ((control-point-1 (paths:make-point cx1 cy1))
(control-point-2 (paths:make-point cx2 cy2))
(end-point (paths:make-point x y)))
(paths:path-extend (path state)
(paths:make-bezier-curve (list control-point-1
control-point-2))
end-point)))
(defun %quadratic-to (state cx cy x y)
"Draw a quadratic Bezier curve from the current point to (x,y)
through one control point."
(paths:path-extend (path state)
(paths:make-bezier-curve (list (paths:make-point cx cy)))
(paths:make-point x y)))
(defun draw-arc-curves (curves)
(destructuring-bind (((startx . starty) &rest ignored-curve)
&rest ignored-curves)
curves
(declare (ignore ignored-curve ignored-curves))
(if (path *graphics-state*)
(line-to startx starty)
(move-to startx starty)))
(loop for ((x1 . y1)
(cx1 . cy1)
(cx2 . cy2)
(x2 . y2)) in curves
do (curve-to cx1 cy1 cx2 cy2 x2 y2)))
(defun %close-subpath (state)
(setf (paths::path-type (path state)) :closed-polyline))
;;; Clipping path
(defun %end-path-no-op (state)
(after-painting state))
(defun %clip-path (state)
(call-after-painting state
(make-clipping-path-function state :nonzero-winding)))
(defun %even-odd-clip-path (state)
(call-after-painting state
(make-clipping-path-function state :even-odd)))
;;; Text
(defun %get-font (state file)
(find-font-loader state file))
(defun %set-font (state loader size)
(let* ((scale (loader-font-scale size loader))
(matrix (scaling-matrix scale scale)))
(setf (font state)
(make-instance 'font
:loader loader
:transform-matrix matrix
:size size))))
(defun %string-paths (state x y string)
(let ((font (font state)))
(unless font
(error "No font currently set"))
(string-primitive-paths x y string font
:character-spacing (character-spacing state))))
(defun %draw-string (state x y string)
(draw-paths/state (%string-paths state x y string)
state))
(defun %draw-centered-string (state x y string)
(let* ((font (font state))
(bbox
(string-bounding-box string
(size font)
(loader font)
:character-spacing (character-spacing state)))
(xmin (xmin bbox))
(width/2 (/ (- (xmax bbox) xmin) 2.0)))
(%draw-string state (- x (+ width/2 xmin)) y string)))
(defun string-paths (x y string)
(setf (paths *graphics-state*)
(append (paths *graphics-state*)
(%string-paths *graphics-state* x y string)))
(values))
(defun centered-string-paths (x y string)
(let* ((font (font *graphics-state*))
(bbox (string-bounding-box string (size font) (loader font)))
(width/2 (/ (- (xmax bbox) (xmin bbox)) 2.0)))
(setf (paths *graphics-state*)
(append (paths *graphics-state*)
(%string-paths *graphics-state* (- x width/2) y string)))
(values)))
;;; Low-level transforms
(defun %translate (state tx ty)
(apply-matrix state (translation-matrix tx ty)))
(defun %scale (state sx sy)
(apply-matrix state (scaling-matrix sx sy)))
(defun %skew (state x y)
(apply-matrix state (skewing-matrix x y)))
(defun %rotate (state radians)
(apply-matrix state (rotation-matrix radians)))
;;; User-level commands
(defun move-to (x y)
(%move-to *graphics-state* x y))
(defun line-to (x y)
(%line-to *graphics-state* x y))
(defun curve-to (cx1 cy1 cx2 cy2 x y)
(%curve-to *graphics-state* cx1 cy1 cx2 cy2 x y))
(defun quadratic-to (cx cy x y)
(%quadratic-to *graphics-state* cx cy x y))
(defun arc (cx cy r theta1 theta2)
(loop while (< theta2 theta1) do (incf theta2 (* 2 pi)))
(let ((curves
(approximate-elliptical-arc cx cy r r 0 theta1 theta2)))
(draw-arc-curves curves)))
(defun arcn (cx cy r theta1 theta2)
(loop while (< theta1 theta2) do (decf theta2 (* 2 pi)))
(let ((curves (approximate-elliptical-arc cx cy r r 0 theta2 theta1)))
(draw-arc-curves (nreverse (mapcar #'nreverse curves)))))
(defun ellipse-arc (cx cy rx ry theta eta1 eta2)
(loop while (< eta2 eta1) do (incf eta2 (* 2 pi)))
(let ((curves (approximate-elliptical-arc cx cy rx ry theta eta1 eta2)))
(draw-arc-curves curves)))
(defun ellipse-arcn (cx cy rx ry theta eta1 eta2)
(loop while (< eta1 eta2) do (decf eta2 (* 2 pi)))
(let ((curves (approximate-elliptical-arc cx cy rx ry theta eta2 eta1)))
(draw-arc-curves (nreverse (mapcar #'nreverse curves)))))
(defun close-subpath ()
(%close-subpath *graphics-state*))
(defun end-path-no-op ()
(%end-path-no-op *graphics-state*)
(clear-paths *graphics-state*))
(defun clip-path ()
(%clip-path *graphics-state*))
(defun even-odd-clip-path ()
(%even-odd-clip-path *graphics-state*))
(defun get-font (file)
(%get-font *graphics-state* file))
(defun set-font (font size)
(%set-font *graphics-state* font size))
(defun set-character-spacing (spacing)
(setf (character-spacing *graphics-state*) spacing))
(defun draw-string (x y string)
(%draw-string *graphics-state* x y string))
(defun draw-centered-string (x y string)
(%draw-centered-string *graphics-state* x y string))
(defun set-dash-pattern (vector phase)
(if (zerop (length vector))
(setf (dash-vector *graphics-state*) nil
(dash-phase *graphics-state*) nil)
(setf (dash-vector *graphics-state*) vector
(dash-phase *graphics-state*) phase)))
(defun set-line-cap (style)
(assert (member style '(:butt :square :round)))
(setf (cap-style *graphics-state*) style))
(defun set-line-join (style)
(assert (member style '(:bevel :miter :round)))
(setf (join-style *graphics-state*) (if (eql style :bevel) :none style)))
(defun set-line-width (width)
(setf (line-width *graphics-state*) width))
(defun set-rgba-color (color r g b a)
(setf (red color) (clamp-range 0.0 r 1.0)
(green color) (clamp-range 0.0 g 1.0)
(blue color) (clamp-range 0.0 b 1.0)
(alpha color) (clamp-range 0.0 a 1.0))
color)
(defun set-rgb-color (color r g b)
(setf (red color) (clamp-range 0.0 r 1.0)
(green color) (clamp-range 0.0 g 1.0)
(blue color) (clamp-range 0.0 b 1.0)
(alpha color) 1.0)
color)
(defun set-rgb-stroke (r g b)
(set-rgb-color (stroke-color *graphics-state*) r g b))
(defun set-rgba-stroke (r g b a)
(set-rgba-color (stroke-color *graphics-state*) r g b a))
(defun set-rgb-fill (r g b)
(clear-fill-source *graphics-state*)
(set-rgb-color (fill-color *graphics-state*) r g b))
(defun set-rgba-fill (r g b a)
(clear-fill-source *graphics-state*)
(set-rgba-color (fill-color *graphics-state*) r g b a))
(defun stroke ()
(draw-stroked-paths *graphics-state*)
(clear-paths *graphics-state*))
(defun stroke-to-paths ()
(let ((paths (state-stroke-paths *graphics-state*)))
(clear-paths *graphics-state*)
(setf (paths *graphics-state*) paths)
(%close-subpath *graphics-state*)))
(defun fill-path ()
(draw-filled-paths *graphics-state*)
(after-painting *graphics-state*)
(clear-paths *graphics-state*))
(defun even-odd-fill ()
(draw-even-odd-filled-paths *graphics-state*)
(after-painting *graphics-state*)
(clear-paths *graphics-state*))
(defun fill-and-stroke ()
(draw-filled-paths *graphics-state*)
(draw-stroked-paths *graphics-state*)
(after-painting *graphics-state*)
(clear-paths *graphics-state*))
(defun even-odd-fill-and-stroke ()
(draw-even-odd-filled-paths *graphics-state*)
(draw-stroked-paths *graphics-state*)
(after-painting *graphics-state*)
(clear-paths *graphics-state*))
(defun clear-canvas ()
(let ((color (fill-color *graphics-state*)))
(fill-image (image-data *graphics-state*)
(red color)
(green color)
(blue color)
(alpha color))))
(defun translate (x y)
(%translate *graphics-state* x y))
(defun scale (x y)
(%scale *graphics-state* x y))
(defun skew (x y)
(%skew *graphics-state* x y))
(defun rotate (radians)
(%rotate *graphics-state* radians))
(defun rotate-degrees (degrees)
(%rotate *graphics-state* (* (/ pi 180) degrees)))
(defgeneric compose (layer x y))
(defun save-png (file)
(zpng:write-png (image *graphics-state*) file))
(defun save-png-stream (stream)
(zpng:write-png-stream (image *graphics-state*) stream))
(defun zpng-object ()
(image *graphics-state*))
(defmacro with-canvas ((&key width height image-data-allocator)
&body body)
`(let ((*graphics-state* (make-instance 'graphics-state)))
(state-image *graphics-state* ,width ,height
,@(when image-data-allocator `(,image-data-allocator)))
(unwind-protect
(progn
,@body)
(clear-state *graphics-state*))))
(defmacro with-graphics-state (&body body)
`(let ((*graphics-state* (copy *graphics-state*)))
,@body))