2020-10-17 13:01:56 +02:00
|
|
|
const fs = require('fs');
|
|
|
|
|
2019-07-03 23:33:03 +02:00
|
|
|
const data = require("./_data/simple-icons.json");
|
|
|
|
const { htmlFriendlyToTitle } = require("./scripts/utils.js");
|
2021-03-02 20:00:18 +02:00
|
|
|
const svgpath = require("svgpath");
|
2021-06-03 21:15:21 +02:00
|
|
|
const svgPathBbox = require("svg-path-bbox");
|
2021-03-02 20:00:18 +02:00
|
|
|
const parsePath = require("svg-path-segments");
|
2019-07-03 23:33:03 +02:00
|
|
|
|
2020-07-05 15:57:00 +02:00
|
|
|
const svgRegexp = /^<svg( [^\s]*=".*"){3}><title>.*<\/title><path d=".*"\/><\/svg>\r?\n?$/;
|
2020-12-14 21:35:27 +02:00
|
|
|
const negativeZerosRegexp = /-0(?=[^\.]|[\s\d\w]|$)/g;
|
2020-06-22 18:24:56 +02:00
|
|
|
|
2020-06-10 11:59:42 +02:00
|
|
|
const iconSize = 24;
|
|
|
|
const iconFloatPrecision = 3;
|
2020-11-19 22:49:49 +02:00
|
|
|
const iconMaxFloatPrecision = 5;
|
2020-07-10 10:06:08 +02:00
|
|
|
const iconTolerance = 0.001;
|
2020-10-17 13:01:56 +02:00
|
|
|
|
|
|
|
// set env SI_UPDATE_IGNORE to recreate the ignore file
|
2021-01-15 22:47:00 +02:00
|
|
|
const updateIgnoreFile = process.env.SI_UPDATE_IGNORE === 'true';
|
2020-10-17 13:01:56 +02:00
|
|
|
const ignoreFile = "./.svglint-ignored.json";
|
|
|
|
const iconIgnored = !updateIgnoreFile ? require(ignoreFile) : {};
|
|
|
|
|
|
|
|
function sortObjectByKey(obj) {
|
|
|
|
return Object
|
|
|
|
.keys(obj)
|
|
|
|
.sort()
|
|
|
|
.reduce((r, k) => Object.assign(r, { [k]: obj[k] }), {});
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortObjectByValue(obj) {
|
|
|
|
return Object
|
|
|
|
.keys(obj)
|
|
|
|
.sort((a, b) => ('' + obj[a]).localeCompare(obj[b]))
|
|
|
|
.reduce((r, k) => Object.assign(r, { [k]: obj[k] }), {});
|
|
|
|
}
|
|
|
|
|
2020-12-13 21:29:01 +02:00
|
|
|
function removeLeadingZeros(number) {
|
|
|
|
// convert 0.03 to '.03'
|
|
|
|
return number.toString().replace(/^(-?)(0)(\.?.+)/, '$1$3');
|
|
|
|
}
|
|
|
|
|
2021-01-03 19:08:06 +02:00
|
|
|
/**
|
|
|
|
* Given three points, returns if the middle one (x2, y2) is collinear
|
|
|
|
* to the line formed by the two limit points.
|
|
|
|
**/
|
|
|
|
function collinear(x1, y1, x2, y2, x3, y3) {
|
|
|
|
return (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) === 0;
|
|
|
|
}
|
|
|
|
|
2021-03-02 20:00:18 +02:00
|
|
|
/**
|
|
|
|
* Returns the number of digits after the decimal point.
|
|
|
|
* @param num The number of interest.
|
|
|
|
*/
|
|
|
|
function countDecimals(num) {
|
|
|
|
if (num && num % 1) {
|
|
|
|
let [base, op, trail] = num.toExponential().split(/e([+-])/);
|
|
|
|
let elen = parseInt(trail, 10);
|
|
|
|
let idx = base.indexOf('.');
|
|
|
|
return idx == -1 ? elen : base.length - idx - 1 + (op === '+' ? -elen : elen);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the index at which the first path value of an SVG starts.
|
|
|
|
* @param svgFileContent The raw SVG as text.
|
|
|
|
*/
|
|
|
|
function getPathDIndex(svgFileContent) {
|
|
|
|
const pathDStart = '<path d="';
|
|
|
|
return svgFileContent.indexOf(pathDStart) + pathDStart.length;
|
|
|
|
}
|
|
|
|
|
2020-10-17 13:01:56 +02:00
|
|
|
if (updateIgnoreFile) {
|
|
|
|
process.on('exit', () => {
|
|
|
|
// ensure object output order is consistent due to async svglint processing
|
|
|
|
const sorted = sortObjectByKey(iconIgnored)
|
|
|
|
for (const linterName in sorted) {
|
|
|
|
sorted[linterName] = sortObjectByValue(sorted[linterName])
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
ignoreFile,
|
|
|
|
JSON.stringify(sorted, null, 2) + '\n',
|
|
|
|
{flag: 'w'}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function isIgnored(linterName, path) {
|
2020-12-13 21:29:01 +02:00
|
|
|
return iconIgnored[linterName] && iconIgnored[linterName].hasOwnProperty(path);
|
2020-10-17 13:01:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function ignoreIcon(linterName, path, $) {
|
|
|
|
if (!iconIgnored[linterName]) {
|
|
|
|
iconIgnored[linterName] = {};
|
|
|
|
}
|
|
|
|
|
2021-05-26 22:20:20 +02:00
|
|
|
const title = $.find("title").text();
|
2020-10-17 13:01:56 +02:00
|
|
|
const iconName = htmlFriendlyToTitle(title);
|
|
|
|
|
|
|
|
iconIgnored[linterName][path] = iconName;
|
|
|
|
}
|
2019-07-03 23:33:03 +02:00
|
|
|
|
2018-08-16 11:33:32 +02:00
|
|
|
module.exports = {
|
|
|
|
rules: {
|
|
|
|
elm: {
|
|
|
|
"svg": 1,
|
|
|
|
"svg > title": 1,
|
2019-03-28 21:40:31 +02:00
|
|
|
"svg > path": 1,
|
|
|
|
"*": false,
|
2018-08-16 11:33:32 +02:00
|
|
|
},
|
|
|
|
attr: [
|
|
|
|
{ // ensure that the SVG elm has the appropriate attrs
|
|
|
|
"role": "img",
|
2020-06-10 11:59:42 +02:00
|
|
|
"viewBox": `0 0 ${iconSize} ${iconSize}`,
|
2018-08-16 11:33:32 +02:00
|
|
|
"xmlns": "http://www.w3.org/2000/svg",
|
|
|
|
"rule::selector": "svg",
|
|
|
|
"rule::whitelist": true,
|
|
|
|
},
|
|
|
|
{ // ensure that the title elm has the appropriate attr
|
|
|
|
"rule::selector": "svg > title",
|
|
|
|
"rule::whitelist": true,
|
2019-03-20 09:55:03 +02:00
|
|
|
},
|
|
|
|
{ // ensure that the path element only has the 'd' attr (no style, opacity, etc.)
|
|
|
|
"d": /^[,a-zA-Z0-9\. -]+$/,
|
|
|
|
"rule::selector": "svg > path",
|
|
|
|
"rule::whitelist": true,
|
2018-08-16 11:33:32 +02:00
|
|
|
}
|
2019-07-03 23:33:03 +02:00
|
|
|
],
|
|
|
|
custom: [
|
|
|
|
function(reporter, $, ast) {
|
2020-06-10 11:59:42 +02:00
|
|
|
reporter.name = "icon-title";
|
|
|
|
|
2019-07-03 23:33:03 +02:00
|
|
|
const iconTitleText = $.find("title").text();
|
2021-05-26 22:20:20 +02:00
|
|
|
const iconName = htmlFriendlyToTitle(iconTitleText);
|
|
|
|
const iconExists = data.icons.some(icon => icon.title === iconName);
|
|
|
|
if (!iconExists) {
|
|
|
|
reporter.error(`No icon with title "${iconName}" found in simple-icons.json`);
|
2019-07-03 23:33:03 +02:00
|
|
|
}
|
|
|
|
},
|
2020-06-10 11:59:42 +02:00
|
|
|
function(reporter, $, ast) {
|
|
|
|
reporter.name = "icon-size";
|
|
|
|
|
|
|
|
const iconPath = $.find("path").attr("d");
|
2020-10-17 13:01:56 +02:00
|
|
|
if (!updateIgnoreFile && isIgnored(reporter.name, iconPath)) {
|
2020-06-10 11:59:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-23 12:36:10 +02:00
|
|
|
const [minX, minY, maxX, maxY] = svgPathBbox(iconPath);
|
|
|
|
const width = +(maxX - minX).toFixed(iconFloatPrecision);
|
|
|
|
const height = +(maxY - minY).toFixed(iconFloatPrecision);
|
2020-06-10 11:59:42 +02:00
|
|
|
|
|
|
|
if (width === 0 && height === 0) {
|
|
|
|
reporter.error("Path bounds were reported as 0 x 0; check if the path is valid");
|
2020-10-17 13:01:56 +02:00
|
|
|
if (updateIgnoreFile) {
|
|
|
|
ignoreIcon(reporter.name, iconPath, $);
|
|
|
|
}
|
2020-06-10 11:59:42 +02:00
|
|
|
} else if (width !== iconSize && height !== iconSize) {
|
|
|
|
reporter.error(`Size of <path> must be exactly ${iconSize} in one dimension; the size is currently ${width} x ${height}`);
|
2020-10-17 13:01:56 +02:00
|
|
|
if (updateIgnoreFile) {
|
|
|
|
ignoreIcon(reporter.name, iconPath, $);
|
|
|
|
}
|
2020-06-10 11:59:42 +02:00
|
|
|
}
|
|
|
|
},
|
2020-11-19 22:49:49 +02:00
|
|
|
function(reporter, $, ast) {
|
|
|
|
reporter.name = "icon-precision";
|
|
|
|
|
|
|
|
const iconPath = $.find("path").attr("d");
|
|
|
|
if (!updateIgnoreFile && isIgnored(reporter.name, iconPath)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-13 21:29:01 +02:00
|
|
|
|
2021-03-02 20:00:18 +02:00
|
|
|
const segments = parsePath(iconPath),
|
|
|
|
svgFileContent = $.html();
|
|
|
|
|
|
|
|
segments.forEach((segment) => {
|
|
|
|
const precisionMax = Math.max(...segment.params.slice(1).map(countDecimals));
|
|
|
|
if (precisionMax > iconMaxFloatPrecision) {
|
|
|
|
let errorMsg = `found ${precisionMax} decimals in segment "${iconPath.substring(segment.start, segment.end)}"`;
|
|
|
|
if (segment.chained) {
|
2021-06-03 12:12:39 +02:00
|
|
|
let readableChain = iconPath.substring(segment.chainStart, segment.chainEnd);
|
2021-03-02 20:00:18 +02:00
|
|
|
if (readableChain.length > 20) {
|
|
|
|
readableChain = `${readableChain.substring(0, 20)}...`;
|
|
|
|
}
|
|
|
|
errorMsg += ` of chain "${readableChain}"`
|
|
|
|
}
|
|
|
|
errorMsg += ` at index ${segment.start + getPathDIndex(svgFileContent)}`;
|
|
|
|
reporter.error(`Maximum precision should not be greater than ${iconMaxFloatPrecision}; ${errorMsg}`);
|
|
|
|
if (updateIgnoreFile) {
|
|
|
|
ignoreIcon(reporter.name, iconPath, $);
|
|
|
|
}
|
2020-11-19 22:49:49 +02:00
|
|
|
}
|
2021-03-02 20:00:18 +02:00
|
|
|
})
|
2020-11-19 22:49:49 +02:00
|
|
|
},
|
2020-12-13 21:29:01 +02:00
|
|
|
function(reporter, $, ast) {
|
|
|
|
reporter.name = "ineffective-segments";
|
|
|
|
|
|
|
|
const iconPath = $.find("path").attr("d");
|
|
|
|
if (!updateIgnoreFile && isIgnored(reporter.name, iconPath)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-02 20:00:18 +02:00
|
|
|
const segments = parsePath(iconPath);
|
|
|
|
const absSegments = svgpath(iconPath).abs().unshort().segments;
|
2020-12-13 21:29:01 +02:00
|
|
|
|
|
|
|
const lowerMovementCommands = ['m', 'l'];
|
|
|
|
const lowerDirectionCommands = ['h', 'v'];
|
|
|
|
const lowerCurveCommand = 'c';
|
|
|
|
const lowerShorthandCurveCommand = 's';
|
|
|
|
const lowerCurveCommands = [lowerCurveCommand, lowerShorthandCurveCommand];
|
|
|
|
const upperMovementCommands = ['M', 'L'];
|
|
|
|
const upperHorDirectionCommand = 'H';
|
|
|
|
const upperVerDirectionCommand = 'V';
|
|
|
|
const upperDirectionCommands = [upperHorDirectionCommand, upperVerDirectionCommand];
|
|
|
|
const upperCurveCommand = 'C';
|
|
|
|
const upperShorthandCurveCommand = 'S';
|
|
|
|
const upperCurveCommands = [upperCurveCommand, upperShorthandCurveCommand];
|
|
|
|
const curveCommands = [...lowerCurveCommands, ...upperCurveCommands];
|
|
|
|
const commands = [...lowerMovementCommands, ...lowerDirectionCommands, ...upperMovementCommands, ...upperDirectionCommands, ...curveCommands];
|
2021-03-02 20:00:18 +02:00
|
|
|
const isInvalidSegment = ([command, x1Coord, y1Coord, ...rest], index) => {
|
2020-12-13 21:29:01 +02:00
|
|
|
if (commands.includes(command)) {
|
|
|
|
// Relative directions (h or v) having a length of 0
|
|
|
|
if (lowerDirectionCommands.includes(command) && x1Coord === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Relative movement (m or l) having a distance of 0
|
2021-03-02 20:00:18 +02:00
|
|
|
if (index > 0 && lowerMovementCommands.includes(command) && x1Coord === 0 && y1Coord === 0) {
|
2020-12-13 21:29:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (lowerCurveCommands.includes(command) && x1Coord === 0 && y1Coord === 0) {
|
|
|
|
const [x2Coord, y2Coord] = rest;
|
|
|
|
if (
|
|
|
|
// Relative shorthand curve (s) having a control point of 0
|
|
|
|
command === lowerShorthandCurveCommand ||
|
|
|
|
// Relative bézier curve (c) having control points of 0
|
|
|
|
(command === lowerCurveCommand && x2Coord === 0 && y2Coord === 0)
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index > 0) {
|
2021-01-15 22:47:00 +02:00
|
|
|
let [yPrevCoord, xPrevCoord] = [...absSegments[index - 1]].reverse();
|
2020-12-13 21:29:01 +02:00
|
|
|
// If the previous command was a direction one, we need to iterate back until we find the missing coordinates
|
|
|
|
if (upperDirectionCommands.includes(xPrevCoord)) {
|
|
|
|
xPrevCoord = undefined;
|
|
|
|
yPrevCoord = undefined;
|
|
|
|
let idx = index;
|
|
|
|
while (--idx > 0 && (xPrevCoord === undefined || yPrevCoord === undefined)) {
|
2021-01-15 22:47:00 +02:00
|
|
|
let [yPrevCoordDeep, xPrevCoordDeep] = [...absSegments[idx]].reverse();
|
2020-12-13 21:29:01 +02:00
|
|
|
// If the previous command was a horizontal movement, we need to consider the single coordinate as x
|
|
|
|
if (upperHorDirectionCommand === xPrevCoordDeep) {
|
|
|
|
xPrevCoordDeep = yPrevCoordDeep;
|
|
|
|
yPrevCoordDeep = undefined;
|
|
|
|
}
|
|
|
|
// If the previous command was a vertical movement, we need to consider the single coordinate as y
|
|
|
|
if (upperVerDirectionCommand === xPrevCoordDeep) {
|
|
|
|
xPrevCoordDeep = undefined;
|
|
|
|
}
|
|
|
|
if (xPrevCoord === undefined && xPrevCoordDeep !== undefined) {
|
|
|
|
xPrevCoord = xPrevCoordDeep;
|
|
|
|
}
|
|
|
|
if (yPrevCoord === undefined && yPrevCoordDeep !== undefined) {
|
|
|
|
yPrevCoord = yPrevCoordDeep;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (upperCurveCommands.includes(command)) {
|
|
|
|
const [x2Coord, y2Coord, xCoord, yCoord] = rest;
|
|
|
|
// Absolute shorthand curve (S) having the same coordinate as the previous segment and a control point equal to the ending point
|
|
|
|
if (upperShorthandCurveCommand === command && x1Coord === xPrevCoord && y1Coord === yPrevCoord && x1Coord === x2Coord && y1Coord === y2Coord) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Absolute bézier curve (C) having the same coordinate as the previous segment and last control point equal to the ending point
|
|
|
|
if (upperCurveCommand === command && x1Coord === xPrevCoord && y1Coord === yPrevCoord && x2Coord === xCoord && y2Coord === yCoord) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
// Absolute horizontal direction (H) having the same x coordinate as the previous segment
|
|
|
|
(upperHorDirectionCommand === command && x1Coord === xPrevCoord) ||
|
|
|
|
// Absolute vertical direction (V) having the same y coordinate as the previous segment
|
|
|
|
(upperVerDirectionCommand === command && x1Coord === yPrevCoord) ||
|
|
|
|
// Absolute movement (M or L) having the same coordinate as the previous segment
|
|
|
|
(upperMovementCommands.includes(command) && x1Coord === xPrevCoord && y1Coord === yPrevCoord)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-02 20:00:18 +02:00
|
|
|
const svgFileContent = $.html();
|
|
|
|
|
|
|
|
segments.forEach((segment, index) => {
|
|
|
|
if (isInvalidSegment(segment.params, index)) {
|
|
|
|
const [command, x1, y1, ...rest] = segment.params;
|
|
|
|
|
|
|
|
let errorMsg = `Innefective segment "${iconPath.substring(segment.start, segment.end)}" found`,
|
2020-12-13 21:29:01 +02:00
|
|
|
resolutionTip = 'should be removed';
|
2021-03-02 20:00:18 +02:00
|
|
|
|
2020-12-13 21:29:01 +02:00
|
|
|
if (curveCommands.includes(command)) {
|
2021-03-02 20:00:18 +02:00
|
|
|
const [x2, y2, x, y] = rest;
|
|
|
|
|
|
|
|
if (command === lowerShorthandCurveCommand && (x2 !== 0 || y2 !== 0)) {
|
|
|
|
resolutionTip = `should be "l${removeLeadingZeros(x2)} ${removeLeadingZeros(y2)}" or removed`;
|
2020-12-13 21:29:01 +02:00
|
|
|
}
|
|
|
|
if (command === upperShorthandCurveCommand) {
|
2021-03-02 20:00:18 +02:00
|
|
|
resolutionTip = `should be "L${removeLeadingZeros(x2)} ${removeLeadingZeros(y2)}" or removed`;
|
2020-12-13 21:29:01 +02:00
|
|
|
}
|
2021-03-02 20:00:18 +02:00
|
|
|
if (command === lowerCurveCommand && (x !== 0 || y !== 0)) {
|
|
|
|
resolutionTip = `should be "l${removeLeadingZeros(x)} ${removeLeadingZeros(y)}" or removed`;
|
2020-12-13 21:29:01 +02:00
|
|
|
}
|
|
|
|
if (command === upperCurveCommand) {
|
2021-03-02 20:00:18 +02:00
|
|
|
resolutionTip = `should be "L${removeLeadingZeros(x)} ${removeLeadingZeros(y)}" or removed`;
|
2020-12-13 21:29:01 +02:00
|
|
|
}
|
2021-03-02 20:00:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (segment.chained) {
|
|
|
|
let readableChain = iconPath.substring(segment.chainStart, segment.chainEnd);
|
|
|
|
if (readableChain.length > 20) {
|
|
|
|
readableChain = `${chain.substring(0, 20)}...`
|
|
|
|
}
|
|
|
|
errorMsg += ` in chain "${readableChain}"`
|
|
|
|
}
|
|
|
|
errorMsg += ` at index ${segment.start + getPathDIndex(svgFileContent)}`;
|
|
|
|
|
|
|
|
reporter.error(`${errorMsg} (${resolutionTip})`);
|
|
|
|
|
|
|
|
if (updateIgnoreFile) {
|
|
|
|
ignoreIcon(reporter.name, iconPath, $);
|
2020-12-13 21:29:01 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-02 20:00:18 +02:00
|
|
|
})
|
2020-12-13 21:29:01 +02:00
|
|
|
},
|
2021-01-03 19:08:06 +02:00
|
|
|
function(reporter, $, ast) {
|
|
|
|
reporter.name = "collinear-segments";
|
|
|
|
|
|
|
|
const iconPath = $.find("path").attr("d");
|
|
|
|
if (!updateIgnoreFile && isIgnored(reporter.name, iconPath)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-15 22:47:00 +02:00
|
|
|
|
2021-01-03 19:08:06 +02:00
|
|
|
/**
|
|
|
|
* Extracts collinear coordinates from SVG path straight lines
|
|
|
|
* (does not extracts collinear coordinates from curves).
|
|
|
|
**/
|
2021-03-02 20:00:18 +02:00
|
|
|
const getCollinearSegments = (iconPath) => {
|
|
|
|
const segments = parsePath(iconPath),
|
2021-01-03 19:08:06 +02:00
|
|
|
collinearSegments = [],
|
|
|
|
straightLineCommands = 'HhVvLlMm',
|
|
|
|
zCommands = 'Zz';
|
2021-03-02 20:00:18 +02:00
|
|
|
|
2021-01-03 19:08:06 +02:00
|
|
|
let currLine = [],
|
|
|
|
currAbsCoord = [undefined, undefined],
|
2021-01-15 12:16:50 +02:00
|
|
|
startPoint,
|
2021-01-03 19:08:06 +02:00
|
|
|
_inStraightLine = false,
|
2021-01-15 12:16:50 +02:00
|
|
|
_nextInStraightLine = false,
|
|
|
|
_resetStartPoint = false;
|
2021-01-03 19:08:06 +02:00
|
|
|
|
|
|
|
for (let s = 0; s < segments.length; s++) {
|
2021-03-02 20:00:18 +02:00
|
|
|
let seg = segments[s].params,
|
2021-01-03 19:08:06 +02:00
|
|
|
cmd = seg[0],
|
|
|
|
nextCmd = s + 1 < segments.length ? segments[s + 1][0] : null;
|
2021-01-15 22:47:00 +02:00
|
|
|
|
2021-03-10 19:52:08 +02:00
|
|
|
if (cmd === 'L') {
|
2021-01-04 12:05:20 +02:00
|
|
|
currAbsCoord[0] = seg[1];
|
|
|
|
currAbsCoord[1] = seg[2];
|
2021-03-10 19:52:08 +02:00
|
|
|
} else if (cmd === 'l') {
|
2021-01-04 12:05:20 +02:00
|
|
|
currAbsCoord[0] = (!currAbsCoord[0] ? 0 : currAbsCoord[0]) + seg[1];
|
|
|
|
currAbsCoord[1] = (!currAbsCoord[1] ? 0 : currAbsCoord[1]) + seg[2];
|
2021-03-10 19:52:08 +02:00
|
|
|
} else if (cmd === 'm') {
|
|
|
|
currAbsCoord[0] = (!currAbsCoord[0] ? 0 : currAbsCoord[0]) + seg[1];
|
|
|
|
currAbsCoord[1] = (!currAbsCoord[1] ? 0 : currAbsCoord[1]) + seg[2];
|
|
|
|
startPoint = undefined;
|
|
|
|
} else if (cmd === 'M') {
|
|
|
|
currAbsCoord[0] = seg[1];
|
|
|
|
currAbsCoord[1] = seg[2];
|
|
|
|
startPoint = undefined;
|
2021-01-04 12:05:20 +02:00
|
|
|
} else if (cmd === 'H') {
|
|
|
|
currAbsCoord[0] = seg[1];
|
|
|
|
} else if (cmd === 'h') {
|
|
|
|
currAbsCoord[0] = (!currAbsCoord[0] ? 0 : currAbsCoord[0]) + seg[1];
|
|
|
|
} else if (cmd === 'V') {
|
|
|
|
currAbsCoord[1] = seg[1];
|
|
|
|
} else if (cmd === 'v') {
|
|
|
|
currAbsCoord[1] = (!currAbsCoord[1] ? 0 : currAbsCoord[1]) + seg[1];
|
|
|
|
} else if (cmd === 'C') {
|
|
|
|
currAbsCoord[0] = seg[5];
|
|
|
|
currAbsCoord[1] = seg[6];
|
2021-03-02 20:00:18 +02:00
|
|
|
} else if (cmd === "a") {
|
|
|
|
currAbsCoord[0] = (!currAbsCoord[0] ? 0 : currAbsCoord[0]) + seg[6];
|
|
|
|
currAbsCoord[1] = (!currAbsCoord[1] ? 0 : currAbsCoord[1]) + seg[7];
|
|
|
|
} else if (cmd === "A") {
|
|
|
|
currAbsCoord[0] = seg[6];
|
|
|
|
currAbsCoord[1] = seg[7];
|
|
|
|
} else if (cmd === "s") {
|
|
|
|
currAbsCoord[0] = (!currAbsCoord[0] ? 0 : currAbsCoord[0]) + seg[1];
|
|
|
|
currAbsCoord[1] = (!currAbsCoord[1] ? 0 : currAbsCoord[1]) + seg[2];
|
|
|
|
} else if (cmd === "S") {
|
|
|
|
currAbsCoord[0] = seg[1];
|
|
|
|
currAbsCoord[1] = seg[2];
|
|
|
|
} else if (cmd === "t") {
|
|
|
|
currAbsCoord[0] = (!currAbsCoord[0] ? 0 : currAbsCoord[0]) + seg[1];
|
|
|
|
currAbsCoord[1] = (!currAbsCoord[1] ? 0 : currAbsCoord[1]) + seg[2];
|
|
|
|
} else if (cmd === "T") {
|
|
|
|
currAbsCoord[0] = seg[1];
|
|
|
|
currAbsCoord[1] = seg[2];
|
2021-01-04 12:05:20 +02:00
|
|
|
} else if (cmd === 'c') {
|
|
|
|
currAbsCoord[0] = (!currAbsCoord[0] ? 0 : currAbsCoord[0]) + seg[5];
|
|
|
|
currAbsCoord[1] = (!currAbsCoord[1] ? 0 : currAbsCoord[1]) + seg[6];
|
|
|
|
} else if (cmd === 'Q') {
|
|
|
|
currAbsCoord[0] = seg[3];
|
|
|
|
currAbsCoord[1] = seg[4];
|
|
|
|
} else if (cmd === 'q') {
|
|
|
|
currAbsCoord[0] = (!currAbsCoord[0] ? 0 : currAbsCoord[0]) + seg[3];
|
|
|
|
currAbsCoord[1] = (!currAbsCoord[1] ? 0 : currAbsCoord[1]) + seg[4];
|
|
|
|
} else if (zCommands.includes(cmd)) {
|
|
|
|
// Overlapping in Z should be handled in another rule
|
2021-01-15 12:16:50 +02:00
|
|
|
currAbsCoord = [startPoint[0], startPoint[1]];
|
|
|
|
_resetStartPoint = true;
|
2021-01-04 12:05:20 +02:00
|
|
|
} else {
|
2021-01-15 22:47:00 +02:00
|
|
|
throw new Error(`"${cmd}" command not handled`);
|
2021-01-04 12:05:20 +02:00
|
|
|
}
|
|
|
|
|
2021-01-15 12:16:50 +02:00
|
|
|
if (startPoint === undefined) {
|
|
|
|
startPoint = [currAbsCoord[0], currAbsCoord[1]];
|
|
|
|
} else if (_resetStartPoint) {
|
|
|
|
startPoint = undefined;
|
|
|
|
_resetStartPoint = false;
|
|
|
|
}
|
|
|
|
|
2021-01-03 19:08:06 +02:00
|
|
|
_nextInStraightLine = straightLineCommands.includes(nextCmd);
|
|
|
|
let _exitingStraightLine = (_inStraightLine && !_nextInStraightLine);
|
|
|
|
_inStraightLine = straightLineCommands.includes(cmd);
|
|
|
|
|
|
|
|
if (_inStraightLine) {
|
|
|
|
currLine.push([currAbsCoord[0], currAbsCoord[1]]);
|
|
|
|
} else {
|
|
|
|
if (_exitingStraightLine) {
|
2021-01-04 12:05:20 +02:00
|
|
|
if (straightLineCommands.includes(cmd)) {
|
2021-01-03 19:08:06 +02:00
|
|
|
currLine.push([currAbsCoord[0], currAbsCoord[1]]);
|
|
|
|
}
|
|
|
|
// Get collinear coordinates
|
2021-01-04 12:05:20 +02:00
|
|
|
for (let p = 1; p < currLine.length - 1; p++) {
|
2021-01-03 19:08:06 +02:00
|
|
|
let _collinearCoord = collinear(currLine[p - 1][0],
|
|
|
|
currLine[p - 1][1],
|
|
|
|
currLine[p][0],
|
|
|
|
currLine[p][1],
|
|
|
|
currLine[p + 1][0],
|
2021-01-15 22:47:00 +02:00
|
|
|
currLine[p + 1][1]);
|
2021-01-03 19:08:06 +02:00
|
|
|
if (_collinearCoord) {
|
2021-03-02 20:00:18 +02:00
|
|
|
collinearSegments.push(
|
|
|
|
segments[s - currLine.length + p + 1]
|
|
|
|
);
|
2021-01-03 19:08:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
currLine = [];
|
|
|
|
}
|
|
|
|
}
|
2021-01-15 22:47:00 +02:00
|
|
|
|
2021-01-03 19:08:06 +02:00
|
|
|
return collinearSegments;
|
|
|
|
}
|
|
|
|
|
2021-03-02 20:00:18 +02:00
|
|
|
const collinearSegments = getCollinearSegments(iconPath),
|
|
|
|
pathDIndex = getPathDIndex($.html());
|
|
|
|
collinearSegments.forEach((segment) => {
|
|
|
|
let errorMsg = `Collinear segment "${iconPath.substring(segment.start, segment.end)}" found`
|
|
|
|
if (segment.chained) {
|
|
|
|
let readableChain = iconPath.substring(segment.chainStart, segment.chainEnd);
|
|
|
|
if (readableChain.length > 20) {
|
|
|
|
readableChain = `${readableChain.substring(0, 20)}...`
|
|
|
|
}
|
|
|
|
errorMsg += ` in chain "${readableChain}"`;
|
2021-01-03 19:08:06 +02:00
|
|
|
}
|
2021-03-02 20:00:18 +02:00
|
|
|
errorMsg += ` at index ${segment.start + pathDIndex} (should be removed)`;
|
|
|
|
reporter.error(errorMsg);
|
2021-01-03 19:08:06 +02:00
|
|
|
});
|
2021-03-02 20:00:18 +02:00
|
|
|
|
|
|
|
if (collinearSegments.length) {
|
|
|
|
if (updateIgnoreFile) {
|
|
|
|
ignoreIcon(reporter.name, iconPath, $);
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 19:08:06 +02:00
|
|
|
},
|
2020-06-22 18:24:56 +02:00
|
|
|
function(reporter, $, ast) {
|
|
|
|
reporter.name = "extraneous";
|
|
|
|
|
2021-03-02 20:00:18 +02:00
|
|
|
if (!svgRegexp.test($.html())) {
|
2020-07-05 15:57:00 +02:00
|
|
|
reporter.error("Unexpected character(s), most likely extraneous whitespace, detected in SVG markup");
|
2020-06-22 18:24:56 +02:00
|
|
|
}
|
|
|
|
},
|
2020-12-14 21:35:27 +02:00
|
|
|
function(reporter, $, ast) {
|
|
|
|
reporter.name = "negative-zeros";
|
|
|
|
|
|
|
|
const iconPath = $.find("path").attr("d");
|
|
|
|
if (!updateIgnoreFile && isIgnored(reporter.name, iconPath)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find negative zeros inside path
|
|
|
|
const negativeZeroMatches = Array.from(iconPath.matchAll(negativeZerosRegexp));
|
|
|
|
if (negativeZeroMatches.length) {
|
|
|
|
// Calculate the index for each match in the file
|
2021-03-02 20:00:18 +02:00
|
|
|
const svgFileContent = $.html();
|
|
|
|
const pathDIndex = getPathDIndex(svgFileContent);
|
2020-12-14 21:35:27 +02:00
|
|
|
|
|
|
|
negativeZeroMatches.forEach((match) => {
|
|
|
|
const negativeZeroFileIndex = match.index + pathDIndex;
|
2021-03-02 20:00:18 +02:00
|
|
|
const previousChar = svgFileContent[negativeZeroFileIndex - 1];
|
2020-12-14 21:35:27 +02:00
|
|
|
const replacement = "0123456789".includes(previousChar) ? " 0" : "0";
|
|
|
|
reporter.error(`Found "-0" at index ${negativeZeroFileIndex} (should be "${replacement}")`);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
2020-07-10 10:06:08 +02:00
|
|
|
function(reporter, $, ast) {
|
|
|
|
reporter.name = "icon-centered";
|
|
|
|
|
2020-10-17 13:01:56 +02:00
|
|
|
const iconPath = $.find("path").attr("d");
|
|
|
|
if (!updateIgnoreFile && isIgnored(reporter.name, iconPath)) {
|
2020-07-28 12:33:40 +02:00
|
|
|
return;
|
2020-07-10 10:06:08 +02:00
|
|
|
}
|
|
|
|
|
2020-11-23 12:36:10 +02:00
|
|
|
const [minX, minY, maxX, maxY] = svgPathBbox(iconPath);
|
2020-07-10 10:06:08 +02:00
|
|
|
const targetCenter = iconSize / 2;
|
2020-11-23 12:36:10 +02:00
|
|
|
const centerX = +((minX + maxX) / 2).toFixed(iconFloatPrecision);
|
2020-07-10 10:06:08 +02:00
|
|
|
const devianceX = centerX - targetCenter;
|
2020-11-23 12:36:10 +02:00
|
|
|
const centerY = +((minY + maxY) / 2).toFixed(iconFloatPrecision);
|
2020-07-10 10:06:08 +02:00
|
|
|
const devianceY = centerY - targetCenter;
|
|
|
|
|
|
|
|
if (
|
|
|
|
Math.abs(devianceX) > iconTolerance ||
|
|
|
|
Math.abs(devianceY) > iconTolerance
|
|
|
|
) {
|
|
|
|
reporter.error(`<path> must be centered at (${targetCenter}, ${targetCenter}); the center is currently (${centerX}, ${centerY})`);
|
2020-10-17 13:01:56 +02:00
|
|
|
if (updateIgnoreFile) {
|
|
|
|
ignoreIcon(reporter.name, iconPath, $);
|
|
|
|
}
|
2020-07-10 10:06:08 +02:00
|
|
|
}
|
2021-02-22 18:20:07 +02:00
|
|
|
},
|
|
|
|
function(reporter, $, ast) {
|
|
|
|
reporter.name = "path-format";
|
|
|
|
|
|
|
|
const iconPath = $.find("path").attr("d");
|
|
|
|
|
|
|
|
const validPathFormatRegex = /^[Mm][MmZzLlHhVvCcSsQqTtAaEe0-9-,.\s]+$/;
|
|
|
|
if (!validPathFormatRegex.test(iconPath)) {
|
|
|
|
let errorMsg = "Invalid path format", reason;
|
|
|
|
|
|
|
|
if (!(/^[Mm]/.test(iconPath))) {
|
|
|
|
// doesn't start with moveto
|
|
|
|
reason = `should start with \"moveto\" command (\"M\" or \"m\"), but starts with \"${iconPath[0]}\"`;
|
|
|
|
reporter.error(`${errorMsg}: ${reason}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const validPathCharacters = "MmZzLlHhVvCcSsQqTtAaEe0123456789-,. ",
|
|
|
|
invalidCharactersMsgs = [],
|
2021-03-02 20:00:18 +02:00
|
|
|
pathDIndex = getPathDIndex($.html());
|
2021-02-22 18:20:07 +02:00
|
|
|
|
|
|
|
for (let [i, char] of Object.entries(iconPath)) {
|
|
|
|
if (validPathCharacters.indexOf(char) === -1) {
|
|
|
|
invalidCharactersMsgs.push(`"${char}" at index ${pathDIndex + parseInt(i)}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// contains invalid characters
|
|
|
|
if (invalidCharactersMsgs.length > 0) {
|
|
|
|
reason = `unexpected character${invalidCharactersMsgs.length > 1 ? 's' : ''} found`;
|
|
|
|
reason += ` (${invalidCharactersMsgs.join(", ")})`;
|
|
|
|
reporter.error(`${errorMsg}: ${reason}`);
|
|
|
|
}
|
|
|
|
}
|
2020-07-10 10:06:08 +02:00
|
|
|
}
|
2018-08-16 11:33:32 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|