svg-path-simplify 0.4.5 → 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 +9 -0
- package/README.md +8 -0
- package/dist/svg-path-simplify.esm.js +1617 -1219
- package/dist/svg-path-simplify.esm.min.js +2 -2
- package/dist/svg-path-simplify.js +1617 -1218
- package/dist/svg-path-simplify.min.js +2 -2
- package/dist/svg-path-simplify.pathdata.esm.js +293 -665
- 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 +11 -5
- 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 +14 -34
- package/src/pathData_simplify_cubic_extrapolate.js +147 -8
- package/src/pathData_simplify_cubicsToArcs.js +65 -21
- package/src/pathSimplify-main.js +256 -59
- package/src/pathSimplify-presets.js +1 -0
- 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/svgii/geometry.js +130 -27
- package/src/svgii/geometry_bbox.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_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 +12 -0
- package/src/svgii/poly_to_pathdata.js +477 -8
- package/src/svgii/rounding.js +29 -39
- package/src/svgii/svg_cleanup.js +42 -54
- package/src/svgii/svg_cleanup_normalize_transforms.js +1 -1
- 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
|
|
@@ -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
|
|
@@ -366,8 +491,9 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
366
491
|
let pathDataSub = subPathArr[i];
|
|
367
492
|
let poly = []
|
|
368
493
|
let coms = Array.from(new Set(pathDataSub.map(com => com.type))).join('')
|
|
369
|
-
isPoly = !(/[acqts]/gi).test(coms)
|
|
370
|
-
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);
|
|
371
497
|
|
|
372
498
|
if (isPoly && !mode) {
|
|
373
499
|
|
|
@@ -385,9 +511,10 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
385
511
|
//poly = simplifyRDP_rel(poly, simplifyRDP, bb.width, bb.height)
|
|
386
512
|
}
|
|
387
513
|
|
|
388
|
-
toPolygon = false;
|
|
514
|
+
//toPolygon = false;
|
|
389
515
|
pathDataSub = pathDataFromPoly(poly, closed)
|
|
390
|
-
//
|
|
516
|
+
//console.log('pathDataSub2', isPoly, pathDataSub, pathDataToD(pathDataSub));
|
|
517
|
+
|
|
391
518
|
}
|
|
392
519
|
|
|
393
520
|
|
|
@@ -395,29 +522,64 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
395
522
|
* convert curves to polygon
|
|
396
523
|
* flattening
|
|
397
524
|
*/
|
|
398
|
-
|
|
525
|
+
//console.log('toPolygon', toPolygon);
|
|
526
|
+
|
|
527
|
+
if (toPolygon) {
|
|
399
528
|
simplifyBezier = false
|
|
400
529
|
smoothPoly = false;
|
|
401
530
|
harmonizeCpts = false;
|
|
402
531
|
|
|
403
|
-
pathDataSub = getPathDataVerbose(pathDataSub);
|
|
404
532
|
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
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
|
+
}
|
|
413
562
|
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
pathDataSub = polyData.pathData
|
|
417
|
-
isPoly = true;
|
|
563
|
+
// harmonize cpts
|
|
564
|
+
// if (harmonizeCpts) pathDataSub = harmonizeCubicCpts(pathDataSub)
|
|
418
565
|
|
|
566
|
+
if (smoothPoly) {
|
|
567
|
+
//removeColinear=true
|
|
568
|
+
removeZeroLength=true
|
|
569
|
+
optimizeOrder=true
|
|
419
570
|
}
|
|
420
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
|
+
|
|
421
583
|
|
|
422
584
|
/**
|
|
423
585
|
* poly to beziers via
|
|
@@ -428,12 +590,20 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
428
590
|
//flatBezierToLinetos=false
|
|
429
591
|
|
|
430
592
|
if (isPoly) {
|
|
593
|
+
/*
|
|
594
|
+
pathDataSub = pathDataToTopLeft(pathDataSub)
|
|
431
595
|
pathDataSub = removeZeroLengthLinetos(pathDataSub)
|
|
596
|
+
pathDataSub = pathDataRemoveColinear(pathDataSub, { tolerance, flatBezierToLinetos: true });
|
|
597
|
+
*/
|
|
598
|
+
|
|
599
|
+
//let d2 = pathDataToD(pathDataSub)
|
|
600
|
+
//console.log(d2);
|
|
432
601
|
let poly = getPathDataVertices(pathDataSub)
|
|
433
602
|
|
|
434
603
|
// options for poly simplification
|
|
435
604
|
let optionsPoly = {
|
|
436
|
-
denoise: 0.8,
|
|
605
|
+
//denoise: 0.8,
|
|
606
|
+
denoise: 0,
|
|
437
607
|
tolerance,
|
|
438
608
|
width: bb_poly.width,
|
|
439
609
|
height: bb_poly.height,
|
|
@@ -445,29 +615,17 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
445
615
|
closed,
|
|
446
616
|
simplifyRD,
|
|
447
617
|
simplifyRDP,
|
|
618
|
+
isClosed,
|
|
448
619
|
}
|
|
449
620
|
|
|
450
621
|
//console.log('smooth');
|
|
451
622
|
pathDataSub = simplifyPolygonToPathData(poly, optionsPoly)
|
|
452
623
|
// flag as non poly as we're smoothing to curves
|
|
453
|
-
|
|
624
|
+
isPoly = false
|
|
454
625
|
}
|
|
455
626
|
}
|
|
456
627
|
|
|
457
628
|
|
|
458
|
-
// harmonize cpts
|
|
459
|
-
// if (harmonizeCpts) pathDataSub = harmonizeCubicCpts(pathDataSub)
|
|
460
|
-
|
|
461
|
-
// remove zero length linetos
|
|
462
|
-
if (removeColinear || removeZeroLength) pathDataSub = removeZeroLengthLinetos(pathDataSub)
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
// sort to top left
|
|
466
|
-
if (optimizeOrder) pathDataSub = pathDataToTopLeft(pathDataSub);
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
// Preprocessing: remove colinear - ignore flat beziers (removed later)
|
|
470
|
-
if (removeColinear) pathDataSub = pathDataRemoveColinear(pathDataSub, { tolerance, flatBezierToLinetos: false });
|
|
471
629
|
|
|
472
630
|
let tMin = 0, tMax = 1;
|
|
473
631
|
if (addExtremes) pathDataSub = addExtremePoints(pathDataSub,
|
|
@@ -493,15 +651,16 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
493
651
|
pathDataPlus.bb = getPolyBBox(getPathDataVertices(pathDataCubic))
|
|
494
652
|
}
|
|
495
653
|
pathDataPlus.dimA = pathDataPlus.bb.width + pathDataPlus.bb.height;
|
|
654
|
+
|
|
496
655
|
pathDataPlus.pathData = getPathDataVerbose(pathDataSub, {
|
|
497
656
|
addSquareLength: false,
|
|
498
657
|
addArea: false,
|
|
499
658
|
addAverageDim: false
|
|
500
659
|
})
|
|
660
|
+
//console.log(pathDataPlus);
|
|
501
661
|
}
|
|
502
662
|
|
|
503
663
|
|
|
504
|
-
|
|
505
664
|
// simplify beziers
|
|
506
665
|
let { pathData, bb, dimA } = pathDataPlus;
|
|
507
666
|
|
|
@@ -511,6 +670,12 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
511
670
|
|
|
512
671
|
if (refineClosing) pathData = refineClosingCommand(pathData, { threshold: dimA * 0.001 })
|
|
513
672
|
|
|
673
|
+
// refine round segment sequences
|
|
674
|
+
if (simplifyRound) {
|
|
675
|
+
pathData = refineRoundSegments(pathData);
|
|
676
|
+
pathData = simplifyAdjacentRound(pathData);
|
|
677
|
+
}
|
|
678
|
+
|
|
514
679
|
pathData = simplifyBezier ? simplifyPathDataCubic(pathData, { simplifyBezier, keepInflections, keepExtremes, keepCorners, revertToQuadratics, tolerance }) : pathData;
|
|
515
680
|
|
|
516
681
|
|
|
@@ -539,11 +704,6 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
539
704
|
pathData = refineRoundedCorners(pathData, { threshold, tolerance, simplifyQuadraticCorners })
|
|
540
705
|
}
|
|
541
706
|
|
|
542
|
-
// refine round segment sequences
|
|
543
|
-
if (simplifyRound) {
|
|
544
|
-
pathData = refineRoundSegments(pathData);
|
|
545
|
-
pathData = simplifyAdjacentRound(pathData);
|
|
546
|
-
}
|
|
547
707
|
|
|
548
708
|
// simplify to quadratics
|
|
549
709
|
if (revertToQuadratics) pathData = pathDataRevertCubicToQuadratic(pathData, tolerance);
|
|
@@ -563,6 +723,9 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
563
723
|
|
|
564
724
|
}
|
|
565
725
|
|
|
726
|
+
// offset path
|
|
727
|
+
//let pathDataOffset = offsetPathData(pathData)
|
|
728
|
+
|
|
566
729
|
// update
|
|
567
730
|
pathDataPlusArr.push({ pathData, bb })
|
|
568
731
|
|
|
@@ -579,6 +742,7 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
579
742
|
let isPortrait = bb_global.height > bb_global.width;
|
|
580
743
|
|
|
581
744
|
//console.log(xMin, xMax, 'y:', yMin, yMax, 'bb_global', bb_global);
|
|
745
|
+
|
|
582
746
|
//console.log(i, pathDataPlusArr);
|
|
583
747
|
|
|
584
748
|
|
|
@@ -636,6 +800,7 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
636
800
|
}
|
|
637
801
|
|
|
638
802
|
// add simplified poly - if not populated by toPoly conversion
|
|
803
|
+
/*
|
|
639
804
|
if (isPoly) {
|
|
640
805
|
//console.log('5. isPoly', isPoly);
|
|
641
806
|
|
|
@@ -646,8 +811,8 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
646
811
|
}
|
|
647
812
|
polys.push(poly)
|
|
648
813
|
})
|
|
649
|
-
|
|
650
814
|
}
|
|
815
|
+
*/
|
|
651
816
|
|
|
652
817
|
|
|
653
818
|
// split into sub paths - returns svg with multiple paths
|
|
@@ -754,9 +919,7 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
754
919
|
svg = stringifySVG(svg, { omitNamespace, removeComments, format: minifyD });
|
|
755
920
|
//console.log('!!!svg', svg);
|
|
756
921
|
|
|
757
|
-
//svgSizeOpt = new Blob([svg]).size
|
|
758
922
|
svgSizeOpt = svg.length;
|
|
759
|
-
//compression = +(100/svgSize * (svgSize-svgSizeOpt)).toFixed(2)
|
|
760
923
|
compression = +(100 / svgSize * (svgSizeOpt)).toFixed(2)
|
|
761
924
|
|
|
762
925
|
svgSize = +(svgSize / 1024).toFixed(3)
|
|
@@ -780,25 +943,59 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
780
943
|
}
|
|
781
944
|
|
|
782
945
|
|
|
783
|
-
|
|
784
946
|
} else {
|
|
785
947
|
({ d, report } = paths[0]);
|
|
786
948
|
}
|
|
787
949
|
|
|
788
|
-
if (polys.length && polys.length === 1) {
|
|
789
|
-
polys = polys[0]
|
|
790
|
-
}
|
|
791
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
|
+
})
|
|
964
|
+
|
|
965
|
+
if (polys.length === 1) {
|
|
966
|
+
polys = polys[0]
|
|
967
|
+
}
|
|
792
968
|
|
|
793
|
-
|
|
794
|
-
|
|
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
|
+
}
|
|
795
975
|
|
|
796
|
-
if (polyFormat === 'string' && polys.length) {
|
|
797
|
-
polys = polys.flat().map(pt => `${pt.x},${pt.y}`).join(' ')
|
|
798
976
|
}
|
|
799
977
|
|
|
800
978
|
|
|
801
|
-
|
|
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;
|
|
802
999
|
|
|
803
1000
|
}
|
|
804
1001
|
|