-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.ts
427 lines (357 loc) · 14.4 KB
/
build.ts
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import { readFile, rm, mkdir, writeFile } from 'fs/promises';
// @ts-ignore, https://github.com/svgdotjs/svgdom/issues/69
import { createSVGDocument, HTMLParser } from 'svgdom';
import { SVGPathData } from 'svg-pathdata';
import { renderAsync as renderSVG } from '@resvg/resvg-js';
import YAML from 'json-to-pretty-yaml';
interface BuildConfig {
combinations: CombinationConfig[];
templates: Record<string, TemplateConfig>;
prides: Record<string, PrideConfig>;
}
interface CombinationConfig {
templates: "*" | string[];
prides: "*" | string[];
}
interface TemplateConfig {
name: string;
overwriteAliasId?: string;
templateFile: string;
stripesId: string;
stripesRotation?: number;
accentIds?: string[];
meta?: Metadata;
}
interface PrideConfig {
name: string;
stripes: string[];
accents?: Record<string, string>;
variants?: Record<string, PrideVariant>;
tags?: string[];
}
interface PrideVariant {
name: string;
stripes?: string[];
accents?: Record<string, string>;
tags?: string[];
}
interface ReadmeData {
id: string;
name: string;
svg: string;
png: string;
}
interface Metadata {
sources?: Array<{ name: string, link: string }>;
license?: string;
tags?: string[];
}
const DEBUG = false;
const readmeData: ReadmeData[] = [];
async function exportSvg(name: string, id: string, meta: Metadata, dom: Element) {
// Export the SVG data
const svgData = dom.outerHTML;
// Remove " since this breaks resvg
const svgDataFixed = svgData.replace(/"/g, '');
// Write the SVG
const svgLocation = `./dist/svg/${id}.svg`;
await writeFile(svgLocation, svgDataFixed);
// Write the metadata
const metadata = { name, ...meta };
await writeFile(`./dist/json/${id}.json`, JSON.stringify(metadata));
await writeFile(`./dist/yaml/${id}.svg.yml`, YAML.stringify(metadata));
// Render to a png
const pngData = await renderSVG(svgDataFixed, { fitTo: { mode: 'width', value: 360 } });
// Write the png
const pngLocation = `./dist/png/${id}.png`;
await writeFile(pngLocation, pngData.asPng());
readmeData.push({
id, name,
svg: svgLocation,
png: pngLocation
});
}
function roundToDecimal(num: number, decimals: number): string {
return num.toFixed(decimals).replace(/([0-9]+(\.[0-9]+[1-9])?)(\.?0+$)/, '$1')
}
function getRotatedBounds(pathElement: Element, rotation: number) {
const path = pathElement.getAttribute("d") || "";
const bounds = new SVGPathData(path)
.normalizeHVZ()
.rotate(rotation * (Math.PI / 180))
.getBounds();
return {
h: bounds.maxY - bounds.minY,
w: bounds.maxX - bounds.minX,
x: bounds.minX,
y: bounds.minY,
}
}
function getPathBounds(pathElement: Element) {
const path = pathElement.getAttribute("d") || "";
const bounds = new SVGPathData(path).getBounds();
return {
h: bounds.maxY - bounds.minY,
w: bounds.maxX - bounds.minX,
x: bounds.minX,
y: bounds.minY,
}
}
function addElement(parent: Element, element: Element | string, attributes?: Record<string, string | number>) {
const el = typeof element === 'string' ? parent.ownerDocument.createElement(element) : element;
if (attributes) {
Object.keys(attributes).forEach(v => el.setAttribute(v, attributes[v]!.toString()));
}
parent.appendChild(el);
return el;
}
function fillStripes(templateDom: Element, id: string, stripes: string[], rotation: number) {
// Get the stripe container
const stripeContainer = templateDom.querySelector("#" + id);
if (!stripeContainer) {
throw new Error(`Invalid stripeContainer '#${id}'.`);
}
if (DEBUG) {
const bb = getPathBounds(stripeContainer);
addElement(templateDom, "rect", {
x: roundToDecimal(bb.x, 3),
y: roundToDecimal(bb.y, 3),
width: roundToDecimal(bb.w, 3),
height: roundToDecimal(bb.h, 3),
fill: "none",
stroke: "green",
"stroke-width": 0.5
});
addElement(templateDom, stripeContainer.cloneNode() as Element, {
fill: "none",
stroke: "green",
"stroke-width": "0.5",
});
}
// Rotate the stripe container
stripeContainer.setAttribute("transform", `rotate(${rotation})`);
// Get the bounding box
const bb = getRotatedBounds(stripeContainer, rotation);
if (DEBUG) {
const stripeContainerPath = stripeContainer.getAttribute("d") || "";
const stripeContainerPathRotated = new SVGPathData(stripeContainerPath)
.normalizeHVZ()
.rotate(rotation * (Math.PI / 180))
.encode();
addElement(templateDom, "path", {
d: stripeContainerPathRotated,
fill: "none",
stroke: "black",
"stroke-width": 0.5,
id: "StripeContainerWithRotation"
});
addElement(templateDom, "rect", {
x: roundToDecimal(bb.x, 3),
y: roundToDecimal(bb.y, 3),
width: roundToDecimal(bb.w, 3),
height: roundToDecimal(bb.h, 3),
fill: "none",
stroke: "red",
"stroke-width": 0.5,
id: "StripeContainerBoundsWithRotation"
});
const bb2 = getPathBounds(stripeContainer);
addElement(templateDom, "rect", {
x: roundToDecimal(bb2.x, 3),
y: roundToDecimal(bb2.y, 3),
width: roundToDecimal(bb2.w, 3),
height: roundToDecimal(bb2.h, 3),
fill: "none",
stroke: "#00ffff",
"stroke-width": 0.5,
id: "StripeContainerBoundsWithoutRotation"
});
addElement(templateDom, stripeContainer.cloneNode() as Element, {
fill: "none",
stroke: "#ff00ff",
"stroke-width": "0.5",
id: "StripeContainer"
});
}
const parent = stripeContainer.parentNode as Element ?? templateDom;
// Create a group for all the stripes
const stripesGroup = addElement(parent, 'g', {
"transform": `rotate(${-rotation})`, // Rotate the stripes
"clip-path": "url(#stripesClip)" // Clip the stripes
});
// Make sure it's in the right layer
parent.insertBefore(stripesGroup, stripeContainer);
// Fill the bounding box with stripes
const stripeCount = stripes.length;
const stripeHeight = bb.h / stripeCount;
for (let i = 0; i < stripeCount; i++) {
const color = stripes[i]!;
addElement(stripesGroup, "rect", {
x: roundToDecimal(bb.x, 3),
y: roundToDecimal(bb.y + (stripeHeight * i), 3),
width: roundToDecimal(bb.w, 3),
height: roundToDecimal(stripeHeight + 0.1, 3), // Small overlap to prevent some aliasing issues
fill: color,
});
}
if (DEBUG) {
addElement(templateDom, "rect", {
x: roundToDecimal(bb.x, 3),
y: roundToDecimal(bb.y, 3),
width: roundToDecimal(bb.w, 3),
height: roundToDecimal(bb.h, 3),
fill: "none",
stroke: "blue",
"stroke-width": 0.5,
transform: `rotate(${-rotation})`
});
}
// Create a clip path for the stripe container
const defs = addElement(templateDom, "defs");
const clipPath = addElement(defs, "clipPath", { id: "stripesClip" });
clipPath.appendChild(stripeContainer);
}
function fillAccents(element: Element, accentIds: string[], accentColors: Map<string, string>, fallbackColor: string) {
accentIds.forEach((value) => {
const accent = element.querySelector("#" + value);
if (accent) {
accent.setAttribute("fill", accentColors.get(value) || fallbackColor);
}
});
}
function parseSVG(svg: string): Element {
const document = createSVGDocument() as HTMLDocument;
HTMLParser(svg, document);
return document.documentElement;
}
async function run() {
// Read the build config
let buildConfigJson = await readFile("./build-config.json", { encoding: "utf-8" });
buildConfigJson = buildConfigJson.replace(/^\W*\/\/.*$/gm, ""); // Remove comments
const buildConfig: BuildConfig = JSON.parse(buildConfigJson);
// Clean the dist directory
await rm("./dist", { force: true, recursive: true });
await mkdir("./dist/svg", { recursive: true });
await mkdir("./dist/png", { recursive: true });
await mkdir("./dist/json", { recursive: true });
await mkdir("./dist/yaml", { recursive: true });
// Generate emotes
const { combinations, templates, prides } = buildConfig;
const availableTemplates = new Map(Object.entries(templates));
const availablePrides = new Map(Object.entries(prides));
for (let i = 0; i < combinations.length; i++) {
console.log(`Processing combination ${i + 1} of ${combinations.length}...`);
const combination = combinations[i]!;
// Collect the templates
let comboTemplates: Map<string, TemplateConfig>;
if (combination.templates === "*") {
comboTemplates = availableTemplates;
} else {
comboTemplates = new Map<string, TemplateConfig>();
for (const key of combination.templates) {
const template = availableTemplates.get(key);
if (template) {
comboTemplates.set(key, template);
} else {
console.warn(`A template with id '${key}' could not be found.`)
}
}
}
// Collect the prides
let comboPrides: Map<string, PrideConfig>;
if (combination.prides === "*") {
comboPrides = availablePrides;
} else {
comboPrides = new Map<string, PrideConfig>();
for (const key of combination.prides) {
const pride = availablePrides.get(key);
if (pride) {
comboPrides.set(key, pride);
} else {
console.warn(`A pride with id '${key}' could not be found.`)
}
}
}
// Combine
for (const [templateId, template] of comboTemplates) {
const templateFileContent = await readFile(template.templateFile, { encoding: "utf-8" });
const templateAccentIds = template.accentIds || [];
for (const [prideId, pride] of comboPrides) {
console.log(`Generating ${prideId}-${templateId}`);
// Parse the template
const root = parseSVG(templateFileContent);
// Fill the stripes
fillStripes(root, template.stripesId, pride.stripes, template.stripesRotation || 0);
// Fill accents
if (template.accentIds) {
const prideAccents = new Map(Object.entries(pride.accents || {}).filter(([k]) => templateAccentIds.includes(k)))
fillAccents(root, template.accentIds, prideAccents, pride.stripes[0] || "red");
}
const meta = {
...template.meta,
tags: [
...(template.meta?.tags || []),
...(pride.tags || []),
]
}
// Export
const exportTemplateId = template.overwriteAliasId || templateId;
await exportSvg(`${pride.name} ${template.name}`, `${prideId}-${exportTemplateId}`, meta, root);
// Handle variants
if (pride.variants) {
for (const [variantId, variant] of Object.entries(pride.variants)) {
// Only render the variant if it has new stripes or new accents
if (variant.stripes || (variant.accents && Object.keys(variant.accents).some(k => templateAccentIds.includes(k)))) {
// Parse the template
const variantRoot = parseSVG(templateFileContent);
// Fill the stripes
fillStripes(variantRoot, template.stripesId, variant.stripes || pride.stripes, template.stripesRotation || 0);
// Fill accents
if (template.accentIds) {
const variantAccents = new Map(Object.entries(variant.accents || pride.accents || {}).filter(([k]) => templateAccentIds.includes(k)))
fillAccents(variantRoot, template.accentIds, variantAccents, variant.stripes?.[0] || pride.stripes[0] || "red");
}
const variant_meta = {
...meta,
tags: [
...meta.tags,
...(variant.tags || []),
]
};
// Export
await exportSvg(`${pride.name} ${template.name} (${variant.name})`, `${prideId}-${variantId}-${exportTemplateId}`, variant_meta, variantRoot);
}
}
}
}
}
}
// Fill in README
const fileHost = "https://pridemoji.cp3.es/";
const tableWidth = 5;
const cells = readmeData.map(({ name, svg, png }) => `<img src="${joinUrl(fileHost, png)}" height="64" title="${name}"/><br/> [svg](${joinUrl(fileHost, svg)}) - [png](${joinUrl(fileHost, png)})`);
const table = "|" + Array(tableWidth).fill(" ").join("|") + "|\n"
+ "|" + Array(tableWidth).fill("-").join("|") + "|\n"
+ chunk(cells, tableWidth).map(c => "|" + c.join("|") + "|\n").join("");
const readmeContent = await readFile("./README.md");
const startIndex = readmeContent.indexOf("<!-- EMOJIGRID -->");
const endIndex = readmeContent.indexOf("<!-- ENDEMOJIGRID -->");
const newContent = readmeContent.slice(0, startIndex)
+ "<!-- EMOJIGRID -->\n"
+ table
+ readmeContent.slice(endIndex);
await writeFile("./README.md", newContent);
}
function joinUrl(a: string, b: string) {
return b.replace("./dist", a);
//return new URL(b, a).href;
}
function chunk<T>(arr: T[], size: number) {
const rtn = [];
let i, j;
for (i = 0, j = arr.length; i < j; i += size) {
rtn.push(arr.slice(i, i + size));
}
return rtn;
}
run().catch(console.error);