svg-path-commander 2.1.3 → 2.1.5
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.
- package/README.md +4 -4
- package/dist/svg-path-commander.cjs +1 -1
- package/dist/svg-path-commander.cjs.map +1 -1
- package/dist/svg-path-commander.d.ts +755 -830
- package/dist/svg-path-commander.js +1 -1
- package/dist/svg-path-commander.js.map +1 -1
- package/dist/svg-path-commander.mjs +287 -56
- package/dist/svg-path-commander.mjs.map +1 -1
- package/package.json +14 -22
- package/.eslintrc.cjs +0 -225
- package/.prettierrc.json +0 -15
- package/dts.config.ts +0 -15
- package/src/convert/pathToAbsolute.ts +0 -18
- package/src/convert/pathToCurve.ts +0 -43
- package/src/convert/pathToRelative.ts +0 -18
- package/src/convert/pathToString.ts +0 -50
- package/src/index.ts +0 -454
- package/src/interface.ts +0 -130
- package/src/math/arcTools.ts +0 -388
- package/src/math/bezier.ts +0 -261
- package/src/math/cubicTools.ts +0 -123
- package/src/math/distanceSquareRoot.ts +0 -15
- package/src/math/lineTools.ts +0 -61
- package/src/math/midPoint.ts +0 -18
- package/src/math/polygonTools.ts +0 -48
- package/src/math/quadTools.ts +0 -98
- package/src/math/rotateVector.ts +0 -17
- package/src/math/roundTo.ts +0 -7
- package/src/options/options.ts +0 -9
- package/src/parser/error.ts +0 -2
- package/src/parser/finalizeSegment.ts +0 -35
- package/src/parser/invalidPathValue.ts +0 -2
- package/src/parser/isArcCommand.ts +0 -11
- package/src/parser/isDigit.ts +0 -12
- package/src/parser/isDigitStart.ts +0 -14
- package/src/parser/isMoveCommand.ts +0 -17
- package/src/parser/isPathCommand.ts +0 -28
- package/src/parser/isSpace.ts +0 -23
- package/src/parser/paramsCount.ts +0 -16
- package/src/parser/paramsParser.ts +0 -14
- package/src/parser/parsePathString.ts +0 -33
- package/src/parser/pathParser.ts +0 -29
- package/src/parser/scanFlag.ts +0 -29
- package/src/parser/scanParam.ts +0 -102
- package/src/parser/scanSegment.ts +0 -84
- package/src/parser/skipSpaces.ts +0 -17
- package/src/process/absolutizeSegment.ts +0 -67
- package/src/process/arcToCubic.ts +0 -128
- package/src/process/getSVGMatrix.ts +0 -70
- package/src/process/iterate.ts +0 -58
- package/src/process/lineToCubic.ts +0 -17
- package/src/process/normalizePath.ts +0 -33
- package/src/process/normalizeSegment.ts +0 -84
- package/src/process/optimizePath.ts +0 -62
- package/src/process/projection2d.ts +0 -52
- package/src/process/quadToCubic.ts +0 -31
- package/src/process/relativizeSegment.ts +0 -63
- package/src/process/reverseCurve.ts +0 -24
- package/src/process/reversePath.ts +0 -114
- package/src/process/roundPath.ts +0 -33
- package/src/process/roundSegment.ts +0 -9
- package/src/process/segmentToCubic.ts +0 -48
- package/src/process/shortenSegment.ts +0 -71
- package/src/process/splitCubic.ts +0 -29
- package/src/process/splitPath.ts +0 -63
- package/src/process/transformPath.ts +0 -114
- package/src/types.ts +0 -228
- package/src/util/distanceEpsilon.ts +0 -3
- package/src/util/getClosestPoint.ts +0 -15
- package/src/util/getDrawDirection.ts +0 -16
- package/src/util/getPathArea.ts +0 -70
- package/src/util/getPathBBox.ts +0 -171
- package/src/util/getPointAtLength.ts +0 -110
- package/src/util/getPropertiesAtLength.ts +0 -67
- package/src/util/getPropertiesAtPoint.ts +0 -84
- package/src/util/getSegmentAtLength.ts +0 -15
- package/src/util/getSegmentOfPoint.ts +0 -18
- package/src/util/getTotalLength.ts +0 -133
- package/src/util/isAbsoluteArray.ts +0 -18
- package/src/util/isCurveArray.ts +0 -15
- package/src/util/isNormalizedArray.ts +0 -16
- package/src/util/isPathArray.ts +0 -24
- package/src/util/isPointInStroke.ts +0 -16
- package/src/util/isRelativeArray.ts +0 -18
- package/src/util/isValidPath.ts +0 -27
- package/src/util/shapeParams.ts +0 -16
- package/src/util/shapeToPath.ts +0 -86
- package/src/util/shapeToPathArray.ts +0 -183
- package/test/class.test.ts +0 -506
- package/test/fixtures/getMarkup.ts +0 -17
- package/test/fixtures/shapeObjects.ts +0 -11
- package/test/fixtures/shapes.js +0 -104
- package/test/fixtures/simpleShapes.js +0 -87
- package/test/static.test.ts +0 -330
- package/vite.config.mts +0 -41
- package/vitest.config-ui.mts +0 -31
- package/vitest.config.mts +0 -31
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import type { CircleAttr, EllipseAttr, GlyphAttr, LineAttr, PolyAttr, RectAttr, ShapeParams } from '../interface';
|
|
2
|
-
import type { PathArray, PathSegment, ShapeOps, ShapeTypes } from '../types';
|
|
3
|
-
import error from '../parser/error';
|
|
4
|
-
import parsePathString from '../parser/parsePathString';
|
|
5
|
-
import shapeParams from './shapeParams';
|
|
6
|
-
import isPathArray from './isPathArray';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Returns a new `pathArray` from line attributes.
|
|
10
|
-
*
|
|
11
|
-
* @param attr shape configuration
|
|
12
|
-
* @returns a new line `pathArray`
|
|
13
|
-
*/
|
|
14
|
-
export const getLinePath = (attr: LineAttr): PathArray => {
|
|
15
|
-
let { x1, y1, x2, y2 } = attr;
|
|
16
|
-
[x1, y1, x2, y2] = [x1, y1, x2, y2].map(a => +a);
|
|
17
|
-
return [
|
|
18
|
-
['M', x1, y1],
|
|
19
|
-
['L', x2, y2],
|
|
20
|
-
];
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Returns a new `pathArray` like from polyline/polygon attributes.
|
|
25
|
-
*
|
|
26
|
-
* @param attr shape configuration
|
|
27
|
-
* @return a new polygon/polyline `pathArray`
|
|
28
|
-
*/
|
|
29
|
-
export const getPolyPath = (attr: PolyAttr): PathArray => {
|
|
30
|
-
const pathArray = [] as PathSegment[];
|
|
31
|
-
const points = (attr.points || '')
|
|
32
|
-
.trim()
|
|
33
|
-
.split(/[\s|,]/)
|
|
34
|
-
.map(a => +a);
|
|
35
|
-
|
|
36
|
-
let index = 0;
|
|
37
|
-
while (index < points.length) {
|
|
38
|
-
pathArray.push([index ? 'L' : 'M', points[index], points[index + 1]]);
|
|
39
|
-
index += 2;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return (attr.type === 'polygon' ? [...pathArray, ['z']] : pathArray) as PathArray;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Returns a new `pathArray` from circle attributes.
|
|
47
|
-
*
|
|
48
|
-
* @param attr shape configuration
|
|
49
|
-
* @return a circle `pathArray`
|
|
50
|
-
*/
|
|
51
|
-
export const getCirclePath = (attr: CircleAttr): PathArray => {
|
|
52
|
-
let { cx, cy, r } = attr;
|
|
53
|
-
[cx, cy, r] = [cx, cy, r].map(a => +a);
|
|
54
|
-
|
|
55
|
-
return [
|
|
56
|
-
['M', cx - r, cy],
|
|
57
|
-
['a', r, r, 0, 1, 0, 2 * r, 0],
|
|
58
|
-
['a', r, r, 0, 1, 0, -2 * r, 0],
|
|
59
|
-
];
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Returns a new `pathArray` from ellipse attributes.
|
|
64
|
-
*
|
|
65
|
-
* @param attr shape configuration
|
|
66
|
-
* @return an ellipse `pathArray`
|
|
67
|
-
*/
|
|
68
|
-
export const getEllipsePath = (attr: EllipseAttr): PathArray => {
|
|
69
|
-
let { cx, cy } = attr;
|
|
70
|
-
let rx = attr.rx || 0;
|
|
71
|
-
let ry = attr.ry || rx;
|
|
72
|
-
[cx, cy, rx, ry] = [cx, cy, rx, ry].map(a => +a);
|
|
73
|
-
|
|
74
|
-
return [
|
|
75
|
-
['M', cx - rx, cy],
|
|
76
|
-
['a', rx, ry, 0, 1, 0, 2 * rx, 0],
|
|
77
|
-
['a', rx, ry, 0, 1, 0, -2 * rx, 0],
|
|
78
|
-
];
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Returns a new `pathArray` like from rect attributes.
|
|
83
|
-
*
|
|
84
|
-
* @param attr object with properties above
|
|
85
|
-
* @return a new `pathArray` from `<rect>` attributes
|
|
86
|
-
*/
|
|
87
|
-
export const getRectanglePath = (attr: RectAttr): PathArray => {
|
|
88
|
-
const x = +attr.x || 0;
|
|
89
|
-
const y = +attr.y || 0;
|
|
90
|
-
const w = +attr.width;
|
|
91
|
-
const h = +attr.height;
|
|
92
|
-
let rx = +(attr.rx || 0);
|
|
93
|
-
let ry = +(attr.ry || rx);
|
|
94
|
-
|
|
95
|
-
// Validity checks from http://www.w3.org/TR/SVG/shapes.html#RectElement:
|
|
96
|
-
if (rx || ry) {
|
|
97
|
-
// rx = !rx ? ry : rx;
|
|
98
|
-
// ry = !ry ? rx : ry;
|
|
99
|
-
|
|
100
|
-
/* istanbul ignore else @preserve */
|
|
101
|
-
if (rx * 2 > w) rx -= (rx * 2 - w) / 2;
|
|
102
|
-
/* istanbul ignore else @preserve */
|
|
103
|
-
if (ry * 2 > h) ry -= (ry * 2 - h) / 2;
|
|
104
|
-
|
|
105
|
-
return [
|
|
106
|
-
['M', x + rx, y],
|
|
107
|
-
['h', w - rx * 2],
|
|
108
|
-
['s', rx, 0, rx, ry],
|
|
109
|
-
['v', h - ry * 2],
|
|
110
|
-
['s', 0, ry, -rx, ry],
|
|
111
|
-
['h', -w + rx * 2],
|
|
112
|
-
['s', -rx, 0, -rx, -ry],
|
|
113
|
-
['v', -h + ry * 2],
|
|
114
|
-
['s', 0, -ry, rx, -ry],
|
|
115
|
-
];
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return [['M', x, y], ['h', w], ['v', h], ['H', x], ['Z']];
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Returns a new `pathArray` created from attributes of a `<line>`, `<polyline>`,
|
|
123
|
-
* `<polygon>`, `<rect>`, `<ellipse>`, `<circle>`, <path> or `<glyph>`.
|
|
124
|
-
*
|
|
125
|
-
* The default `ownerDocument` is your current `document` browser page,
|
|
126
|
-
* if you want to use in server-side using `jsdom`, you can pass the
|
|
127
|
-
* `jsdom` `document` to `ownDocument`.
|
|
128
|
-
*
|
|
129
|
-
* It can also work with an options object, see the type below
|
|
130
|
-
*
|
|
131
|
-
* @see ShapeOps
|
|
132
|
-
*
|
|
133
|
-
* @param element target shape
|
|
134
|
-
* @param ownerDocument document for create element
|
|
135
|
-
* @return the newly created `<path>` element
|
|
136
|
-
*/
|
|
137
|
-
const shapeToPathArray = (element: ShapeTypes | ShapeOps, ownerDocument?: Document) => {
|
|
138
|
-
const doc = ownerDocument || document;
|
|
139
|
-
const win = doc.defaultView || /* istanbul ignore next */ window;
|
|
140
|
-
const supportedShapes = Object.keys(shapeParams) as (keyof ShapeParams)[];
|
|
141
|
-
const targetIsElement = element instanceof win.SVGElement;
|
|
142
|
-
const tagName = targetIsElement ? element.tagName : null;
|
|
143
|
-
|
|
144
|
-
if (tagName && [...supportedShapes, 'path'].every(s => tagName !== s)) {
|
|
145
|
-
throw TypeError(`${error}: "${tagName}" is not SVGElement`);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const type = (targetIsElement ? tagName : element.type) as ShapeOps['type'];
|
|
149
|
-
const shapeAttrs = shapeParams[type] as string[];
|
|
150
|
-
const config = { type } as Record<string, string>;
|
|
151
|
-
|
|
152
|
-
if (targetIsElement) {
|
|
153
|
-
shapeAttrs.forEach(p => {
|
|
154
|
-
config[p] = element.getAttribute(p) as string;
|
|
155
|
-
});
|
|
156
|
-
} else {
|
|
157
|
-
Object.assign(config, element);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// set d
|
|
161
|
-
let pathArray = [] as unknown as PathArray;
|
|
162
|
-
|
|
163
|
-
/* istanbul ignore else */
|
|
164
|
-
if (type === 'circle') pathArray = getCirclePath(config as unknown as CircleAttr);
|
|
165
|
-
else if (type === 'ellipse') pathArray = getEllipsePath(config as unknown as EllipseAttr);
|
|
166
|
-
else if (['polyline', 'polygon'].includes(type)) pathArray = getPolyPath(config as unknown as PolyAttr);
|
|
167
|
-
else if (type === 'rect') pathArray = getRectanglePath(config as unknown as RectAttr);
|
|
168
|
-
else if (type === 'line') pathArray = getLinePath(config as unknown as LineAttr);
|
|
169
|
-
else if (['glyph', 'path'].includes(type)) {
|
|
170
|
-
pathArray = parsePathString(
|
|
171
|
-
targetIsElement
|
|
172
|
-
? element.getAttribute('d') || /* istanbul ignore next @preserve */ ''
|
|
173
|
-
: (element as GlyphAttr).d || '',
|
|
174
|
-
);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// replace target element
|
|
178
|
-
if (isPathArray(pathArray) && pathArray.length) {
|
|
179
|
-
return pathArray;
|
|
180
|
-
}
|
|
181
|
-
return false;
|
|
182
|
-
};
|
|
183
|
-
export default shapeToPathArray;
|