From 258fecfa6b4a037d98ad2614f5c9573728f6959d Mon Sep 17 00:00:00 2001 From: GreLI Date: Sat, 13 Jul 2019 16:18:08 +0300 Subject: [PATCH] =?UTF-8?q?Remove=20spaces=20after=20=E2=80=98arcto?= =?UTF-8?q?=E2=80=99=20path=20command=20flags?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/svgo/tools.js | 60 ++++++++++++----------------- plugins/_path.js | 6 ++- plugins/convertPathData.js | 7 +++- plugins/mergePaths.js | 3 +- test/coa/testSvg/test.1.svg | 2 +- test/coa/testSvg/test.svg | 2 +- test/plugins/convertPathData.02.svg | 2 +- test/plugins/convertPathData.03.svg | 4 +- test/plugins/convertPathData.04.svg | 2 +- test/plugins/convertPathData.11.svg | 18 ++++----- test/plugins/convertPathData.14.svg | 16 ++++---- test/plugins/convertPathData.15.svg | 2 +- test/plugins/convertPathData.16.svg | 2 +- test/plugins/convertPathData.18.svg | 2 +- test/plugins/convertPathData.20.svg | 2 +- test/plugins/mergePaths.03.svg | 2 +- 16 files changed, 66 insertions(+), 66 deletions(-) diff --git a/lib/svgo/tools.js b/lib/svgo/tools.js index c6b3c80e..d57a39a3 100644 --- a/lib/svgo/tools.js +++ b/lib/svgo/tools.js @@ -10,33 +10,23 @@ var FS = require('fs'); * @return {String} output string */ exports.encodeSVGDatauri = function(str, type) { - var prefix = 'data:image/svg+xml'; - - // base64 if (!type || type === 'base64') { - + // base64 prefix += ';base64,'; if (Buffer.from) { str = prefix + Buffer.from(str).toString('base64'); } else { str = prefix + new Buffer(str).toString('base64'); } - - // URI encoded } else if (type === 'enc') { - + // URI encoded str = prefix + ',' + encodeURIComponent(str); - - // unencoded } else if (type === 'unenc') { - + // unencoded str = prefix + ',' + str; - } - return str; - }; /** @@ -54,23 +44,16 @@ exports.decodeSVGDatauri = function(str) { var data = match[3]; - // base64 if (match[2]) { - + // base64 str = new Buffer(data, 'base64').toString('utf8'); - - // URI encoded } else if (data.charAt(0) === '%') { - + // URI encoded str = decodeURIComponent(data); - - // unencoded } else if (data.charAt(0) === '<') { - + // unencoded str = data; - } - return str; }; @@ -80,20 +63,33 @@ exports.intersectArrays = function(a, b) { }); }; -exports.cleanupOutData = function(data, params) { - +/** + * Convert a row of numbers to an optimized string view. + * + * @example + * [0, -1, .5, .5] → 0-1 .5.5 + * + * @param {number[]} data + * @param {Object} params + * @param {string?} command path data instruction + * @return {[type]} + */ +exports.cleanupOutData = function(data, params, command) { var str = '', delimiter, prev; data.forEach(function(item, i) { - // space delimiter by default delimiter = ' '; // no extra space in front of first number - if (i === 0) { - delimiter = ''; + if (i == 0) delimiter = ''; + + // no extra space after 'arcto' command flags + if (params.noSpaceAfterFlags && (command == 'A' || command == 'a')) { + var pos = i % 7; + if (pos == 4 || pos == 5) delimiter = ''; } // remove floating-point numbers leading zeros @@ -107,22 +103,18 @@ exports.cleanupOutData = function(data, params) { // in front of a floating number if a previous number is floating too if ( params.negativeExtraSpace && + delimiter != '' && (item < 0 || (String(item).charCodeAt(0) == 46 && prev % 1 !== 0) ) ) { delimiter = ''; } - // save prev item value prev = item; - str += delimiter + item; - }); - return str; - }; /** @@ -146,9 +138,7 @@ var removeLeadingZero = exports.removeLeadingZero = function(num) { } else if (-1 < num && num < 0 && strNum.charCodeAt(1) == 48) { strNum = strNum.charAt(0) + strNum.slice(2); } - return strNum; - }; diff --git a/plugins/_path.js b/plugins/_path.js index 5b1687cb..bb613c6a 100644 --- a/plugins/_path.js +++ b/plugins/_path.js @@ -553,7 +553,11 @@ exports.js2path = function(path, data, params) { } path.attr('d').value = data.reduce(function(pathString, item) { - return pathString += item.instruction + (item.data ? cleanupOutData(item.data, params) : ''); + var strData = ''; + if (item.data) { + strData = cleanupOutData(item.data, params, item.instruction); + } + return pathString += item.instruction + strData; }, ''); }; diff --git a/plugins/convertPathData.js b/plugins/convertPathData.js index 23b18fe8..0eb9ad05 100644 --- a/plugins/convertPathData.js +++ b/plugins/convertPathData.js @@ -23,6 +23,7 @@ exports.params = { utilizeAbsolute: true, leadingZero: true, negativeExtraSpace: true, + noSpaceAfterFlags: true, forceAbsolutePath: false }; @@ -960,6 +961,10 @@ function findArcAngle(curve, relCircle) { function data2Path(params, pathData) { return pathData.reduce(function(pathString, item) { - return pathString + item.instruction + (item.data ? cleanupOutData(roundData(item.data.slice()), params) : ''); + var strData = ''; + if (item.data) { + strData = cleanupOutData(roundData(item.data.slice()), params); + } + return pathString + item.instruction + strData; }, ''); } diff --git a/plugins/mergePaths.js b/plugins/mergePaths.js index 63cf56e8..066b3f2e 100644 --- a/plugins/mergePaths.js +++ b/plugins/mergePaths.js @@ -9,7 +9,8 @@ exports.description = 'merges multiple paths in one if possible'; exports.params = { collapseRepeated: true, leadingZero: true, - negativeExtraSpace: true + negativeExtraSpace: true, + noSpaceAfterFlags: true }; var path2js = require('./_path.js').path2js, diff --git a/test/coa/testSvg/test.1.svg b/test/coa/testSvg/test.1.svg index de7975de..78b902d7 100644 --- a/test/coa/testSvg/test.1.svg +++ b/test/coa/testSvg/test.1.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/test/coa/testSvg/test.svg b/test/coa/testSvg/test.svg index de7975de..78b902d7 100644 --- a/test/coa/testSvg/test.svg +++ b/test/coa/testSvg/test.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/test/plugins/convertPathData.02.svg b/test/plugins/convertPathData.02.svg index c950860b..b2334d68 100644 --- a/test/plugins/convertPathData.02.svg +++ b/test/plugins/convertPathData.02.svg @@ -15,5 +15,5 @@ - + diff --git a/test/plugins/convertPathData.03.svg b/test/plugins/convertPathData.03.svg index 8081173d..c065263c 100644 --- a/test/plugins/convertPathData.03.svg +++ b/test/plugins/convertPathData.03.svg @@ -32,8 +32,8 @@ - - + + diff --git a/test/plugins/convertPathData.04.svg b/test/plugins/convertPathData.04.svg index 72d56cc0..0dae5fde 100644 --- a/test/plugins/convertPathData.04.svg +++ b/test/plugins/convertPathData.04.svg @@ -15,5 +15,5 @@ - + diff --git a/test/plugins/convertPathData.11.svg b/test/plugins/convertPathData.11.svg index 965c2840..0466038d 100644 --- a/test/plugins/convertPathData.11.svg +++ b/test/plugins/convertPathData.11.svg @@ -40,7 +40,7 @@ - + @@ -58,14 +58,14 @@ - - - - - - + + + + + + - - + + diff --git a/test/plugins/convertPathData.14.svg b/test/plugins/convertPathData.14.svg index c1115fe8..a82daaf1 100644 --- a/test/plugins/convertPathData.14.svg +++ b/test/plugins/convertPathData.14.svg @@ -12,12 +12,12 @@ @@@ - - - - - - - - + + + + + + + + diff --git a/test/plugins/convertPathData.15.svg b/test/plugins/convertPathData.15.svg index dc9427fe..eec62baa 100644 --- a/test/plugins/convertPathData.15.svg +++ b/test/plugins/convertPathData.15.svg @@ -6,7 +6,7 @@ @@@ - + diff --git a/test/plugins/convertPathData.16.svg b/test/plugins/convertPathData.16.svg index 4fcee12f..b31955fe 100644 --- a/test/plugins/convertPathData.16.svg +++ b/test/plugins/convertPathData.16.svg @@ -6,7 +6,7 @@ @@@ - + diff --git a/test/plugins/convertPathData.18.svg b/test/plugins/convertPathData.18.svg index e4af82d9..b70935c5 100644 --- a/test/plugins/convertPathData.18.svg +++ b/test/plugins/convertPathData.18.svg @@ -5,5 +5,5 @@ @@@ - + diff --git a/test/plugins/convertPathData.20.svg b/test/plugins/convertPathData.20.svg index 2e00cbb4..960038e3 100644 --- a/test/plugins/convertPathData.20.svg +++ b/test/plugins/convertPathData.20.svg @@ -5,5 +5,5 @@ @@@ - + diff --git a/test/plugins/mergePaths.03.svg b/test/plugins/mergePaths.03.svg index ee0613a2..a7af7b5b 100644 --- a/test/plugins/mergePaths.03.svg +++ b/test/plugins/mergePaths.03.svg @@ -29,7 +29,7 @@ - +