plotboilerplate 1.19.0 → 1.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/README.md +2 -1
  2. package/dist/index.esm.js +81 -27
  3. package/dist/index.esm.js.map +1 -1
  4. package/dist/index.esm.min.js +1 -1
  5. package/dist/index.esm.min.js.map +1 -1
  6. package/dist/plotboilerplate.browser.min.js +1 -1
  7. package/dist/plotboilerplate.browser.min.js.map +1 -1
  8. package/dist/plotboilerplate.js +81 -39
  9. package/dist/plotboilerplate.min.js +1 -1
  10. package/package.json +1 -1
  11. package/src/cjs/Bounds.d.ts +4 -5
  12. package/src/cjs/Bounds.js +6 -8
  13. package/src/cjs/Bounds.js.map +1 -1
  14. package/src/cjs/PlotBoilerplate.d.ts +2 -1
  15. package/src/cjs/PlotBoilerplate.js +9 -3
  16. package/src/cjs/PlotBoilerplate.js.map +1 -1
  17. package/src/cjs/VEllipseSector.d.ts +4 -3
  18. package/src/cjs/VEllipseSector.js +15 -8
  19. package/src/cjs/VEllipseSector.js.map +1 -1
  20. package/src/cjs/Vertex.d.ts +33 -1
  21. package/src/cjs/Vertex.js +43 -1
  22. package/src/cjs/Vertex.js.map +1 -1
  23. package/src/cjs/drawutilssvg.d.ts +2 -1
  24. package/src/cjs/drawutilssvg.js +7 -19
  25. package/src/cjs/drawutilssvg.js.map +1 -1
  26. package/src/cjs/entry.js +1 -0
  27. package/src/cjs/interfaces/core.d.ts +3 -0
  28. package/src/cjs/interfaces/core.js +1 -0
  29. package/src/cjs/interfaces/core.js.map +1 -1
  30. package/src/cjs/utils/algorithms/detectPaths.d.ts +3 -2
  31. package/src/cjs/utils/creategui.js +7 -0
  32. package/src/cjs/utils/datastructures/Color.d.ts +1 -1
  33. package/src/cjs/utils/datastructures/Color.js +4 -2
  34. package/src/cjs/utils/datastructures/Color.js.map +1 -1
  35. package/src/cjs/utils/io/FileDrop.d.ts +11 -1
  36. package/src/cjs/utils/io/FileDrop.js +41 -10
  37. package/src/cjs/utils/io/FileDrop.js.map +1 -1
  38. package/src/cjs/utils/parsers/svg/parseSVGPathData.d.ts +27 -0
  39. package/src/cjs/utils/parsers/svg/parseSVGPathData.js +486 -0
  40. package/src/cjs/utils/parsers/svg/parseSVGPathData.js.map +1 -0
  41. package/src/cjs/utils/parsers/svg/splitSVGPathData.d.ts +20 -0
  42. package/src/cjs/utils/parsers/svg/splitSVGPathData.js +53 -0
  43. package/src/cjs/utils/parsers/svg/splitSVGPathData.js.map +1 -0
  44. package/src/cjs/utils/parsers/svg/types.d.ts +63 -0
  45. package/src/cjs/utils/parsers/svg/types.js +17 -0
  46. package/src/cjs/utils/parsers/svg/types.js.map +1 -0
  47. package/src/esm/Bounds.d.ts +4 -5
  48. package/src/esm/Bounds.js +6 -8
  49. package/src/esm/Bounds.js.map +1 -1
  50. package/src/esm/PlotBoilerplate.d.ts +2 -1
  51. package/src/esm/PlotBoilerplate.js +10 -4
  52. package/src/esm/PlotBoilerplate.js.map +1 -1
  53. package/src/esm/VEllipseSector.d.ts +4 -3
  54. package/src/esm/VEllipseSector.js +15 -8
  55. package/src/esm/VEllipseSector.js.map +1 -1
  56. package/src/esm/Vertex.d.ts +33 -1
  57. package/src/esm/Vertex.js +43 -1
  58. package/src/esm/Vertex.js.map +1 -1
  59. package/src/esm/drawutilssvg.d.ts +2 -1
  60. package/src/esm/drawutilssvg.js +7 -19
  61. package/src/esm/drawutilssvg.js.map +1 -1
  62. package/src/esm/entry.js +1 -0
  63. package/src/esm/interfaces/core.d.ts +3 -0
  64. package/src/esm/interfaces/core.js +1 -0
  65. package/src/esm/interfaces/core.js.map +1 -1
  66. package/src/esm/utils/algorithms/detectPaths.d.ts +3 -2
  67. package/src/esm/utils/algorithms/detectPaths.js.map +1 -1
  68. package/src/esm/utils/datastructures/Color.d.ts +1 -1
  69. package/src/esm/utils/datastructures/Color.js +4 -2
  70. package/src/esm/utils/datastructures/Color.js.map +1 -1
  71. package/src/esm/utils/io/FileDrop.d.ts +11 -1
  72. package/src/esm/utils/io/FileDrop.js +41 -10
  73. package/src/esm/utils/io/FileDrop.js.map +1 -1
  74. package/src/esm/utils/parsers/svg/parseSVGPathData.d.ts +27 -0
  75. package/src/esm/utils/parsers/svg/parseSVGPathData.js +482 -0
  76. package/src/esm/utils/parsers/svg/parseSVGPathData.js.map +1 -0
  77. package/src/esm/utils/parsers/svg/splitSVGPathData.d.ts +20 -0
  78. package/src/esm/utils/parsers/svg/splitSVGPathData.js +49 -0
  79. package/src/esm/utils/parsers/svg/splitSVGPathData.js.map +1 -0
  80. package/src/esm/utils/parsers/svg/types.d.ts +63 -0
  81. package/src/esm/utils/parsers/svg/types.js +16 -0
  82. package/src/esm/utils/parsers/svg/types.js.map +1 -0
  83. package/src/ts/Bounds.ts +6 -8
  84. package/src/ts/PlotBoilerplate.ts +11 -4
  85. package/src/ts/VEllipseSector.ts +22 -15
  86. package/src/ts/Vertex.ts +46 -1
  87. package/src/ts/drawutilssvg.ts +8 -19
  88. package/src/ts/interfaces/core.ts +3 -0
  89. package/src/ts/utils/algorithms/detectPaths.ts +1 -1
  90. package/src/ts/utils/datastructures/Color.ts +8 -4
  91. package/src/ts/utils/io/FileDrop.ts +51 -10
  92. package/src/ts/utils/parsers/svg/parseSVGPathData.ts +631 -0
  93. package/src/ts/utils/parsers/svg/splitSVGPathData.ts +54 -0
  94. package/src/ts/utils/parsers/svg/types.ts +90 -0
