From 039259ef2d60ac904f1c915c3fb408fc79efc3bb Mon Sep 17 00:00:00 2001 From: Jochen Rauschenbusch Date: Sat, 26 Mar 2016 11:57:37 +0100 Subject: [PATCH 1/3] Introduce option parameter to enable/disable SVGO optimizer --- Gruntfile.js | 20 ++++++++++++++++ Readme.md | 6 +++++ package.json | 3 ++- tasks/engines/node.js | 54 +++++++++++++++++++++++++++++-------------- tasks/webfont.js | 1 + test/webfont_test.js | 18 +++++++++++++++ 6 files changed, 84 insertions(+), 18 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 3f0800d..bacf06d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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', diff --git a/Readme.md b/Readme.md index 0c2f0a0..1dc5778 100644 --- a/Readme.md +++ b/Readme.md @@ -353,6 +353,12 @@ Type: `boolean` Default: `false` Adds IE7 support using a `*zoom: expression()` hack. +#### optimize + +Type: `boolean` Default: `false` + +If `true` 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` diff --git a/package.json b/package.json index 45376c9..d07ae7a 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,8 @@ "grunt-jscs": "~1.0.0", "load-grunt-tasks": "~3.4.0", "stylus": "~0.53.0", - "xml2js": "~0.4.16" + "xml2js": "~0.4.16", + "svgutils": "~0.13.5" }, "peerDependencies": { "grunt": ">=0.4.0" diff --git a/tasks/engines/node.js b/tasks/engines/node.js index 0990272..d1e6196 100644 --- a/tasks/engines/node.js +++ b/tasks/engines/node.js @@ -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 { diff --git a/tasks/webfont.js b/tasks/webfont.js index 4eab457..621704f 100755 --- a/tasks/webfont.js +++ b/tasks/webfont.js @@ -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 === true, round: options.round !== undefined ? options.round : 10e12, fontHeight: options.fontHeight !== undefined ? options.fontHeight : 512, descent: options.descent !== undefined ? options.descent : 64, diff --git a/test/webfont_test.js b/test/webfont_test.js index 03cd1a1..b6f5d7a 100644 --- a/test/webfont_test.js +++ b/test/webfont_test.js @@ -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'); From c8924b2b01ca2419cb6883b316d21cbfe882e05f Mon Sep 17 00:00:00 2001 From: Jochen Rauschenbusch Date: Sat, 26 Mar 2016 12:05:39 +0100 Subject: [PATCH 2/3] Fixing default option --- Readme.md | 4 ++-- tasks/webfont.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index 1dc5778..8cbc7cd 100644 --- a/Readme.md +++ b/Readme.md @@ -355,9 +355,9 @@ Adds IE7 support using a `*zoom: expression()` hack. #### optimize -Type: `boolean` Default: `false` +Type: `boolean` Default: `true` -If `true` 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. +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 diff --git a/tasks/webfont.js b/tasks/webfont.js index 621704f..6d22d6f 100755 --- a/tasks/webfont.js +++ b/tasks/webfont.js @@ -99,7 +99,7 @@ module.exports = function(grunt) { startCodepoint: options.startCodepoint || wf.UNICODE_PUA_START, ie7: options.ie7 === true, normalize: options.normalize === true, - optimize: options.optimize === 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, From ac704c137da7ba3af98c7a5eb2d48e2c23af4646 Mon Sep 17 00:00:00 2001 From: Jochen Rauschenbusch Date: Sat, 26 Mar 2016 12:09:52 +0100 Subject: [PATCH 3/3] Remove unused svgutils library --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index d07ae7a..45376c9 100644 --- a/package.json +++ b/package.json @@ -52,8 +52,7 @@ "grunt-jscs": "~1.0.0", "load-grunt-tasks": "~3.4.0", "stylus": "~0.53.0", - "xml2js": "~0.4.16", - "svgutils": "~0.13.5" + "xml2js": "~0.4.16" }, "peerDependencies": { "grunt": ">=0.4.0"