Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
deepsweet committed Apr 15, 2013
2 parents 201e1eb + edcf422 commit 0f62051
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -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)
Expand Down Expand Up @@ -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).
Expand Down
4 changes: 2 additions & 2 deletions README.ru.md
Expand Up @@ -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)
Expand Down Expand Up @@ -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).
Expand Down
7 changes: 6 additions & 1 deletion lib/svgo.js
Expand Up @@ -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);

Expand All @@ -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));
Expand Down
10 changes: 10 additions & 0 deletions lib/svgo/coa.js
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion lib/svgo/svg2js.js
Expand Up @@ -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);

};

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -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/",
Expand Down
39 changes: 25 additions & 14 deletions plugins/transformsWithOnePath.js
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -238,7 +260,6 @@ exports.fn = function(data, params) {
}

if (transform) {
console.log(transform);

pathElem.addAttr({
name: 'transform',
Expand All @@ -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;
}

}

});
Expand Down

0 comments on commit 0f62051

Please sign in to comment.