svg-path-simplify 0.1.2 → 0.2.1
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 +10 -0
- package/dist/svg-path-simplify.esm.js +3935 -1441
- package/dist/svg-path-simplify.esm.min.js +13 -1
- package/dist/svg-path-simplify.js +3953 -1459
- package/dist/svg-path-simplify.min.js +13 -1
- package/dist/svg-path-simplify.min.js.gz +0 -0
- package/dist/svg-path-simplify.poly.cjs +0 -1
- package/index.html +69 -31
- package/package.json +5 -9
- package/src/constants.js +3 -0
- package/src/index-node.js +0 -1
- package/src/index-poly.js +0 -1
- package/src/index.js +26 -0
- package/src/pathData_simplify_cubic.js +75 -46
- package/src/pathData_simplify_cubicsToArcs.js +566 -0
- package/src/pathData_simplify_harmonize_cpts.js +170 -0
- package/src/pathData_simplify_revertToquadratics.js +21 -0
- package/src/pathSimplify-main.js +274 -61
- package/src/poly-fit-curve-schneider.js +570 -0
- package/src/simplify_poly_RDP.js +146 -0
- package/src/simplify_poly_radial_distance.js +100 -0
- package/src/svg_getViewbox.js +28 -15
- package/src/svgii/geometry.js +389 -63
- package/src/svgii/geometry_area.js +2 -1
- package/src/svgii/pathData_analyze.js +259 -212
- package/src/svgii/pathData_convert.js +91 -663
- package/src/svgii/pathData_fromPoly.js +12 -0
- package/src/svgii/pathData_parse.js +90 -89
- package/src/svgii/pathData_parse_els.js +3 -0
- package/src/svgii/pathData_parse_fontello.js +449 -0
- package/src/svgii/pathData_remove_collinear.js +44 -37
- package/src/svgii/pathData_reorder.js +2 -1
- package/src/svgii/pathData_simplify_redraw.js +343 -0
- package/src/svgii/pathData_simplify_refineCorners.js +18 -9
- package/src/svgii/pathData_simplify_refineExtremes.js +19 -78
- package/src/svgii/pathData_split.js +42 -45
- package/src/svgii/pathData_toPolygon.js +130 -4
- package/src/svgii/pathData_transform_scale.js +51 -0
- package/src/svgii/poly_analyze.js +470 -14
- package/src/svgii/poly_to_pathdata.js +224 -19
- package/src/svgii/rounding.js +55 -112
- package/src/svgii/svg_cleanup.js +13 -1
- package/src/svgii/visualize.js +8 -3
- package/{debug.cjs → tests/debug.cjs} +3 -0
- package/{testSVG.js → tests/testSVG.js} +1 -1
- /package/{test.js → tests/test.js} +0 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* radialDistance simplification
|
|
4
|
+
* sloppy but fast
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { getDistManhattan, getSquareDistance, reducePoints } from "./svgii/geometry";
|
|
8
|
+
import { getPolyBBox } from "./svgii/geometry_bbox";
|
|
9
|
+
import { renderPoint } from "./svgii/visualize";
|
|
10
|
+
|
|
11
|
+
export function simplifyRD(pts, {
|
|
12
|
+
quality = 0.9,
|
|
13
|
+
width = 0,
|
|
14
|
+
height = 0,
|
|
15
|
+
absolute = false,
|
|
16
|
+
// use square or manhattan distances
|
|
17
|
+
manhattan = false,
|
|
18
|
+
exclude = []
|
|
19
|
+
} = {}) {
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* switch between absolute or
|
|
23
|
+
* quality based relative thresholds
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
if (typeof quality === 'string') {
|
|
27
|
+
let value = parseFloat(quality);
|
|
28
|
+
absolute = true;
|
|
29
|
+
quality = value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// nothing to do - exit
|
|
33
|
+
if (pts.length < 4 || (!absolute && quality) >= 1) return pts;
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
// convert quality to squaredistance tolerance
|
|
37
|
+
let tolerance = quality;
|
|
38
|
+
|
|
39
|
+
if (!absolute) {
|
|
40
|
+
|
|
41
|
+
// quality to tolerance
|
|
42
|
+
tolerance = 1 - quality;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* approximate dimensions
|
|
46
|
+
* adjust tolerance for
|
|
47
|
+
* very small polygons e.g geodata
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
if (!width && !height) {
|
|
51
|
+
let polyS = reducePoints(pts, 12);
|
|
52
|
+
({ width, height } = getPolyBBox(polyS));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!manhattan) {
|
|
56
|
+
// average side lengths
|
|
57
|
+
let dimAvg = (width + height) / 2;
|
|
58
|
+
let scale = dimAvg / 25;
|
|
59
|
+
tolerance = (tolerance * (scale)) ** 2
|
|
60
|
+
} else {
|
|
61
|
+
// use manhattan
|
|
62
|
+
tolerance = (width + height) * 0.05 * (1 - quality)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let p0 = pts[0];
|
|
68
|
+
let pt;
|
|
69
|
+
|
|
70
|
+
// new simplified point array
|
|
71
|
+
let ptsSmp = [p0];
|
|
72
|
+
let l = pts.length
|
|
73
|
+
let lenExclude = exclude.length;
|
|
74
|
+
let dist = 0
|
|
75
|
+
|
|
76
|
+
for (let i = 1; i < l; i++) {
|
|
77
|
+
pt = pts[i];
|
|
78
|
+
|
|
79
|
+
// skip
|
|
80
|
+
dist = manhattan ? getDistManhattan(p0, pt) : getSquareDistance(p0, pt);
|
|
81
|
+
|
|
82
|
+
if ( dist < tolerance && (!lenExclude || !exclude.includes(i)) ) {
|
|
83
|
+
p0 = pt;
|
|
84
|
+
continue
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
ptsSmp.push(pt);
|
|
88
|
+
p0 = pt;
|
|
89
|
+
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// add last point - if not coinciding with first point
|
|
93
|
+
if (p0.x !== pt.x && p0.y !== pt.y) {
|
|
94
|
+
ptsSmp.push(pt);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
//console.log('ptsSmp', ptsSmp, l);
|
|
98
|
+
return ptsSmp;
|
|
99
|
+
|
|
100
|
+
}
|
package/src/svg_getViewbox.js
CHANGED
|
@@ -4,29 +4,42 @@
|
|
|
4
4
|
* width and height attributes
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
export function getViewBox(svg = null,
|
|
7
|
+
export function getViewBox(svg = null, decimals = -1) {
|
|
8
|
+
|
|
9
|
+
const getUnit=(val)=>{
|
|
10
|
+
return val && isNaN(val) ? val.match(/[^\d|.]+/g)[0] : '';
|
|
11
|
+
}
|
|
8
12
|
|
|
9
13
|
// browser default
|
|
10
|
-
if (!svg) return
|
|
14
|
+
if (!svg) return false
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
let hasWidth = svg.hasAttribute('width')
|
|
18
|
+
let hasHeight = svg.hasAttribute('height')
|
|
19
|
+
let hasViewBox = svg.hasAttribute('viewBox')
|
|
20
|
+
|
|
11
21
|
|
|
12
|
-
let
|
|
22
|
+
let widthAtt = hasWidth ? svg.getAttribute('width') : 0;
|
|
23
|
+
let heightAtt = hasHeight ? svg.getAttribute('height') : 0;
|
|
13
24
|
|
|
14
|
-
// the baseVal API method also converts physical units to pixels/user-units
|
|
15
|
-
let w = svg.hasAttribute('width') ? svg.width.baseVal.value : parseFloat(style.width) || 300;
|
|
16
|
-
let h = svg.hasAttribute('height') ? svg.height.baseVal.value : parseFloat(style.height) || 150;
|
|
17
25
|
|
|
18
|
-
let viewBox = svg.getAttribute('viewBox') ? svg.viewBox.baseVal : { x: 0, y: 0, width: w, height: h };
|
|
19
26
|
|
|
20
|
-
|
|
21
|
-
let
|
|
22
|
-
viewBox = { x, y, width, height };
|
|
27
|
+
let widthUnit = hasWidth ? getUnit(widthAtt) : false;
|
|
28
|
+
let heightUnit = hasHeight ? getUnit(widthAtt) : false
|
|
23
29
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
let w = widthAtt ? (!widthAtt.includes('%') ? parseFloat(widthAtt) : 0 ) : 300
|
|
31
|
+
let h = heightAtt ? (!heightAtt.includes('%') ? parseFloat(heightAtt) : 0 ) : 150
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
let viewBoxVals = hasViewBox ? svg.getAttribute('viewBox').split(/,| /).filter(Boolean).map(Number) : [0, 0, w, h];
|
|
35
|
+
|
|
36
|
+
// round
|
|
37
|
+
if (decimals>-1) {
|
|
38
|
+
[w, h] = [w, h].map(val=>+val.toFixed(decimals))
|
|
39
|
+
viewBoxVals = viewBoxVals.map(val=>+val.toFixed(decimals))
|
|
29
40
|
}
|
|
30
41
|
|
|
42
|
+
let viewBox = { x:viewBoxVals[0] , y:viewBoxVals[1], width:viewBoxVals[2], height:viewBoxVals[3], w, h, hasViewBox, hasWidth, hasHeight, widthUnit, heightUnit };
|
|
43
|
+
|
|
31
44
|
return viewBox
|
|
32
45
|
}
|