Skip to content
This repository has been archived by the owner on May 29, 2020. It is now read-only.

SVGO optimization should be an option #327

Merged
merged 3 commits into from
Mar 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,26 @@ module.exports = function(grunt) {
syntax: 'bootstrap'
}
},
optimize_enabled: {
src: 'test/src/*.svg',
dest: 'test/tmp/optimize_enabled',
options: {
engine: 'node',
types: 'svg',
autoHint: false,
optimize: true
}
},
optimize_disabled: {
src: 'test/src/*.svg',
dest: 'test/tmp/optimize_disabled',
options: {
engine: 'node',
types: 'svg',
autoHint: false,
optimize: false
}
},
codepoints: {
src: 'test/src/*.svg',
dest: 'test/tmp/codepoints',
Expand Down
6 changes: 6 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ Type: `boolean` Default: `false`

Adds IE7 support using a `*zoom: expression()` hack.

#### optimize

Type: `boolean` Default: `true`

If `false` the SVGO optimization will not be used. This is useful in cases where the optimizer will produce faulty web fonts by removing relevant SVG paths or attributes.

#### startCodepoint

Type: `integer` Default: `0xF101`
Expand Down
54 changes: 37 additions & 17 deletions tasks/engines/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,29 +122,49 @@ module.exports = function(o, allDone) {
}

function svgFilesToStreams(files, done) {

async.map(files, function(file, fileDone) {
var svg = fs.readFileSync(file, 'utf8');
var svgo = new SVGO();
try {
svgo.optimize(svg, function(res) {
var idx = files.indexOf(file);
var name = o.glyphs[idx];
var stream = new MemoryStream(res.data, {
writable: false
});
fileDone(null, {
codepoint: o.codepoints[name],
name: name,
stream: stream
});

function fileStreamed(name, stream) {
fileDone(null, {
codepoint: o.codepoints[name],
name: name,
stream: stream
});
}
catch(err) {
fileDone(err);

function streamSVG(name, file) {
var stream = fs.createReadStream(file);
fileStreamed(name, stream);
}

function streamSVGO(name, file) {
var svg = fs.readFileSync(file, 'utf8');
var svgo = new SVGO();
try {
svgo.optimize(svg, function(res) {
var stream = new MemoryStream(res.data, {
writable: false
});
fileStreamed(name, stream);
});
} catch(err) {
logger.error('Can’t simplify SVG file with SVGO.\n\n' + err);
fileDone(err);
}
}

var idx = files.indexOf(file);
var name = o.glyphs[idx];

if(o.optimize === true) {
streamSVGO(name, file);
} else {
streamSVG(name, file);
}
}, function(err, streams) {
if (err) {
logger.error('Can’t simplify SVG file with SVGO.\n\n' + err);
logger.error('Can’t stream SVG file.\n\n' + err);
allDone(false);
}
else {
Expand Down
1 change: 1 addition & 0 deletions tasks/webfont.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ module.exports = function(grunt) {
startCodepoint: options.startCodepoint || wf.UNICODE_PUA_START,
ie7: options.ie7 === true,
normalize: options.normalize === true,
optimize: options.optimize === false ? false : true,
round: options.round !== undefined ? options.round : 10e12,
fontHeight: options.fontHeight !== undefined ? options.fontHeight : 512,
descent: options.descent !== undefined ? options.descent : 64,
Expand Down
18 changes: 18 additions & 0 deletions test/webfont_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,24 @@ exports.webfont = {
test.done();
},

optimize_enabled: function(test){
var optimizedPathSegment = '280.2V280.098C349.867 293.072 358.595';
var svg = grunt.file.read('test/tmp/optimize_enabled/icons.svg');
if(svg.indexOf(optimizedPathSegment) === -1) {
test.fail(true, 'SVG element must be contains the optimized path');
}
test.done();
},

optimize_disbaled: function(test){
var optimizedPathSegment = '280.2V280.098C349.867 293.072 358.595';
var svg = grunt.file.read('test/tmp/optimize_disabled/icons.svg');
if(svg.indexOf(optimizedPathSegment) > -1) {
test.fail(true, 'SVG element must be contains the un-optimized path');
}
test.done();
},

codepoints: function(test) {
// Default codepoint of 0xE001 can be overidden
var resultSVG = grunt.file.expand('test/tmp/codepoints/icons.svg');
Expand Down