Skip to content

Commit

Permalink
Remove spaces after ‘arcto’ path command flags
Browse files Browse the repository at this point in the history
  • Loading branch information
GreLI committed Jul 13, 2019
1 parent 95f7603 commit 258fecf
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 66 deletions.
60 changes: 25 additions & 35 deletions lib/svgo/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

};

/**
Expand All @@ -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;
};

Expand All @@ -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
Expand All @@ -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;

};

/**
Expand All @@ -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;

};


Expand Down
6 changes: 5 additions & 1 deletion plugins/_path.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}, '');

};
Expand Down
7 changes: 6 additions & 1 deletion plugins/convertPathData.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ exports.params = {
utilizeAbsolute: true,
leadingZero: true,
negativeExtraSpace: true,
noSpaceAfterFlags: true,
forceAbsolutePath: false
};

Expand Down Expand Up @@ -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;
}, '');
}
3 changes: 2 additions & 1 deletion plugins/mergePaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/coa/testSvg/test.1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/coa/testSvg/test.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plugins/convertPathData.02.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions test/plugins/convertPathData.03.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plugins/convertPathData.04.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 9 additions & 9 deletions test/plugins/convertPathData.11.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions test/plugins/convertPathData.14.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plugins/convertPathData.15.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plugins/convertPathData.16.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plugins/convertPathData.18.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plugins/convertPathData.20.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plugins/mergePaths.03.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 258fecf

Please sign in to comment.