svg-path-commander 1.0.4 → 2.0.0-alpha1

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 (132) hide show
  1. package/README.md +29 -7
  2. package/dist/svg-path-commander.cjs +2 -0
  3. package/dist/svg-path-commander.cjs.map +1 -0
  4. package/dist/svg-path-commander.d.ts +454 -0
  5. package/dist/svg-path-commander.js +2 -3775
  6. package/dist/svg-path-commander.js.map +1 -0
  7. package/dist/svg-path-commander.mjs +1194 -0
  8. package/dist/svg-path-commander.mjs.map +1 -0
  9. package/package.json +39 -42
  10. package/src/convert/pathToAbsolute.ts +101 -0
  11. package/src/convert/{pathToCurve.js → pathToCurve.ts} +13 -15
  12. package/src/convert/pathToRelative.ts +92 -0
  13. package/src/convert/pathToString.ts +17 -0
  14. package/src/{svg-path-commander.js → index.ts} +143 -67
  15. package/src/interface.ts +129 -0
  16. package/src/math/distanceSquareRoot.ts +13 -0
  17. package/src/math/midPoint.ts +16 -0
  18. package/src/math/{polygonArea.js → polygonArea.ts} +6 -4
  19. package/src/math/{polygonLength.js → polygonLength.ts} +5 -3
  20. package/src/math/rotateVector.ts +16 -0
  21. package/src/options/options.ts +9 -0
  22. package/src/parser/error.ts +2 -0
  23. package/src/parser/finalizeSegment.ts +31 -0
  24. package/src/parser/{invalidPathValue.js → invalidPathValue.ts} +0 -0
  25. package/src/parser/{isArcCommand.js → isArcCommand.ts} +5 -4
  26. package/src/parser/isDigit.ts +12 -0
  27. package/src/parser/isDigitStart.ts +14 -0
  28. package/src/parser/isPathCommand.ts +28 -0
  29. package/src/parser/isSpace.ts +23 -0
  30. package/src/parser/paramsCount.ts +16 -0
  31. package/src/parser/paramsParser.ts +14 -0
  32. package/src/parser/{parsePathString.js → parsePathString.ts} +13 -7
  33. package/src/parser/pathParser.ts +29 -0
  34. package/src/parser/{scanFlag.js → scanFlag.ts} +8 -5
  35. package/src/parser/{scanParam.js → scanParam.ts} +12 -11
  36. package/src/parser/{scanSegment.js → scanSegment.ts} +9 -5
  37. package/src/parser/{skipSpaces.js → skipSpaces.ts} +5 -3
  38. package/src/process/{arcToCubic.js → arcToCubic.ts} +42 -28
  39. package/src/process/fixArc.ts +23 -0
  40. package/src/process/getSVGMatrix.ts +70 -0
  41. package/src/process/lineToCubic.ts +17 -0
  42. package/src/process/{normalizePath.js → normalizePath.ts} +10 -10
  43. package/src/process/normalizeSegment.ts +47 -0
  44. package/src/process/{optimizePath.js → optimizePath.ts} +18 -21
  45. package/src/process/{projection2d.js → projection2d.ts} +16 -15
  46. package/src/process/quadToCubic.ts +31 -0
  47. package/src/process/reverseCurve.ts +21 -0
  48. package/src/process/reversePath.ts +101 -0
  49. package/src/process/roundPath.ts +29 -0
  50. package/src/process/segmentToCubic.ts +46 -0
  51. package/src/process/shortenSegment.ts +79 -0
  52. package/src/process/splitCubic.ts +28 -0
  53. package/src/process/{splitPath.js → splitPath.ts} +9 -8
  54. package/src/process/{transformPath.js → transformPath.ts} +55 -48
  55. package/src/types.ts +193 -0
  56. package/src/util/getClosestPoint.ts +15 -0
  57. package/src/util/{getDrawDirection.js → getDrawDirection.ts} +7 -4
  58. package/src/util/getPathArea.ts +70 -0
  59. package/src/util/{getPathBBox.js → getPathBBox.ts} +16 -5
  60. package/src/util/getPointAtLength.ts +14 -0
  61. package/src/util/{getPropertiesAtLength.js → getPropertiesAtLength.ts} +28 -19
  62. package/src/util/{getPropertiesAtPoint.js → getPropertiesAtPoint.ts} +16 -12
  63. package/src/util/getSegmentAtLength.ts +15 -0
  64. package/src/util/getSegmentOfPoint.ts +18 -0
  65. package/src/util/{getTotalLength.js → getTotalLength.ts} +6 -4
  66. package/src/util/isAbsoluteArray.ts +18 -0
  67. package/src/util/isCurveArray.ts +15 -0
  68. package/src/util/{isNormalizedArray.js → isNormalizedArray.ts} +5 -3
  69. package/src/util/isPathArray.ts +19 -0
  70. package/src/util/isPointInStroke.ts +15 -0
  71. package/src/util/isRelativeArray.ts +18 -0
  72. package/src/util/{isValidPath.js → isValidPath.ts} +5 -4
  73. package/src/util/{pathLengthFactory.js → pathLengthFactory.ts} +38 -31
  74. package/src/util/{segmentArcFactory.js → segmentArcFactory.ts} +72 -56
  75. package/src/util/segmentCubicFactory.ts +114 -0
  76. package/src/util/{segmentLineFactory.js → segmentLineFactory.ts} +11 -9
  77. package/src/util/{segmentQuadFactory.js → segmentQuadFactory.ts} +48 -33
  78. package/src/util/shapeToPath.ts +214 -0
  79. package/dist/svg-path-commander.es5.js +0 -3888
  80. package/dist/svg-path-commander.es5.min.js +0 -2
  81. package/dist/svg-path-commander.esm.js +0 -3767
  82. package/dist/svg-path-commander.esm.min.js +0 -2
  83. package/dist/svg-path-commander.min.js +0 -2
  84. package/src/convert/pathToAbsolute.js +0 -93
  85. package/src/convert/pathToRelative.js +0 -84
  86. package/src/convert/pathToString.js +0 -14
  87. package/src/index.js +0 -10
  88. package/src/math/distanceSquareRoot.js +0 -14
  89. package/src/math/epsilon.js +0 -8
  90. package/src/math/midPoint.js +0 -13
  91. package/src/math/rotateVector.js +0 -14
  92. package/src/options/options.js +0 -10
  93. package/src/parser/error.js +0 -2
  94. package/src/parser/finalizeSegment.js +0 -28
  95. package/src/parser/isDigit.js +0 -9
  96. package/src/parser/isDigitStart.js +0 -13
  97. package/src/parser/isPathCommand.js +0 -25
  98. package/src/parser/isSpace.js +0 -16
  99. package/src/parser/paramsCount.js +0 -9
  100. package/src/parser/paramsParser.js +0 -8
  101. package/src/parser/pathParser.js +0 -25
  102. package/src/process/clonePath.js +0 -9
  103. package/src/process/fixArc.js +0 -22
  104. package/src/process/fixPath.js +0 -31
  105. package/src/process/getSVGMatrix.js +0 -61
  106. package/src/process/lineToCubic.js +0 -33
  107. package/src/process/normalizeSegment.js +0 -45
  108. package/src/process/quadToCubic.js +0 -22
  109. package/src/process/reverseCurve.js +0 -18
  110. package/src/process/reversePath.js +0 -89
  111. package/src/process/roundPath.js +0 -26
  112. package/src/process/segmentToCubic.js +0 -46
  113. package/src/process/shortenSegment.js +0 -58
  114. package/src/process/splitCubic.js +0 -26
  115. package/src/util/getClosestPoint.js +0 -12
  116. package/src/util/getPathArea.js +0 -47
  117. package/src/util/getPointAtLength.js +0 -12
  118. package/src/util/getSegmentAtLength.js +0 -11
  119. package/src/util/getSegmentOfPoint.js +0 -12
  120. package/src/util/isAbsoluteArray.js +0 -14
  121. package/src/util/isCurveArray.js +0 -13
  122. package/src/util/isPathArray.js +0 -14
  123. package/src/util/isPointInStroke.js +0 -13
  124. package/src/util/isRelativeArray.js +0 -14
  125. package/src/util/segmentCubicFactory.js +0 -97
  126. package/src/util/shapeToPath.js +0 -204
  127. package/src/util/util.js +0 -82
  128. package/src/version.js +0 -8
  129. package/types/index.d.ts +0 -120
  130. package/types/more/modules.ts +0 -82
  131. package/types/more/svg.d.ts +0 -211
  132. package/types/svg-path-commander.d.ts +0 -1089
