schnee effeckt und fehler Korektur
This commit is contained in:
225
node_modules/svgo/plugins/cleanupListOfValues.js
generated
vendored
225
node_modules/svgo/plugins/cleanupListOfValues.js
generated
vendored
@@ -1,29 +1,25 @@
|
||||
'use strict';
|
||||
|
||||
exports.type = 'perItem';
|
||||
const { removeLeadingZero } = require('../lib/svgo/tools.js');
|
||||
|
||||
exports.name = 'cleanupListOfValues';
|
||||
exports.type = 'visitor';
|
||||
exports.active = false;
|
||||
|
||||
exports.description = 'rounds list of values to the fixed precision';
|
||||
|
||||
exports.params = {
|
||||
floatPrecision: 3,
|
||||
leadingZero: true,
|
||||
defaultPx: true,
|
||||
convertToPx: true
|
||||
const regNumericValues =
|
||||
/^([-+]?\d*\.?\d+([eE][-+]?\d+)?)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/;
|
||||
const regSeparator = /\s+,?\s*|,\s*/;
|
||||
const absoluteLengths = {
|
||||
// relative to px
|
||||
cm: 96 / 2.54,
|
||||
mm: 96 / 25.4,
|
||||
in: 96,
|
||||
pt: 4 / 3,
|
||||
pc: 16,
|
||||
px: 1,
|
||||
};
|
||||
|
||||
var regNumericValues = /^([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/,
|
||||
regSeparator = /\s+,?\s*|,\s*/,
|
||||
removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero,
|
||||
absoluteLengths = { // relative to px
|
||||
cm: 96/2.54,
|
||||
mm: 96/25.4,
|
||||
in: 96,
|
||||
pt: 4/3,
|
||||
pc: 16
|
||||
};
|
||||
|
||||
/**
|
||||
* Round list of values to the fixed precision.
|
||||
*
|
||||
@@ -32,110 +28,127 @@ var regNumericValues = /^([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)(px|pt|pc|mm|cm|m|in|f
|
||||
* ⬇
|
||||
* <svg viewBox="0 0 200.284 200.284" enable-background="new 0 0 200.284 200.284">
|
||||
*
|
||||
*
|
||||
* <polygon points="208.250977 77.1308594 223.069336 ... "/>
|
||||
* ⬇
|
||||
* <polygon points="208.251 77.131 223.069 ... "/>
|
||||
*
|
||||
*
|
||||
* @param {Object} item current iteration item
|
||||
* @param {Object} params plugin params
|
||||
* @return {Boolean} if false, item will be filtered out
|
||||
*
|
||||
* @author kiyopikko
|
||||
*
|
||||
* @type {import('../lib/types').Plugin<{
|
||||
* floatPrecision?: number,
|
||||
* leadingZero?: boolean,
|
||||
* defaultPx?: boolean,
|
||||
* convertToPx?: boolean
|
||||
* }>}
|
||||
*/
|
||||
exports.fn = function(item, params) {
|
||||
exports.fn = (_root, params) => {
|
||||
const {
|
||||
floatPrecision = 3,
|
||||
leadingZero = true,
|
||||
defaultPx = true,
|
||||
convertToPx = true,
|
||||
} = params;
|
||||
|
||||
/**
|
||||
* @type {(lists: string) => string}
|
||||
*/
|
||||
const roundValues = (lists) => {
|
||||
const roundedList = [];
|
||||
|
||||
if ( item.hasAttr('points') ) {
|
||||
roundValues(item.attrs.points);
|
||||
for (const elem of lists.split(regSeparator)) {
|
||||
const match = elem.match(regNumericValues);
|
||||
const matchNew = elem.match(/new/);
|
||||
|
||||
// if attribute value matches regNumericValues
|
||||
if (match) {
|
||||
// round it to the fixed precision
|
||||
let num = Number(Number(match[1]).toFixed(floatPrecision));
|
||||
/**
|
||||
* @type {any}
|
||||
*/
|
||||
let matchedUnit = match[3] || '';
|
||||
/**
|
||||
* @type{'' | keyof typeof absoluteLengths}
|
||||
*/
|
||||
let units = matchedUnit;
|
||||
|
||||
// convert absolute values to pixels
|
||||
if (convertToPx && units && units in absoluteLengths) {
|
||||
const pxNum = Number(
|
||||
(absoluteLengths[units] * Number(match[1])).toFixed(floatPrecision)
|
||||
);
|
||||
|
||||
if (pxNum.toString().length < match[0].length) {
|
||||
num = pxNum;
|
||||
units = 'px';
|
||||
}
|
||||
}
|
||||
|
||||
// and remove leading zero
|
||||
let str;
|
||||
if (leadingZero) {
|
||||
str = removeLeadingZero(num);
|
||||
} else {
|
||||
str = num.toString();
|
||||
}
|
||||
|
||||
// remove default 'px' units
|
||||
if (defaultPx && units === 'px') {
|
||||
units = '';
|
||||
}
|
||||
|
||||
roundedList.push(str + units);
|
||||
}
|
||||
// if attribute value is "new"(only enable-background).
|
||||
else if (matchNew) {
|
||||
roundedList.push('new');
|
||||
} else if (elem) {
|
||||
roundedList.push(elem);
|
||||
}
|
||||
}
|
||||
|
||||
if ( item.hasAttr('enable-background') ) {
|
||||
roundValues(item.attrs['enable-background']);
|
||||
}
|
||||
return roundedList.join(' ');
|
||||
};
|
||||
|
||||
if ( item.hasAttr('viewBox') ) {
|
||||
roundValues(item.attrs.viewBox);
|
||||
}
|
||||
return {
|
||||
element: {
|
||||
enter: (node) => {
|
||||
if (node.attributes.points != null) {
|
||||
node.attributes.points = roundValues(node.attributes.points);
|
||||
}
|
||||
|
||||
if ( item.hasAttr('stroke-dasharray') ) {
|
||||
roundValues(item.attrs['stroke-dasharray']);
|
||||
}
|
||||
if (node.attributes['enable-background'] != null) {
|
||||
node.attributes['enable-background'] = roundValues(
|
||||
node.attributes['enable-background']
|
||||
);
|
||||
}
|
||||
|
||||
if ( item.hasAttr('dx') ) {
|
||||
roundValues(item.attrs.dx);
|
||||
}
|
||||
if (node.attributes.viewBox != null) {
|
||||
node.attributes.viewBox = roundValues(node.attributes.viewBox);
|
||||
}
|
||||
|
||||
if ( item.hasAttr('dy') ) {
|
||||
roundValues(item.attrs.dy);
|
||||
}
|
||||
if (node.attributes['stroke-dasharray'] != null) {
|
||||
node.attributes['stroke-dasharray'] = roundValues(
|
||||
node.attributes['stroke-dasharray']
|
||||
);
|
||||
}
|
||||
|
||||
if ( item.hasAttr('x') ) {
|
||||
roundValues(item.attrs.x);
|
||||
}
|
||||
if (node.attributes.dx != null) {
|
||||
node.attributes.dx = roundValues(node.attributes.dx);
|
||||
}
|
||||
|
||||
if ( item.hasAttr('y') ) {
|
||||
roundValues(item.attrs.y);
|
||||
}
|
||||
if (node.attributes.dy != null) {
|
||||
node.attributes.dy = roundValues(node.attributes.dy);
|
||||
}
|
||||
|
||||
if (node.attributes.x != null) {
|
||||
node.attributes.x = roundValues(node.attributes.x);
|
||||
}
|
||||
|
||||
function roundValues($prop){
|
||||
|
||||
var num, units,
|
||||
match,
|
||||
matchNew,
|
||||
lists = $prop.value,
|
||||
listsArr = lists.split(regSeparator),
|
||||
roundedListArr = [],
|
||||
roundedList;
|
||||
|
||||
listsArr.forEach(function(elem){
|
||||
|
||||
match = elem.match(regNumericValues);
|
||||
matchNew = elem.match(/new/);
|
||||
|
||||
// if attribute value matches regNumericValues
|
||||
if(match){
|
||||
|
||||
// round it to the fixed precision
|
||||
num = +(+match[1]).toFixed(params.floatPrecision),
|
||||
units = match[3] || '';
|
||||
|
||||
// convert absolute values to pixels
|
||||
if (params.convertToPx && units && (units in absoluteLengths)) {
|
||||
var pxNum = +(absoluteLengths[units] * match[1]).toFixed(params.floatPrecision);
|
||||
|
||||
if (String(pxNum).length < match[0].length)
|
||||
num = pxNum,
|
||||
units = 'px';
|
||||
}
|
||||
|
||||
// and remove leading zero
|
||||
if (params.leadingZero) {
|
||||
num = removeLeadingZero(num);
|
||||
}
|
||||
|
||||
// remove default 'px' units
|
||||
if (params.defaultPx && units === 'px') {
|
||||
units = '';
|
||||
}
|
||||
|
||||
roundedListArr.push(num+units);
|
||||
|
||||
}
|
||||
// if attribute value is "new"(only enable-background).
|
||||
else if(matchNew){
|
||||
|
||||
roundedListArr.push('new');
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
roundedList = roundedListArr.join(' ');
|
||||
$prop.value = roundedList;
|
||||
|
||||
}
|
||||
|
||||
if (node.attributes.y != null) {
|
||||
node.attributes.y = roundValues(node.attributes.y);
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user