diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2023-12-21 22:46:53 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2023-12-21 22:46:53 +0800 |
| commit | f06956efd2dc4bf77244a9fcce2b883a00b925af (patch) | |
| tree | 0123cb73ce67f1f4b17e1d6cbabff076ee44e31b /node_modules/svgo/plugins/convertPathData.js | |
| parent | 8d980c13970c15b6d2312b6fc4ac51698dd0d220 (diff) | |
updated node modules
Diffstat (limited to 'node_modules/svgo/plugins/convertPathData.js')
| -rw-r--r-- | node_modules/svgo/plugins/convertPathData.js | 80 |
1 files changed, 64 insertions, 16 deletions
diff --git a/node_modules/svgo/plugins/convertPathData.js b/node_modules/svgo/plugins/convertPathData.js index a5f375ba..6229d9f7 100644 --- a/node_modules/svgo/plugins/convertPathData.js +++ b/node_modules/svgo/plugins/convertPathData.js @@ -46,6 +46,7 @@ let arcTolerance; * }, * straightCurves: boolean, * lineShorthands: boolean, + * convertToZ: boolean, * curveSmoothShorthands: boolean, * floatPrecision: number | false, * transformPrecision: number, @@ -95,6 +96,7 @@ exports.fn = (root, params) => { }, straightCurves = true, lineShorthands = true, + convertToZ = true, curveSmoothShorthands = true, floatPrecision = 3, transformPrecision = 5, @@ -116,6 +118,7 @@ exports.fn = (root, params) => { makeArcs, straightCurves, lineShorthands, + convertToZ, curveSmoothShorthands, floatPrecision, transformPrecision, @@ -167,6 +170,12 @@ exports.fn = (root, params) => { (computedStyle['stroke-linecap'].type === 'dynamic' || computedStyle['stroke-linecap'].value !== 'butt'); const maybeHasStrokeAndLinecap = maybeHasStroke && maybeHasLinecap; + const isSafeToUseZ = maybeHasStroke + ? computedStyle['stroke-linecap']?.type === 'static' && + computedStyle['stroke-linecap'].value === 'round' && + computedStyle['stroke-linejoin']?.type === 'static' && + computedStyle['stroke-linejoin'].value === 'round' + : true; var data = path2js(node); @@ -175,6 +184,7 @@ exports.fn = (root, params) => { convertToRelative(data); data = filters(data, newParams, { + isSafeToUseZ, maybeHasStrokeAndLinecap, hasMarkerMid, }); @@ -371,10 +381,14 @@ const convertToRelative = (pathData) => { * @type {( * path: PathDataItem[], * params: InternalParams, - * aux: { maybeHasStrokeAndLinecap: boolean, hasMarkerMid: boolean } + * aux: { isSafeToUseZ: boolean, maybeHasStrokeAndLinecap: boolean, hasMarkerMid: boolean } * ) => PathDataItem[]} */ -function filters(path, params, { maybeHasStrokeAndLinecap, hasMarkerMid }) { +function filters( + path, + params, + { isSafeToUseZ, maybeHasStrokeAndLinecap, hasMarkerMid } +) { var stringify = data2Path.bind(null, params), relSubpoint = [0, 0], pathBase = [0, 0], @@ -579,7 +593,7 @@ function filters(path, params, { maybeHasStrokeAndLinecap, hasMarkerMid }) { } } - // Rounding relative coordinates, taking in account accummulating error + // Rounding relative coordinates, taking in account accumulating error // to get closer to absolute coordinates. Sum of rounded value remains same: // l .25 3 .25 2 .25 3 .25 2 -> l .3 3 .2 2 .3 3 .2 2 if (precision !== false) { @@ -664,6 +678,24 @@ function filters(path, params, { maybeHasStrokeAndLinecap, hasMarkerMid }) { } } + // convert going home to z + // m 0 0 h 5 v 5 l -5 -5 -> m 0 0 h 5 v 5 z + if ( + params.convertToZ && + (isSafeToUseZ || next?.command === 'Z' || next?.command === 'z') && + (command === 'l' || command === 'h' || command === 'v') + ) { + if ( + // @ts-ignore + Math.abs(pathBase[0] - item.coords[0]) < error && + // @ts-ignore + Math.abs(pathBase[1] - item.coords[1]) < error + ) { + command = 'z'; + data = []; + } + } + // collapse repeated commands // h 20 h 30 -> h 50 if ( @@ -796,17 +828,25 @@ function filters(path, params, { maybeHasStrokeAndLinecap, hasMarkerMid }) { item.command = command; item.args = data; - - prev = item; } else { // z resets coordinates relSubpoint[0] = pathBase[0]; relSubpoint[1] = pathBase[1]; // @ts-ignore if (prev.command === 'Z' || prev.command === 'z') return false; - prev = item; } + if ( + (command === 'Z' || command === 'z') && + params.removeUseless && + isSafeToUseZ && + // @ts-ignore + Math.abs(item.base[0] - item.coords[0]) < error / 10 && + // @ts-ignore + Math.abs(item.base[1] - item.coords[1]) < error / 10 + ) + return false; + prev = item; return true; }); @@ -935,7 +975,7 @@ function getIntersection(coords) { c2 = coords[4] * coords[7] - coords[5] * coords[6], // x1 * y2 - x2 * y1 denom = a1 * b2 - a2 * b1; - if (!denom) return; // parallel lines havn't an intersection + if (!denom) return; // parallel lines haven't an intersection /** * @type {Point} @@ -952,6 +992,16 @@ function getIntersection(coords) { } /** + * Does the same as `Number.prototype.toFixed` but without casting + * the return value to a string. + * @type {(num: number, precision: number) => number} + */ +function toFixed(num, precision) { + const pow = 10 ** precision; + return Math.round(num * pow) / pow; +} + +/** * Decrease accuracy of floating-point numbers * in path data keeping a specified number of decimals. * Smart rounds values like 2.3491 to 2.35 instead of 2.349. @@ -960,16 +1010,14 @@ function getIntersection(coords) { * @type {(data: number[]) => number[]} */ function strongRound(data) { - for (var i = data.length; i-- > 0; ) { - // @ts-ignore - if (data[i].toFixed(precision) != data[i]) { - // @ts-ignore - var rounded = +data[i].toFixed(precision - 1); + const precisionNum = precision || 0; + for (let i = data.length; i-- > 0; ) { + const fixed = toFixed(data[i], precisionNum); + if (fixed !== data[i]) { + const rounded = toFixed(data[i], precisionNum - 1); data[i] = - // @ts-ignore - +Math.abs(rounded - data[i]).toFixed(precision + 1) >= error - ? // @ts-ignore - +data[i].toFixed(precision) + toFixed(Math.abs(rounded - data[i]), precisionNum + 1) >= error + ? fixed : rounded; } } |
