-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.lisp
332 lines (295 loc) · 9.39 KB
/
test.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
(in-package #:vecto)
(defun test (output-file)
(with-canvas (:width 100 :height 100)
(set-line-width 5.0)
;; red stroke
(set-rgb-stroke 1 0 0)
(move-to 10 10)
(line-to 90 90)
(stroke)
;; green stroke
(set-rgb-stroke 0 1 0)
(move-to 10 90)
(line-to 90 10)
(stroke)
;; blue+alpha transform stroke
(set-rgba-stroke 0 0 1 0.5)
(flet ((elbow (radians)
(with-graphics-state
(translate 50 50)
(rotate radians)
(scale 0.25 0.25)
(move-to 0 0)
(curve-to 0 100
0 100
100 100)
(set-line-width 10.0)
(stroke))))
(let* ((rotations 25)
(step (/ (* pi 2) rotations)))
(dotimes (i rotations)
(elbow (* i step)))))
(save-png output-file)))
(defun test-rotate (output-file)
(with-canvas (:width 100 :height 100)
(translate 50 50)
(move-to 0 0)
(line-to 0 10)
(rotate (- (/ pi 4)))
(set-line-width 15)
(stroke)
(save-png output-file)))
(defun test-skew (output-file)
(with-canvas (:width 100 :height 100)
(move-to 0 0)
(line-to 0 75)
(skew (- (/ pi 4)) (- (/ pi 4)))
(set-line-width 15)
(stroke)
(save-png output-file)))
(defun hole-test (file)
(with-canvas (:width 100 :height 100)
(translate 10 10)
(scale 50 50)
(set-line-width 0.1)
(move-to 0 0)
(line-to 0 1)
(line-to 1 1)
(line-to 1 0)
(line-to 0 0)
(move-to 0.1 0.8)
(line-to 0.1 0.1)
(line-to 0.8 0.1)
(line-to 0.8 0.8)
(line-to 0.1 0.8)
(fill-path)
(save-png file)))
(defun rectangle-test (file)
(with-canvas (:width 100 :height 100)
(rectangle 10 10 50 50)
(fill-path)
(save-png file)))
(defun rectangle-fill-test (file)
(with-canvas (:width 5 :height 5)
(set-rgba-fill 1 0 0 0.5)
(rectangle 0 0 5 5)
(fill-path)
(save-png file)))
(defun circle-test (string file)
(with-canvas (:width 250 :height 180)
(set-rgb-fill 1 1 1)
(set-line-width 1)
(translate 10 10)
(centered-circle-path 0 0 5)
(fill-and-stroke)
(translate 15 15)
(centered-circle-path 0 0 8)
(fill-and-stroke)
(translate 20 24)
(centered-circle-path 0 0 11)
(fill-and-stroke)
(centered-ellipse-path 75 60 100 40)
(fill-and-stroke)
(let ((font (get-font "/home/xach/.fonts/vagron.ttf")))
(set-font font 25)
(translate -5 50)
(let ((bbox (string-bounding-box string font)))
(set-line-width 1)
(set-rgba-fill 1 0 0 0.5)
(rectangle (xmin bbox) (ymin bbox)
(- (xmax bbox) (xmin bbox))
(- (ymax bbox) (ymin bbox)))
(fill-path))
(set-rgb-fill 0 1 0)
(draw-string string))
(save-png file)))
(defun center-test (string file)
(with-canvas (:width 200 :height 100)
(let ((font (get-font #p"times.ttf")))
(set-font font 36)
(draw-centered-string 100 25 string)
(set-rgba-fill 1 0 0 0.5)
(set-rgb-stroke 0 0 0)
(centered-circle-path 100 25 5)
(stroke)
(save-png file))))
(defun twittertext (string size font file)
(zpb-ttf:with-font-loader (loader font)
(let ((bbox (string-bounding-box string size loader)))
(with-canvas (:width (- (ceiling (xmax bbox)) (floor (xmin bbox)))
:height (- (ceiling (ymax bbox)) (floor (ymin bbox))))
(set-font loader size)
(set-rgba-fill 1 1 1 0.1)
(clear-canvas)
(set-rgb-fill 0 0 0)
(translate (- (xmin bbox)) (- (ymin bbox)))
(draw-string 0 0 string)
(save-png file)))))
(defun arc-to (center-x center-y radius start extent)
;; An arc of extent zero will generate an error at bezarc (divide by zero).
;; This case may be given by two aligned points in a polyline.
;; Better do nothing.
(unless (zerop extent)
(if (<= (abs extent) (/ pi 2.0))
(multiple-value-bind (x1 y1 x2 y2 x3 y3)
(bezarc center-x center-y radius start extent)
(curve-to x1 y1 x2 y2 x3 y3))
(let ((half-extent (/ extent 2.0)))
(arc-to center-x center-y radius start half-extent)
(arc-to center-x center-y radius (+ start half-extent) half-extent)))))
(defun bezarc (center-x center-y radius start extent)
;; start and extent should be in radians.
;; Returns first-control-point-x first-control-point-y
;; second-control-point-x second-control-point-y
;; end-point-x end-point-y
(let* ((end (+ start extent))
(s-start (sin start)) (c-start (cos start))
(s-end (sin end)) (c-end (cos end))
(ang/2 (/ extent 2.0))
(kappa (* (/ 4.0 3.0)
(/ (- 1 (cos ang/2))
(sin ang/2))))
(x1 (- c-start (* kappa s-start)))
(y1 (+ s-start (* kappa c-start)))
(x2 (+ c-end (* kappa s-end)))
(y2 (- s-end (* kappa c-end))))
(values (+ (* x1 radius) center-x)(+ (* y1 radius) center-y)
(+ (* x2 radius) center-x)(+ (* y2 radius) center-y)
(+ (* c-end radius) center-x)(+ (* s-end radius) center-y))))
(defun degrees (degrees)
(* (/ pi 180) degrees))
(defun arc-test (file)
(with-canvas (:width 100 :height 100)
(rotate-degrees 15)
(translate 0 10)
(set-line-width 10)
(move-to 75 0)
(arc-to 0 0 75 0 (degrees 15))
(stroke)
(save-png file)))
(defun rect-test (file)
(with-canvas (:width 5 :height 5)
(set-rgba-fill 1 0 0 0.5)
(rectangle 0 0 5 5)
(fill-path)
(save-png file)))
(defun text-test (&key string size font file)
(with-canvas (:width 200 :height 200)
(let ((loader (get-font font)))
(set-rgb-fill 0.8 0.8 0.9)
(clear-canvas)
(set-font loader size)
(set-rgb-fill 0.0 0.0 0.3)
(scale 0.5 0.5)
(rotate (* 15 (/ pi 180)))
(draw-string 10 10 string)
(save-png file))))
(defun dash-test (file)
(with-canvas (:width 200 :height 200)
(rectangle 10 10 125 125)
(set-rgba-fill 0.3 0.5 0.9 0.5)
(set-line-width 4)
(set-dash-pattern #(10 10) 5)
(fill-and-stroke)
(save-png file)))
(defun sign-test (string font file &key
(font-size 72)
(outer-border 2)
(stripe-width 5)
(inner-border 2)
(corner-radius 10))
(zpb-ttf:with-font-loader (loader font)
(let* ((bbox (string-bounding-box string font-size loader))
(text-height (ceiling (- (ymax bbox) (ymin bbox))))
(text-width (ceiling (- (xmax bbox) (xmin bbox))))
(stripe/2 (/ stripe-width 2.0))
(b1 (+ outer-border stripe/2))
(b2 (+ inner-border stripe/2))
(x0 0)
(x1 (+ x0 b1))
(x2 (+ x1 b2))
(y0 0)
(y1 (+ y0 b1))
(y2 (+ y1 b2))
(width (truncate (+ text-width (* 2 (+ b1 b2)))))
(width1 (- width (* b1 2)))
(height (truncate (+ text-height (* 2 (+ b1 b2)))))
(height1 (- height (* b1 2))))
(with-canvas (:width width :height height)
(set-rgb-fill 0.0 0.43 0.33)
(set-rgb-stroke 0.95 0.95 0.95)
;; Stripe shadow + stripe
(set-line-width stripe-width)
(with-graphics-state
(translate 2 -2)
(set-rgba-stroke 0.0 0.0 0.0 0.3)
(rounded-rectangle x1 y1
width1 height1
corner-radius corner-radius)
(fill-and-stroke))
(rounded-rectangle x1 y1
width1 height1
corner-radius corner-radius)
(set-dash-pattern #(10 20) 0)
(stroke)
;; Text shadow & text
(set-font loader font-size)
(translate (- (xmin bbox)) (- (ymin bbox)))
(with-graphics-state
(translate 1 -1)
(set-rgba-fill 0.0 0.0 0.0 1.0)
(draw-string x2 y2 string))
(set-rgb-fill 0.95 0.95 0.95)
(draw-string x2 y2 string)
(save-png file)))))
(defun fill-test (file)
(with-canvas (:width 100 :height 100)
(set-rgb-stroke 1 0 0)
(set-rgb-fill 0 1 0)
(move-to 0 0)
(line-to 50 50)
(line-to 100 10)
(fill-and-stroke)
(save-png file)))
(defun circle-test (file)
(with-canvas (:width 1000 :height 1000)
(scale 5 10)
(set-line-width 3)
(centered-circle-path 50 50 45)
(set-rgb-fill 1 1 0)
(fill-and-stroke)
(save-png file)))
(defun test-gradient (file fun)
(with-canvas (:width 500 :height 500)
(with-graphics-state
(set-gradient 100 100 1 0 0 1
200 235 0 1 0 1
:domain-function fun)
(rectangle 0 0 500 500)
(fill-path))
(with-graphics-state
(set-rgba-stroke 1 1 1 0.5)
(set-dash-pattern #(10 10) 0)
(move-to 100 100)
(line-to 200 235)
(stroke))
(set-rgb-stroke 1 1 1)
(centered-circle-path 100 100 10)
(stroke)
(set-rgb-stroke 0 0 0)
(centered-circle-path 200 235 10)
(stroke)
(set-rgb-fill 1 1 1)
(let* ((font (get-font #p"~/.fonts/cour.ttf"))
(name (string-downcase fun))
(bbox (geometry:bbox-box (string-bounding-box name 24 font))))
(translate 200 300)
(set-font font 24)
(setf bbox (geometry:expand bbox 10))
(rectangle (geometry:xmin bbox) (geometry:ymin bbox)
(geometry:width bbox) (geometry:height bbox))
(fill-and-stroke)
(set-rgb-fill 0 0 0)
(draw-string 0 0 (string-downcase fun))
(fill-path)
(save-png file))))