svg-path-simplify 0.0.8 → 0.0.9
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 +25 -5
- package/dist/svg-path-simplify.esm.js +576 -494
- package/dist/svg-path-simplify.esm.min.js +1 -1
- package/dist/svg-path-simplify.js +576 -494
- package/dist/svg-path-simplify.min.js +1 -1
- package/dist/svg-path-simplify.node.js +576 -494
- package/dist/svg-path-simplify.node.min.js +1 -1
- package/index.html +86 -29
- package/package.json +1 -1
- package/src/detect_input.js +17 -10
- package/src/index.js +3 -0
- package/src/pathData_simplify_cubic.js +113 -106
- package/src/pathData_simplify_cubic_extrapolate.js +25 -11
- package/src/pathSimplify-main.js +89 -182
- package/src/svgii/geometry_flatness.js +29 -36
- package/src/svgii/pathData_analyze.js +4 -0
- package/src/svgii/pathData_convert.js +26 -17
- package/src/svgii/pathData_interpolate.js +65 -0
- package/src/svgii/pathData_parse.js +25 -9
- package/src/svgii/pathData_parse_els.js +18 -12
- package/src/svgii/pathData_remove_collinear.js +31 -28
- package/src/svgii/pathData_remove_orphaned.js +5 -4
- package/src/svgii/pathData_remove_zerolength.js +8 -4
- package/src/svgii/pathData_reorder.js +6 -2
- package/src/svgii/pathData_simplify_refineCorners.js +160 -0
- package/src/svgii/{simplify_refineExtremes.js → pathData_simplify_refineExtremes.js} +78 -43
- package/src/svgii/pathData_split.js +42 -15
- package/src/svgii/pathData_stringify.js +3 -12
- package/src/svgii/rounding.js +16 -14
- package/src/svgii/svg_cleanup.js +1 -1
- package/src/pathData_simplify_cubic_arr.js +0 -50
- package/src/svgii/simplify.js +0 -248
- package/src/svgii/simplify_bezier.js +0 -470
- package/src/svgii/simplify_linetos.js +0 -93
package/src/svgii/simplify.js
DELETED
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* split path data into chunks
|
|
4
|
-
* to detect subsequent cubic segments
|
|
5
|
-
* that could be combined
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
//import { splitSubpaths, shiftSvgStartingPoint } from "./convert_segments";
|
|
9
|
-
import { shiftSvgStartingPoint } from "./pathData_reorder.js";
|
|
10
|
-
import { splitSubpaths, getPathDataPlusChunks } from './pathData_split.js';
|
|
11
|
-
|
|
12
|
-
import { getAngle, bezierhasExtreme, getPathDataVertices } from "./geometry";
|
|
13
|
-
import { renderPoint, renderPath } from "./visualize";
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
//import { optimizeStartingPoints } from './cleanup.js';
|
|
17
|
-
//import { getPathDataVertices, getPointOnEllipse, pointAtT, checkLineIntersection, getDistance, interpolate } from './geometry.js';
|
|
18
|
-
|
|
19
|
-
import { getPolygonArea, getPathArea, getRelativeAreaDiff } from './geometry_area.js';
|
|
20
|
-
import { getPathDataBBox, getPolyBBox } from './geometry_bbox.js';
|
|
21
|
-
|
|
22
|
-
import { optimizeStartingPoints, cleanUpPathData } from './pathdata_cleanup.js';
|
|
23
|
-
|
|
24
|
-
import { pathDataArcsToCubics, pathDataQuadraticToCubic, quadratic2Cubic, pathDataToRelative, pathDataToAbsolute, pathDataToLonghands, pathDataToShorthands, pathDataToQuadratic, cubicToQuad, arcToBezier, pathDataToVerbose, convertArrayPathData, revertPathDataToArray, combineArcs, replaceCubicsByArcs } from './pathData_convert.js';
|
|
25
|
-
|
|
26
|
-
import {unitePolygon} from './simplify_polygon.js';
|
|
27
|
-
|
|
28
|
-
import { simplifyBezierSequence } from './simplify_bezier.js';
|
|
29
|
-
//import { simplifyBezierSequence } from './simplify_bezier_back16_working.js';
|
|
30
|
-
//import { simplifyBezierSequence } from './simplify_bezier_back17_working.js';
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
import { simplifyLinetoSequence } from './simplify_linetos.js';
|
|
35
|
-
import { analyzePathData } from "./pathData_anylyse.js";
|
|
36
|
-
import { scalePathData } from "./pathData_scale.js";
|
|
37
|
-
//import { analyzePathData } from "./pathData_anylyse_back1.js";
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
export function simplifyPathData(pathData, tolerance = 3, keepDetails = true, forceCubic = false, cubicToArc = true, multipass = false, debug = false) {
|
|
42
|
-
|
|
43
|
-
///devcomment
|
|
44
|
-
|
|
45
|
-
//console.log('forceCubic simplifyPathData', forceCubic);
|
|
46
|
-
|
|
47
|
-
// unoptimized area
|
|
48
|
-
let area0 = getPathArea(pathData);
|
|
49
|
-
|
|
50
|
-
// get bbox for adjustment scaling
|
|
51
|
-
let bb = getPathDataBBox(pathData);
|
|
52
|
-
//console.log('bb', bb);
|
|
53
|
-
|
|
54
|
-
let dimA = (bb.width + bb.height) / 2;
|
|
55
|
-
let scale = dimA < 10 ? 100 / dimA : 1;
|
|
56
|
-
|
|
57
|
-
// scale small paths
|
|
58
|
-
if (scale != 1) pathData = scalePathData(pathData, scale, scale)
|
|
59
|
-
|
|
60
|
-
// remove zero length commands and shift starting point
|
|
61
|
-
let addExtremes = true;
|
|
62
|
-
addExtremes = false;
|
|
63
|
-
|
|
64
|
-
let removeFinalLineto = false
|
|
65
|
-
let startToTop = true;
|
|
66
|
-
//tolerance = 5;
|
|
67
|
-
|
|
68
|
-
// show chunks
|
|
69
|
-
//debug = true
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* optimize starting point
|
|
73
|
-
* remove zero length segments
|
|
74
|
-
*/
|
|
75
|
-
pathData = cleanUpPathData(pathData, addExtremes, removeFinalLineto, startToTop, debug)
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
// get verbose pathdata properties
|
|
79
|
-
let pathDataPlus = analyzePathData(pathData);
|
|
80
|
-
|
|
81
|
-
// add chunks to path object
|
|
82
|
-
let pathDataPlusChunks = getPathDataPlusChunks(pathDataPlus, debug);
|
|
83
|
-
|
|
84
|
-
// create simplified pathData
|
|
85
|
-
let pathDataSimple = [];
|
|
86
|
-
|
|
87
|
-
// loop sup path
|
|
88
|
-
for (let s = 0, l = pathDataPlusChunks.length; l && s < l; s++) {
|
|
89
|
-
let sub = pathDataPlusChunks[s];
|
|
90
|
-
let { chunks, dimA, area } = sub;
|
|
91
|
-
|
|
92
|
-
let thresh = dimA * 0.1
|
|
93
|
-
let len = chunks.length;
|
|
94
|
-
let simplified;
|
|
95
|
-
//console.log('sub', chunks);
|
|
96
|
-
|
|
97
|
-
//forceCubic = true
|
|
98
|
-
|
|
99
|
-
for (let i = 0; i < len; i++) {
|
|
100
|
-
let chunk = chunks[i];
|
|
101
|
-
let type = chunk[0].type;
|
|
102
|
-
|
|
103
|
-
// try to convert cubic to quadratic
|
|
104
|
-
|
|
105
|
-
//forceCubic = true
|
|
106
|
-
|
|
107
|
-
if (!forceCubic && chunk.length === 1 && type === 'C') {
|
|
108
|
-
simplified = simplifyBezierSequence(chunk);
|
|
109
|
-
pathDataSimple.push(...simplified);
|
|
110
|
-
//console.log('simplified cubic to quadratic', simplified);
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// nothing to combine
|
|
115
|
-
if (chunk.length < 2) {
|
|
116
|
-
pathDataSimple.push(...chunk);
|
|
117
|
-
//console.log('simple',chunk );
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// simplify linetos
|
|
122
|
-
if (type === 'L' && chunk.length > 1) {
|
|
123
|
-
//simplified = simplifyLinetoSequence(chunk, thresh);
|
|
124
|
-
//console.log('lineto');
|
|
125
|
-
simplified = simplifyLinetoSequence(chunk);
|
|
126
|
-
pathDataSimple.push(...simplified);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// Béziers
|
|
130
|
-
else if (chunk.length > 1 && (type === 'C' || type === 'Q')) {
|
|
131
|
-
//console.log('hasCubics');
|
|
132
|
-
if (chunk.length) {
|
|
133
|
-
|
|
134
|
-
multipass = false
|
|
135
|
-
//multipass = true
|
|
136
|
-
|
|
137
|
-
let directionChange = chunk[0].directionChange;
|
|
138
|
-
//directionChange = false
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* prevent too aggressive simplification
|
|
142
|
-
* e.g for quadratic glyphs
|
|
143
|
-
* by splitting large chunks in two
|
|
144
|
-
*/
|
|
145
|
-
//keepDetails = false
|
|
146
|
-
|
|
147
|
-
//(directionChange && chunk.length > 4) || (!directionChange && chunk.length > 4)
|
|
148
|
-
if (keepDetails && (chunk.length > 4) && !multipass) {
|
|
149
|
-
let split = Math.ceil((chunk.length - 1) / 2)
|
|
150
|
-
let chunk1 = chunk.slice(0, split)
|
|
151
|
-
let chunk2 = chunk.slice(split)
|
|
152
|
-
//console.log('chunk:', chunk);
|
|
153
|
-
//renderPoint(svg1,chunk[0].p0, 'magenta' )
|
|
154
|
-
|
|
155
|
-
//console.log('forceCubic keepDetails', forceCubic);
|
|
156
|
-
let simplified1 = simplifyBezierSequence(chunk1, tolerance, keepDetails, forceCubic);
|
|
157
|
-
let simplified2 = simplifyBezierSequence(chunk2, tolerance, keepDetails, forceCubic);
|
|
158
|
-
|
|
159
|
-
pathDataSimple.push(...simplified1, ...simplified2);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
else {
|
|
163
|
-
simplified = simplifyBezierSequence(chunk, tolerance, keepDetails, forceCubic);
|
|
164
|
-
pathDataSimple.push(...simplified);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// No match, keep original commands
|
|
170
|
-
else {
|
|
171
|
-
//chunk.forEach(com => pathDataSimple.push({ type: com.type, values: com.values }));
|
|
172
|
-
pathDataSimple.push(...chunk);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* try to replace cubics
|
|
180
|
-
* to arcs
|
|
181
|
-
*/
|
|
182
|
-
//cubicToArc = false;
|
|
183
|
-
if (cubicToArc) {
|
|
184
|
-
//console.log();
|
|
185
|
-
pathDataSimple = replaceCubicsByArcs(pathDataSimple, tolerance * 0.5);
|
|
186
|
-
|
|
187
|
-
// combine adjacent arcs
|
|
188
|
-
pathDataSimple = combineArcs(pathDataSimple);
|
|
189
|
-
|
|
190
|
-
console.log('arcs', pathDataSimple);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
// rescale small paths
|
|
195
|
-
if (scale != 1) pathDataSimple = scalePathData(pathDataSimple, 1 / scale, 1 / scale)
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* final area check
|
|
200
|
-
* fallback to original if difference is too large
|
|
201
|
-
*/
|
|
202
|
-
/*
|
|
203
|
-
let areaS = getPathArea(pathDataSimple);
|
|
204
|
-
let areaDiff = getRelativeAreaDiff(area0, areaS)
|
|
205
|
-
|
|
206
|
-
if (areaDiff > tolerance) {
|
|
207
|
-
//pathDataSimple = pathData;
|
|
208
|
-
//console.log('take original', pathDataSimple);
|
|
209
|
-
}
|
|
210
|
-
*/
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* final optimization
|
|
215
|
-
* simplify adjacent linetos
|
|
216
|
-
* optimize start points
|
|
217
|
-
* we done it before
|
|
218
|
-
* but we need to apply this again to
|
|
219
|
-
* avoid unnecessary close linetos
|
|
220
|
-
*/
|
|
221
|
-
|
|
222
|
-
// prefer first lineto to allow implicit closing linetos by "Z"
|
|
223
|
-
removeFinalLineto = true;
|
|
224
|
-
startToTop = false;
|
|
225
|
-
addExtremes = false;
|
|
226
|
-
debug = false;
|
|
227
|
-
|
|
228
|
-
pathDataSimple = cleanUpPathData(pathDataSimple, addExtremes, removeFinalLineto, startToTop, debug)
|
|
229
|
-
console.log('pathDataSimple post', pathDataSimple);
|
|
230
|
-
|
|
231
|
-
return pathDataSimple;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|