Source canvas api path.nas
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#-------------------------------------------------------------------------------
# canvas.Path
#-------------------------------------------------------------------------------
# Class for an (OpenVG) path element on a canvas
#
var Path = {
# Path segment commands (VGPathCommand)
VG_CLOSE_PATH: 0,
VG_MOVE_TO: 2,
VG_MOVE_TO_ABS: 2,
VG_MOVE_TO_REL: 3,
VG_LINE_TO: 4,
VG_LINE_TO_ABS: 4,
VG_LINE_TO_REL: 5,
VG_HLINE_TO: 6,
VG_HLINE_TO_ABS: 6,
VG_HLINE_TO_REL: 7,
VG_VLINE_TO: 8,
VG_VLINE_TO_ABS: 8,
VG_VLINE_TO_REL: 9,
VG_QUAD_TO: 10,
VG_QUAD_TO_ABS: 10,
VG_QUAD_TO_REL: 11,
VG_CUBIC_TO: 12,
VG_CUBIC_TO_ABS: 12,
VG_CUBIC_TO_REL: 13,
VG_SQUAD_TO: 14,
VG_SQUAD_TO_ABS: 14,
VG_SQUAD_TO_REL: 15,
VG_SCUBIC_TO: 16,
VG_SCUBIC_TO_ABS: 16,
VG_SCUBIC_TO_REL: 17,
VG_SCCWARC_TO: 20, # Note that CC and CCW commands are swapped. This is
VG_SCCWARC_TO_ABS:20, # needed due to the different coordinate systems used.
VG_SCCWARC_TO_REL:21, # In OpenVG values along the y-axis increase from bottom
VG_SCWARC_TO: 18, # to top, whereas in the Canvas system it is flipped.
VG_SCWARC_TO_ABS: 18,
VG_SCWARC_TO_REL: 19,
VG_LCCWARC_TO: 24,
VG_LCCWARC_TO_ABS:24,
VG_LCCWARC_TO_REL:25,
VG_LCWARC_TO: 22,
VG_LCWARC_TO_ABS: 22,
VG_LCWARC_TO_REL: 23,
# Number of coordinates per command
num_coords: [
0, 0, # VG_CLOSE_PATH
2, 2, # VG_MOVE_TO
2, 2, # VG_LINE_TO
1, 1, # VG_HLINE_TO
1, 1, # VG_VLINE_TO
4, 4, # VG_QUAD_TO
6, 6, # VG_CUBIC_TO
2, 2, # VG_SQUAD_TO
4, 4, # VG_SCUBIC_TO
5, 5, # VG_SCCWARC_TO
5, 5, # VG_SCWARC_TO
5, 5, # VG_LCCWARC_TO
5, 5, # VG_LCWARC_TO
],
#
new: func(ghost)
{
var obj = {
parents: [Path, Element.new(ghost)],
_first_cmd: 0,
_first_coord: 0,
_last_cmd: -1,
_last_coord: -1
};
return obj;
},
# Remove all existing path data
reset: func {
me._node.removeChildren("cmd", 0);
me._node.removeChildren("coord", 0);
me._node.removeChildren("coord-geo", 0);
me._first_cmd = 0;
me._first_coord = 0;
me._last_cmd = -1;
me._last_coord = -1;
return me;
},
# Set the path data (commands and coordinates)
setData: func(cmds, coords) {
me.reset();
me._node.setValues({cmd: cmds, coord: coords});
me._last_cmd = size(cmds) - 1;
me._last_coord = size(coords) - 1;
return me;
},
setDataGeo: func(cmds, coords) {
me.reset();
me._node.setValues({cmd: cmds, "coord-geo": coords});
me._last_cmd = size(cmds) - 1;
me._last_coord = size(coords) - 1;
return me;
},
# Add a path segment
addSegment: func(cmd, coords...) {
var coords = _arg2valarray(coords);
var num_coords = me.num_coords[cmd];
if (size(coords) != num_coords) {
debug.warn("Invalid number of arguments (expected "~num_coords~")");
}
else {
me.setInt("cmd["~(me._last_cmd += 1)~"]", cmd);
for (var i = 0; i < num_coords; i += 1)
me.setDouble("coord["~(me._last_coord += 1)~"]", coords[i]);
}
return me;
},
addSegmentGeo: func(cmd, coords...) {
var coords = _arg2valarray(coords);
var num_coords = me.num_coords[cmd];
if (size(coords) != num_coords) {
debug.warn("Invalid number of arguments (expected "~num_coords~")");
}
else {
me.setInt("cmd["~(me._last_cmd += 1)~"]", cmd);
for (var i = 0; i < num_coords; i += 1)
me.set("coord-geo["~(me._last_coord += 1)~"]", coords[i]);
}
return me;
},
# Remove first segment
pop_front: func {
me._removeSegment(1);
},
# Remove last segment
pop_back: func {
me._removeSegment(0);
},
# Get the number of segments
getNumSegments: func() {
return me._last_cmd - me._first_cmd + 1;
},
# Get the number of coordinates (each command has 0..n coords)
getNumCoords: func() {
return me._last_coord - me._first_coord + 1;
},
# Move path cursor
moveTo: func me.addSegment(me.VG_MOVE_TO_ABS, arg),
move: func me.addSegment(me.VG_MOVE_TO_REL, arg),
# Add a line
lineTo: func me.addSegment(me.VG_LINE_TO_ABS, arg),
line: func me.addSegment(me.VG_LINE_TO_REL, arg),
# Add a horizontal line
horizTo: func me.addSegment(me.VG_HLINE_TO_ABS, arg),
horiz: func me.addSegment(me.VG_HLINE_TO_REL, arg),
# Add a vertical line
vertTo: func me.addSegment(me.VG_VLINE_TO_ABS, arg),
vert: func me.addSegment(me.VG_VLINE_TO_REL, arg),
# Add a quadratic Bézier curve
quadTo: func me.addSegment(me.VG_QUAD_TO_ABS, arg),
quad: func me.addSegment(me.VG_QUAD_TO_REL, arg),
# Add a cubic Bézier curve
cubicTo: func me.addSegment(me.VG_CUBIC_TO_ABS, arg),
cubic: func me.addSegment(me.VG_CUBIC_TO_REL, arg),
# Add a smooth quadratic Bézier curve
squadTo: func me.addSegment(me.VG_SQUAD_TO_ABS, arg),
squad: func me.addSegment(me.VG_SQUAD_TO_REL, arg),
# Add a smooth cubic Bézier curve
scubicTo: func me.addSegment(me.VG_SCUBIC_TO_ABS, arg),
scubic: func me.addSegment(me.VG_SCUBIC_TO_REL, arg),
# Draw an elliptical arc (shorter counter-clockwise arc)
arcSmallCCWTo: func me.addSegment(me.VG_SCCWARC_TO_ABS, arg),
arcSmallCCW: func me.addSegment(me.VG_SCCWARC_TO_REL, arg),
# Draw an elliptical arc (shorter clockwise arc)
arcSmallCWTo: func me.addSegment(me.VG_SCWARC_TO_ABS, arg),
arcSmallCW: func me.addSegment(me.VG_SCWARC_TO_REL, arg),
# Draw an elliptical arc (longer counter-clockwise arc)
arcLargeCCWTo: func me.addSegment(me.VG_LCCWARC_TO_ABS, arg),
arcLargeCCW: func me.addSegment(me.VG_LCCWARC_TO_REL, arg),
# Draw an elliptical arc (shorter clockwise arc)
arcLargeCWTo: func me.addSegment(me.VG_LCWARC_TO_ABS, arg),
arcLargeCW: func me.addSegment(me.VG_LCWARC_TO_REL, arg),
# Close the path (implicit lineTo to first point of path)
close: func me.addSegment(me.VG_CLOSE_PATH),
# Add a (rounded) rectangle to the path
#
# @param x Position of left border
# @param y Position of top border
# @param w Width
# @param h Height
# @param cfg Optional settings (eg. {"border-top-radius": 5})
rect: func(x, y, w, h, cfg = nil) {
var opts = (cfg != nil) ? cfg : {};
# resolve border-[top-,bottom-][left-,right-]radius
var br = opts["border-radius"];
if (isscalar(br)) {
br = [br, br];
}
var _parseRadius = func(id) {
if ((var r = opts["border-"~id~"-radius"]) == nil) {
# parse top, bottom, left, right separate if no value specified for
# single corner
foreach(var s; ["top", "bottom", "left", "right"]) {
if (id.starts_with(s~"-")) {
r = opts["border-"~s~"-radius"];
break;
}
}
}
if (r == nil) { return br; }
else if (isscalar(r)) { return [r, r]; }
else { return r; }
};
# top-left
if ((var r = _parseRadius("top-left")) != nil) {
me.moveTo(x, y + r[1]).arcSmallCWTo(r[0], r[1], 0, x + r[0], y);
}
else { me.moveTo(x, y); }
# top-right
if ((r = _parseRadius("top-right")) != nil) {
me.horizTo(x + w - r[0]).arcSmallCWTo(r[0], r[1], 0, x + w, y + r[1]);
}
else { me.horizTo(x + w); }
# bottom-right
if ((r = _parseRadius("bottom-right")) != nil) {
me.vertTo(y + h - r[1]).arcSmallCWTo(r[0], r[1], 0, x + w - r[0], y + h);
}
else { me.vertTo(y + h); }
# bottom-left
if ((r = _parseRadius("bottom-left")) != nil) {
me.horizTo(x + r[0]).arcSmallCWTo(r[0], r[1], 0, x, y + h - r[1]);
}
else { me.horizTo(x); }
return me.close();
},
# Add a (rounded) square to the path
#
# @param x Position of left border
# @param y Position of top border
# @param l length
# @param cfg Optional settings (eg. {"border-top-radius": 5})
square: func(x, y, l, cfg = nil) {
return me.rect(x, y, l, l, cfg);
},
# Add an ellipse to the path
#
# @param rx radius x
# @param ry radius y
# @param cx (optional) center x coordinate or vector [cx, cy]
# @param cy (optional) center y coordinate
ellipse: func(rx, ry, cx = nil, cy = nil) {
if (isvec(cx)) {
cy = cx[1];
cx = cx[0];
}
else {
cx = num(cx) or 0;
cy = num(cy) or 0;
}
me.moveTo(cx - rx, cy)
.arcSmallCW(rx, ry, 0, 2*rx, 0)
.arcSmallCW(rx, ry, 0, -2*rx, 0);
return me;
},
# Add a circle to the path
#
# @param r radius
# @param cx (optional) center x coordinate or vector [cx, cy]
# @param cy (optional) center y coordinate
circle: func(r, cx = nil, cy = nil) {
return me.ellipse(r, r, cx, cy);
},
setColor: func {
me.setStroke(_getColor(arg));
},
getColor: func {
me.getStroke();
},
setColorFill: func {
me.setFill(_getColor(arg));
},
getColorFill: func {
me.getColorFill();
},
setFill: func(fill) {
me.set("fill", fill);
},
setStroke: func(stroke) {
me.set("stroke", stroke);
},
getStroke: func {
me.get("stroke");
},
setStrokeLineWidth: func(width) {
me.setDouble("stroke-width", width);
},
# Set stroke linecap
#
# @param linecap String, "butt", "round" or "square"
#
# See http://www.w3.org/TR/SVG/painting.html#StrokeLinecapProperty for details
setStrokeLineCap: func(linecap) {
me.set("stroke-linecap", linecap);
},
# Set stroke linejoin
#
# @param linejoin String, "miter", "round" or "bevel"
#
# See http://www.w3.org/TR/SVG/painting.html#StrokeLinejoinProperty for details
setStrokeLineJoin: func(linejoin) {
me.set("stroke-linejoin", linejoin);
},
# Set stroke dasharray
#
# @param pattern Vector, Vector of alternating dash and gap lengths
# [on1, off1, on2, ...]
setStrokeDashArray: func(pattern) {
if (isvec(pattern)) {
me.set("stroke-dasharray", string.join(",", pattern));
}
else {
debug.warn("setStrokeDashArray: vector expected!");
}
return me;
},
# private:
_removeSegment: func(front) {
if (me.getNumSegments() < 1) {
debug.warn("No segment available");
return me;
}
var cmd = front ? me._first_cmd : me._last_cmd;
var num_coords = me.num_coords[me.get("cmd["~cmd~"]")];
if (me.getNumCoords() < num_coords) {
debug.warn("To few coords available");
}
me._node.removeChild("cmd", cmd);
var first_coord = front ? me._first_coord : me._last_coord - num_coords + 1;
for (var i = 0; i < num_coords; i += 1) {
me._node.removeChild("coord", first_coord + i);
}
if (front) {
me._first_cmd += 1;
me._first_coord += num_coords;
}
else {
me._last_cmd -= 1;
me._last_coord -= num_coords;
}
return me;
},
};