1
0
mirror of https://github.com/svg/svgo.git synced 2025-08-09 02:22:08 +03:00

Remove spaces after ‘arcto’ path command flags

This commit is contained in:
GreLI
2019-07-13 16:18:08 +03:00
parent 95f7603983
commit 258fecfa6b
16 changed files with 66 additions and 66 deletions

View File

@@ -10,33 +10,23 @@ var FS = require('fs');
* @return {String} output string * @return {String} output string
*/ */
exports.encodeSVGDatauri = function(str, type) { exports.encodeSVGDatauri = function(str, type) {
var prefix = 'data:image/svg+xml'; var prefix = 'data:image/svg+xml';
// base64
if (!type || type === 'base64') { if (!type || type === 'base64') {
// base64
prefix += ';base64,'; prefix += ';base64,';
if (Buffer.from) { if (Buffer.from) {
str = prefix + Buffer.from(str).toString('base64'); str = prefix + Buffer.from(str).toString('base64');
} else { } else {
str = prefix + new Buffer(str).toString('base64'); str = prefix + new Buffer(str).toString('base64');
} }
// URI encoded
} else if (type === 'enc') { } else if (type === 'enc') {
// URI encoded
str = prefix + ',' + encodeURIComponent(str); str = prefix + ',' + encodeURIComponent(str);
// unencoded
} else if (type === 'unenc') { } else if (type === 'unenc') {
// unencoded
str = prefix + ',' + str; str = prefix + ',' + str;
} }
return str; return str;
}; };
/** /**
@@ -54,23 +44,16 @@ exports.decodeSVGDatauri = function(str) {
var data = match[3]; var data = match[3];
// base64
if (match[2]) { if (match[2]) {
// base64
str = new Buffer(data, 'base64').toString('utf8'); str = new Buffer(data, 'base64').toString('utf8');
// URI encoded
} else if (data.charAt(0) === '%') { } else if (data.charAt(0) === '%') {
// URI encoded
str = decodeURIComponent(data); str = decodeURIComponent(data);
// unencoded
} else if (data.charAt(0) === '<') { } else if (data.charAt(0) === '<') {
// unencoded
str = data; str = data;
} }
return str; 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 = '', var str = '',
delimiter, delimiter,
prev; prev;
data.forEach(function(item, i) { data.forEach(function(item, i) {
// space delimiter by default // space delimiter by default
delimiter = ' '; delimiter = ' ';
// no extra space in front of first number // no extra space in front of first number
if (i === 0) { if (i == 0) delimiter = '';
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 // 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 // in front of a floating number if a previous number is floating too
if ( if (
params.negativeExtraSpace && params.negativeExtraSpace &&
delimiter != '' &&
(item < 0 || (item < 0 ||
(String(item).charCodeAt(0) == 46 && prev % 1 !== 0) (String(item).charCodeAt(0) == 46 && prev % 1 !== 0)
) )
) { ) {
delimiter = ''; delimiter = '';
} }
// save prev item value // save prev item value
prev = item; prev = item;
str += delimiter + item; str += delimiter + item;
}); });
return str; return str;
}; };
/** /**
@@ -146,9 +138,7 @@ var removeLeadingZero = exports.removeLeadingZero = function(num) {
} else if (-1 < num && num < 0 && strNum.charCodeAt(1) == 48) { } else if (-1 < num && num < 0 && strNum.charCodeAt(1) == 48) {
strNum = strNum.charAt(0) + strNum.slice(2); strNum = strNum.charAt(0) + strNum.slice(2);
} }
return strNum; return strNum;
}; };

View File

@@ -553,7 +553,11 @@ exports.js2path = function(path, data, params) {
} }
path.attr('d').value = data.reduce(function(pathString, item) { 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;
}, ''); }, '');
}; };

View File

@@ -23,6 +23,7 @@ exports.params = {
utilizeAbsolute: true, utilizeAbsolute: true,
leadingZero: true, leadingZero: true,
negativeExtraSpace: true, negativeExtraSpace: true,
noSpaceAfterFlags: true,
forceAbsolutePath: false forceAbsolutePath: false
}; };
@@ -960,6 +961,10 @@ function findArcAngle(curve, relCircle) {
function data2Path(params, pathData) { function data2Path(params, pathData) {
return pathData.reduce(function(pathString, item) { 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;
}, ''); }, '');
} }

View File

@@ -9,7 +9,8 @@ exports.description = 'merges multiple paths in one if possible';
exports.params = { exports.params = {
collapseRepeated: true, collapseRepeated: true,
leadingZero: true, leadingZero: true,
negativeExtraSpace: true negativeExtraSpace: true,
noSpaceAfterFlags: true
}; };
var path2js = require('./_path.js').path2js, var path2js = require('./_path.js').path2js,

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 22 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 22 KiB