@@ -0,0 +1,631 @@
1
+ /**
2
+ * Parse SVG path data strings and convert to PlotBoilerplate path objects.
3
+ *
4
+ * As this parser function does not detect all possible path data strings it should cover the most daily use-cases.
5
+ *
6
+ * For more insight see
7
+ * https://www.w3.org/TR/SVG/paths.html
8
+ *
9
+ * @author Ikaros Kappler
10
+ * @date 2022-11-06
11
+ * @modified 2022-12-21 (winter solstice) Porting this to Typescript.
12
+ * @modified 2023-01-17 Handling multiple parameter sets now.
13
+ * @version 0.0.2-alpha
14
+ **/
15
+
16
+ import { CubicBezierCurve } from "../../../CubicBezierCurve";
17
+ import { XYCoords } from "../../../interfaces/core";
18
+ import { Line } from "../../../Line";
19
+ import { VEllipseSector } from "../../../VEllipseSector";
20
+ import { Vertex } from "../../../Vertex";
21
+ import { splitSVGPathData } from "./splitSVGPathData";
22
+ import {
23
+ SVGPathArcToCommand,
24
+ SVGPathCloseCommand,
25
+ SVGPathCommand,
26
+ SVGPathCubicCurveToCommand,
27
+ SVGPathHorizontalLineToCommand,
28
+ SVGPathLineToCommand,
29
+ SVGPathMoveToCommand,
30
+ SVGPathQuadraticCurveToCommand,
31
+ SVGPathShorthandCubicCurveToCommand,
32
+ SVGPathShorthandQuadraticCurveToCommand,
33
+ SVGPathVerticalLineToCommand
34
+ } from "./types";
35
+
36
+ const DEG_TO_RAD = Math.PI / 180;
37
+
38
+ /**
39
+ * Transform the given path data (translate and scale. rotating is not intended here).
40
+ *
41
+ * @date 2022-11-06
42
+ *
43
+ * @name parseSVGPathData
44
+ * @static
45
+ * @param {string} data - The data to parse.
46
+ * @return An array of straight line segments (Line) or curve segments (CubicBezierCurve) representing the path.
47
+ */
48
+ export const parseSVGPathData = (dataString: string): Array<Line | CubicBezierCurve> | null => {
49
+ const dataElements: Array<SVGPathCommand> | null = splitSVGPathData(dataString);
50
+
51
+ if (!dataElements) {
52
+ return null;
53
+ }
54
+
55
+ // Array<PathSegments>
56
+ const result: Array<Line | CubicBezierCurve> = [];
57
+
58
+ var i = 0;
59
+ const firstPoint: XYCoords = { x: NaN, y: NaN };
60
+ const lastPoint: XYCoords = { x: NaN, y: NaN };
61
+ const lastControlPoint: XYCoords = { x: NaN, y: NaN };
62
+ const lastQuadraticControlPoint: XYCoords = { x: NaN, y: NaN };
63
+
64
+ while (i < dataElements.length) {
65
+ // Could this also be SVGPathShorthandQuadraticCurveToCommand?
66
+ const data: SVGPathCommand = dataElements[i];
67
+ const cmd = data[0];
68
+ // console.log("cmd", cmd, "data", data);
69
+ switch (data[0]) {
70
+ case "M":
71
+ // MoveTo: M|m x y
72
+ _handleMove(data, false, firstPoint, lastPoint, lastControlPoint, result);
73
+ break;
74
+ case "m":
75
+ // MoveTo: M|m x y
76
+ _handleMove(data, true, firstPoint, lastPoint, lastControlPoint, result);
77
+ break;
78
+ case "L":
79
+ // LineTo L|l x y
80
+ _handleLineTo(data, false, firstPoint, lastPoint, lastControlPoint, result);
81
+ break;
82
+ case "l":
83
+ // LineTo L|l x y
84
+ _handleLineTo(data, true, firstPoint, lastPoint, lastControlPoint, result);
85
+ break;
86
+ case "H":
87
+ // HorizontalLineTo: H|h x
88
+ _handleHorizontalLineTo(data, false, firstPoint, lastPoint, lastControlPoint, result);
89
+ break;
90
+ case "h":
91
+ // HorizontalLineTo: H|h x
92
+ _handleHorizontalLineTo(data, true, firstPoint, lastPoint, lastControlPoint, result);
93
+ break;
94
+ case "V":
95
+ // VerticalLineTo: V|v y
96
+ _handleVerticalLineTo(data, false, firstPoint, lastPoint, lastControlPoint, result);
97
+ break;
98
+ case "v":
99
+ // VerticalLineTo: V|v y
100
+ _handleVerticalLineTo(data, true, firstPoint, lastPoint, lastControlPoint, result);
101
+ break;
102
+ case "C":
103
+ // CurveTo: C|c x1 y1 x2 y2 x y
104
+ _handleCubicBezierTo(data, false, firstPoint, lastPoint, lastControlPoint, result);
105
+ break;
106
+ case "c":
107
+ // CurveTo: C|c x1 y1 x2 y2 x y
108
+ _handleCubicBezierTo(data, true, firstPoint, lastPoint, lastControlPoint, result);
109
+ break;
110
+ case "S":
111
+ _handleShorthandCubicCurveTo(data, false, firstPoint, lastPoint, lastControlPoint, result);
112
+ break;
113
+ case "s":
114
+ _handleShorthandCubicCurveTo(data, true, firstPoint, lastPoint, lastControlPoint, result);
115
+ break;
116
+ case "Q":
117
+ // QuadraticCurveTo: Q|q x1 y1 x y
118
+ _handleQuadraticCurveTo(data, false, firstPoint, lastPoint, lastControlPoint, result);
119
+ break;
120
+ case "q":
121
+ // QuadraticCurveTo: Q|q x1 y1 x y
122
+ _handleQuadraticCurveTo(data, true, firstPoint, lastPoint, lastControlPoint, result);
123
+ break;
124
+ case "A":
125
+ // EllipticalArcTo: A|a rx ry x-axis-rotation large-arc-flag sweep-flag x y
126
+ _handleArcTo(data, false, firstPoint, lastPoint, lastControlPoint, result);
127
+ break;
128
+ case "a":
129
+ // EllipticalArcTo: A|a rx ry x-axis-rotation large-arc-flag sweep-flag x y
130
+ _handleArcTo(data, true, firstPoint, lastPoint, lastControlPoint, result);
131
+ break;
132
+ case "z":
133
+ _handleClosePath(data, false, firstPoint, lastPoint, lastControlPoint, result);
134
+ break;
135
+ case "Z":
136
+ // ClosePath: Z|z (no arguments)
137
+ _handleClosePath(data, true, firstPoint, lastPoint, lastControlPoint, result);
138
+ // i++;
139
+ break;
140
+ default:
141
+ if ((cmd as string) === "T") {
142
+ throw "T command only allowed after q command.";
143
+ }
144
+ if ((cmd as string) === "t") {
145
+ throw "t command only allowed after q command.";
146
+ }
147
+ throw "Unknown SVG path command found: " + cmd + ".";
148
+ }
149
+ // Safepoint: continue reading token by token until something is recognized again
150
+ i++;
151
+ } // END while
152
+
153
+ return result;
154
+ }; // END parseSVGPathData
155
+
156
+ // Just update the current position
157
+ const _handleMove = (
158
+ data: SVGPathMoveToCommand,
159
+ isRelative: boolean,
160
+ firstPoint: XYCoords,
161
+ lastPoint: XYCoords,
162
+ lastControlPoint: XYCoords,
163
+ _result: Array<Line | CubicBezierCurve>
164
+ ) => {
165
+ if (data.length < 3) {
166
+ throw "Unsufficient params for MOVE";
167
+ }
168
+ // result.push( new)
169
+ if (isRelative && !isNaN(lastPoint.x) && !isNaN(lastPoint.y)) {
170
+ lastPoint.x += Number(data[1]);
171
+ lastPoint.y += Number(data[2]);
172
+ lastControlPoint.x = lastPoint.x;
173
+ lastControlPoint.y = lastPoint.y;
174
+ } else {
175
+ lastPoint.x = Number(data[1]);
176
+ lastPoint.y = Number(data[2]);
177
+ lastControlPoint.x = lastPoint.x;
178
+ lastControlPoint.y = lastPoint.y;
179
+ }
180
+ // console.log("AFTER MOVE", lastPoint);
181
+ if (isNaN(firstPoint.y)) {
182
+ firstPoint.x = lastPoint.x;
183
+ firstPoint.y = lastPoint.y;
184
+ }
185
+ };
186
+
187
+ // Draw a line segment from the current position
188
+ const _handleLineTo = (
189
+ data: SVGPathLineToCommand,
190
+ isRelative: boolean,
191
+ firstPoint: XYCoords,
192
+ lastPoint: XYCoords,
193
+ lastControlPoint: XYCoords,
194
+ result: Array<Line | CubicBezierCurve>
195
+ ) => {
196
+ if (data.length < 3) {
197
+ throw "Unsufficient params for LINETO";
198
+ }
199
+
200
+ for (var i = 1; i + 1 < data.length; i += 2) {
201
+ const line = new Line(new Vertex(lastPoint), new Vertex(lastPoint));
202
+ if (isRelative) {
203
+ line.b.x += Number(data[i]);
204
+ line.b.y += Number(data[i + 1]);
205
+ } else {
206
+ line.b.x = Number(data[i]);
207
+ line.b.y = Number(data[i + 1]);
208
+ }
209
+ result.push(line);
210
+ lastPoint.x = line.b.x;
211
+ lastPoint.y = line.b.y;
212
+ lastControlPoint.x = line.b.x;
213
+ lastControlPoint.y = line.b.y;
214
+ if (isNaN(firstPoint.y)) {
215
+ firstPoint.x = line.a.x;
216
+ firstPoint.y = line.a.y;
217
+ }
218
+ console.log("LINETO LINE", i, line.toString());
219
+ }
220
+ };
221
+
222
+ const _handleHorizontalLineTo = (
223
+ data: SVGPathHorizontalLineToCommand,
224
+ isRelative: boolean,
225
+ firstPoint: XYCoords,
226
+ lastPoint: XYCoords,
227
+ lastControlPoint: XYCoords,
228
+ result: Array<Line | CubicBezierCurve>
229
+ ) => {
230
+ // console.log("Handle HORIZONTALLINETO", data);
231
+ if (data.length < 2) {
232
+ throw "Unsufficient params for HORIZONTALLINETO";
233
+ }
234
+
235
+ for (var i = 1; i < data.length; i++) {
236
+ const line = new Line(new Vertex(lastPoint), new Vertex(lastPoint));
237
+ if (isRelative) {
238
+ line.b.x += Number(data[i]);
239
+ // line.b.y += Number(data[2]);
240
+ } else {
241
+ line.b.x = Number(data[i]);
242
+ // line.b.y = Number(data[2]);
243
+ }
244
+ result.push(line);
245
+ lastPoint.x = line.b.x;
246
+ // lastPoint.y = line.b.y;
247
+ lastControlPoint.x = line.b.x;
248
+ if (isNaN(firstPoint.y)) {
249
+ firstPoint.x = line.a.x;
250
+ firstPoint.y = line.a.y;
251
+ }
252
+ }
253
+ };
254
+
255
+ const _handleVerticalLineTo = (
256
+ data: SVGPathVerticalLineToCommand,
257
+ isRelative: boolean,
258
+ firstPoint: XYCoords,
259
+ lastPoint: XYCoords,
260
+ lastControlPoint: XYCoords,
261
+ result: Array<Line | CubicBezierCurve>
262
+ ) => {
263
+ if (data.length < 2) {
264
+ throw "Unsufficient params for VERTICALLINETO";
265
+ }
266
+
267
+ for (var i = 1; i < data.length; i++) {
268
+ const line = new Line(new Vertex(lastPoint), new Vertex(lastPoint));
269
+ if (isRelative) {
270
+ line.b.y += Number(data[i]);
271
+ } else {
272
+ line.b.y = Number(data[i]);
273
+ }
274
+ result.push(line);
275
+ // lastPoint.x = line.b.x;
276
+ lastPoint.y = line.b.y;
277
+ lastControlPoint.y = line.b.y;
278
+ if (isNaN(firstPoint.y)) {
279
+ firstPoint.x = line.a.x;
280
+ firstPoint.y = line.a.y;
281
+ }
282
+ }
283
+ };
284
+
285
+ // CurveTo: C|c x1 y1 x2 y2 x y
286
+ const _handleCubicBezierTo = (
287
+ data: SVGPathCubicCurveToCommand,
288
+ isRelative: boolean,
289
+ firstPoint: XYCoords,
290
+ lastPoint: XYCoords,
291
+ lastControlPoint: XYCoords,
292
+ result: Array<Line | CubicBezierCurve>
293
+ ) => {
294
+ // console.log("Handle CUBICBEZIERTO", data);
295
+ if (data.length < 7) {
296
+ throw "Unsufficient params for CUBICBEZIERTO";
297
+ }
298
+
299
+ for (var i = 1; i + 5 < data.length; i += 6) {
300
+ const curve = new CubicBezierCurve(
301
+ new Vertex(lastPoint),
302
+ new Vertex(lastPoint),
303
+ new Vertex(lastPoint),
304
+ new Vertex(lastPoint)
305
+ );
306
+ if (isRelative) {
307
+ curve.startControlPoint.x += Number(data[i]);
308
+ curve.startControlPoint.y += Number(data[i + 1]);
309
+ curve.endControlPoint.x += Number(data[i + 2]);
310
+ curve.endControlPoint.y += Number(data[i + 3]);
311
+ curve.endPoint.x += Number(data[i + 4]);
312
+ curve.endPoint.y += Number(data[i + 5]);
313
+ } else {
314
+ curve.startControlPoint.x = Number(data[i]);
315
+ curve.startControlPoint.y = Number(data[i + 1]);
316
+ curve.endControlPoint.x = Number(data[i + 2]);
317
+ curve.endControlPoint.y = Number(data[i + 3]);
318
+ curve.endPoint.x = Number(data[i + 4]);
319
+ curve.endPoint.y = Number(data[i + 5]);
320
+ }
321
+ result.push(curve);
322
+ lastPoint.x = curve.endPoint.x;
323
+ lastPoint.y = curve.endPoint.y;
324
+ lastControlPoint.x = curve.endControlPoint.x;
325
+ lastControlPoint.y = curve.endControlPoint.y;
326
+ if (isNaN(firstPoint.x)) {
327
+ firstPoint.x = curve.startPoint.x;
328
+ firstPoint.y = curve.startPoint.y;
329
+ }
330
+ }
331
+ };
332
+
333
+ // QuadraticCurveTo: Q|q x1 y1 x y
334
+ const _handleQuadraticCurveTo = (
335
+ data: SVGPathQuadraticCurveToCommand,
336
+ isRelative: boolean,
337
+ firstPoint: XYCoords,
338
+ lastPoint: XYCoords,
339
+ lastControlPoint: XYCoords,
340
+ result: Array<Line | CubicBezierCurve>
341
+ ) => {
342
+ // console.log("Handle QUADRATICBEZIERTO", data);
343
+ if (data.length < 5) {
344
+ throw "Unsufficient params for QUADRATICBEZIERTO";
345
+ }
346
+
347
+ var i = 1;
348
+ var localLastQuadraticControlPoint = { x: 0, y: 0 };
349
+ // This loops runs at least once
350
+ while (i + 3 < data.length && data[i] !== "t" && data[i] !== "T") {
351
+ const curve = new CubicBezierCurve(
352
+ new Vertex(lastPoint),
353
+ new Vertex(lastPoint),
354
+ new Vertex(lastPoint),
355
+ new Vertex(lastPoint)
356
+ );
357
+ if (isRelative) {
358
+ curve.startControlPoint.x += curve.startPoint.x + (Number(data[i]) - curve.startControlPoint.x); // * 0.666;
359
+ curve.startControlPoint.y += curve.startPoint.y + (Number(data[i + 1]) - curve.startControlPoint.y); // * 0.666;
360
+ curve.endPoint.x += Number(data[i + 2]);
361
+ curve.endPoint.y += Number(data[i + 3]);
362
+ curve.endControlPoint.x = curve.startControlPoint.x;
363
+ curve.endControlPoint.y = curve.startControlPoint.y;
364
+ } else {
365
+ curve.startControlPoint.x = curve.startPoint.x + (Number(data[i]) - curve.startControlPoint.x); // * 0.666;
366
+ curve.startControlPoint.y = curve.startPoint.y + (Number(data[i + 1]) - curve.startControlPoint.y); // * 0.666;
367
+ curve.endPoint.x = Number(data[i + 2]);
368
+ curve.endPoint.y = Number(data[i + 3]);
369
+ curve.endControlPoint.x = curve.startControlPoint.x;
370
+ curve.endControlPoint.y = curve.startControlPoint.y;
371
+ }
372
+ // var lastQuadraticControlPoint = { x: Number(data[1]), y: Number(data[2]) };
373
+ localLastQuadraticControlPoint = { x: curve.endControlPoint.x, y: curve.endControlPoint.y };
374
+ // Convert quadratic curve to cubic curve
375
+ curve.startControlPoint.x = curve.startPoint.x + (curve.startControlPoint.x - curve.startPoint.x) * 0.666;
376
+ curve.startControlPoint.y = curve.startPoint.y + (curve.startControlPoint.y - curve.startPoint.y) * 0.666;
377
+ curve.endControlPoint.x = curve.endPoint.x + (curve.endControlPoint.x - curve.endPoint.x) * 0.666;
378
+ curve.endControlPoint.y = curve.endPoint.y + (curve.endControlPoint.y - curve.endPoint.y) * 0.666;
379
+
380
+ result.push(curve);
381
+ lastPoint.x = curve.endPoint.x;
382
+ lastPoint.y = curve.endPoint.y;
383
+ lastControlPoint.x = curve.endControlPoint.x;
384
+ lastControlPoint.y = curve.endControlPoint.y;
385
+ if (isNaN(firstPoint.x)) {
386
+ firstPoint.x = curve.startPoint.x;
387
+ firstPoint.y = curve.startPoint.y;
388
+ }
389
+ i += 4;
390
+ } // END while
391
+
392
+ // 'T' or 't' command may follow
393
+ // if (data.length >= 8) {
394
+ if (i < data.length && (data[i] == "t" || data[i] === "T")) {
395
+ // TODO: think about this type cast!
396
+ const subData: SVGPathShorthandQuadraticCurveToCommand = data.slice(5) as SVGPathShorthandQuadraticCurveToCommand;
397
+ // var lastQuadraticControlPoint = { x: Number(data[1]), y: Number(data[2]) };
398
+ if (subData[0] === "T") {
399
+ // _handleShorthandQuadraticCurveTo(subData, false, firstPoint, lastPoint, lastControlPoint, lastQuadraticControlPoint, result);
400
+ _handleShorthandQuadraticCurveTo(
401
+ subData,
402
+ false,
403
+ firstPoint,
404
+ lastPoint,
405
+ lastControlPoint,
406
+ localLastQuadraticControlPoint,
407
+ result
408
+ );
409
+ } else if (subData[0] === "t") {
410
+ // _handleShorthandQuadraticCurveTo(subData, true, firstPoint, lastPoint, lastControlPoint, lastQuadraticControlPoint, result);
411
+ _handleShorthandQuadraticCurveTo(
412
+ subData,
413
+ true,
414
+ firstPoint,
415
+ lastPoint,
416
+ lastControlPoint,
417
+ localLastQuadraticControlPoint,
418
+ result
419
+ );
420
+ }
421
+ }
422
+ };
423
+
424
+ // This is a helper function and works only in combination with Quadratic Bézier Curves
425
+ // T|t (x y)+
426
+ const _handleShorthandQuadraticCurveTo = (
427
+ data: SVGPathShorthandQuadraticCurveToCommand,
428
+ isRelative: boolean,
429
+ firstPoint: XYCoords,
430
+ lastPoint: XYCoords,
431
+ lastControlPoint: XYCoords,
432
+ lastQuadraticControlPoint: XYCoords,
433
+ result: Array<Line | CubicBezierCurve>
434
+ ) => {
435
+ if (data.length < 3) {
436
+ throw "Unsufficient params for SHORTHANDQUADRATICCURVETO";
437
+ }
438
+ var i = 1;
439
+ while (i + 1 < data.length) {
440
+ // Respect multiple 'T|t' commands here
441
+ if (data[i] === "T" || data[i] === "t") {
442
+ i++;
443
+ continue;
444
+ }
445
+ const curve = new CubicBezierCurve(
446
+ new Vertex(lastPoint),
447
+ new Vertex(lastPoint),
448
+ new Vertex(lastPoint),
449
+ new Vertex(lastPoint)
450
+ );
451
+ if (isRelative) {
452
+ curve.endPoint.x += Number(data[i]);
453
+ curve.endPoint.y += Number(data[i + 1]);
454
+ curve.startControlPoint.x += curve.startPoint.x - (lastQuadraticControlPoint.x - lastPoint.x);
455
+ curve.startControlPoint.y += curve.startPoint.y - (lastQuadraticControlPoint.y - lastPoint.y);
456
+ } else {
457
+ curve.endPoint.x = Number(data[i]);
458
+ curve.endPoint.y = Number(data[i + 1]);
459
+ curve.startControlPoint.x = curve.startPoint.x - (lastQuadraticControlPoint.x - lastPoint.x);
460
+ curve.startControlPoint.y = curve.startPoint.y - (lastQuadraticControlPoint.y - lastPoint.y);
461
+ }
462
+ // First handle as symmetrical cubic curve
463
+ curve.endControlPoint.x = curve.startControlPoint.x;
464
+ curve.endControlPoint.y = curve.startControlPoint.y;
465
+ lastQuadraticControlPoint.y = curve.endControlPoint.y;
466
+ lastQuadraticControlPoint.x = curve.endControlPoint.x;
467
+
468
+ // Convert quadratic curve to cubic curve
469
+ var scaleFactor = 0.666; // i === 1 ? 0.666 : 1.0;
470
+ curve.startControlPoint.x = curve.startPoint.x + (curve.startControlPoint.x - curve.startPoint.x) * scaleFactor;
471
+ curve.startControlPoint.y = curve.startPoint.y + (curve.startControlPoint.y - curve.startPoint.y) * scaleFactor;
472
+ curve.endControlPoint.x = curve.endPoint.x + (curve.endControlPoint.x - curve.endPoint.x) * scaleFactor;
473
+ curve.endControlPoint.y = curve.endPoint.y + (curve.endControlPoint.y - curve.endPoint.y) * scaleFactor;
474
+
475
+ // console.log("ADDING T CURVE", curve);
476
+ result.push(curve);
477
+ lastPoint.x = curve.endPoint.x;
478
+ lastPoint.y = curve.endPoint.y;
479
+ lastControlPoint.x = curve.endControlPoint.x;
480
+ lastControlPoint.y = curve.endControlPoint.y;
481
+ if (isNaN(firstPoint.x)) {
482
+ firstPoint.x = curve.startPoint.x;
483
+ firstPoint.y = curve.startPoint.y;
484
+ }
485
+
486
+ i += 2;
487
+ }
488
+ };
489
+
490
+ // The S|s x2 y2 x y
491
+ const _handleShorthandCubicCurveTo = (
492
+ data: SVGPathShorthandCubicCurveToCommand,
493
+ isRelative: boolean,
494
+ firstPoint: XYCoords,
495
+ lastPoint: XYCoords,
496
+ lastControlPoint: XYCoords,
497
+ result: Array<Line | CubicBezierCurve>
498
+ ) => {
499
+ // console.log("Handle SHORTHANDCUBICBEZIERTO", data);
500
+ if (data.length < 5) {
501
+ throw "Unsufficient params for SHORTHANDCUBICBEZIERTO";
502
+ }
503
+ const curve = new CubicBezierCurve(new Vertex(lastPoint), new Vertex(lastPoint), new Vertex(lastPoint), new Vertex(lastPoint));
504
+
505
+ if (isRelative) {
506
+ curve.startControlPoint.x = lastPoint.x - (lastControlPoint.x - lastPoint.x);
507
+ curve.startControlPoint.y = lastPoint.y - (lastControlPoint.y - lastPoint.y);
508
+ curve.endControlPoint.x += Number(data[1]);
509
+ curve.endControlPoint.y += Number(data[2]);
510
+ curve.endPoint.x += Number(data[3]);
511
+ curve.endPoint.y += Number(data[4]);
512
+ } else {
513
+ curve.startControlPoint.x = lastPoint.x - (lastControlPoint.x - lastPoint.x);
514
+ curve.startControlPoint.y = lastPoint.y - (lastControlPoint.y - lastPoint.y);
515
+ curve.endControlPoint.x = Number(data[1]);
516
+ curve.endControlPoint.y = Number(data[2]);
517
+ curve.endPoint.x = Number(data[3]);
518
+ curve.endPoint.y = Number(data[4]);
519
+ }
520
+
521
+ result.push(curve);
522
+
523
+ lastPoint.x = curve.endPoint.x;
524
+ lastPoint.y = curve.endPoint.y;
525
+ lastControlPoint.x = curve.endControlPoint.x;
526
+ lastControlPoint.y = curve.endControlPoint.y;
527
+ if (isNaN(firstPoint.x)) {
528
+ firstPoint.x = curve.startPoint.x;
529
+ firstPoint.y = curve.startPoint.y;
530
+ }
531
+ };
532
+
533
+ // EllipticalArcTo: A|a rx ry x-axis-rotation large-arc-flag sweep-flag x y
534
+ const _handleArcTo = (
535
+ data: SVGPathArcToCommand,
536
+ isRelative: boolean,
537
+ firstPoint: XYCoords,
538
+ lastPoint: XYCoords,
539
+ lastControlPoint: XYCoords,
540
+ result: Array<Line | CubicBezierCurve>
541
+ ) => {
542
+ if (data.length < 8) {
543
+ throw "Unsufficient params for ARCTO";
544
+ }
545
+
546
+ for (var i = 1; i + 6 < data.length; i += 7) {
547
+ const arcEndPoint = { x: Number(data[i + 5]), y: Number(data[i + 6]) };
548
+ if (isRelative) {
549
+ arcEndPoint.x += lastPoint.x;
550
+ arcEndPoint.y += lastPoint.y;
551
+ }
552
+ // console.log(
553
+ // "ARC",
554
+ // "data.length",
555
+ // data.length,
556
+ // "i",
557
+ // i,
558
+ // "lastPoint.x",
559
+ // lastPoint.x, // x1
560
+ // "lastPoint.y",
561
+ // lastPoint.y, // y1
562
+ // "data[i]",
563
+ // Number(data[i]), // rx
564
+ // "data[i+1]",
565
+ // Number(data[i + 1]), // ry
566
+ // "data[i+2]",
567
+ // Number(data[i + 2]), // phi: number,
568
+ // "data[i+3]",
569
+ // Boolean(data[i + 3]), // fa: boolean,
570
+ // "data[i+4]",
571
+ // Boolean(data[i + 4]), // fs: boolean,
572
+ // "data[i+5]",
573
+ // arcEndPoint.x, // Number(data[6]), // x2: number,
574
+ // "data[i+6]",
575
+ // arcEndPoint.y // Number(data[7]) // y2: number
576
+ // );
577
+ // A 5 4 0 1 1 -10 -5
578
+ // TODO: respect relative/absolute here
579
+ const ellipseSector = VEllipseSector.ellipseSectorUtils.endpointToCenterParameters(
580
+ lastPoint.x,
581
+ lastPoint.y,
582
+ Number(data[i]),
583
+ Number(data[i + 1]),
584
+ Number(data[i + 2]) * DEG_TO_RAD,
585
+ Boolean(data[i + 3]),
586
+ Boolean(data[i + 4]),
587
+ arcEndPoint.x,
588
+ arcEndPoint.y // Number(data[7]) // y2: number
589
+ );
590
+
591
+ // console.log("ellipseSector", ellipseSector);
592
+ const curves = ellipseSector.toCubicBezier(4); // 4 segments already seems to be a good approximation
593
+ for (var j = 0; j < curves.length; j++) {
594
+ result.push(curves[j]); // Destruct!
595
+ }
596
+ // result.push(ellipseSector.ellipse);
597
+ if (curves.length > 0) {
598
+ // console.log("curves", curves);
599
+ const lastCurve = curves[curves.length - 1];
600
+ // lastPoint.x = lastCurve.endPoint.x;
601
+ // lastPoint.y = lastCurve.endPoint.y;
602
+ lastControlPoint.x = lastCurve.endControlPoint.x;
603
+ lastControlPoint.y = lastCurve.endControlPoint.y;
604
+ if (isNaN(firstPoint.x)) {
605
+ firstPoint.x = curves[0].startPoint.x;
606
+ firstPoint.y = curves[0].startPoint.y;
607
+ }
608
+ }
609
+ lastPoint.x = arcEndPoint.x;
610
+ lastPoint.y = arcEndPoint.y;
611
+ } // END for
612
+ // TODO: track first/last/control point
613
+ };
614
+
615
+ const _handleClosePath = (
616
+ _data: SVGPathCloseCommand,
617
+ _isRelative: boolean,
618
+ firstPoint: XYCoords,
619
+ lastPoint: XYCoords,
620
+ lastControlPoint: XYCoords,
621
+ result: Array<Line | CubicBezierCurve>
622
+ ) => {
623
+ // console.log("Handle CLOSEPATH", "lastPoint", lastPoint, "firstPoint", firstPoint);
624
+ const line = new Line(new Vertex(lastPoint), new Vertex(firstPoint));
625
+ result.push(line);
626
+
627
+ lastPoint.x = line.b.x;
628
+ lastPoint.y = line.b.y;
629
+ lastControlPoint.x = line.b.x;
630
+ lastControlPoint.y = line.b.y;
631
+ };
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Split SVG path data `d` strings.
3
+ *
4
+ * As this helper function does not detect all possible path data strings it should cover the most daily use-cases.
5
+ *
6
+ * For more insight see
7
+ * https://www.w3.org/TR/SVG/paths.html
8
+ *
9
+ * @author Ikaros Kappler
10
+ * @date 2022-11-22
11
+ * @modified 2022-12-21 Ported to Typescript
12
+ * @version 0.0.1-alpha
13
+ */
14
+
15
+ import { SVGPathCommand } from "./types";
16
+
17
+ /**
18
+ * @name splitSVGPathData
19
+ * @static
20
+ * @param {string} dataString - The SVG path data to split. This should be some string
21
+ **/
22
+ export const splitSVGPathData = (dataString: string): Array<SVGPathCommand> | null => {
23
+ // I used a modified version of this Josh Frank's SVG path data RexEx.
24
+ // Source
25
+ // https://javascript.plainenglish.io/june-3-parsing-and-validating-svg-paths-with-regex-7bd0e245115
26
+ // const validCommand =
27
+ // /([ml](\s?-?((\d+(\.\d+)?)|(\.\d+)))[,\s]?(-?((\d+(\.\d+)?)|(\.\d+))))|([hv](\s?-?((\d+(\.\d+)?)|(\.\d+))))|(c(\s?-?((\d+(\.\d+)?)|(\.\d+)))([,\s]?(-?((\d+(\.\d+)?)|(\.\d+)))){5})|(q(\s?-?((\d+(\.\d+)?)|(\.\d+)))([,\s]?(-?((\d+(\.\d+)?)|(\.\d+)))){3}(\s?t?(\s?-?((\d+(\.\d+)?)|(\.\d+)))[,\s]?(-?((\d+(\.\d+)?)|(\.\d+))))*)|(a(\s?-?((\d+(\.\d+)?)|(\.\d+)))([,\s]?(-?((\d+(\.\d+)?)|(\.\d+)))){2}[,\s]?[01][,\s]+[01][,\s]+([,\s]?(-?((\d+(\.\d+)?)|(\.\d+)))){2})|(s(\s?-?((\d+(\.\d+)?)|(\.\d+)))([,\s]?(-?((\d+(\.\d+)?)|(\.\d+)))){3})|z/gi;
28
+ //
29
+ // My Version allows multiple param sets, too, like 'm|M (x y)+' or 'q|Q (x1 y1 x y)+'. Look at the plus.
30
+ const validCommand =
31
+ /([ml]((\s?-?((\d+(\.\d+)?)|(\.\d+)))[,\s]?(-?((\d+(\.\d+)?)|(\.\d+))))+)|([hv]((\s?-?((\d+(\.\d+)?)|(\.\d+))))+)|(c((\s?-?((\d+(\.\d+)?)|(\.\d+)))([,\s]?(-?((\d+(\.\d+)?)|(\.\d+)))){5})+)|(q((\s?-?((\d+(\.\d+)?)|(\.\d+)))([,\s]?(-?((\d+(\.\d+)?)|(\.\d+)))){3})+(\s?(t?(\s?-?((\d+(\.\d+)?)|(\.\d+)))[,\s]?(-?((\d+(\.\d+)?)|(\.\d+))))*)+)|(a((\s?-?((\d+(\.\d+)?)|(\.\d+)))([,\s]?(-?((\d+(\.\d+)?)|(\.\d+)))){2}[,\s]?[01][,\s]+[01][,\s]+([,\s]?(-?((\d+(\.\d+)?)|(\.\d+)))){2})+)|(s(\s?-?((\d+(\.\d+)?)|(\.\d+)))([,\s]?(-?((\d+(\.\d+)?)|(\.\d+)))){3})|z/gi;
32
+
33
+ const dataElements = dataString.match(validCommand);
34
+ if (!dataElements) {
35
+ return null;
36
+ }
37
+
38
+ const result: Array<SVGPathCommand> = [];
39
+
40
+ var i = 0;
41
+ while (i < dataElements.length) {
42
+ const token = dataElements[i];
43
+ // Ctwheels comment was really useful here
44
+ // https://stackoverflow.com/questions/47801455/what-is-the-regex-that-properly-splits-svg-d-attributes-into-tokens
45
+ const dataRaw = token.match(/-?(?:\d*\.)?\d+|[a-z]/gi);
46
+ if (dataRaw) {
47
+ result.push(dataRaw as SVGPathCommand);
48
+ } else {
49
+ // throw Error?
50
+ }
51
+ i++;
52
+ }
53
+ return result;
54
+ }; // END splitSVGPathData