mirror of
https://github.com/svg/svgo.git
synced 2025-07-31 07:44:22 +03:00
Remove spaces after ‘arcto’ path command flags
This commit is contained in:
@ -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;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
@ -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;
|
||||
}, '');
|
||||
|
||||
};
|
||||
|
@ -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;
|
||||
}, '');
|
||||
}
|
||||
|
@ -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,
|
||||
|
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 |
Reference in New Issue
Block a user