Skip to content

Commit

Permalink
Fixed consecutive transforms precision handling. Fixes #421
Browse files Browse the repository at this point in the history
  • Loading branch information
GreLI committed Sep 2, 2015
1 parent 7102ed5 commit 614bc00
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
24 changes: 11 additions & 13 deletions plugins/convertTransform.js
Expand Up @@ -22,6 +22,7 @@ exports.params = {
};

var cleanupOutData = require('../lib/svgo/tools').cleanupOutData,
EXTEND = require('whet.extend'),
transform2js = require('./_transforms.js').transform2js,
transformsMultiply = require('./_transforms.js').transformsMultiply,
matrixToTransform = require('./_transforms.js').matrixToTransform,
Expand Down Expand Up @@ -75,7 +76,7 @@ exports.fn = function(item, params) {
*/
function convertTransform(item, attrName, params) {
var data = transform2js(item.attr(attrName).value);
definePrecision(data, params);
params = definePrecision(data, params);

if (params.collapseIntoOne && data.length > 1) {
data = [transformsMultiply(data)];
Expand All @@ -84,9 +85,7 @@ function convertTransform(item, attrName, params) {
if (params.convertToShorts) {
data = convertToShorts(data, params);
} else {
data.forEach(function(transform) {
transform = roundTransform(transform, params);
});
data.forEach(roundTransform);
}

if (params.removeUseless) {
Expand Down Expand Up @@ -116,6 +115,9 @@ function definePrecision(data, params) {
var matrixData = data.reduce(getMatrixData, []),
significantDigits = params.transformPrecision;

// Clone params so it don't affect other elements transformations.
params = EXTEND({}, params);

// Limit transform precision with matrix one. Calculating with larger precision doesn't add any value.
if (matrixData.length) {
params.transformPrecision = Math.min(params.transformPrecision,
Expand All @@ -130,9 +132,11 @@ function definePrecision(data, params) {
params.degPrecision = Math.max(0, Math.min(params.floatPrecision, significantDigits - 2));
}

degRound = params.degPrecision >= 1 ? smartRound.bind(this, params.degPrecision) : round;
floatRound = params.floatPrecision >= 1 ? smartRound.bind(this, params.floatPrecision) : round;
degRound = params.degPrecision >= 1 ? smartRound.bind(this, params.degPrecision) : round;
transformRound = params.transformPrecision >= 1 ? smartRound.bind(this, params.transformPrecision) : round;

return params;
}

/**
Expand Down Expand Up @@ -180,15 +184,9 @@ function convertToShorts(transforms, params) {
transform = transforms[i];
}

transform = roundTransform(transform, params);

// fixed-point numbers
// 12.754997 → 12.755
if (params.transformPrecision !== false) {
transform.data = transform.data.map(function(num) {
return +num.toFixed(params.transformPrecision);
});
}
roundTransform(transform);

// convert long translate transform notation to the shorts one
// translate(10 0) → translate(10)
Expand Down Expand Up @@ -298,7 +296,7 @@ function js2transform(transformJS, params) {

// collect output value string
transformJS.forEach(function(transform) {
transform = roundTransform(transform, params);
roundTransform(transform);
transformString += (transformString && ' ') + transform.name + '(' + cleanupOutData(transform.data, params) + ')';
});

Expand Down
4 changes: 4 additions & 0 deletions test/plugins/convertTransform.01.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 614bc00

Please sign in to comment.