Permalink
Browse files

Merge branch 'dev'

  • Loading branch information...
2 parents 201e1eb + edcf422 commit 0f620511c830a98d0fc97eaafae72f9f20ff91c8 @deepsweet deepsweet committed Apr 15, 2013
Showing with 52 additions and 21 deletions.
  1. +4 −0 CHANGELOG.md
  2. +2 −2 README.md
  3. +2 −2 README.ru.md
  4. +6 −1 lib/svgo.js
  5. +10 −0 lib/svgo/coa.js
  6. +2 −1 lib/svgo/svg2js.js
  7. +1 −1 package.json
  8. +25 −14 plugins/transformsWithOnePath.js
View
@@ -1,3 +1,7 @@
+### [ [>](https://github.com/svg/svgo/tree/v0.3.1) ] 0.3.1 / 15.04.2013
+* plugins/transformsWithOnePath: resize SVG and automatically rescale inner Path
+* better errors handling
+
### [ [>](https://github.com/svg/svgo/tree/v0.3.0) ] 0.3.0 / 12.04.2013
* global refactoring: getting rid of the many dependencies
* new plugin [plugins/mergePaths](https://github.com/svg/svgo/blob/master/plugins/mergePaths.js)
View
@@ -3,7 +3,7 @@
<img src="http://soulshine.in/svgo.svg" width="200" height="200" alt="logo"/>
-## SVGO v0.3.0 [![Build Status](https://secure.travis-ci.org/svg/svgo.png)](http://travis-ci.org/svg/svgo)
+## SVGO v0.3.1 [![Build Status](https://secure.travis-ci.org/svg/svgo.png)](http://travis-ci.org/svg/svgo)
**SVG O**ptimizer is a Nodejs-based tool for optimizing SVG vector graphics files.
![](https://mc.yandex.ru/watch/18431326)
@@ -43,7 +43,7 @@ Today we have:
* [ [>](https://github.com/svg/svgo/blob/master/plugins/moveGroupAttrsToElems.js) ] move some group attributes to the content elements
* [ [>](https://github.com/svg/svgo/blob/master/plugins/collapseGroups.js) ] collapse useless groups
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeRasterImages.js) ] remove raster images (disabled by default)
-* [ [>](https://github.com/svg/svgo/blob/master/plugins/mergePath.js) ] merge multiple Paths into one
+* [ [>](https://github.com/svg/svgo/blob/master/plugins/mergePaths.js) ] merge multiple Paths into one
* [ [>](https://github.com/svg/svgo/blob/master/plugins/transformsWithOnePath.js) ] apply transforms, crop by real width, center vertical alignment and resize SVG with one Path inside
Want to know how it works and how to write your own plugin? [Of course you want to](https://github.com/svg/svgo/blob/master/docs/how-it-works/en.md).
View
@@ -3,7 +3,7 @@
<img src="http://soulshine.in/svgo.svg" width="200" height="200" alt="logo"/>
-## SVGO v0.3.0 [![Build Status](https://secure.travis-ci.org/svg/svgo.png)](http://travis-ci.org/svg/svgo)
+## SVGO v0.3.1 [![Build Status](https://secure.travis-ci.org/svg/svgo.png)](http://travis-ci.org/svg/svgo)
**SVG** **O**ptimizer – это инструмент для оптимизации векторной графики в формате SVG, написанный на Node.js.
![](https://mc.yandex.ru/watch/18431326)
@@ -43,7 +43,7 @@ SVGO имеет расширяемую архитектуру, в которой
* [ [>](https://github.com/svg/svgo/blob/master/plugins/moveGroupAttrsToElems.js) ] перемещение некоторых атрибутов группы на элементы внутри
* [ [>](https://github.com/svg/svgo/blob/master/plugins/collapseGroups.js) ] схлопывание бесполезных групп `<g>`
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeRasterImages.js) ] удаление растровых изображений (выключено по умолчанию)
-* [ [>](https://github.com/svg/svgo/blob/master/plugins/mergePath.js) ] склеивание нескольких Path в одну кривую
+* [ [>](https://github.com/svg/svgo/blob/master/plugins/mergePaths.js) ] склеивание нескольких Path в одну кривую
* [ [>](https://github.com/svg/svgo/blob/master/plugins/transformsWithOnePath.js) ] применение трансформаций, обрезка по реальной ширине, вертикальное выравнивание по центру и изменение размеров SVG с одним Path внутри
Хотите узнать, как это работает и как написать свой плагин? [Конечно же, да!](https://github.com/svg/svgo/blob/master/docs/how-it-works/ru.md).
View
@@ -15,7 +15,7 @@ var CONFIG = require('./svgo/config'),
PLUGINS = require('./svgo/plugins'),
JS2SVG = require('./svgo/js2svg');
-var SVGO = module.exports = function(config, full) {
+var SVGO = module.exports = function(config) {
this.config = CONFIG(config);
@@ -27,6 +27,11 @@ SVGO.prototype.optimize = function(svgstr, callback) {
SVG2JS(svgstr, function(svgjs) {
+ if (svgjs.error) {
+ callback(svgjs);
+ return;
+ }
+
svgjs = PLUGINS(svgjs, config.plugins);
callback(JS2SVG(svgjs, config.js2svg));
View
@@ -205,6 +205,11 @@ function optimizeFromString(svgstr, config, datauri, input, output) {
svgo.optimize(svgstr, function(result) {
+ if (result.error) {
+ console.log(result.error);
+ return;
+ }
+
if (datauri) {
result.data = encodeSVGDatauri(result.data, datauri);
}
@@ -380,6 +385,11 @@ function optimizeFolder(path, config) {
svgo.optimize(data, function(result) {
+ if (result.error) {
+ console.log(result.error);
+ return;
+ }
+
outBytes = Buffer.byteLength(result.data, 'utf8');
time = Date.now() - startTime;
View
@@ -117,7 +117,8 @@ module.exports = function(data, callback) {
// and must be deleted before parsing can continue"
this.error = null;
- throw new Error('svg2js: ' + e.message);
+ callback({ error: e.message });
+ throw new Error(e.message);
};
View
@@ -1,6 +1,6 @@
{
"name": "svgo",
- "version": "0.3.0",
+ "version": "0.3.1",
"description": "Nodejs-based tool for optimizing SVG vector graphics files",
"keywords": [ "svgo", "svg", "optimize", "minify" ],
"homepage": "http://svg.github.com/svgo/",
@@ -5,13 +5,24 @@ exports.type = 'full';
exports.active = false;
exports.params = {
+ // width and height to resize SVG and rescale inner Path
width: false,
height: false,
+
+ // scale inner Path without resizing SVG
scale: false,
+
+ // shiftX/Y inner Path
shiftX: false,
shiftY: false,
+
+ // crop SVG width along the real width of inner Path
hcrop: false,
+
+ // vertical center inner Path inside SVG height
vcenter: false,
+
+ // stringify params
floatPrecision: 3,
leadingZero: true,
negativeExtraSpace: true
@@ -194,12 +205,14 @@ exports.fn = function(data, params) {
xmax = Math.max.apply(this, xs).toFixed(params.floatPrecision),
ymin = Math.min.apply(this, ys).toFixed(params.floatPrecision),
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),
centerX = realWidth / 2,
centerY = realHeight / 2,
- transform = '';
+ transform = '',
+ scale;
// hcrop
if (params.hcrop) {
@@ -208,14 +221,23 @@ exports.fn = function(data, params) {
svgElem.attr('width').value = realWidth;
}
+ if (params.width && params.height) {
+ scale = Math.min(params.width / svgWidth, params.height / svgHeight);
+
+ 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) + ')';
}
// scale
if (params.scale) {
- var scale = params.scale;
+ scale = params.scale;
realWidth = realWidth * scale;
realHeight = realHeight * scale;
@@ -227,7 +249,7 @@ exports.fn = function(data, params) {
if (params.shiftX) {
var shiftX = params.shiftX;
- transform += ' translate(' + realWidth * shiftX + ')';
+ transform += ' translate(' + realWidth * shiftX + ', 0)';
}
// shiftY
@@ -238,7 +260,6 @@ exports.fn = function(data, params) {
}
if (transform) {
- console.log(transform);
pathElem.addAttr({
name: 'transform',
@@ -262,16 +283,6 @@ exports.fn = function(data, params) {
pathElem.attr('d').value = js2path(path, params);
}
- // width
- if (params.width) {
- svgElem.attr('width').value = params.width;
- }
-
- // height
- if (params.height) {
- svgElem.attr('height').value = params.height;
- }
-
}
});

0 comments on commit 0f62051

Please sign in to comment.