1
0
mirror of https://github.com/svg/svgo.git synced 2025-08-06 04:22:39 +03:00

Convert convertPathData to visitor

This commit is contained in:
Bogdan Chadkin
2021-03-29 11:01:13 +03:00
parent 06110b4fc0
commit 3d79f57a10
3 changed files with 49 additions and 45 deletions

1
input.svg Normal file
View File

@@ -0,0 +1 @@
<svg viewBox="0 0 300 300" xmlns="http://www.w3.org/2000/svg"> <g transform="matrix(1 0 0 1 30 40)"> <circle cx="50" cy="50" r="50" /> </g> <g style="transform:matrix(1,0,0,1,20,20)"> <circle cx="50" cy="50" r="50" /> </g> </svg>

After

Width:  |  Height:  |  Size: 230 B

1
output.svg Normal file
View File

@@ -0,0 +1 @@
<svg viewBox="0 0 300 300" xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" r="50" transform="translate(30 40)"/><circle cx="50" cy="50" r="50" style="transform:matrix(1,0,0,1,20,20)"/></svg>

After

Width:  |  Height:  |  Size: 201 B

View File

@@ -6,10 +6,8 @@ const { path2js, js2path } = require('./_path.js');
const { applyTransforms } = require('./_applyTransforms.js'); const { applyTransforms } = require('./_applyTransforms.js');
const { cleanupOutData } = require('../lib/svgo/tools'); const { cleanupOutData } = require('../lib/svgo/tools');
exports.type = 'perItem'; exports.type = 'visitor';
exports.active = true; exports.active = true;
exports.description = exports.description =
'optimizes path data: writes in shorter form, applies transformations'; 'optimizes path data: writes in shorter form, applies transformations';
@@ -56,55 +54,59 @@ let arcTolerance;
* *
* @author Kir Belevich * @author Kir Belevich
*/ */
exports.fn = function (item, params) { exports.fn = (root, params) => {
if ( return {
item.type === 'element' && element: {
pathElems.includes(item.name) && enter: (node) => {
item.attributes.d != null if (pathElems.includes(node.name) && node.attributes.d != null) {
) { const computedStyle = computeStyle(node);
const computedStyle = computeStyle(item); precision = params.floatPrecision;
precision = params.floatPrecision; error =
error = precision !== false
precision !== false ? +Math.pow(0.1, precision).toFixed(precision) : 1e-2; ? +Math.pow(0.1, precision).toFixed(precision)
roundData = precision > 0 && precision < 20 ? strongRound : round; : 1e-2;
if (params.makeArcs) { roundData = precision > 0 && precision < 20 ? strongRound : round;
arcThreshold = params.makeArcs.threshold; if (params.makeArcs) {
arcTolerance = params.makeArcs.tolerance; arcThreshold = params.makeArcs.threshold;
} arcTolerance = params.makeArcs.tolerance;
const hasMarkerMid = computedStyle['marker-mid'] != null; }
const hasMarkerMid = computedStyle['marker-mid'] != null;
const maybeHasStroke = const maybeHasStroke =
computedStyle.stroke && computedStyle.stroke &&
(computedStyle.stroke.type === 'dynamic' || (computedStyle.stroke.type === 'dynamic' ||
computedStyle.stroke.value !== 'none'); computedStyle.stroke.value !== 'none');
const maybeHasLinecap = const maybeHasLinecap =
computedStyle['stroke-linecap'] && computedStyle['stroke-linecap'] &&
(computedStyle['stroke-linecap'].type === 'dynamic' || (computedStyle['stroke-linecap'].type === 'dynamic' ||
computedStyle['stroke-linecap'].value !== 'butt'); computedStyle['stroke-linecap'].value !== 'butt');
const maybeHasStrokeAndLinecap = maybeHasStroke && maybeHasLinecap; const maybeHasStrokeAndLinecap = maybeHasStroke && maybeHasLinecap;
var data = path2js(item); var data = path2js(node);
// TODO: get rid of functions returns // TODO: get rid of functions returns
if (data.length) { if (data.length) {
if (params.applyTransforms) { if (params.applyTransforms) {
applyTransforms(item, data, params); applyTransforms(node, data, params);
} }
convertToRelative(data); convertToRelative(data);
data = filters(data, params, { data = filters(data, params, {
maybeHasStrokeAndLinecap, maybeHasStrokeAndLinecap,
hasMarkerMid, hasMarkerMid,
}); });
if (params.utilizeAbsolute) { if (params.utilizeAbsolute) {
data = convertToMixed(data, params); data = convertToMixed(data, params);
} }
js2path(item, data, params); js2path(node, data, params);
} }
} }
},
},
};
}; };
/** /**