svg-path-simplify 0.4.4 → 0.4.6
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 +19 -0
- package/README.md +9 -1
- package/dist/svg-path-simplify.esm.js +1649 -1205
- package/dist/svg-path-simplify.esm.min.js +2 -2
- package/dist/svg-path-simplify.js +1649 -1204
- package/dist/svg-path-simplify.min.js +2 -2
- package/dist/svg-path-simplify.pathdata.esm.js +301 -637
- package/dist/svg-path-simplify.pathdata.esm.min.js +2 -2
- package/dist/svg-path-simplify.poly.cjs +8 -9
- package/drawing.svg +62 -0
- package/index.html +13 -8
- package/package.json +1 -1
- package/src/index.js +3 -1
- package/src/pathData_get_intersections.js +777 -0
- package/src/pathData_offset.js +201 -0
- package/src/pathData_simplify_cubic.js +19 -21
- package/src/pathData_simplify_cubic_extrapolate.js +147 -8
- package/src/pathData_simplify_cubicsToArcs.js +65 -21
- package/src/pathSimplify-main.js +267 -64
- package/src/pathSimplify-presets.js +1 -1
- package/src/poly-fit-curve-schneider.js +171 -132
- package/src/poly-fit-curve-schneider_check_bulge.js +131 -0
- package/src/poly-offset.js +133 -0
- package/src/simplify_poly_RC.js +29 -7
- package/src/svg-getAttributes.js +1 -1
- package/src/svgii/geometry.js +130 -27
- package/src/svgii/geometry_bbox.js +1 -1
- package/src/svgii/geometry_length.js +1 -1
- package/src/svgii/pathData_analyze.js +6 -28
- package/src/svgii/pathData_convert.js +63 -15
- package/src/svgii/pathData_remove_collinear.js +32 -36
- package/src/svgii/pathData_reorder.js +3 -2
- package/src/svgii/pathData_simplify_refineExtremes.js +1 -3
- package/src/svgii/pathData_stringify.js +132 -6
- package/src/svgii/pathData_toPolygon.js +33 -23
- package/src/svgii/poly_analyze.js +272 -125
- package/src/svgii/poly_analyze_cleanup.js +311 -0
- package/src/svgii/poly_analyze_getTangents.js +125 -0
- package/src/svgii/poly_analyze_get_chunks.js +3 -1
- package/src/svgii/poly_normalize.js +21 -8
- package/src/svgii/poly_to_pathdata.js +477 -8
- package/src/svgii/rounding.js +35 -14
- package/src/svgii/svg_cleanup.js +44 -51
- package/src/svgii/svg_cleanup_convertPathLength.js +5 -0
- package/src/svgii/svg_cleanup_normalize_transforms.js +2 -2
- package/src/svgii/visualize.js +33 -2
- package/v/0.4.5/index.html +1040 -0
- package/v/0.4.5/svg-path-simplify.js +13227 -0
package/src/pathSimplify-main.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { detectInputType } from './detect_input';
|
|
2
2
|
import { simplifyPathDataCubic } from './pathData_simplify_cubic';
|
|
3
3
|
import { getDistManhattan, getDistance, getPathDataVertices, getSquareDistance, interpolate, pointAtT, reducePoints, svgArcToCenterParam, toParametricAngle } from './svgii/geometry';
|
|
4
|
-
import { getPolyBBox } from './svgii/geometry_bbox';
|
|
4
|
+
import { getPathDataBBox, getPolyBBox } from './svgii/geometry_bbox';
|
|
5
5
|
import { analyzePathData, getPathDataVerbose } from './svgii/pathData_analyze';
|
|
6
6
|
import { normalizePathData, parsePathDataNormalized, convertPathData } from './svgii/pathData_convert';
|
|
7
7
|
import { shapeElToPath } from './svgii/pathData_parse_els';
|
|
@@ -12,7 +12,7 @@ import { optimizeClosePath, pathDataToTopLeft } from './svgii/pathData_reorder';
|
|
|
12
12
|
import { reversePathData } from './svgii/pathData_reverse';
|
|
13
13
|
import { addExtremePoints, splitSubpaths } from './svgii/pathData_split';
|
|
14
14
|
import { pathDataToD } from './svgii/pathData_stringify';
|
|
15
|
-
import { detectAccuracy, roundPathData, roundTo } from './svgii/rounding';
|
|
15
|
+
import { detectAccuracy, roundPathData, roundPoly, roundTo } from './svgii/rounding';
|
|
16
16
|
import { refineAdjacentExtremes } from './svgii/pathData_simplify_refineExtremes';
|
|
17
17
|
import { cleanUpSVG, removeEmptySVGEls } from './svgii/svg_cleanup';
|
|
18
18
|
import { refineRoundedCorners } from './svgii/pathData_simplify_refineCorners';
|
|
@@ -32,16 +32,138 @@ import { normalizePoly, polyPtsToArray } from './svgii/poly_normalize';
|
|
|
32
32
|
import { simplifyPolyRD } from './simplify_poly_radial_distance';
|
|
33
33
|
import { simplifyPolyRDP, simplifyPolyRDP__, simplifyRDP_rel } from './simplify_poly_RDP';
|
|
34
34
|
import { getEllipseLengthLG, getLegendreGaussValues, getLength, waArr_global } from './svgii/geometry_length';
|
|
35
|
-
import { deg2rad, dummySVG } from './constants';
|
|
35
|
+
import { deg2rad, dummySVG, svgNs } from './constants';
|
|
36
36
|
import { getPathDataLength } from './svgii/pathData_getLength';
|
|
37
37
|
import { stringifySVG } from './string_helpers';
|
|
38
38
|
import { presetSettings, settingsDefaults } from './pathSimplify-presets';
|
|
39
39
|
import { splitCompundGroups } from './svgii/pathData_split_to_groups';
|
|
40
|
+
import { removeCoincidingVertices } from './simplify_poly_RC';
|
|
41
|
+
//import { offsetPathData } from './pathData_offset';
|
|
40
42
|
//import { getPolyChunks } from "./svgii/poly_analyze_get_chunks";
|
|
41
43
|
|
|
42
44
|
|
|
43
45
|
//import { installDOMPolyfills } from './dom-polyfill';
|
|
44
46
|
|
|
47
|
+
export function SlickVGObj(props = {}) {
|
|
48
|
+
//console.log(props);
|
|
49
|
+
Object.assign(this, props)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
SlickVGObj.prototype.getD = function () {
|
|
53
|
+
let d = this.d;
|
|
54
|
+
return d;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
SlickVGObj.prototype.getSvg = function () {
|
|
58
|
+
let svg = this.svg;
|
|
59
|
+
|
|
60
|
+
if (!svg) {
|
|
61
|
+
let xArr = [];
|
|
62
|
+
let yArr = [];
|
|
63
|
+
let d = this.d;
|
|
64
|
+
let pathDataPlusArr = this.pathDataPlusArr || []
|
|
65
|
+
pathDataPlusArr.forEach(path => {
|
|
66
|
+
path.forEach(sub => {
|
|
67
|
+
let { pathData } = sub;
|
|
68
|
+
let bb = getPathDataBBox(pathData);
|
|
69
|
+
let { x, y, right, bottom } = bb
|
|
70
|
+
xArr.push(x, right);
|
|
71
|
+
yArr.push(y, bottom);
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
let x = Math.min(...xArr)
|
|
76
|
+
let right = Math.max(...xArr)
|
|
77
|
+
let y = Math.min(...yArr)
|
|
78
|
+
let bottom = Math.max(...yArr)
|
|
79
|
+
let width = right - x
|
|
80
|
+
let height = bottom - y
|
|
81
|
+
|
|
82
|
+
svg = `<svg xmlns="${svgNs}" viewBox="${[x, y, width, height].join(' ')}"><path d="${d}"/></svg>`
|
|
83
|
+
|
|
84
|
+
}
|
|
85
|
+
return svg;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* retrieve poly
|
|
90
|
+
* formats: points, array, string, pathData, d,
|
|
91
|
+
*/
|
|
92
|
+
SlickVGObj.prototype.getPoly = function (options = {}
|
|
93
|
+
) {
|
|
94
|
+
|
|
95
|
+
options = {
|
|
96
|
+
...{
|
|
97
|
+
precisionPoly: 1,
|
|
98
|
+
simplifyRDP: 0,
|
|
99
|
+
simplifyRD: 0,
|
|
100
|
+
autoAccuracy: true,
|
|
101
|
+
decimals: 3,
|
|
102
|
+
format: 'object'
|
|
103
|
+
},
|
|
104
|
+
...options
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
let { precisionPoly, simplifyRDP, simplifyRD, autoAccuracy, decimals, format } = options;
|
|
108
|
+
|
|
109
|
+
let polyFormat = format;
|
|
110
|
+
|
|
111
|
+
//console.log(precisionPoly, simplifyRDP, simplifyRD, autoAccuracy, decimals);
|
|
112
|
+
|
|
113
|
+
let polys = this.polys;
|
|
114
|
+
if (!polys.length) {
|
|
115
|
+
let pathDataPlusArr = this.pathDataPlusArr || []
|
|
116
|
+
let poly = []
|
|
117
|
+
let polyPaths = []
|
|
118
|
+
let dPoly = ''
|
|
119
|
+
pathDataPlusArr.forEach(path => {
|
|
120
|
+
path.forEach(sub => {
|
|
121
|
+
let { pathData } = sub;
|
|
122
|
+
|
|
123
|
+
let polyData = pathDataToPolygonOpt(pathData, {
|
|
124
|
+
precisionPoly,
|
|
125
|
+
autoAccuracy,
|
|
126
|
+
decimals,
|
|
127
|
+
simplifyRD,
|
|
128
|
+
simplifyRDP,
|
|
129
|
+
polyFormat
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
dPoly += polyData.d;
|
|
133
|
+
poly.push(polyData.poly)
|
|
134
|
+
polyPaths.push(polyData.pathData)
|
|
135
|
+
|
|
136
|
+
})
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
if (polyFormat === 'object' || polyFormat === 'array' || polyFormat === 'string') {
|
|
140
|
+
polys = poly;
|
|
141
|
+
}
|
|
142
|
+
else if (polyFormat === 'pathData') {
|
|
143
|
+
polys = polyPaths.flat();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
else if (polyFormat === 'd') {
|
|
147
|
+
polys = dPoly
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
}
|
|
151
|
+
return polys;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
/*
|
|
157
|
+
export function PathLengthObject(props = {}) {
|
|
158
|
+
Object.assign(this, props);
|
|
159
|
+
}
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
export function SlickVG(input = '', settings = {}) {
|
|
163
|
+
settings.getObject = true;
|
|
164
|
+
return svgPathSimplify(input, settings)
|
|
165
|
+
}
|
|
166
|
+
|
|
45
167
|
export function svgPathSimplify(input = '', settings = {}) {
|
|
46
168
|
|
|
47
169
|
let preset = settings['preset'] !== undefined && settings['preset'] ? settings['preset'] : null;
|
|
@@ -55,7 +177,7 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
55
177
|
}
|
|
56
178
|
|
|
57
179
|
|
|
58
|
-
let { getObject = false, removeComments, removeOffCanvas, unGroup, mergePaths, removeElements, removeDimensions, removeIds, removeClassNames, omitNamespace, cleanUpStrokes, addViewBox, addDimensions, removePrologue, removeHidden, removeUnused, cleanupDefs, cleanupClip, cleanupSVGAtts, removeNameSpaced, removeNameSpacedAtts, attributesToGroup, minifyRgbColors, stylesToAttributes, fixHref, legacyHref, allowMeta, allowDataAtts, allowAriaAtts, removeSVGAttributes, removeElAttributes, shapesToPaths, shapeConvert, convertShapes, simplifyBezier, optimizeOrder, autoClose, removeZeroLength, refineClosing, removeColinear, flatBezierToLinetos, revertToQuadratics, refineExtremes, simplifyCorners, fixDirections, keepExtremes, keepCorners, keepInflections, addExtremes, reversePath, toAbsolute, toRelative, toMixed, toShorthands, toLonghands, quadraticToCubic, arcToCubic, cubicToArc, lineToCubic, decimals, autoAccuracy, minifyD, tolerance, toPolygon, smoothPoly, polyFormat, precisionPoly, simplifyRD, simplifyRDP, harmonizeCpts, removeOrphanSubpaths, simplifyRound, simplifyQuadraticCorners, scale, scaleTo, crop, alignToOrigin, convertTransforms, keepSmaller, splitCompound, convertPathLength, toAbsoluteUnits } = settings;
|
|
180
|
+
let { getObject = false, removeComments, removeOffCanvas, unGroup, mergePaths, removeElements, removeDimensions, removeIds, removeClassNames, omitNamespace, cleanUpStrokes, addViewBox, addDimensions, removePrologue, removeHidden, removeUnused, cleanupDefs, cleanupClip, cleanupSVGAtts, removeNameSpaced, removeNameSpacedAtts, attributesToGroup, minifyRgbColors, stylesToAttributes, fixHref, legacyHref, allowMeta, allowDataAtts, allowAriaAtts, removeSVGAttributes, removeElAttributes, shapesToPaths, shapeConvert, convertShapes, simplifyBezier, optimizeOrder, autoClose, removeZeroLength, refineClosing, removeColinear, flatBezierToLinetos, revertToQuadratics, refineExtremes, simplifyCorners, fixDirections, keepExtremes, keepCorners, keepInflections, addExtremes, reversePath, toAbsolute, toRelative, toMixed, toShorthands, toLonghands, quadraticToCubic, arcToCubic, cubicToArc, lineToCubic, decimals, autoAccuracy, minifyD, tolerance, toPolygon, smoothPoly, polyFormat, isClosed, precisionPoly, simplifyRD, simplifyRDP, harmonizeCpts, removeOrphanSubpaths, simplifyRound, simplifyQuadraticCorners, scale, scaleTo, crop, alignToOrigin, convertTransforms, keepSmaller, splitCompound, convertPathLength, toAbsoluteUnits } = settings;
|
|
59
181
|
|
|
60
182
|
//toAbsolute = !toRelative;
|
|
61
183
|
|
|
@@ -68,6 +190,10 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
68
190
|
settings.convertTransforms = true
|
|
69
191
|
}
|
|
70
192
|
|
|
193
|
+
if (shapeConvert === 'toShapes' || shapeConvert === 'shapesToPaths') {
|
|
194
|
+
keepSmaller = false;
|
|
195
|
+
}
|
|
196
|
+
|
|
71
197
|
|
|
72
198
|
/**
|
|
73
199
|
* intercept
|
|
@@ -91,11 +217,11 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
91
217
|
original: 0,
|
|
92
218
|
new: 0,
|
|
93
219
|
saved: 0,
|
|
94
|
-
svgSize:0,
|
|
95
|
-
svgSizeOpt:0,
|
|
96
|
-
compression:0,
|
|
97
|
-
decimals:0,
|
|
98
|
-
invalid:true
|
|
220
|
+
svgSize: 0,
|
|
221
|
+
svgSizeOpt: 0,
|
|
222
|
+
compression: 0,
|
|
223
|
+
decimals: 0,
|
|
224
|
+
invalid: true
|
|
99
225
|
}
|
|
100
226
|
|
|
101
227
|
return { svg: dummySVG, d: '', polys: [], report, pathDataPlusArr: [], pathDataPlusArr_global: [], inputType: 'invalid', dOriginal: '' };
|
|
@@ -240,14 +366,10 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
240
366
|
convertShapes = ['rect', 'polygon', 'polyline', 'line', 'circle', 'ellipse']
|
|
241
367
|
}
|
|
242
368
|
|
|
243
|
-
//console.log('shapesToPaths', shapesToPaths, 'shapeConvert', shapeConvert, convert_rects, convert_ellipses, convert_poly);
|
|
244
369
|
|
|
245
370
|
// sanitize SVG - clone/decouple settings
|
|
246
371
|
let svgPropObject = cleanUpSVG(input, JSON.parse(JSON.stringify(settings)));
|
|
247
372
|
|
|
248
|
-
//console.log('settings', settings);
|
|
249
|
-
//console.log('svgPropObject', svgPropObject);
|
|
250
|
-
|
|
251
373
|
let { svgElProps } = svgPropObject
|
|
252
374
|
svg = svgPropObject.svg;
|
|
253
375
|
//console.log(svgPropObject);
|
|
@@ -281,8 +403,11 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
281
403
|
toRelative,
|
|
282
404
|
toMixed,
|
|
283
405
|
toShorthands,
|
|
406
|
+
// return true arc radii or minified/parametrized
|
|
407
|
+
optimizeArcs: minifyD < 1,
|
|
284
408
|
decimals,
|
|
285
409
|
}
|
|
410
|
+
|
|
286
411
|
//console.log('pathOptions', pathOptions);
|
|
287
412
|
|
|
288
413
|
let comCount = 0
|
|
@@ -297,6 +422,12 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
297
422
|
let dN = ''
|
|
298
423
|
let isPoly = false;
|
|
299
424
|
|
|
425
|
+
// disable reordering for elements with stroke dash-array
|
|
426
|
+
if (el && (el.hasAttribute('stroke-dasharray') || el.hasAttribute('stroke-dashoffset'))) {
|
|
427
|
+
optimizeOrder = false;
|
|
428
|
+
//optimizeClosePath=false;
|
|
429
|
+
}
|
|
430
|
+
|
|
300
431
|
// if polygon we already heave absolute coordinates
|
|
301
432
|
//let isPolyPath = !mode && isPoly && Array.isArray(d)
|
|
302
433
|
//let pathData = !isPolyPath ? parsePathDataNormalized(d, { quadraticToCubic, arcToCubic }) : d;
|
|
@@ -360,8 +491,9 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
360
491
|
let pathDataSub = subPathArr[i];
|
|
361
492
|
let poly = []
|
|
362
493
|
let coms = Array.from(new Set(pathDataSub.map(com => com.type))).join('')
|
|
363
|
-
isPoly = !(/[acqts]/gi).test(coms)
|
|
364
|
-
let closed = isPoly ? true : false;
|
|
494
|
+
let isPoly = !(/[acqts]/gi).test(coms)
|
|
495
|
+
//let closed = isPoly ? true : false;
|
|
496
|
+
let closed = (/z/gi).test(coms);
|
|
365
497
|
|
|
366
498
|
if (isPoly && !mode) {
|
|
367
499
|
|
|
@@ -379,9 +511,10 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
379
511
|
//poly = simplifyRDP_rel(poly, simplifyRDP, bb.width, bb.height)
|
|
380
512
|
}
|
|
381
513
|
|
|
382
|
-
toPolygon = false;
|
|
514
|
+
//toPolygon = false;
|
|
383
515
|
pathDataSub = pathDataFromPoly(poly, closed)
|
|
384
|
-
//
|
|
516
|
+
//console.log('pathDataSub2', isPoly, pathDataSub, pathDataToD(pathDataSub));
|
|
517
|
+
|
|
385
518
|
}
|
|
386
519
|
|
|
387
520
|
|
|
@@ -389,29 +522,64 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
389
522
|
* convert curves to polygon
|
|
390
523
|
* flattening
|
|
391
524
|
*/
|
|
392
|
-
|
|
525
|
+
//console.log('toPolygon', toPolygon);
|
|
526
|
+
|
|
527
|
+
if (toPolygon) {
|
|
393
528
|
simplifyBezier = false
|
|
394
529
|
smoothPoly = false;
|
|
395
530
|
harmonizeCpts = false;
|
|
396
531
|
|
|
397
|
-
pathDataSub = getPathDataVerbose(pathDataSub);
|
|
398
532
|
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
533
|
+
/**
|
|
534
|
+
* if pathdata is already polygon- pass through
|
|
535
|
+
* otherwise create precise polygon by curve splitting
|
|
536
|
+
* */
|
|
537
|
+
|
|
538
|
+
if (!isPoly) {
|
|
539
|
+
pathDataSub = getPathDataVerbose(pathDataSub);
|
|
540
|
+
let polyData = pathDataToPolygonOpt(pathDataSub, {
|
|
541
|
+
precisionPoly,
|
|
542
|
+
autoAccuracy,
|
|
543
|
+
polyFormat,
|
|
544
|
+
//decimals,
|
|
545
|
+
simplifyRD,
|
|
546
|
+
simplifyRDP
|
|
547
|
+
})
|
|
548
|
+
|
|
549
|
+
poly = polyData.poly
|
|
550
|
+
pathDataSub = polyData.pathData
|
|
551
|
+
isPoly = true;
|
|
552
|
+
//console.log(poly);
|
|
553
|
+
//console.log(pathDataSub);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
//console.log('toPolygon', poly);
|
|
558
|
+
polys.push(poly)
|
|
559
|
+
//pathDataSub = polyData.pathData
|
|
560
|
+
|
|
561
|
+
}
|
|
407
562
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
pathDataSub = polyData.pathData
|
|
411
|
-
isPoly = true;
|
|
563
|
+
// harmonize cpts
|
|
564
|
+
// if (harmonizeCpts) pathDataSub = harmonizeCubicCpts(pathDataSub)
|
|
412
565
|
|
|
566
|
+
if (smoothPoly) {
|
|
567
|
+
//removeColinear=true
|
|
568
|
+
removeZeroLength=true
|
|
569
|
+
optimizeOrder=true
|
|
413
570
|
}
|
|
414
571
|
|
|
572
|
+
// remove zero length linetos
|
|
573
|
+
if (removeColinear || removeZeroLength) pathDataSub = removeZeroLengthLinetos(pathDataSub)
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
// sort to top left
|
|
577
|
+
if (optimizeOrder) pathDataSub = pathDataToTopLeft(pathDataSub);
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
// Preprocessing: remove colinear - ignore flat beziers (removed later)
|
|
581
|
+
if (removeColinear) pathDataSub = pathDataRemoveColinear(pathDataSub, { tolerance, flatBezierToLinetos: false });
|
|
582
|
+
|
|
415
583
|
|
|
416
584
|
/**
|
|
417
585
|
* poly to beziers via
|
|
@@ -422,12 +590,20 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
422
590
|
//flatBezierToLinetos=false
|
|
423
591
|
|
|
424
592
|
if (isPoly) {
|
|
593
|
+
/*
|
|
594
|
+
pathDataSub = pathDataToTopLeft(pathDataSub)
|
|
425
595
|
pathDataSub = removeZeroLengthLinetos(pathDataSub)
|
|
596
|
+
pathDataSub = pathDataRemoveColinear(pathDataSub, { tolerance, flatBezierToLinetos: true });
|
|
597
|
+
*/
|
|
598
|
+
|
|
599
|
+
//let d2 = pathDataToD(pathDataSub)
|
|
600
|
+
//console.log(d2);
|
|
426
601
|
let poly = getPathDataVertices(pathDataSub)
|
|
427
602
|
|
|
428
603
|
// options for poly simplification
|
|
429
604
|
let optionsPoly = {
|
|
430
|
-
denoise: 0.8,
|
|
605
|
+
//denoise: 0.8,
|
|
606
|
+
denoise: 0,
|
|
431
607
|
tolerance,
|
|
432
608
|
width: bb_poly.width,
|
|
433
609
|
height: bb_poly.height,
|
|
@@ -439,29 +615,17 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
439
615
|
closed,
|
|
440
616
|
simplifyRD,
|
|
441
617
|
simplifyRDP,
|
|
618
|
+
isClosed,
|
|
442
619
|
}
|
|
443
620
|
|
|
444
621
|
//console.log('smooth');
|
|
445
622
|
pathDataSub = simplifyPolygonToPathData(poly, optionsPoly)
|
|
446
623
|
// flag as non poly as we're smoothing to curves
|
|
447
|
-
|
|
624
|
+
isPoly = false
|
|
448
625
|
}
|
|
449
626
|
}
|
|
450
627
|
|
|
451
628
|
|
|
452
|
-
// harmonize cpts
|
|
453
|
-
// if (harmonizeCpts) pathDataSub = harmonizeCubicCpts(pathDataSub)
|
|
454
|
-
|
|
455
|
-
// remove zero length linetos
|
|
456
|
-
if (removeColinear || removeZeroLength) pathDataSub = removeZeroLengthLinetos(pathDataSub)
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
// sort to top left
|
|
460
|
-
if (optimizeOrder) pathDataSub = pathDataToTopLeft(pathDataSub);
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
// Preprocessing: remove colinear - ignore flat beziers (removed later)
|
|
464
|
-
if (removeColinear) pathDataSub = pathDataRemoveColinear(pathDataSub, { tolerance, flatBezierToLinetos: false });
|
|
465
629
|
|
|
466
630
|
let tMin = 0, tMax = 1;
|
|
467
631
|
if (addExtremes) pathDataSub = addExtremePoints(pathDataSub,
|
|
@@ -487,15 +651,16 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
487
651
|
pathDataPlus.bb = getPolyBBox(getPathDataVertices(pathDataCubic))
|
|
488
652
|
}
|
|
489
653
|
pathDataPlus.dimA = pathDataPlus.bb.width + pathDataPlus.bb.height;
|
|
654
|
+
|
|
490
655
|
pathDataPlus.pathData = getPathDataVerbose(pathDataSub, {
|
|
491
656
|
addSquareLength: false,
|
|
492
657
|
addArea: false,
|
|
493
658
|
addAverageDim: false
|
|
494
659
|
})
|
|
660
|
+
//console.log(pathDataPlus);
|
|
495
661
|
}
|
|
496
662
|
|
|
497
663
|
|
|
498
|
-
|
|
499
664
|
// simplify beziers
|
|
500
665
|
let { pathData, bb, dimA } = pathDataPlus;
|
|
501
666
|
|
|
@@ -505,6 +670,12 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
505
670
|
|
|
506
671
|
if (refineClosing) pathData = refineClosingCommand(pathData, { threshold: dimA * 0.001 })
|
|
507
672
|
|
|
673
|
+
// refine round segment sequences
|
|
674
|
+
if (simplifyRound) {
|
|
675
|
+
pathData = refineRoundSegments(pathData);
|
|
676
|
+
pathData = simplifyAdjacentRound(pathData);
|
|
677
|
+
}
|
|
678
|
+
|
|
508
679
|
pathData = simplifyBezier ? simplifyPathDataCubic(pathData, { simplifyBezier, keepInflections, keepExtremes, keepCorners, revertToQuadratics, tolerance }) : pathData;
|
|
509
680
|
|
|
510
681
|
|
|
@@ -533,11 +704,6 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
533
704
|
pathData = refineRoundedCorners(pathData, { threshold, tolerance, simplifyQuadraticCorners })
|
|
534
705
|
}
|
|
535
706
|
|
|
536
|
-
// refine round segment sequences
|
|
537
|
-
if (simplifyRound) {
|
|
538
|
-
pathData = refineRoundSegments(pathData);
|
|
539
|
-
pathData = simplifyAdjacentRound(pathData);
|
|
540
|
-
}
|
|
541
707
|
|
|
542
708
|
// simplify to quadratics
|
|
543
709
|
if (revertToQuadratics) pathData = pathDataRevertCubicToQuadratic(pathData, tolerance);
|
|
@@ -557,6 +723,9 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
557
723
|
|
|
558
724
|
}
|
|
559
725
|
|
|
726
|
+
// offset path
|
|
727
|
+
//let pathDataOffset = offsetPathData(pathData)
|
|
728
|
+
|
|
560
729
|
// update
|
|
561
730
|
pathDataPlusArr.push({ pathData, bb })
|
|
562
731
|
|
|
@@ -573,6 +742,7 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
573
742
|
let isPortrait = bb_global.height > bb_global.width;
|
|
574
743
|
|
|
575
744
|
//console.log(xMin, xMax, 'y:', yMin, yMax, 'bb_global', bb_global);
|
|
745
|
+
|
|
576
746
|
//console.log(i, pathDataPlusArr);
|
|
577
747
|
|
|
578
748
|
|
|
@@ -630,6 +800,7 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
630
800
|
}
|
|
631
801
|
|
|
632
802
|
// add simplified poly - if not populated by toPoly conversion
|
|
803
|
+
/*
|
|
633
804
|
if (isPoly) {
|
|
634
805
|
//console.log('5. isPoly', isPoly);
|
|
635
806
|
|
|
@@ -640,8 +811,8 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
640
811
|
}
|
|
641
812
|
polys.push(poly)
|
|
642
813
|
})
|
|
643
|
-
|
|
644
814
|
}
|
|
815
|
+
*/
|
|
645
816
|
|
|
646
817
|
|
|
647
818
|
// split into sub paths - returns svg with multiple paths
|
|
@@ -748,9 +919,7 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
748
919
|
svg = stringifySVG(svg, { omitNamespace, removeComments, format: minifyD });
|
|
749
920
|
//console.log('!!!svg', svg);
|
|
750
921
|
|
|
751
|
-
//svgSizeOpt = new Blob([svg]).size
|
|
752
922
|
svgSizeOpt = svg.length;
|
|
753
|
-
//compression = +(100/svgSize * (svgSize-svgSizeOpt)).toFixed(2)
|
|
754
923
|
compression = +(100 / svgSize * (svgSizeOpt)).toFixed(2)
|
|
755
924
|
|
|
756
925
|
svgSize = +(svgSize / 1024).toFixed(3)
|
|
@@ -774,25 +943,59 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
774
943
|
}
|
|
775
944
|
|
|
776
945
|
|
|
777
|
-
|
|
778
946
|
} else {
|
|
779
947
|
({ d, report } = paths[0]);
|
|
780
948
|
}
|
|
781
949
|
|
|
782
|
-
if (polys.length && polys.length === 1) {
|
|
783
|
-
polys = polys[0]
|
|
784
|
-
}
|
|
785
950
|
|
|
951
|
+
// sanitize poly output
|
|
952
|
+
if (polys.length) {
|
|
953
|
+
|
|
954
|
+
// round point data
|
|
955
|
+
polys.forEach((poly, i) => {
|
|
956
|
+
|
|
957
|
+
if (polyFormat === 'string') poly = normalizePoly(poly)
|
|
958
|
+
//console.log(poly);
|
|
959
|
+
|
|
960
|
+
poly = roundPoly(poly, decimals)
|
|
961
|
+
// remove coinciding points
|
|
962
|
+
polys[i] = removeCoincidingVertices(poly)
|
|
963
|
+
})
|
|
786
964
|
|
|
787
|
-
|
|
788
|
-
|
|
965
|
+
if (polys.length === 1) {
|
|
966
|
+
polys = polys[0]
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
if (polyFormat === 'string') {
|
|
970
|
+
//console.log(polyFormat, polys);
|
|
971
|
+
polys = normalizePoly(polys, { toArray: true, flatten: true })
|
|
972
|
+
//polys = polys.flat().map(pt => `${pt.x},${pt.y}`).join(' ')
|
|
973
|
+
polys = polys.flat().join(' ')
|
|
974
|
+
}
|
|
789
975
|
|
|
790
|
-
if (polyFormat === 'string' && polys.length) {
|
|
791
|
-
polys = polys.flat().map(pt => `${pt.x},${pt.y}`).join(' ')
|
|
792
976
|
}
|
|
793
977
|
|
|
794
978
|
|
|
795
|
-
|
|
979
|
+
// create object
|
|
980
|
+
let svgObj = new SlickVGObj({ svg, d, polys, report, pathDataPlusArr: pathDataPlusArr_global, inputType, dOriginal })
|
|
981
|
+
|
|
982
|
+
/*
|
|
983
|
+
let d2 = svgObj.getD()
|
|
984
|
+
console.log('d2', d2);
|
|
985
|
+
|
|
986
|
+
let svg2 = svgObj.getSvg()
|
|
987
|
+
console.log('svg2', svg2);
|
|
988
|
+
|
|
989
|
+
let format = 'd'
|
|
990
|
+
format = 'pathData'
|
|
991
|
+
format = 'd'
|
|
992
|
+
format = 'points'
|
|
993
|
+
format = 'array'
|
|
994
|
+
let poly2 = svgObj.getPoly({ format })
|
|
995
|
+
console.log('poly2', 'format', format, poly2);
|
|
996
|
+
*/
|
|
997
|
+
|
|
998
|
+
return !getObject ? (d ? d : svg) : svgObj;
|
|
796
999
|
|
|
797
1000
|
}
|
|
798
1001
|
|
|
@@ -94,6 +94,7 @@ export let settingsDefaults = {
|
|
|
94
94
|
// polygon
|
|
95
95
|
toPolygon: false,
|
|
96
96
|
smoothPoly: false,
|
|
97
|
+
isClosed:true,
|
|
97
98
|
polyFormat: 'object',
|
|
98
99
|
precisionPoly: 1,
|
|
99
100
|
simplifyRD: 0,
|
|
@@ -245,7 +246,6 @@ export const presetSettings = {
|
|
|
245
246
|
addViewBox: true,
|
|
246
247
|
removeDimensions: true,
|
|
247
248
|
removeOffCanvas: true,
|
|
248
|
-
|
|
249
249
|
/*
|
|
250
250
|
*/
|
|
251
251
|
}
|