import fs from 'node:fs/promises';
import opentype from '@shuding/opentype.js';
import sharp from 'sharp';
const fontBytes=await fs.readFile(new URL('../lib/fonts/Vazirmatn-Bold.ttf',import.meta.url));
const font=opentype.parse(fontBytes.buffer.slice(fontBytes.byteOffset,fontBytes.byteOffset+fontBytes.byteLength));
const glyph=font.getPath('س',0,0,64);
const xs=glyph.commands.flatMap(c=>[c.x,c.x1,c.x2].filter(Number.isFinite));const ys=glyph.commands.flatMap(c=>[c.y,c.y1,c.y2].filter(Number.isFinite));
const box={x1:Math.min(...xs),x2:Math.max(...xs),y1:Math.min(...ys),y2:Math.max(...ys)};const scale=Math.min(34/(box.x2-box.x1),27/(box.y2-box.y1));
const dx=32-(box.x1+box.x2)/2*scale,dy=34-(box.y1+box.y2)/2*scale;
const svg=``;
const publicDir=new URL('../public/',import.meta.url);
await fs.writeFile(new URL('favicon.svg',publicDir),svg);
await sharp(Buffer.from(svg)).resize(32,32).png().toFile(new URL('favicon-32.png',publicDir).pathname);
await sharp(Buffer.from(svg)).resize(180,180).png().toFile(new URL('apple-touch-icon.png',publicDir).pathname);
const sizes=[16,32,48];const pngs=await Promise.all(sizes.map(size=>sharp(Buffer.from(svg)).resize(size,size).png().toBuffer()));
const header=Buffer.alloc(6+16*sizes.length);header.writeUInt16LE(1,2);header.writeUInt16LE(sizes.length,4);let offset=header.length;
for(let i=0;i