Branchwell

Bucksley / website / tools / render-icons.js

1// Renders the Bucksley app icon ("Stacks", Afterhours) to the site's PNG icons.
2// Run from the repository root: node tools/render-icons.js [out-dir-for-sibling-icons]
3// mark.svg is the master drawing; tools/mark-small.svg is the pixel-fitted drawing for small sizes.
4// Writes assets/icon.png (512, with a soft shadow), favicon.png (64, small drawing) and
5// assets/apple-touch-icon.png (180, full-bleed square; iOS rounds it). With an out dir it also
6// writes bucksley-96.png and bucksley-144.png for the More from Project ATOM blocks
7// (convert the 96 to WebP with cwebp).
8const fs = require('fs');
9const path = require('path');
10const { chromium } = require('playwright');
11const root = path.resolve(__dirname, '..');
12const master = fs.readFileSync(path.join(root, 'mark.svg'), 'utf8');
13const small = fs.readFileSync(path.join(__dirname, 'mark-small.svg'), 'utf8');
14const sized = (svg) => svg.replace('<svg ', '<svg style="display:block;width:100%;height:100%" ');
15const rounded = (svg, size) => `<html><body style="margin:0;background:transparent">
16 <div style="width:${size}px;height:${size}px;display:grid;place-items:center">
17 <div style="width:${size * 0.92}px;height:${size * 0.92}px;border-radius:${size * 0.92 * 0.225}px;box-shadow:0 ${size * 0.01}px ${size * 0.03}px rgba(13,14,23,.32)">
18 ${sized(svg)}
19 </div></div></body></html>`;
20// Full-bleed square for iOS: drop the rounded corners and the edge line.
21const squareSvg = master.replace(/<rect width="1024" height="1024" rx="[\d.]+"/, '<rect width="1024" height="1024"').replace(/<rect x="2" y="2"[^>]*\/>/, '');
22const square = (size) => `<html><body style="margin:0">${squareSvg.replace('<svg ', `<svg style="display:block;width:${size}px;height:${size}px" `)}</body></html>`;
23(async () => {
24 const browser = await chromium.launch(process.env.PW_CHROME ? { executablePath: process.env.PW_CHROME } : {});
25 const shot = async (html, size, file) => {
26 const page = await browser.newPage({ viewport: { width: size, height: size } });
27 await page.setContent(html);
28 await page.screenshot({ path: file, omitBackground: true });
29 await page.close();
30 };
31 await shot(rounded(master, 512), 512, path.join(root, 'assets/icon.png'));
32 await shot(rounded(small, 64), 64, path.join(root, 'favicon.png'));
33 await shot(square(180), 180, path.join(root, 'assets/apple-touch-icon.png'));
34 const out = process.argv[2];
35 if (out) {
36 await shot(rounded(master, 96), 96, path.join(out, 'bucksley-96.png'));
37 await shot(rounded(master, 144), 144, path.join(out, 'bucksley-144.png'));
38 }
39 await browser.close();
40})();