Skip to content

Commit

Permalink
fix(Icon): support all icons tags
Browse files Browse the repository at this point in the history
  • Loading branch information
lukicenturi committed Dec 25, 2024
1 parent 4c23309 commit a36bd03
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions scripts/generate-icons.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,18 @@ function convertToSinglePath(elements) {
return rectToPath(attrs);
case 'circle':
return circleToPath(attrs);
case 'ellipse':
return ellipseToPath(attrs);
case 'line':
return lineToPath(attrs);
case 'polyline':
return polylineToPath(attrs);
case 'polygon':
return polygonToPath(attrs);
case 'path':
return convertPath(attrs.d, index);
default:
console.log(`tag ${type} is skipped`);
return '';
}
});
Expand Down Expand Up @@ -261,10 +268,55 @@ function circleToPath({ cx = 0, cy = 0, r }) {
`.trim().replace(/\s+/g, ' ');
}

function ellipseToPath({ cx = 0, cy = 0, rx = 0, ry = 0 }) {
// Similar to a circle but using different radii for x and y coordinates
return `
M${cx - rx},${cy}
a${rx},${ry} 0 1,0 ${rx * 2},0
a${rx},${ry} 0 1,0 -${rx * 2},0
Z
`.trim().replace(/\s+/g, ' ');
}

function lineToPath({ x1 = 0, y1 = 0, x2 = 0, y2 = 0 }) {
return `M${x1} ${y1} L${x2} ${y2}`;
}

function polylineToPath({ points }) {
if (!points)
return '';

// Split points string into array of coordinates
const coordinates = points.trim().split(/\s+|,/).map(Number);

if (coordinates.length < 4)
return ''; // Need at least 2 points

// Start path at first point
let path = `M${coordinates[0]} ${coordinates[1]}`;

// Add line segments to remaining points
for (let i = 2; i < coordinates.length; i += 2) {
path += ` L${coordinates[i]} ${coordinates[i + 1]}`;
}

return path;
}

function polygonToPath({ points }) {
if (!points)
return '';

// Use polylineToPath to create the base path
const basePath = polylineToPath({ points });

if (!basePath)
return '';

// Close the path by adding 'Z' command
return `${basePath} Z`;
}

async function getLucideSvgDataFromPath(pathDir) {
const type = await lstat(pathDir);
if (type.isDirectory()) {
Expand Down

0 comments on commit a36bd03

Please sign in to comment.