-
Notifications
You must be signed in to change notification settings - Fork 0
/
process-svgs.js
92 lines (78 loc) · 2.59 KB
/
process-svgs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const fs = require('fs');
const path = require('path');
const xml2js = require('xml2js');
// Function to parse the SVG path to points
function parseSvgPathToPoints(path, width, height) {
const commands = path.match(/[a-zA-Z][^a-zA-Z]*/g);
const points = [];
let currentPoint = [0, 0];
for (const command of commands) {
const type = command[0];
const values = command.slice(1).trim().split(/[\s,]+/).map(parseFloat);
switch (type) {
case 'M':
case 'L':
currentPoint = [values[0] / width, values[1] / height];
points.push(currentPoint);
break;
case 'H':
currentPoint = [values[0] / width, currentPoint[1]];
points.push(currentPoint);
break;
case 'V':
currentPoint = [currentPoint[0], values[0] / height];
points.push(currentPoint);
break;
case 'Z':
// Only add the starting point if it's not already the last point
if (points.length && (points[0][0] !== currentPoint[0] || points[0][1] !== currentPoint[1])) {
points.push(points[0]);
}
break;
default:
console.warn(`Unhandled command type: ${type}`);
}
}
return points;
}
// Directory containing the SVG files
const svgDirectory = './svgs';
// Read all SVG files in the directory
fs.readdir(svgDirectory, (err, files) => {
if (err) {
console.error('Error reading directory:', err);
return;
}
const polygonPoints = [];
files.forEach((file) => {
if (path.extname(file) === '.svg') {
const filePath = path.join(svgDirectory, file);
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
xml2js.parseString(data, (err, result) => {
if (err) {
console.error('Error parsing XML:', err);
return;
}
const svgWidth = parseFloat(result.svg.$.width);
const svgHeight = parseFloat(result.svg.$.height);
const pathData = result.svg.path[0].$.d;
const points = parseSvgPathToPoints(pathData, svgWidth, svgHeight);
polygonPoints.push(points);
// Write to TypeScript file
const tsContent = `export const POLYGON_POINTS = ${JSON.stringify(polygonPoints, null, 2)};\n`;
fs.writeFile('./polygonPoints.ts', tsContent, (err) => {
if (err) {
console.error('Error writing file:', err);
} else {
console.log('polygonPoints.ts file has been saved.');
}
});
});
});
}
});
});