| 1 |
#------------------------------------------------------------------------------- |
| 2 |
# canvas.Path |
| 3 |
#------------------------------------------------------------------------------- |
| 4 |
# Class for an (OpenVG) path element on a canvas |
| 5 |
# |
| 6 |
var Path = { |
| 7 |
# Path segment commands (VGPathCommand) |
| 8 |
VG_CLOSE_PATH: 0, |
| 9 |
VG_MOVE_TO: 2, |
| 10 |
VG_MOVE_TO_ABS: 2, |
| 11 |
VG_MOVE_TO_REL: 3, |
| 12 |
VG_LINE_TO: 4, |
| 13 |
VG_LINE_TO_ABS: 4, |
| 14 |
VG_LINE_TO_REL: 5, |
| 15 |
VG_HLINE_TO: 6, |
| 16 |
VG_HLINE_TO_ABS: 6, |
| 17 |
VG_HLINE_TO_REL: 7, |
| 18 |
VG_VLINE_TO: 8, |
| 19 |
VG_VLINE_TO_ABS: 8, |
| 20 |
VG_VLINE_TO_REL: 9, |
| 21 |
VG_QUAD_TO: 10, |
| 22 |
VG_QUAD_TO_ABS: 10, |
| 23 |
VG_QUAD_TO_REL: 11, |
| 24 |
VG_CUBIC_TO: 12, |
| 25 |
VG_CUBIC_TO_ABS: 12, |
| 26 |
VG_CUBIC_TO_REL: 13, |
| 27 |
VG_SQUAD_TO: 14, |
| 28 |
VG_SQUAD_TO_ABS: 14, |
| 29 |
VG_SQUAD_TO_REL: 15, |
| 30 |
VG_SCUBIC_TO: 16, |
| 31 |
VG_SCUBIC_TO_ABS: 16, |
| 32 |
VG_SCUBIC_TO_REL: 17, |
| 33 |
VG_SCCWARC_TO: 20, # Note that CC and CCW commands are swapped. This is |
| 34 |
VG_SCCWARC_TO_ABS:20, # needed due to the different coordinate systems used. |
| 35 |
VG_SCCWARC_TO_REL:21, # In OpenVG values along the y-axis increase from bottom |
| 36 |
VG_SCWARC_TO: 18, # to top, whereas in the Canvas system it is flipped. |
| 37 |
VG_SCWARC_TO_ABS: 18, |
| 38 |
VG_SCWARC_TO_REL: 19, |
| 39 |
VG_LCCWARC_TO: 24, |
| 40 |
VG_LCCWARC_TO_ABS:24, |
| 41 |
VG_LCCWARC_TO_REL:25, |
| 42 |
VG_LCWARC_TO: 22, |
| 43 |
VG_LCWARC_TO_ABS: 22, |
| 44 |
VG_LCWARC_TO_REL: 23, |
| 45 |
|
| 46 |
# Number of coordinates per command |
| 47 |
num_coords: [ |
| 48 |
0, 0, # VG_CLOSE_PATH |
| 49 |
2, 2, # VG_MOVE_TO |
| 50 |
2, 2, # VG_LINE_TO |
| 51 |
1, 1, # VG_HLINE_TO |
| 52 |
1, 1, # VG_VLINE_TO |
| 53 |
4, 4, # VG_QUAD_TO |
| 54 |
6, 6, # VG_CUBIC_TO |
| 55 |
2, 2, # VG_SQUAD_TO |
| 56 |
4, 4, # VG_SCUBIC_TO |
| 57 |
5, 5, # VG_SCCWARC_TO |
| 58 |
5, 5, # VG_SCWARC_TO |
| 59 |
5, 5, # VG_LCCWARC_TO |
| 60 |
5, 5, # VG_LCWARC_TO |
| 61 |
], |
| 62 |
|
| 63 |
# |
| 64 |
new: func(ghost) |
| 65 |
{ |
| 66 |
var obj = { |
| 67 |
parents: [Path, Element.new(ghost)], |
| 68 |
_first_cmd: 0, |
| 69 |
_first_coord: 0, |
| 70 |
_last_cmd: -1, |
| 71 |
_last_coord: -1 |
| 72 |
}; |
| 73 |
return obj; |
| 74 |
}, |
| 75 |
|
| 76 |
# Remove all existing path data |
| 77 |
reset: func { |
| 78 |
me._node.removeChildren("cmd", 0); |
| 79 |
me._node.removeChildren("coord", 0); |
| 80 |
me._node.removeChildren("coord-geo", 0); |
| 81 |
me._first_cmd = 0; |
| 82 |
me._first_coord = 0; |
| 83 |
me._last_cmd = -1; |
| 84 |
me._last_coord = -1; |
| 85 |
return me; |
| 86 |
}, |
| 87 |
|
| 88 |
# Set the path data (commands and coordinates) |
| 89 |
setData: func(cmds, coords) { |
| 90 |
me.reset(); |
| 91 |
me._node.setValues({cmd: cmds, coord: coords}); |
| 92 |
me._last_cmd = size(cmds) - 1; |
| 93 |
me._last_coord = size(coords) - 1; |
| 94 |
return me; |
| 95 |
}, |
| 96 |
|
| 97 |
setDataGeo: func(cmds, coords) { |
| 98 |
me.reset(); |
| 99 |
me._node.setValues({cmd: cmds, "coord-geo": coords}); |
| 100 |
me._last_cmd = size(cmds) - 1; |
| 101 |
me._last_coord = size(coords) - 1; |
| 102 |
return me; |
| 103 |
}, |
| 104 |
|
| 105 |
# Add a path segment |
| 106 |
addSegment: func(cmd, coords...) { |
| 107 |
var coords = _arg2valarray(coords); |
| 108 |
var num_coords = me.num_coords[cmd]; |
| 109 |
if (size(coords) != num_coords) { |
| 110 |
debug.warn("Invalid number of arguments (expected "~num_coords~")"); |
| 111 |
} |
| 112 |
else { |
| 113 |
me.setInt("cmd["~(me._last_cmd += 1)~"]", cmd); |
| 114 |
for (var i = 0; i < num_coords; i += 1) |
| 115 |
me.setDouble("coord["~(me._last_coord += 1)~"]", coords[i]); |
| 116 |
} |
| 117 |
|
| 118 |
return me; |
| 119 |
}, |
| 120 |
|
| 121 |
addSegmentGeo: func(cmd, coords...) { |
| 122 |
var coords = _arg2valarray(coords); |
| 123 |
var num_coords = me.num_coords[cmd]; |
| 124 |
if (size(coords) != num_coords) { |
| 125 |
debug.warn("Invalid number of arguments (expected "~num_coords~")"); |
| 126 |
} |
| 127 |
else { |
| 128 |
me.setInt("cmd["~(me._last_cmd += 1)~"]", cmd); |
| 129 |
for (var i = 0; i < num_coords; i += 1) |
| 130 |
me.set("coord-geo["~(me._last_coord += 1)~"]", coords[i]); |
| 131 |
} |
| 132 |
return me; |
| 133 |
}, |
| 134 |
|
| 135 |
# Remove first segment |
| 136 |
pop_front: func { |
| 137 |
me._removeSegment(1); |
| 138 |
}, |
| 139 |
|
| 140 |
# Remove last segment |
| 141 |
pop_back: func { |
| 142 |
me._removeSegment(0); |
| 143 |
}, |
| 144 |
|
| 145 |
# Get the number of segments |
| 146 |
getNumSegments: func() { |
| 147 |
return me._last_cmd - me._first_cmd + 1; |
| 148 |
}, |
| 149 |
|
| 150 |
# Get the number of coordinates (each command has 0..n coords) |
| 151 |
getNumCoords: func() { |
| 152 |
return me._last_coord - me._first_coord + 1; |
| 153 |
}, |
| 154 |
|
| 155 |
# Move path cursor |
| 156 |
moveTo: func me.addSegment(me.VG_MOVE_TO_ABS, arg), |
| 157 |
move: func me.addSegment(me.VG_MOVE_TO_REL, arg), |
| 158 |
# Add a line |
| 159 |
lineTo: func me.addSegment(me.VG_LINE_TO_ABS, arg), |
| 160 |
line: func me.addSegment(me.VG_LINE_TO_REL, arg), |
| 161 |
# Add a horizontal line |
| 162 |
horizTo: func me.addSegment(me.VG_HLINE_TO_ABS, arg), |
| 163 |
horiz: func me.addSegment(me.VG_HLINE_TO_REL, arg), |
| 164 |
# Add a vertical line |
| 165 |
vertTo: func me.addSegment(me.VG_VLINE_TO_ABS, arg), |
| 166 |
vert: func me.addSegment(me.VG_VLINE_TO_REL, arg), |
| 167 |
# Add a quadratic Bézier curve |
| 168 |
quadTo: func me.addSegment(me.VG_QUAD_TO_ABS, arg), |
| 169 |
quad: func me.addSegment(me.VG_QUAD_TO_REL, arg), |
| 170 |
# Add a cubic Bézier curve |
| 171 |
cubicTo: func me.addSegment(me.VG_CUBIC_TO_ABS, arg), |
| 172 |
cubic: func me.addSegment(me.VG_CUBIC_TO_REL, arg), |
| 173 |
# Add a smooth quadratic Bézier curve |
| 174 |
squadTo: func me.addSegment(me.VG_SQUAD_TO_ABS, arg), |
| 175 |
squad: func me.addSegment(me.VG_SQUAD_TO_REL, arg), |
| 176 |
# Add a smooth cubic Bézier curve |
| 177 |
scubicTo: func me.addSegment(me.VG_SCUBIC_TO_ABS, arg), |
| 178 |
scubic: func me.addSegment(me.VG_SCUBIC_TO_REL, arg), |
| 179 |
# Draw an elliptical arc (shorter counter-clockwise arc) |
| 180 |
arcSmallCCWTo: func me.addSegment(me.VG_SCCWARC_TO_ABS, arg), |
| 181 |
arcSmallCCW: func me.addSegment(me.VG_SCCWARC_TO_REL, arg), |
| 182 |
# Draw an elliptical arc (shorter clockwise arc) |
| 183 |
arcSmallCWTo: func me.addSegment(me.VG_SCWARC_TO_ABS, arg), |
| 184 |
arcSmallCW: func me.addSegment(me.VG_SCWARC_TO_REL, arg), |
| 185 |
# Draw an elliptical arc (longer counter-clockwise arc) |
| 186 |
arcLargeCCWTo: func me.addSegment(me.VG_LCCWARC_TO_ABS, arg), |
| 187 |
arcLargeCCW: func me.addSegment(me.VG_LCCWARC_TO_REL, arg), |
| 188 |
# Draw an elliptical arc (shorter clockwise arc) |
| 189 |
arcLargeCWTo: func me.addSegment(me.VG_LCWARC_TO_ABS, arg), |
| 190 |
arcLargeCW: func me.addSegment(me.VG_LCWARC_TO_REL, arg), |
| 191 |
# Close the path (implicit lineTo to first point of path) |
| 192 |
close: func me.addSegment(me.VG_CLOSE_PATH), |
| 193 |
|
| 194 |
# Add a (rounded) rectangle to the path |
| 195 |
# |
| 196 |
# @param x Position of left border |
| 197 |
# @param y Position of top border |
| 198 |
# @param w Width |
| 199 |
# @param h Height |
| 200 |
# @param cfg Optional settings (eg. {"border-top-radius": 5}) |
| 201 |
rect: func(x, y, w, h, cfg = nil) { |
| 202 |
var opts = (cfg != nil) ? cfg : {}; |
| 203 |
|
| 204 |
# resolve border-[top-,bottom-][left-,right-]radius |
| 205 |
var br = opts["border-radius"]; |
| 206 |
if (isscalar(br)) { |
| 207 |
br = [br, br]; |
| 208 |
} |
| 209 |
|
| 210 |
var _parseRadius = func(id) { |
| 211 |
if ((var r = opts["border-"~id~"-radius"]) == nil) { |
| 212 |
# parse top, bottom, left, right separate if no value specified for |
| 213 |
# single corner |
| 214 |
foreach(var s; ["top", "bottom", "left", "right"]) { |
| 215 |
if (id.starts_with(s~"-")) { |
| 216 |
r = opts["border-"~s~"-radius"]; |
| 217 |
break; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if (r == nil) { return br; } |
| 223 |
else if (isscalar(r)) { return [r, r]; } |
| 224 |
else { return r; } |
| 225 |
}; |
| 226 |
|
| 227 |
# top-left |
| 228 |
if ((var r = _parseRadius("top-left")) != nil) { |
| 229 |
me.moveTo(x, y + r[1]).arcSmallCWTo(r[0], r[1], 0, x + r[0], y); |
| 230 |
} |
| 231 |
else { me.moveTo(x, y); } |
| 232 |
|
| 233 |
# top-right |
| 234 |
if ((r = _parseRadius("top-right")) != nil) { |
| 235 |
me.horizTo(x + w - r[0]).arcSmallCWTo(r[0], r[1], 0, x + w, y + r[1]); |
| 236 |
} |
| 237 |
else { me.horizTo(x + w); } |
| 238 |
|
| 239 |
# bottom-right |
| 240 |
if ((r = _parseRadius("bottom-right")) != nil) { |
| 241 |
me.vertTo(y + h - r[1]).arcSmallCWTo(r[0], r[1], 0, x + w - r[0], y + h); |
| 242 |
} |
| 243 |
else { me.vertTo(y + h); } |
| 244 |
|
| 245 |
# bottom-left |
| 246 |
if ((r = _parseRadius("bottom-left")) != nil) { |
| 247 |
me.horizTo(x + r[0]).arcSmallCWTo(r[0], r[1], 0, x, y + h - r[1]); |
| 248 |
} |
| 249 |
else { me.horizTo(x); } |
| 250 |
return me.close(); |
| 251 |
}, |
| 252 |
|
| 253 |
# Add a (rounded) square to the path |
| 254 |
# |
| 255 |
# @param x Position of left border |
| 256 |
# @param y Position of top border |
| 257 |
# @param l length |
| 258 |
# @param cfg Optional settings (eg. {"border-top-radius": 5}) |
| 259 |
square: func(x, y, l, cfg = nil) { |
| 260 |
return me.rect(x, y, l, l, cfg); |
| 261 |
}, |
| 262 |
|
| 263 |
# Add an ellipse to the path |
| 264 |
# |
| 265 |
# @param rx radius x |
| 266 |
# @param ry radius y |
| 267 |
# @param cx (optional) center x coordinate or vector [cx, cy] |
| 268 |
# @param cy (optional) center y coordinate |
| 269 |
ellipse: func(rx, ry, cx = nil, cy = nil) { |
| 270 |
if (isvec(cx)) { |
| 271 |
cy = cx[1]; |
| 272 |
cx = cx[0]; |
| 273 |
} |
| 274 |
else { |
| 275 |
cx = num(cx) or 0; |
| 276 |
cy = num(cy) or 0; |
| 277 |
} |
| 278 |
me.moveTo(cx - rx, cy) |
| 279 |
.arcSmallCW(rx, ry, 0, 2*rx, 0) |
| 280 |
.arcSmallCW(rx, ry, 0, -2*rx, 0); |
| 281 |
return me; |
| 282 |
}, |
| 283 |
|
| 284 |
# Add a circle to the path |
| 285 |
# |
| 286 |
# @param r radius |
| 287 |
# @param cx (optional) center x coordinate or vector [cx, cy] |
| 288 |
# @param cy (optional) center y coordinate |
| 289 |
circle: func(r, cx = nil, cy = nil) { |
| 290 |
return me.ellipse(r, r, cx, cy); |
| 291 |
}, |
| 292 |
|
| 293 |
setColor: func { |
| 294 |
me.setStroke(_getColor(arg)); |
| 295 |
}, |
| 296 |
|
| 297 |
getColor: func { |
| 298 |
me.getStroke(); |
| 299 |
}, |
| 300 |
|
| 301 |
setColorFill: func { |
| 302 |
me.setFill(_getColor(arg)); |
| 303 |
}, |
| 304 |
|
| 305 |
getColorFill: func { |
| 306 |
me.getColorFill(); |
| 307 |
}, |
| 308 |
|
| 309 |
setFill: func(fill) { |
| 310 |
me.set("fill", fill); |
| 311 |
}, |
| 312 |
|
| 313 |
setStroke: func(stroke) { |
| 314 |
me.set("stroke", stroke); |
| 315 |
}, |
| 316 |
|
| 317 |
getStroke: func { |
| 318 |
me.get("stroke"); |
| 319 |
}, |
| 320 |
|
| 321 |
setStrokeLineWidth: func(width) { |
| 322 |
me.setDouble("stroke-width", width); |
| 323 |
}, |
| 324 |
|
| 325 |
# Set stroke linecap |
| 326 |
# |
| 327 |
# @param linecap String, "butt", "round" or "square" |
| 328 |
# |
| 329 |
# See http://www.w3.org/TR/SVG/painting.html#StrokeLinecapProperty for details |
| 330 |
setStrokeLineCap: func(linecap) { |
| 331 |
me.set("stroke-linecap", linecap); |
| 332 |
}, |
| 333 |
|
| 334 |
# Set stroke linejoin |
| 335 |
# |
| 336 |
# @param linejoin String, "miter", "round" or "bevel" |
| 337 |
# |
| 338 |
# See http://www.w3.org/TR/SVG/painting.html#StrokeLinejoinProperty for details |
| 339 |
setStrokeLineJoin: func(linejoin) { |
| 340 |
me.set("stroke-linejoin", linejoin); |
| 341 |
}, |
| 342 |
|
| 343 |
# Set stroke dasharray |
| 344 |
# |
| 345 |
# @param pattern Vector, Vector of alternating dash and gap lengths |
| 346 |
# [on1, off1, on2, ...] |
| 347 |
setStrokeDashArray: func(pattern) { |
| 348 |
if (isvec(pattern)) { |
| 349 |
me.set("stroke-dasharray", string.join(",", pattern)); |
| 350 |
} |
| 351 |
else { |
| 352 |
debug.warn("setStrokeDashArray: vector expected!"); |
| 353 |
} |
| 354 |
return me; |
| 355 |
}, |
| 356 |
|
| 357 |
# private: |
| 358 |
_removeSegment: func(front) { |
| 359 |
if (me.getNumSegments() < 1) { |
| 360 |
debug.warn("No segment available"); |
| 361 |
return me; |
| 362 |
} |
| 363 |
|
| 364 |
var cmd = front ? me._first_cmd : me._last_cmd; |
| 365 |
var num_coords = me.num_coords[me.get("cmd["~cmd~"]")]; |
| 366 |
if (me.getNumCoords() < num_coords) { |
| 367 |
debug.warn("To few coords available"); |
| 368 |
} |
| 369 |
|
| 370 |
me._node.removeChild("cmd", cmd); |
| 371 |
|
| 372 |
var first_coord = front ? me._first_coord : me._last_coord - num_coords + 1; |
| 373 |
for (var i = 0; i < num_coords; i += 1) { |
| 374 |
me._node.removeChild("coord", first_coord + i); |
| 375 |
} |
| 376 |
|
| 377 |
if (front) { |
| 378 |
me._first_cmd += 1; |
| 379 |
me._first_coord += num_coords; |
| 380 |
} |
| 381 |
else { |
| 382 |
me._last_cmd -= 1; |
| 383 |
me._last_coord -= num_coords; |
| 384 |
} |
| 385 |
return me; |
| 386 |
}, |
| 387 |
}; |
| 388 |
|