From 3d79f57a10d7b3dba4bcd30d0b94e22410626cae Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Mon, 29 Mar 2021 11:01:13 +0300 Subject: [PATCH] Convert convertPathData to visitor --- input.svg | 1 + output.svg | 1 + plugins/convertPathData.js | 92 +++++++++++++++++++------------------- 3 files changed, 49 insertions(+), 45 deletions(-) create mode 100644 input.svg create mode 100644 output.svg diff --git a/input.svg b/input.svg new file mode 100644 index 00000000..30fa0af1 --- /dev/null +++ b/input.svg @@ -0,0 +1 @@ + diff --git a/output.svg b/output.svg new file mode 100644 index 00000000..62a358f8 --- /dev/null +++ b/output.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/plugins/convertPathData.js b/plugins/convertPathData.js index 3b5ef63c..e09c2890 100644 --- a/plugins/convertPathData.js +++ b/plugins/convertPathData.js @@ -6,10 +6,8 @@ const { path2js, js2path } = require('./_path.js'); const { applyTransforms } = require('./_applyTransforms.js'); const { cleanupOutData } = require('../lib/svgo/tools'); -exports.type = 'perItem'; - +exports.type = 'visitor'; exports.active = true; - exports.description = 'optimizes path data: writes in shorter form, applies transformations'; @@ -56,55 +54,59 @@ let arcTolerance; * * @author Kir Belevich */ -exports.fn = function (item, params) { - if ( - item.type === 'element' && - pathElems.includes(item.name) && - item.attributes.d != null - ) { - const computedStyle = computeStyle(item); - precision = params.floatPrecision; - error = - precision !== false ? +Math.pow(0.1, precision).toFixed(precision) : 1e-2; - roundData = precision > 0 && precision < 20 ? strongRound : round; - if (params.makeArcs) { - arcThreshold = params.makeArcs.threshold; - arcTolerance = params.makeArcs.tolerance; - } - const hasMarkerMid = computedStyle['marker-mid'] != null; +exports.fn = (root, params) => { + return { + element: { + enter: (node) => { + if (pathElems.includes(node.name) && node.attributes.d != null) { + const computedStyle = computeStyle(node); + precision = params.floatPrecision; + error = + precision !== false + ? +Math.pow(0.1, precision).toFixed(precision) + : 1e-2; + roundData = precision > 0 && precision < 20 ? strongRound : round; + if (params.makeArcs) { + arcThreshold = params.makeArcs.threshold; + arcTolerance = params.makeArcs.tolerance; + } + const hasMarkerMid = computedStyle['marker-mid'] != null; - const maybeHasStroke = - computedStyle.stroke && - (computedStyle.stroke.type === 'dynamic' || - computedStyle.stroke.value !== 'none'); - const maybeHasLinecap = - computedStyle['stroke-linecap'] && - (computedStyle['stroke-linecap'].type === 'dynamic' || - computedStyle['stroke-linecap'].value !== 'butt'); - const maybeHasStrokeAndLinecap = maybeHasStroke && maybeHasLinecap; + const maybeHasStroke = + computedStyle.stroke && + (computedStyle.stroke.type === 'dynamic' || + computedStyle.stroke.value !== 'none'); + const maybeHasLinecap = + computedStyle['stroke-linecap'] && + (computedStyle['stroke-linecap'].type === 'dynamic' || + computedStyle['stroke-linecap'].value !== 'butt'); + const maybeHasStrokeAndLinecap = maybeHasStroke && maybeHasLinecap; - var data = path2js(item); + var data = path2js(node); - // TODO: get rid of functions returns - if (data.length) { - if (params.applyTransforms) { - applyTransforms(item, data, params); - } + // TODO: get rid of functions returns + if (data.length) { + if (params.applyTransforms) { + applyTransforms(node, data, params); + } - convertToRelative(data); + convertToRelative(data); - data = filters(data, params, { - maybeHasStrokeAndLinecap, - hasMarkerMid, - }); + data = filters(data, params, { + maybeHasStrokeAndLinecap, + hasMarkerMid, + }); - if (params.utilizeAbsolute) { - data = convertToMixed(data, params); - } + if (params.utilizeAbsolute) { + data = convertToMixed(data, params); + } - js2path(item, data, params); - } - } + js2path(node, data, params); + } + } + }, + }, + }; }; /**