svg-path-simplify 0.4.5 → 0.4.7
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 +14 -0
- package/README.md +24 -14
- package/dist/svg-path-simplify.esm.js +1620 -1216
- package/dist/svg-path-simplify.esm.min.js +2 -2
- package/dist/svg-path-simplify.js +1620 -1215
- package/dist/svg-path-simplify.min.js +2 -2
- package/dist/svg-path-simplify.pathdata.esm.js +298 -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 +259 -60
- 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 +70 -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/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
|
|
@@ -307,7 +432,9 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
307
432
|
//let isPolyPath = !mode && isPoly && Array.isArray(d)
|
|
308
433
|
//let pathData = !isPolyPath ? parsePathDataNormalized(d, { quadraticToCubic, arcToCubic }) : d;
|
|
309
434
|
let pathData = parsePathDataNormalized(d, { quadraticToCubic, arcToCubic });
|
|
310
|
-
|
|
435
|
+
console.log('!!!pathData', pathData, arcToCubic);
|
|
436
|
+
|
|
437
|
+
|
|
311
438
|
//console.log(isPoly);
|
|
312
439
|
|
|
313
440
|
// get polygon bbox
|
|
@@ -366,8 +493,9 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
366
493
|
let pathDataSub = subPathArr[i];
|
|
367
494
|
let poly = []
|
|
368
495
|
let coms = Array.from(new Set(pathDataSub.map(com => com.type))).join('')
|
|
369
|
-
isPoly = !(/[acqts]/gi).test(coms)
|
|
370
|
-
let closed = isPoly ? true : false;
|
|
496
|
+
let isPoly = !(/[acqts]/gi).test(coms)
|
|
497
|
+
//let closed = isPoly ? true : false;
|
|
498
|
+
let closed = (/z/gi).test(coms);
|
|
371
499
|
|
|
372
500
|
if (isPoly && !mode) {
|
|
373
501
|
|
|
@@ -385,9 +513,10 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
385
513
|
//poly = simplifyRDP_rel(poly, simplifyRDP, bb.width, bb.height)
|
|
386
514
|
}
|
|
387
515
|
|
|
388
|
-
toPolygon = false;
|
|
516
|
+
//toPolygon = false;
|
|
389
517
|
pathDataSub = pathDataFromPoly(poly, closed)
|
|
390
|
-
//
|
|
518
|
+
//console.log('pathDataSub2', isPoly, pathDataSub, pathDataToD(pathDataSub));
|
|
519
|
+
|
|
391
520
|
}
|
|
392
521
|
|
|
393
522
|
|
|
@@ -395,29 +524,64 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
395
524
|
* convert curves to polygon
|
|
396
525
|
* flattening
|
|
397
526
|
*/
|
|
398
|
-
|
|
527
|
+
//console.log('toPolygon', toPolygon);
|
|
528
|
+
|
|
529
|
+
if (toPolygon) {
|
|
399
530
|
simplifyBezier = false
|
|
400
531
|
smoothPoly = false;
|
|
401
532
|
harmonizeCpts = false;
|
|
402
533
|
|
|
403
|
-
pathDataSub = getPathDataVerbose(pathDataSub);
|
|
404
534
|
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
535
|
+
/**
|
|
536
|
+
* if pathdata is already polygon- pass through
|
|
537
|
+
* otherwise create precise polygon by curve splitting
|
|
538
|
+
* */
|
|
539
|
+
|
|
540
|
+
if (!isPoly) {
|
|
541
|
+
pathDataSub = getPathDataVerbose(pathDataSub);
|
|
542
|
+
let polyData = pathDataToPolygonOpt(pathDataSub, {
|
|
543
|
+
precisionPoly,
|
|
544
|
+
autoAccuracy,
|
|
545
|
+
polyFormat,
|
|
546
|
+
//decimals,
|
|
547
|
+
simplifyRD,
|
|
548
|
+
simplifyRDP
|
|
549
|
+
})
|
|
550
|
+
|
|
551
|
+
poly = polyData.poly
|
|
552
|
+
pathDataSub = polyData.pathData
|
|
553
|
+
isPoly = true;
|
|
554
|
+
//console.log(poly);
|
|
555
|
+
//console.log(pathDataSub);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
//console.log('toPolygon', poly);
|
|
560
|
+
polys.push(poly)
|
|
561
|
+
//pathDataSub = polyData.pathData
|
|
562
|
+
|
|
563
|
+
}
|
|
413
564
|
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
pathDataSub = polyData.pathData
|
|
417
|
-
isPoly = true;
|
|
565
|
+
// harmonize cpts
|
|
566
|
+
// if (harmonizeCpts) pathDataSub = harmonizeCubicCpts(pathDataSub)
|
|
418
567
|
|
|
568
|
+
if (smoothPoly) {
|
|
569
|
+
//removeColinear=true
|
|
570
|
+
removeZeroLength=true
|
|
571
|
+
optimizeOrder=true
|
|
419
572
|
}
|
|
420
573
|
|
|
574
|
+
// remove zero length linetos
|
|
575
|
+
if (removeColinear || removeZeroLength) pathDataSub = removeZeroLengthLinetos(pathDataSub)
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
// sort to top left
|
|
579
|
+
if (optimizeOrder) pathDataSub = pathDataToTopLeft(pathDataSub);
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
// Preprocessing: remove colinear - ignore flat beziers (removed later)
|
|
583
|
+
if (removeColinear) pathDataSub = pathDataRemoveColinear(pathDataSub, { tolerance, flatBezierToLinetos: false });
|
|
584
|
+
|
|
421
585
|
|
|
422
586
|
/**
|
|
423
587
|
* poly to beziers via
|
|
@@ -428,12 +592,20 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
428
592
|
//flatBezierToLinetos=false
|
|
429
593
|
|
|
430
594
|
if (isPoly) {
|
|
595
|
+
/*
|
|
596
|
+
pathDataSub = pathDataToTopLeft(pathDataSub)
|
|
431
597
|
pathDataSub = removeZeroLengthLinetos(pathDataSub)
|
|
598
|
+
pathDataSub = pathDataRemoveColinear(pathDataSub, { tolerance, flatBezierToLinetos: true });
|
|
599
|
+
*/
|
|
600
|
+
|
|
601
|
+
//let d2 = pathDataToD(pathDataSub)
|
|
602
|
+
//console.log(d2);
|
|
432
603
|
let poly = getPathDataVertices(pathDataSub)
|
|
433
604
|
|
|
434
605
|
// options for poly simplification
|
|
435
606
|
let optionsPoly = {
|
|
436
|
-
denoise: 0.8,
|
|
607
|
+
//denoise: 0.8,
|
|
608
|
+
denoise: 0,
|
|
437
609
|
tolerance,
|
|
438
610
|
width: bb_poly.width,
|
|
439
611
|
height: bb_poly.height,
|
|
@@ -445,29 +617,17 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
445
617
|
closed,
|
|
446
618
|
simplifyRD,
|
|
447
619
|
simplifyRDP,
|
|
620
|
+
isClosed,
|
|
448
621
|
}
|
|
449
622
|
|
|
450
623
|
//console.log('smooth');
|
|
451
624
|
pathDataSub = simplifyPolygonToPathData(poly, optionsPoly)
|
|
452
625
|
// flag as non poly as we're smoothing to curves
|
|
453
|
-
|
|
626
|
+
isPoly = false
|
|
454
627
|
}
|
|
455
628
|
}
|
|
456
629
|
|
|
457
630
|
|
|
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
631
|
|
|
472
632
|
let tMin = 0, tMax = 1;
|
|
473
633
|
if (addExtremes) pathDataSub = addExtremePoints(pathDataSub,
|
|
@@ -493,15 +653,16 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
493
653
|
pathDataPlus.bb = getPolyBBox(getPathDataVertices(pathDataCubic))
|
|
494
654
|
}
|
|
495
655
|
pathDataPlus.dimA = pathDataPlus.bb.width + pathDataPlus.bb.height;
|
|
656
|
+
|
|
496
657
|
pathDataPlus.pathData = getPathDataVerbose(pathDataSub, {
|
|
497
658
|
addSquareLength: false,
|
|
498
659
|
addArea: false,
|
|
499
660
|
addAverageDim: false
|
|
500
661
|
})
|
|
662
|
+
//console.log(pathDataPlus);
|
|
501
663
|
}
|
|
502
664
|
|
|
503
665
|
|
|
504
|
-
|
|
505
666
|
// simplify beziers
|
|
506
667
|
let { pathData, bb, dimA } = pathDataPlus;
|
|
507
668
|
|
|
@@ -511,6 +672,12 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
511
672
|
|
|
512
673
|
if (refineClosing) pathData = refineClosingCommand(pathData, { threshold: dimA * 0.001 })
|
|
513
674
|
|
|
675
|
+
// refine round segment sequences
|
|
676
|
+
if (simplifyRound) {
|
|
677
|
+
pathData = refineRoundSegments(pathData);
|
|
678
|
+
pathData = simplifyAdjacentRound(pathData);
|
|
679
|
+
}
|
|
680
|
+
|
|
514
681
|
pathData = simplifyBezier ? simplifyPathDataCubic(pathData, { simplifyBezier, keepInflections, keepExtremes, keepCorners, revertToQuadratics, tolerance }) : pathData;
|
|
515
682
|
|
|
516
683
|
|
|
@@ -539,11 +706,6 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
539
706
|
pathData = refineRoundedCorners(pathData, { threshold, tolerance, simplifyQuadraticCorners })
|
|
540
707
|
}
|
|
541
708
|
|
|
542
|
-
// refine round segment sequences
|
|
543
|
-
if (simplifyRound) {
|
|
544
|
-
pathData = refineRoundSegments(pathData);
|
|
545
|
-
pathData = simplifyAdjacentRound(pathData);
|
|
546
|
-
}
|
|
547
709
|
|
|
548
710
|
// simplify to quadratics
|
|
549
711
|
if (revertToQuadratics) pathData = pathDataRevertCubicToQuadratic(pathData, tolerance);
|
|
@@ -563,6 +725,9 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
563
725
|
|
|
564
726
|
}
|
|
565
727
|
|
|
728
|
+
// offset path
|
|
729
|
+
//let pathDataOffset = offsetPathData(pathData)
|
|
730
|
+
|
|
566
731
|
// update
|
|
567
732
|
pathDataPlusArr.push({ pathData, bb })
|
|
568
733
|
|
|
@@ -579,6 +744,7 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
579
744
|
let isPortrait = bb_global.height > bb_global.width;
|
|
580
745
|
|
|
581
746
|
//console.log(xMin, xMax, 'y:', yMin, yMax, 'bb_global', bb_global);
|
|
747
|
+
|
|
582
748
|
//console.log(i, pathDataPlusArr);
|
|
583
749
|
|
|
584
750
|
|
|
@@ -636,6 +802,7 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
636
802
|
}
|
|
637
803
|
|
|
638
804
|
// add simplified poly - if not populated by toPoly conversion
|
|
805
|
+
/*
|
|
639
806
|
if (isPoly) {
|
|
640
807
|
//console.log('5. isPoly', isPoly);
|
|
641
808
|
|
|
@@ -646,8 +813,8 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
646
813
|
}
|
|
647
814
|
polys.push(poly)
|
|
648
815
|
})
|
|
649
|
-
|
|
650
816
|
}
|
|
817
|
+
*/
|
|
651
818
|
|
|
652
819
|
|
|
653
820
|
// split into sub paths - returns svg with multiple paths
|
|
@@ -754,9 +921,7 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
754
921
|
svg = stringifySVG(svg, { omitNamespace, removeComments, format: minifyD });
|
|
755
922
|
//console.log('!!!svg', svg);
|
|
756
923
|
|
|
757
|
-
//svgSizeOpt = new Blob([svg]).size
|
|
758
924
|
svgSizeOpt = svg.length;
|
|
759
|
-
//compression = +(100/svgSize * (svgSize-svgSizeOpt)).toFixed(2)
|
|
760
925
|
compression = +(100 / svgSize * (svgSizeOpt)).toFixed(2)
|
|
761
926
|
|
|
762
927
|
svgSize = +(svgSize / 1024).toFixed(3)
|
|
@@ -780,25 +945,59 @@ export function svgPathSimplify(input = '', settings = {}) {
|
|
|
780
945
|
}
|
|
781
946
|
|
|
782
947
|
|
|
783
|
-
|
|
784
948
|
} else {
|
|
785
949
|
({ d, report } = paths[0]);
|
|
786
950
|
}
|
|
787
951
|
|
|
788
|
-
if (polys.length && polys.length === 1) {
|
|
789
|
-
polys = polys[0]
|
|
790
|
-
}
|
|
791
952
|
|
|
953
|
+
// sanitize poly output
|
|
954
|
+
if (polys.length) {
|
|
955
|
+
|
|
956
|
+
// round point data
|
|
957
|
+
polys.forEach((poly, i) => {
|
|
958
|
+
|
|
959
|
+
if (polyFormat === 'string') poly = normalizePoly(poly)
|
|
960
|
+
//console.log(poly);
|
|
961
|
+
|
|
962
|
+
poly = roundPoly(poly, decimals)
|
|
963
|
+
// remove coinciding points
|
|
964
|
+
polys[i] = removeCoincidingVertices(poly)
|
|
965
|
+
})
|
|
792
966
|
|
|
793
|
-
|
|
794
|
-
|
|
967
|
+
if (polys.length === 1) {
|
|
968
|
+
polys = polys[0]
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
if (polyFormat === 'string') {
|
|
972
|
+
//console.log(polyFormat, polys);
|
|
973
|
+
polys = normalizePoly(polys, { toArray: true, flatten: true })
|
|
974
|
+
//polys = polys.flat().map(pt => `${pt.x},${pt.y}`).join(' ')
|
|
975
|
+
polys = polys.flat().join(' ')
|
|
976
|
+
}
|
|
795
977
|
|
|
796
|
-
if (polyFormat === 'string' && polys.length) {
|
|
797
|
-
polys = polys.flat().map(pt => `${pt.x},${pt.y}`).join(' ')
|
|
798
978
|
}
|
|
799
979
|
|
|
800
980
|
|
|
801
|
-
|
|
981
|
+
// create object
|
|
982
|
+
let svgObj = new SlickVGObj({ svg, d, polys, report, pathDataPlusArr: pathDataPlusArr_global, inputType, dOriginal })
|
|
983
|
+
|
|
984
|
+
/*
|
|
985
|
+
let d2 = svgObj.getD()
|
|
986
|
+
console.log('d2', d2);
|
|
987
|
+
|
|
988
|
+
let svg2 = svgObj.getSvg()
|
|
989
|
+
console.log('svg2', svg2);
|
|
990
|
+
|
|
991
|
+
let format = 'd'
|
|
992
|
+
format = 'pathData'
|
|
993
|
+
format = 'd'
|
|
994
|
+
format = 'points'
|
|
995
|
+
format = 'array'
|
|
996
|
+
let poly2 = svgObj.getPoly({ format })
|
|
997
|
+
console.log('poly2', 'format', format, poly2);
|
|
998
|
+
*/
|
|
999
|
+
|
|
1000
|
+
return !getObject ? (d ? d : svg) : svgObj;
|
|
802
1001
|
|
|
803
1002
|
}
|
|
804
1003
|
|