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 { 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,16 +54,17 @@ 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);
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;
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;
@@ -83,12 +82,12 @@ exports.fn = function (item, params) {
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);
applyTransforms(node, data, params);
}
convertToRelative(data);
@@ -102,9 +101,12 @@ exports.fn = function (item, params) {
data = convertToMixed(data, params);
}
js2path(item, data, params);
js2path(node, data, params);
}
}
},
},
};
};
/**