Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
deepsweet committed May 5, 2013
2 parents a529e27 + 09c0a8c commit 2810d43
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 27 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### [ [>](https://github.com/svg/svgo/tree/v0.3.3) ] 0.3.3 / 05.05.2013
* plugins/convertPathData: convert very first m to M, fix applyTransforms with translate() (fix [#112](https://github.com/svg/svgo/issues/112))
* plugins/transformsWithOnePath: fix real width/height rounding; fix scale transform origin; reorder transforms
* plugins/transformsWithOnePath: ability to set new width or height independently with auto rescaling

### [ [>](https://github.com/svg/svgo/tree/v0.3.2) ] 0.3.2 / 03.05.2013
* new plugin [plugins/sortAttrs](https://github.com/svg/svgo/blob/master/plugins/sortAttrs.js)
* plugins/transformsWithOnePath: buggy hcrop (fix [#111](https://github.com/svg/svgo/issues/111))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svgo",
"version": "0.3.2",
"version": "0.3.3",
"description": "Nodejs-based tool for optimizing SVG vector graphics files",
"keywords": [ "svgo", "svg", "optimize", "minify" ],
"homepage": "http://svg.github.com/svgo/",
Expand Down
21 changes: 15 additions & 6 deletions plugins/convertPathData.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ function convertToRelative(path) {
newPoint,
point = [0, 0],
subpathPoint = [0, 0],
index = 0;
index = 0,
mM = false;

path.forEach(function(item) {

Expand All @@ -106,6 +107,11 @@ function convertToRelative(path) {
point[1] += newPoint[1];

if (instruction === 'm') {
if (index === 1) {
instruction = 'M';
mM = true;
}

subpathPoint = point.slice(-2);
}

Expand All @@ -127,13 +133,16 @@ function convertToRelative(path) {
instruction = 'm';
}

data[0] -= point[0];
data[1] -= point[1];
// if "M" was not transformed from "m"
if (!mM) {
data[0] -= point[0];
data[1] -= point[1];

point[0] += data[0];
point[1] += data[1];
point[0] += data[0];
point[1] += data[1];

subpathPoint = point.slice(-2);
subpathPoint = point.slice(-2);
}

}

Expand Down
75 changes: 57 additions & 18 deletions plugins/transformsWithOnePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,42 +211,52 @@ exports.fn = function(data, params) {
ymax = Math.max.apply(this, ys).toFixed(params.floatPrecision),
svgWidth = +svgElem.attr('width').value,
svgHeight = +svgElem.attr('height').value,
realWidth = Math.ceil(xmax - xmin),
realHeight = Math.ceil(ymax - ymin),
realWidth = Math.round(xmax - xmin),
realHeight = Math.round(ymax - ymin),
centerX = realWidth / 2,
centerY = realHeight / 2,
transform = '',
scale;

// hcrop
if (params.hcrop) {
transform += ' translate(' + (-xmin) + ' 0)';

svgElem.attr('width').value = realWidth;
}

// width & height
if (params.width && params.height) {

scale = Math.min(params.width / svgWidth, params.height / svgHeight);

realWidth = realWidth * scale;
realHeight = realHeight * scale;

svgElem.attr('width').value = params.width;
svgElem.attr('height').value = params.height;

transform += ' scale(' + scale + ')';
}

// vcenter
if (params.vcenter) {
transform += ' translate(0 ' + (((svgHeight - realHeight) / 2) - ymin) + ')';
}
// width
} else if (params.width && !params.height) {

// scale
if (params.scale) {
scale = params.scale;
scale = params.width / svgWidth;

realWidth = realWidth * scale;
realHeight = realHeight * scale;

svgElem.attr('width').value = params.width;
svgElem.attr('height').value = svgHeight * scale;

transform += ' scale(' + scale + ')';

// height
} else if (params.height && !params.width) {

scale = params.height / svgHeight;

realWidth = realWidth * scale;
realHeight = realHeight * scale;

transform += ' translate(' + (-centerX * (scale - 1)) + ', ' + (-centerY * (scale - 1)) + ') scale(' + scale + ')';
svgElem.attr('width').value = svgWidth * scale;
svgElem.attr('height').value = params.height;

transform += ' scale(' + scale + ')';

}

// shiftX
Expand All @@ -263,6 +273,35 @@ exports.fn = function(data, params) {
transform += ' translate(0, ' + realHeight * shiftY + ')';
}

// scale
if (params.scale) {
scale = params.scale;

realWidth = realWidth * scale;
realHeight = realHeight * scale;

centerX = realWidth / 2;
centerY = realHeight / 2;

if (params.shiftX || params.shiftY) {
transform += ' scale(' + scale + ')';
} else {
transform += ' translate(' + (-centerX * (scale - 1)) + ', ' + (-centerY * (scale - 1)) + ') scale(' + scale + ')';
}
}

// hcrop
if (params.hcrop) {
transform += ' translate(' + (-xmin) + ' 0)';

svgElem.attr('width').value = realWidth;
}

// vcenter
if (params.vcenter) {
transform += ' translate(0 ' + (((svgHeight - realHeight) / 2) - ymin) + ')';
}

if (transform) {

pathElem.addAttr({
Expand Down
2 changes: 1 addition & 1 deletion test/plugins/convertPathData.07.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plugins/convertPathData.10.svg
Loading
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 2810d43

Please sign in to comment.