@@ -0,0 +1,454 @@
1
+ import CSSMatrix from '@thednp/dommatrix';
2
+
3
+ export interface SegmentProperties {
4
+ segment: PathSegment;
5
+ index: number;
6
+ length: number;
7
+ lengthAtSegment: number;
8
+ [key: string]: any;
9
+ }
10
+ export interface PointProperties {
11
+ closest: {
12
+ x: number;
13
+ y: number;
14
+ };
15
+ distance: number;
16
+ segment?: SegmentProperties;
17
+ }
18
+ export interface LineAttr {
19
+ type: "line";
20
+ x1: number;
21
+ y1: number;
22
+ x2: number;
23
+ y2: number;
24
+ [key: string]: string | number;
25
+ }
26
+ export interface PolyAttr {
27
+ type: "polygon" | "polyline";
28
+ points: string;
29
+ [key: string]: string | number;
30
+ }
31
+ export interface CircleAttr {
32
+ type: "circle";
33
+ cx: number;
34
+ cy: number;
35
+ r: number;
36
+ [key: string]: string | number;
37
+ }
38
+ export interface EllipseAttr {
39
+ type: "ellipse";
40
+ cx: number;
41
+ cy: number;
42
+ rx: number;
43
+ ry: number;
44
+ [key: string]: string | number;
45
+ }
46
+ export interface RectAttr {
47
+ type: "rect";
48
+ width: number;
49
+ height: number;
50
+ x: number;
51
+ y: number;
52
+ rx: number;
53
+ ry: number;
54
+ [key: string]: string | number;
55
+ }
56
+ export interface GlyphAttr {
57
+ type: "glyph";
58
+ d: string;
59
+ [key: string]: string | number;
60
+ }
61
+ export interface PathBBox {
62
+ width: number;
63
+ height: number;
64
+ x: number;
65
+ y: number;
66
+ x2: number;
67
+ y2: number;
68
+ cx: number;
69
+ cy: number;
70
+ cz: number;
71
+ }
72
+ export interface LengthFactory {
73
+ length: number;
74
+ point: {
75
+ x: number;
76
+ y: number;
77
+ };
78
+ min: {
79
+ x: number;
80
+ y: number;
81
+ };
82
+ max: {
83
+ x: number;
84
+ y: number;
85
+ };
86
+ }
87
+ export interface Options {
88
+ round: "auto" | "off" | number;
89
+ origin: number[];
90
+ }
91
+ export interface TransformObject {
92
+ translate: number | number[];
93
+ rotate: number | number[];
94
+ scale: number | number[];
95
+ skew: number | number[];
96
+ origin: number[];
97
+ }
98
+ export type MCommand = "M";
99
+ export type mCommand = "m";
100
+ export type LCommand = "L";
101
+ export type lCommand = "l";
102
+ export type VCommand = "V";
103
+ export type vCommand = "v";
104
+ export type HCommand = "H";
105
+ export type hCommand = "h";
106
+ export type ZCommand = "Z";
107
+ export type zCommand = "z";
108
+ export type CCommand = "C";
109
+ export type cCommand = "c";
110
+ export type SCommand = "S";
111
+ export type sCommand = "s";
112
+ export type QCommand = "Q";
113
+ export type qCommand = "q";
114
+ export type TCommand = "T";
115
+ export type tCommand = "t";
116
+ export type ACommand = "A";
117
+ export type aCommand = "a";
118
+ export type MSegment = [
119
+ MCommand,
120
+ number,
121
+ number
122
+ ];
123
+ export type mSegment = [
124
+ mCommand,
125
+ number,
126
+ number
127
+ ];
128
+ export type MoveSegment = MSegment | mSegment;
129
+ export type LSegment = [
130
+ LCommand,
131
+ number,
132
+ number
133
+ ];
134
+ export type lSegment = [
135
+ lCommand,
136
+ number,
137
+ number
138
+ ];
139
+ export type LineSegment = LSegment | lSegment;
140
+ export type VSegment = [
141
+ VCommand,
142
+ number
143
+ ];
144
+ export type vSegment = [
145
+ vCommand,
146
+ number
147
+ ];
148
+ export type VertLineSegment = vSegment | VSegment;
149
+ export type HSegment = [
150
+ HCommand,
151
+ number
152
+ ];
153
+ export type hSegment = [
154
+ hCommand,
155
+ number
156
+ ];
157
+ export type HorLineSegment = HSegment | hSegment;
158
+ export type ZSegment = [
159
+ ZCommand
160
+ ];
161
+ export type zSegment = [
162
+ zCommand
163
+ ];
164
+ export type CloseSegment = ZSegment | zSegment;
165
+ export type CSegment = [
166
+ CCommand,
167
+ number,
168
+ number,
169
+ number,
170
+ number,
171
+ number,
172
+ number
173
+ ];
174
+ export type cSegment = [
175
+ cCommand,
176
+ number,
177
+ number,
178
+ number,
179
+ number,
180
+ number,
181
+ number
182
+ ];
183
+ export type CubicSegment = CSegment | cSegment;
184
+ export type SSegment = [
185
+ SCommand,
186
+ number,
187
+ number,
188
+ number,
189
+ number
190
+ ];
191
+ export type sSegment = [
192
+ sCommand,
193
+ number,
194
+ number,
195
+ number,
196
+ number
197
+ ];
198
+ export type ShortCubicSegment = SSegment | sSegment;
199
+ export type QSegment = [
200
+ QCommand,
201
+ number,
202
+ number,
203
+ number,
204
+ number
205
+ ];
206
+ export type qSegment = [
207
+ qCommand,
208
+ number,
209
+ number,
210
+ number,
211
+ number
212
+ ];
213
+ export type QuadSegment = QSegment | qSegment;
214
+ export type TSegment = [
215
+ TCommand,
216
+ number,
217
+ number
218
+ ];
219
+ export type tSegment = [
220
+ tCommand,
221
+ number,
222
+ number
223
+ ];
224
+ export type ShortQuadSegment = TSegment | tSegment;
225
+ export type ASegment = [
226
+ ACommand,
227
+ number,
228
+ number,
229
+ number,
230
+ number,
231
+ number,
232
+ number,
233
+ number
234
+ ];
235
+ export type aSegment = [
236
+ aCommand,
237
+ number,
238
+ number,
239
+ number,
240
+ number,
241
+ number,
242
+ number,
243
+ number
244
+ ];
245
+ export type ArcSegment = ASegment | aSegment;
246
+ export type PathSegment = MoveSegment | LineSegment | VertLineSegment | HorLineSegment | CloseSegment | CubicSegment | ShortCubicSegment | QuadSegment | ShortQuadSegment | ArcSegment;
247
+ export type AbsoluteSegment = MSegment | LSegment | VSegment | HSegment | CSegment | SSegment | QSegment | TSegment | ASegment | ZSegment;
248
+ export type RelativeSegment = mSegment | lSegment | vSegment | hSegment | cSegment | sSegment | qSegment | tSegment | aSegment | zSegment;
249
+ export type NormalSegment = MSegment | LSegment | CSegment | QSegment | ASegment | ZSegment;
250
+ export type PathArray = [
251
+ MSegment | mSegment,
252
+ ...PathSegment[]
253
+ ];
254
+ export type AbsoluteArray = [
255
+ MSegment,
256
+ ...AbsoluteSegment[]
257
+ ];
258
+ export type RelativeArray = [
259
+ MSegment,
260
+ ...RelativeSegment[]
261
+ ];
262
+ export type NormalArray = [
263
+ MSegment,
264
+ ...NormalSegment[]
265
+ ];
266
+ export type CurveArray = [
267
+ MSegment,
268
+ ...CSegment[]
269
+ ];
270
+ export type ShapeTypes = SVGPolylineElement | SVGPolygonElement | SVGLineElement | SVGEllipseElement | SVGCircleElement | SVGRectElement;
271
+ export type ShapeOps = LineAttr | PolyAttr | PolyAttr | EllipseAttr | CircleAttr | RectAttr | GlyphAttr;
272
+ declare class SVGPathCommander {
273
+ static CSSMatrix: typeof CSSMatrix;
274
+ static getPathBBox: (path?: PathArray | undefined) => PathBBox;
275
+ static getPathArea: (path: PathArray) => number;
276
+ static getTotalLength: (pathInput: string | PathArray) => number;
277
+ static getDrawDirection: (path: string | PathArray) => boolean;
278
+ static getPointAtLength: (pathInput: string | PathArray, distance: number) => {
279
+ x: number;
280
+ y: number;
281
+ };
282
+ static pathLengthFactory: (pathInput: string | PathArray, distance?: number | undefined) => LengthFactory;
283
+ static getPropertiesAtLength: (pathInput: string | PathArray, distance?: number | undefined) => SegmentProperties;
284
+ static getPropertiesAtPoint: (pathInput: string | PathArray, point: {
285
+ x: number;
286
+ y: number;
287
+ }) => PointProperties;
288
+ static polygonLength: (polygon: [
289
+ number,
290
+ number
291
+ ][]) => number;
292
+ static polygonArea: (polygon: [
293
+ number,
294
+ number
295
+ ][]) => number;
296
+ static getClosestPoint: (pathInput: string | PathArray, point: {
297
+ x: number;
298
+ y: number;
299
+ }) => {
300
+ x: number;
301
+ y: number;
302
+ };
303
+ static getSegmentOfPoint: (path: string | PathArray, point: {
304
+ x: number;
305
+ y: number;
306
+ }) => SegmentProperties | undefined;
307
+ static getSegmentAtLength: (pathInput: string | PathArray, distance?: number | undefined) => PathSegment | undefined;
308
+ static isPointInStroke: (pathInput: string | PathArray, point: {
309
+ x: number;
310
+ y: number;
311
+ }) => boolean;
312
+ static isValidPath: (pathString: string) => boolean;
313
+ static isPathArray: (path: unknown) => path is PathArray;
314
+ static isAbsoluteArray: (path: unknown) => path is AbsoluteArray;
315
+ static isRelativeArray: (path: unknown) => path is RelativeArray;
316
+ static isCurveArray: (path: unknown) => path is CurveArray;
317
+ static isNormalizedArray: (path: unknown) => path is NormalArray;
318
+ static shapeToPath: (element: ShapeTypes | ShapeOps, replace?: boolean | undefined, ownerDocument?: Document | undefined) => false | SVGPathElement;
319
+ static parsePathString: (pathInput: string | PathArray) => PathArray;
320
+ static roundPath: (path: PathArray, roundOption?: number | "off" | undefined) => PathArray;
321
+ static splitPath: (pathInput: PathArray) => PathArray[];
322
+ static splitCubic: (pts: number[]) => [
323
+ CubicSegment,
324
+ CubicSegment
325
+ ];
326
+ static optimizePath: (pathInput: PathArray, round: number | "off") => PathArray;
327
+ static reverseCurve: (path: CurveArray) => CurveArray;
328
+ static reversePath: (pathInput: PathArray) => PathArray;
329
+ static normalizePath: (pathInput: string | PathArray) => NormalArray;
330
+ static transformPath: (path: string | PathArray, transform?: Partial<TransformObject> | undefined) => PathArray;
331
+ static pathToAbsolute: (pathInput: string | PathArray) => AbsoluteArray;
332
+ static pathToRelative: (pathInput: string | PathArray) => RelativeArray;
333
+ static pathToCurve: (pathInput: string | PathArray) => CurveArray;
334
+ static pathToString: (path: PathArray, round?: number | "off" | undefined) => string;
335
+ segments: PathArray;
336
+ round: number | "off";
337
+ origin: [
338
+ number,
339
+ number,
340
+ number
341
+ ];
342
+ /**
343
+ * @constructor
344
+ * @param {string} pathValue the path string
345
+ * @param {any} config instance options
346
+ */
347
+ constructor(pathValue: string, config?: Partial<Options>);
348
+ /**
349
+ * Returns the path bounding box, equivalent to native `path.getBBox()`.
350
+ *
351
+ * @public
352
+ * @returns the pathBBox
353
+ */
354
+ getBBox(): PathBBox;
355
+ /**
356
+ * Returns the total path length, equivalent to native `path.getTotalLength()`.
357
+ *
358
+ * @public
359
+ * @returns the path total length
360
+ */
361
+ getTotalLength(): number;
362
+ /**
363
+ * Returns an `{x,y}` point in the path stroke at a given length,
364
+ * equivalent to the native `path.getPointAtLength()`.
365
+ *
366
+ * @public
367
+ * @param length the length
368
+ * @returns the requested point
369
+ */
370
+ getPointAtLength(length: number): {
371
+ x: number;
372
+ y: number;
373
+ };
374
+ /**
375
+ * Convert path to absolute values
376
+ *
377
+ * @public
378
+ */
379
+ toAbsolute(): this;
380
+ /**
381
+ * Convert path to relative values
382
+ *
383
+ * @public
384
+ */
385
+ toRelative(): this;
386
+ /**
387
+ * Convert path to cubic-bezier values. In addition, un-necessary `Z`
388
+ * segment is removed if previous segment extends to the `M` segment.
389
+ *
390
+ * @public
391
+ */
392
+ toCurve(): this;
393
+ /**
394
+ * Reverse the order of the segments and their values.
395
+ *
396
+ * @param onlySubpath option to reverse all sub-paths except first
397
+ * @public
398
+ */
399
+ reverse(onlySubpath?: boolean): this;
400
+ /**
401
+ * Normalize path in 2 steps:
402
+ * * convert `pathArray`(s) to absolute values
403
+ * * convert shorthand notation to standard notation
404
+ *
405
+ * @public
406
+ */
407
+ normalize(): this;
408
+ /**
409
+ * Optimize `pathArray` values:
410
+ * * convert segments to absolute and/or relative values
411
+ * * select segments with shortest resulted string
412
+ * * round values to the specified `decimals` option value
413
+ *
414
+ * @public
415
+ */
416
+ optimize(): this;
417
+ /**
418
+ * Transform path using values from an `Object` defined as `transformObject`.
419
+ *
420
+ * @see TransformObject for a quick refference
421
+ *
422
+ * @param source a `transformObject`as described above
423
+ * @public
424
+ */
425
+ transform(source?: Partial<TransformObject>): this;
426
+ /**
427
+ * Rotate path 180deg vertically
428
+ *
429
+ * @public
430
+ */
431
+ flipX(): this;
432
+ /**
433
+ * Rotate path 180deg horizontally
434
+ *
435
+ * @public
436
+ */
437
+ flipY(): this;
438
+ /**
439
+ * Export the current path to be used
440
+ * for the `d` (description) attribute.
441
+ *
442
+ * @public
443
+ * @return the path string
444
+ */
445
+ toString(): string;
446
+ }
447
+
448
+ export {
449
+ SVGPathCommander as default,
450
+ };
451
+
452
+ export as namespace SVGPathCommander;
453
+
454
+ export {};