svg-path-simplify 0.4.3 → 0.4.4
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/CHANGELOG.md +11 -0
- package/README.md +1 -0
- package/dist/svg-path-simplify.esm.js +1610 -495
- package/dist/svg-path-simplify.esm.min.js +2 -2
- package/dist/svg-path-simplify.js +1611 -494
- package/dist/svg-path-simplify.min.js +2 -2
- package/dist/svg-path-simplify.pathdata.esm.js +893 -456
- package/dist/svg-path-simplify.pathdata.esm.min.js +2 -2
- package/dist/svg-path-simplify.poly.cjs +9 -8
- package/index.html +58 -17
- package/package.json +1 -1
- package/src/constants.js +4 -0
- package/src/detect_input.js +47 -29
- package/src/index.js +8 -0
- package/src/pathData_simplify_cubic.js +26 -16
- package/src/pathData_simplify_revertToquadratics.js +0 -1
- package/src/pathSimplify-main.js +75 -20
- package/src/pathSimplify-only-pathdata.js +7 -2
- package/src/pathSimplify-presets.js +15 -4
- package/src/svg-getAttributes.js +4 -2
- package/src/svgii/convert_units.js +1 -1
- package/src/svgii/geometry.js +140 -2
- package/src/svgii/geometry_bbox_element.js +1 -1
- package/src/svgii/geometry_deduceRadius.js +116 -27
- package/src/svgii/geometry_length.js +17 -1
- package/src/svgii/pathData_analyze.js +18 -0
- package/src/svgii/pathData_convert.js +188 -88
- package/src/svgii/pathData_fix_directions.js +10 -18
- package/src/svgii/pathData_reorder.js +122 -16
- package/src/svgii/pathData_simplify_refineCorners.js +130 -35
- package/src/svgii/pathData_simplify_refine_round.js +420 -0
- package/src/svgii/rounding.js +79 -78
- package/src/svgii/svg_cleanup.js +68 -20
- package/src/svgii/svg_cleanup_convertPathLength.js +22 -15
- package/src/svgii/svg_cleanup_remove_els_and_atts.js +6 -1
- package/src/svgii/svg_el_parse_style_props.js +13 -10
- package/src/svgii/svg_validate.js +220 -0
- package/tests/testSVG.js +14 -1
- package/src/svgii/pathData_refine_round.js +0 -222
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
import { checkLineIntersection, getAngle, getDistAv, getDistance, getPointOnEllipse, getSquareDistance, pointAtT, rotatePoint } from "./geometry";
|
|
2
|
-
import { getPolygonArea } from "./geometry_area";
|
|
3
|
-
import { getArcFromPoly } from "./geometry_deduceRadius";
|
|
4
|
-
import { arcToBezierResolved, revertCubicQuadratic } from "./pathData_convert";
|
|
5
|
-
import { pathDataToD } from "./pathData_stringify";
|
|
6
|
-
import { renderPath, renderPoint, renderPoly } from "./visualize";
|
|
7
|
-
|
|
8
|
-
export function refineRoundSegments(pathData, {
|
|
9
|
-
threshold = 0,
|
|
10
|
-
tolerance = 1,
|
|
11
|
-
// take arcs or cubic beziers
|
|
12
|
-
toCubic = false,
|
|
13
|
-
debug = false
|
|
14
|
-
} = {}) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
// min size threshold for corners
|
|
18
|
-
threshold *= tolerance;
|
|
19
|
-
|
|
20
|
-
let l = pathData.length;
|
|
21
|
-
|
|
22
|
-
// add fist command
|
|
23
|
-
let pathDataN = [pathData[0]]
|
|
24
|
-
|
|
25
|
-
// just for debugging
|
|
26
|
-
let pathDataTest = []
|
|
27
|
-
|
|
28
|
-
for (let i = 1; i < l; i++) {
|
|
29
|
-
let com = pathData[i];
|
|
30
|
-
let { type } = com;
|
|
31
|
-
let comP = pathData[i - 1];
|
|
32
|
-
let comN = pathData[i + 1] ? pathData[i + 1] : null;
|
|
33
|
-
let comN2 = pathData[i + 2] ? pathData[i + 2] : null;
|
|
34
|
-
let comN3 = pathData[i + 3] ? pathData[i + 3] : null;
|
|
35
|
-
let comBez = null;
|
|
36
|
-
|
|
37
|
-
if ((com.type === 'C' || com.type === 'Q')) comBez = com;
|
|
38
|
-
else if (comN && (comN.type === 'C' || comN.type === 'Q')) comBez = comN;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
let cpts = comBez ? (comBez.type === 'C' ? [comBez.p0, comBez.cp1, comBez.cp2, comBez.p] : [comBez.p0, comBez.cp1, comBez.p]) : []
|
|
42
|
-
|
|
43
|
-
let areaBez = 0;
|
|
44
|
-
let areaLines = 0;
|
|
45
|
-
let signChange = false;
|
|
46
|
-
let L1, L2;
|
|
47
|
-
let combine = false
|
|
48
|
-
|
|
49
|
-
let p0_S, p_S;
|
|
50
|
-
let poly = []
|
|
51
|
-
let pMid;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
// 2. line-line-bezier-line-line
|
|
55
|
-
if (
|
|
56
|
-
comP.type === 'L' &&
|
|
57
|
-
type === 'L' &&
|
|
58
|
-
comBez &&
|
|
59
|
-
comN2.type === 'L' &&
|
|
60
|
-
comN3 && (comN3.type === 'L' || comN3.type === 'Z')
|
|
61
|
-
) {
|
|
62
|
-
|
|
63
|
-
L1 = [com.p0, com.p];
|
|
64
|
-
L2 = [comN2.p0, comN2.p];
|
|
65
|
-
p0_S = com.p0
|
|
66
|
-
p_S = comN2.p
|
|
67
|
-
|
|
68
|
-
// don't allow sign changes
|
|
69
|
-
areaBez = getPolygonArea(cpts, false)
|
|
70
|
-
areaLines = getPolygonArea([...L1, ...L2], false)
|
|
71
|
-
signChange = (areaBez < 0 && areaLines > 0) || (areaBez > 0 && areaLines < 0)
|
|
72
|
-
|
|
73
|
-
if (!signChange) {
|
|
74
|
-
|
|
75
|
-
// mid point of mid bezier
|
|
76
|
-
pMid = pointAtT(cpts, 0.5)
|
|
77
|
-
|
|
78
|
-
// add to poly
|
|
79
|
-
poly = [p0_S, pMid, p_S]
|
|
80
|
-
|
|
81
|
-
combine = true
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// 1. line-bezier-bezier-line
|
|
87
|
-
else if ((type === 'C' || type === 'Q') && comP.type === 'L') {
|
|
88
|
-
|
|
89
|
-
// 1.2 next is cubic next is lineto
|
|
90
|
-
if ((comN.type === 'C' || comN.type === 'Q') && comN2.type === 'L') {
|
|
91
|
-
|
|
92
|
-
combine = true
|
|
93
|
-
|
|
94
|
-
L1 = [comP.p0, comP.p];
|
|
95
|
-
L2 = [comN2.p0, comN2.p];
|
|
96
|
-
p0_S = comP.p
|
|
97
|
-
p_S = comN2.p0
|
|
98
|
-
|
|
99
|
-
// mid point of mid bezier
|
|
100
|
-
pMid = comBez.p
|
|
101
|
-
|
|
102
|
-
// add to poly
|
|
103
|
-
poly = [p0_S, comBez.p, p_S]
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* calculate either combined
|
|
112
|
-
* cubic or arc commands
|
|
113
|
-
*/
|
|
114
|
-
if (combine) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
// try to find center of arc
|
|
118
|
-
let arcProps = getArcFromPoly(poly)
|
|
119
|
-
if (arcProps) {
|
|
120
|
-
|
|
121
|
-
let { centroid, r, deltaAngle, startAngle, endAngle } = arcProps;
|
|
122
|
-
|
|
123
|
-
let xAxisRotation = 0;
|
|
124
|
-
let sweep = deltaAngle > 0 ? 1 : 0;
|
|
125
|
-
let largeArc = Math.abs(deltaAngle) > Math.PI ? 1 : 0;
|
|
126
|
-
|
|
127
|
-
let pCM = rotatePoint(p0_S, centroid.x, centroid.y, deltaAngle * 0.5)
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
let dist2 = getDistAv(pCM, pMid)
|
|
131
|
-
let thresh = getDistAv(p0_S, p_S) * 0.05
|
|
132
|
-
let bezierCommands;
|
|
133
|
-
|
|
134
|
-
// point is close enough
|
|
135
|
-
if (dist2 < thresh) {
|
|
136
|
-
|
|
137
|
-
//toCubic = false;
|
|
138
|
-
|
|
139
|
-
bezierCommands = arcToBezierResolved(
|
|
140
|
-
{
|
|
141
|
-
p0: p0_S,
|
|
142
|
-
p: p_S,
|
|
143
|
-
centroid,
|
|
144
|
-
rx: r,
|
|
145
|
-
ry: r,
|
|
146
|
-
xAxisRotation,
|
|
147
|
-
sweep,
|
|
148
|
-
largeArc,
|
|
149
|
-
deltaAngle,
|
|
150
|
-
startAngle,
|
|
151
|
-
endAngle
|
|
152
|
-
}
|
|
153
|
-
);
|
|
154
|
-
|
|
155
|
-
if(bezierCommands.length === 1){
|
|
156
|
-
|
|
157
|
-
// prefer more compact quadratic - otherwise arcs
|
|
158
|
-
let comBezier = revertCubicQuadratic(p0_S, bezierCommands[0].cp1, bezierCommands[0].cp2, p_S)
|
|
159
|
-
|
|
160
|
-
if (comBezier.type === 'Q') {
|
|
161
|
-
toCubic = true
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
com = comBezier
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
// prefer arcs if 2 cubics are required
|
|
169
|
-
if (bezierCommands.length > 1) toCubic = false;
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
//toCubic = false
|
|
173
|
-
|
|
174
|
-
// return elliptic arc commands
|
|
175
|
-
if (!toCubic) {
|
|
176
|
-
// rewrite simplified command
|
|
177
|
-
com.type = 'A'
|
|
178
|
-
com.values = [r, r, xAxisRotation, largeArc, sweep, p_S.x, p_S.y];
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
com.p0 = p0_S;
|
|
182
|
-
com.p = p_S;
|
|
183
|
-
com.extreme = false;
|
|
184
|
-
com.corner = false;
|
|
185
|
-
|
|
186
|
-
// test rendering
|
|
187
|
-
//debug=true
|
|
188
|
-
|
|
189
|
-
if (debug) {
|
|
190
|
-
// arcs
|
|
191
|
-
if (!toCubic) {
|
|
192
|
-
pathDataTest = [
|
|
193
|
-
{ type: 'M', values: [p0_S.x, p0_S.y] },
|
|
194
|
-
{ type: 'A', values: [r, r, xAxisRotation, largeArc, sweep, p_S.x, p_S.y] },
|
|
195
|
-
]
|
|
196
|
-
}
|
|
197
|
-
// cubics
|
|
198
|
-
else {
|
|
199
|
-
pathDataTest = [
|
|
200
|
-
{ type: 'M', values: [p0_S.x, p0_S.y] },
|
|
201
|
-
...bezierCommands
|
|
202
|
-
]
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
let d = pathDataToD(pathDataTest);
|
|
206
|
-
renderPath(markers, d, 'orange', '0.5%', '0.5')
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
pathDataN.push(com);
|
|
210
|
-
i++
|
|
211
|
-
continue
|
|
212
|
-
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// pass through
|
|
218
|
-
pathDataN.push(com)
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
return pathDataN;
|
|
222
|
-
}
|