svg-path-simplify 0.4.2 → 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 +21 -0
- package/README.md +7 -4
- package/dist/svg-path-simplify.esm.js +3593 -1279
- package/dist/svg-path-simplify.esm.min.js +2 -2
- package/dist/svg-path-simplify.js +3594 -1278
- package/dist/svg-path-simplify.min.js +2 -2
- package/dist/svg-path-simplify.pathdata.esm.js +1017 -538
- package/dist/svg-path-simplify.pathdata.esm.min.js +2 -2
- package/dist/svg-path-simplify.poly.cjs +9 -8
- package/docs/privacy-webapp.md +24 -0
- package/index.html +331 -152
- package/package.json +1 -1
- package/src/constants.js +4 -0
- package/src/css_parse.js +317 -0
- package/src/detect_input.js +76 -28
- package/src/index.js +8 -0
- package/src/pathData_simplify_cubic.js +26 -16
- package/src/pathData_simplify_harmonize_cpts.js +77 -1
- package/src/pathData_simplify_revertToquadratics.js +0 -1
- package/src/pathSimplify-main.js +304 -276
- package/src/pathSimplify-only-pathdata.js +7 -2
- package/src/pathSimplify-presets.js +254 -0
- package/src/poly-fit-curve-schneider.js +14 -7
- package/src/simplify_poly_RC.js +102 -0
- package/src/simplify_poly_RDP.js +109 -1
- package/src/simplify_poly_radial_distance.js +3 -3
- package/src/string_helpers.js +130 -4
- package/src/svg-getAttributes.js +4 -2
- package/src/svgii/convert_units.js +1 -1
- package/src/svgii/geometry.js +322 -5
- package/src/svgii/geometry_bbox_element.js +1 -1
- package/src/svgii/geometry_deduceRadius.js +116 -27
- package/src/svgii/geometry_length.js +253 -0
- package/src/svgii/pathData_analyze.js +18 -0
- package/src/svgii/pathData_convert.js +193 -89
- package/src/svgii/pathData_fix_directions.js +12 -14
- package/src/svgii/pathData_fromPoly.js +3 -3
- package/src/svgii/pathData_getLength.js +86 -0
- package/src/svgii/pathData_parse.js +2 -0
- package/src/svgii/pathData_parse_els.js +66 -68
- 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/pathData_split_to_groups.js +168 -0
- package/src/svgii/pathData_stringify.js +26 -64
- package/src/svgii/pathData_toPolygon.js +3 -4
- package/src/svgii/poly_analyze.js +61 -0
- package/src/svgii/poly_normalize.js +11 -2
- package/src/svgii/poly_to_pathdata.js +85 -24
- package/src/svgii/rounding.js +80 -78
- package/src/svgii/svg_cleanup.js +421 -619
- package/src/svgii/svg_cleanup_convertPathLength.js +39 -0
- package/src/svgii/svg_cleanup_general_svg_atts.js +97 -0
- package/src/svgii/svg_cleanup_normalize_transforms.js +83 -0
- package/src/svgii/svg_cleanup_remove_els_and_atts.js +77 -0
- package/src/svgii/svg_cleanup_ungroup.js +36 -0
- package/src/svgii/svg_el_parse_style_props.js +72 -47
- package/src/svgii/svg_getElementLength.js +67 -0
- package/src/svgii/svg_validate.js +220 -0
- package/tests/testSVG.js +14 -1
- package/src/svgii/pathData_refine_round.js +0 -222
package/src/svgii/rounding.js
CHANGED
|
@@ -8,6 +8,42 @@ import { getDistAv, getDistManhattan } from "./geometry";
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* round path data
|
|
13
|
+
* either by explicit decimal value or
|
|
14
|
+
* based on suggested accuracy in path data
|
|
15
|
+
*/
|
|
16
|
+
export function roundPathData(pathData, decimalsGlobal = -1) {
|
|
17
|
+
|
|
18
|
+
if (decimalsGlobal < 0) return pathData;
|
|
19
|
+
|
|
20
|
+
let len = pathData.length;
|
|
21
|
+
let decimals = decimalsGlobal
|
|
22
|
+
let decimalsArc = decimals < 3 ? decimals+2 : decimals
|
|
23
|
+
//decimalsArc = decimals
|
|
24
|
+
//console.log(decimalsArc);
|
|
25
|
+
|
|
26
|
+
for (let c = 0; c < len; c++) {
|
|
27
|
+
let com = pathData[c];
|
|
28
|
+
let { type, values } = com
|
|
29
|
+
let valLen = values.length;
|
|
30
|
+
if (!valLen) continue
|
|
31
|
+
|
|
32
|
+
let isArc = type.toLowerCase() === 'a'
|
|
33
|
+
|
|
34
|
+
for (let v = 0; v < valLen; v++) {
|
|
35
|
+
// allow higher accuracy for arc radii (... it's always arcs)
|
|
36
|
+
pathData[c].values[v] = isArc && v < 2 ? roundTo(values[v], decimalsArc) : roundTo(values[v], decimals);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
//console.log(pathData);
|
|
41
|
+
return pathData;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
11
47
|
export function detectAccuracyPoly(pts) {
|
|
12
48
|
|
|
13
49
|
let minDim = Infinity
|
|
@@ -20,7 +56,7 @@ export function detectAccuracyPoly(pts) {
|
|
|
20
56
|
let { p0 = null, p = null, dimA = 0 } = pt;
|
|
21
57
|
|
|
22
58
|
// use existing averave dimension value or calculate
|
|
23
|
-
if (
|
|
59
|
+
if (p && p0) {
|
|
24
60
|
dimA = dimA ? dimA : getDistManhattan(p0, p);
|
|
25
61
|
|
|
26
62
|
if (dimA) dims.push(dimA);
|
|
@@ -72,7 +108,7 @@ export function detectAccuracy(pathData) {
|
|
|
72
108
|
let dim_min = dims.sort()
|
|
73
109
|
//console.log('dim_min', dim_min);
|
|
74
110
|
|
|
75
|
-
let sliceIdx = Math.ceil(dim_min.length /
|
|
111
|
+
let sliceIdx = Math.ceil(dim_min.length / 6);
|
|
76
112
|
dim_min = dim_min.slice(0, sliceIdx);
|
|
77
113
|
let minVal = dim_min.reduce((a, b) => a + b, 0) / sliceIdx;
|
|
78
114
|
|
|
@@ -84,10 +120,37 @@ export function detectAccuracy(pathData) {
|
|
|
84
120
|
|
|
85
121
|
}
|
|
86
122
|
|
|
123
|
+
/**
|
|
124
|
+
* rounding helper
|
|
125
|
+
* allows for quantized rounding
|
|
126
|
+
* e.g 0.5 decimals s
|
|
127
|
+
*/
|
|
128
|
+
export function roundTo(num = 0, decimals = 3) {
|
|
129
|
+
if (decimals < 0) return num;
|
|
130
|
+
// Normal integer rounding
|
|
131
|
+
if (!decimals) return Math.round(num);
|
|
132
|
+
|
|
133
|
+
// stepped rounding
|
|
134
|
+
let intPart = Math.floor(decimals);
|
|
135
|
+
//let fracPart = decimals.toString().split('.');
|
|
136
|
+
//fracPart = fracPart[1] ? +fracPart[1] : 0
|
|
137
|
+
|
|
138
|
+
if (intPart !== decimals) {
|
|
139
|
+
let f = +(decimals - intPart).toFixed(2)
|
|
140
|
+
f = f > 0.5 ? (Math.floor((f) / 0.5) * 0.5) : f;
|
|
141
|
+
//console.log('fracPart', f);
|
|
142
|
+
let step = 10 ** -intPart * f;
|
|
143
|
+
return +(Math.round(num / step) * step).toFixed(8);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
let factor = 10 ** decimals;
|
|
147
|
+
return Math.round(num * factor) / factor;
|
|
148
|
+
}
|
|
87
149
|
|
|
88
150
|
|
|
89
151
|
|
|
90
|
-
export function
|
|
152
|
+
export function roundTo__(num = 0, decimals = 3) {
|
|
153
|
+
if (decimals <= -1) return num;
|
|
91
154
|
if (!decimals) return Math.round(num);
|
|
92
155
|
let factor = 10 ** decimals;
|
|
93
156
|
return Math.round(num * factor) / factor;
|
|
@@ -98,85 +161,24 @@ export function roundTo(num = 0, decimals = 3) {
|
|
|
98
161
|
* floating point accuracy
|
|
99
162
|
* based on numeric value
|
|
100
163
|
*/
|
|
101
|
-
export function autoRound(val, integerThresh =
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
//console.log('val', val);
|
|
105
|
-
|
|
106
|
-
if(val>integerThresh){
|
|
107
|
-
decimals=0
|
|
108
|
-
}
|
|
109
|
-
else if(val>5){
|
|
110
|
-
decimals=1
|
|
111
|
-
}else{
|
|
112
|
-
decimals=Math.ceil(integerThresh/val).toString().length
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
let factor = 10 ** decimals;
|
|
116
|
-
return Math.round(val * factor) / factor;
|
|
117
|
-
}
|
|
164
|
+
export function autoRound(val, integerThresh = 50) {
|
|
165
|
+
let decimals = 8;
|
|
118
166
|
|
|
167
|
+
if (val > integerThresh * 2) {
|
|
168
|
+
decimals = 0
|
|
169
|
+
}
|
|
170
|
+
else if (val > integerThresh) {
|
|
171
|
+
decimals = 1
|
|
172
|
+
} else {
|
|
173
|
+
decimals = Math.ceil(500 / val).toString().length
|
|
174
|
+
//console.log('decimals small', val, decimals);
|
|
175
|
+
}
|
|
119
176
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
* round path data
|
|
124
|
-
* either by explicit decimal value or
|
|
125
|
-
* based on suggested accuracy in path data
|
|
126
|
-
*/
|
|
127
|
-
export function roundPathData(pathData, decimalsGlobal = -1) {
|
|
128
|
-
|
|
129
|
-
if (decimalsGlobal < 0) return pathData;
|
|
130
|
-
|
|
131
|
-
let len = pathData.length;
|
|
132
|
-
//let decimals = pathData[0].decimals ? pathData[0].decimals+1 : decimalsGlobal
|
|
133
|
-
let decimals = decimalsGlobal
|
|
134
|
-
//decimals = decimalsGlobal;
|
|
135
|
-
//console.log('decimals subpath', decimals, pathData[0].decimals, 'decimalsGlobal', decimalsGlobal);
|
|
136
|
-
|
|
137
|
-
for (let c = 0; c < len; c++) {
|
|
138
|
-
let com = pathData[c];
|
|
139
|
-
let {values} = com
|
|
140
|
-
//let values = pathData[c].values
|
|
141
|
-
let valLen = values.length;
|
|
142
|
-
if (!valLen) continue
|
|
143
|
-
|
|
144
|
-
for (let v = 0; v < valLen; v++) {
|
|
145
|
-
pathData[c].values[v] = roundTo(values[v], decimals);
|
|
146
|
-
}
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
//console.log(pathData);
|
|
150
|
-
return pathData;
|
|
177
|
+
//console.log(val, decimals);
|
|
178
|
+
let factor = 10 ** decimals;
|
|
179
|
+
return Math.round(val * factor) / factor;
|
|
151
180
|
}
|
|
152
181
|
|
|
153
182
|
|
|
154
|
-
export function roundPathData_(pathData, decimals = -1) {
|
|
155
|
-
|
|
156
|
-
if (decimals < 0) return pathData;
|
|
157
|
-
|
|
158
|
-
let len = pathData.length;
|
|
159
|
-
let c = 0;
|
|
160
|
-
while (c < len) {
|
|
161
|
-
|
|
162
|
-
//let com = pathData[c];
|
|
163
|
-
let values = pathData[c].values
|
|
164
|
-
let valLen = values.length;
|
|
165
|
-
|
|
166
|
-
// Z commands have no values
|
|
167
|
-
if (!valLen) {
|
|
168
|
-
c++; continue
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
let v = 0;
|
|
172
|
-
while (v < valLen) {
|
|
173
|
-
pathData[c].values[v] = roundTo(values[v], decimals);
|
|
174
|
-
v++
|
|
175
|
-
}
|
|
176
|
-
c++
|
|
177
183
|
|
|
178
|
-
};
|
|
179
184
|
|
|
180
|
-
//console.log(pathData);
|
|
181
|
-
return pathData;
|
|
182
|
-
}
|