- 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
import { FILL_RULE, Path, PATH } from './utils/Path';
import { SVGGraphicsNode } from './SVGGraphicsNode';
import { buildPath } from './utils/buildPath';
import { graphicsUtils } from '@pixi/graphics';
import dPathParser from 'd-path-parser';
graphicsUtils.FILL_COMMANDS[PATH] = buildPath;
/**
* Draws SVG <path /> elements.
*
* @public
*/
export class SVGPathNode extends SVGGraphicsNode
{
private currentPath2: Path;
private startPath(): void
{
if (this.currentPath2)
{
const pts = this.currentPath2.points;
if (pts.length > 0)
{
this.currentPath2.closeContour();
}
}
else
{
this.currentPath2 = new Path();
}
}
private finishPath(): void
{
if (this.currentPath2)
{
this.currentPath2.closeContour();
}
}
// @ts-expect-error
get currentPath(): any
{
return this.currentPath2;
}
set currentPath(nothing: any)
{
if (nothing)
{
throw new Error('currentPath cannot be set');
}
// readonly
}
closePath(): any
{
this.currentPath2.points.push(this.currentPath2.points[0], this.currentPath2.points[1])
this.finishPath();
return this;
}
checkPath(): void
{
if (this.currentPath2.points.find((e) => isNaN(e)) !== undefined)
{
throw new Error('NaN is bad');
}
}
// Redirect moveTo, lineTo, ... onto paths!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :P
startPoly = this.startPath;
finishPoly = this.finishPath;
/**
* Embeds the `SVGPathElement` into this node.
*
* @param element - the path to draw
*/
embedPath(element: SVGPathElement): this
{
const d = element.getAttribute('d');
// Parse path commands using d-path-parser. This is an inefficient solution that causes excess memory allocation
// and should be optimized in the future.
const commands = dPathParser(d.trim());
// Current point
let x = 0;
let y = 0;
for (let i = 0, j = commands.length; i < j; i++)
{
const lastCommand = commands[i - 1];
const command = commands[i];
if (isNaN(x) || isNaN(y))
{
throw new Error('Data corruption');
}
// Taken from: https://github.com/bigtimebuddy/pixi-svg/blob/main/src/SVG.js
// Copyright Matt Karl
switch (command.code)
{
case 'm': {
this.moveTo(
x += command.end.x,
y += command.end.y,
);
break;
}
case 'M': {
this.moveTo(
x = command.end.x,
y = command.end.y,
);
break;
}
case 'H': {
this.lineTo(x = command.value, y);
break;
}
case 'h': {
this.lineTo(x += command.value, y);
break;
}
case 'V': {
this.lineTo(x, y = command.value);
break;
}
case 'v': {
this.lineTo(x, y += command.value);
break;
}
case 'z':
case 'Z': {
x = this.currentPath2?.points[0] || 0;
y = this.currentPath2?.points[1] || 0;
this.closePath();
break;
}
case 'L': {
this.lineTo(
x = command.end.x,
y = command.end.y,
);
break;
}
case 'l': {
this.lineTo(
x += command.end.x,
y += command.end.y,
);
break;
}
case 'C': {
this.bezierCurveTo(
command.cp1.x,
command.cp1.y,
command.cp2.x,
command.cp2.y,
x = command.end.x,
y = command.end.y,
);
break;
}
case 'c': {
const currX = x;
const currY = y;
this.bezierCurveTo(
currX + command.cp1.x,
currY + command.cp1.y,
currX + command.cp2.x,
currY + command.cp2.y,
x += command.end.x,
y += command.end.y,
);
break;
}
case 's':
case 'S': {
const cp1 = { x, y };
const lastCode = commands[i - 1] ? commands[i - 1].code : null;
if (i > 0 && (lastCode === 's' || lastCode === 'S' || lastCode === 'c' || lastCode === 'C'))
{
const lastCommand = commands[i - 1];
const lastCp2 = { ...(lastCommand.cp2 || lastCommand.cp) };
if (commands[i - 1].relative)
{
lastCp2.x += (x - lastCommand.end.x);
lastCp2.y += (y - lastCommand.end.y);
}
cp1.x = (2 * x) - lastCp2.x;
cp1.y = (2 * y) - lastCp2.y;
}
const cp2 = { x: command.cp.x , y: command.cp.y };
if (command.relative)
{
cp2.x += x;
cp2.y += y;
x += command.end.x;
y += command.end.y;
}
else
{
x = command.end.x;
y = command.end.y;
}
this.bezierCurveTo(
cp1.x,
cp1.y,
cp2.x,
cp2.y,
x,
y,
);
break;
}
case 'q': {
const currX = x;
const currY = y;
this.quadraticCurveTo(
currX + command.cp.x,
currY + command.cp.y,
x += command.end.x,
y += command.end.y,
);
break;
}
case 'Q': {
this.quadraticCurveTo(
command.cp.x,
command.cp.y,
x = command.end.x,
y = command.end.y,
);
break;
}
case 'A':
this.ellipticArcTo(
x = command.end.x,
y = command.end.y,
command.radii.x,
command.radii.y,
(command.rotation || 0) * Math.PI / 180,
!command.clockwise,
command.large,
);
break;
case 'a':
this.ellipticArcTo(
x += command.end.x,
y += command.end.y,
command.radii.x,
command.radii.y,
(command.rotation || 0) * Math.PI / 180,
!command.clockwise,
command.large,
);
break;
case 't':
case 'T': {
let cx: number;
let cy: number;
if (lastCommand && lastCommand.cp)
{
let lcx = lastCommand.cp.x;
let lcy = lastCommand.cp.y;
if (lastCommand.relative)
{
const lx = x - lastCommand.end.x;
const ly = y - lastCommand.end.y;
lcx += lx;
lcy += ly;
}
cx = (2 * x) - lcx;
cy = (2 * y) - lcy;
}
else
{
cx = x;
cy = y;
}
if (command.code === 't')
{
this.quadraticCurveTo(
cx,
cy,
x += command.end.x,
y += command.end.y,
);
}
else
{
this.quadraticCurveTo(
cx,
cy,
x = command.end.x,
y = command.end.y,
);
}
break;
}
default: {
console.warn('[PIXI.SVG] Draw command not supported:', command.code, command);
break;
}
}
}
if (this.currentPath2)
{
this.currentPath2.fillRule = element.getAttribute('fill-rule') as FILL_RULE || this.currentPath2.fillRule;
this.drawShape(this.currentPath2 as any);
this.currentPath2 = null;
}
return this;
}
}