#!/usr/bin/env node /** * Genera il marchio e le icone del sito a partire dai font self-hosted (Fraunces, Inter): * * src/assets/logo-mark.svg marchio su fondo chiaro (quadrato navy, «G» bianca, dettaglio oro) * src/assets/logo-mark-dark.svg marchio su fondo scuro (quadrato oro, «G» navy) * src/assets/logo.svg marchio + logotipo «Studio Grasso / TRIBUTARIO E SOCIETARIO» * src/assets/logo-dark.svg variante per fondi scuri * public/favicon.svg favicon vettoriale (= logo-mark.svg) * public/favicon.ico 16, 32 e 48 px (PNG incapsulati in ICO) * public/apple-touch-icon.png 180 × 180 px * public/og-default.png 1200 × 630 px (anteprima per i social) * * I testi sono convertiti in tracciati vettoriali (opentype.js): i file non dipendono da font * installati sul sistema e il risultato è identico su ogni macchina. Le immagini raster sono * prodotte con sharp. Uso: `npm run icons` (i file generati vanno committati). */ import { mkdir, readFile, writeFile } from 'node:fs/promises'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import opentype from 'opentype.js'; import sharp from 'sharp'; const root = resolve(dirname(fileURLToPath(import.meta.url)), '..'); const out = (p) => resolve(root, p); // Colori: stessi valori di src/styles/tokens.css const C = { navy900: '#0b1f33', navy800: '#0f2a44', navy700: '#163a5c', gold500: '#b8902e', gold300: '#e3c878', ink600: '#4f5b66', white: '#ffffff', }; async function loadFont(pkg, file) { const buf = await readFile(out(`node_modules/${pkg}/files/${file}`)); return opentype.parse(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)); } const fraunces600 = await loadFont('@fontsource/fraunces', 'fraunces-latin-600-normal.woff'); const fraunces500 = await loadFont('@fontsource/fraunces', 'fraunces-latin-500-normal.woff'); const inter600 = await loadFont('@fontsource/inter', 'inter-latin-600-normal.woff'); const inter500 = await loadFont('@fontsource/inter', 'inter-latin-500-normal.woff'); const round = (n) => Math.round(n * 100) / 100; /** Serializza i comandi di un tracciato (toPathData di opentype.js produce NaN in alcuni casi). */ function toD(path) { const n = (v) => { if (!Number.isFinite(v)) throw new Error(`Coordinata non valida nel tracciato: ${v}`); return String(round(v)); }; return path.commands .map((c) => { switch (c.type) { case 'M': case 'L': return `${c.type}${n(c.x)} ${n(c.y)}`; case 'Q': return `Q${n(c.x1)} ${n(c.y1)} ${n(c.x)} ${n(c.y)}`; case 'C': return `C${n(c.x1)} ${n(c.y1)} ${n(c.x2)} ${n(c.y2)} ${n(c.x)} ${n(c.y)}`; case 'Z': return 'Z'; default: throw new Error(`Comando di tracciato sconosciuto: ${c.type}`); } }) .join(''); } /** * Impaginazione semplice glifo per glifo (cmap + crenatura), senza sostituzioni OpenType: * il motore di shaping di opentype.js non supporta tutte le tabelle GSUB di questi font, e per * testi brevi in alfabeto latino le sostituzioni non servono. `letterSpacing` è in em. */ function layout(font, text, x, y, size, letterSpacing = 0) { const scale = size / font.unitsPerEm; const glyphs = [...text].map((ch) => font.charToGlyph(ch)); const kerning = font.position?.getKerningTables?.('latn'); let d = ''; let cx = x; glyphs.forEach((glyph, i) => { d += toD(glyph.getPath(cx, y, size)); cx += glyph.advanceWidth * scale; if (i < glyphs.length - 1) { const next = glyphs[i + 1]; const k = kerning ? font.position.getKerningValue(kerning, glyph.index, next.index) : font.getKerningValue(glyph, next); cx += (k || 0) * scale + letterSpacing * size; } }); return { d, width: cx - x }; } /** Testo → tracciato SVG. `x` è il bordo sinistro, `y` la linea di base. */ const textPath = (font, text, x, y, size, ls = 0) => layout(font, text, x, y, size, ls).d; const textWidth = (font, text, size, ls = 0) => layout(font, text, 0, 0, size, ls).width; /** «G» centrata otticamente in un quadrato di lato `box`. */ function markGlyph(box, size) { const glyph = fraunces600.charToGlyph('G'); const bb = glyph.getPath(0, 0, size).getBoundingBox(); const x = (box - (bb.x2 - bb.x1)) / 2 - bb.x1; const y = (box - (bb.y2 - bb.y1)) / 2 - bb.y1; return toD(glyph.getPath(round(x), round(y), size)); } /** Marchio 44 × 44 (proporzioni del brief §6.3: raggio 10, «G» Fraunces, quadratino oro). */ function markSvg({ dark = false, standalone = true } = {}) { const bg = dark ? C.gold500 : C.navy800; const fg = dark ? C.navy900 : C.white; const dot = dark ? '' : ``; const ns = standalone ? ' xmlns="http://www.w3.org/2000/svg"' : ''; return ( `` + `` + `` + dot + `` ); } /** Logo completo: marchio + nome + sottotitolo, testi convertiti in tracciati. */ function logoSvg({ dark = false } = {}) { const nameColor = dark ? C.white : C.navy900; const tagColor = dark ? 'rgba(255,255,255,0.7)' : C.ink600; const nameSize = 21.6; // 1.35rem const tagSize = 10.4; // 0.65rem const nameX = 44 + 12.8; const nameW = textWidth(fraunces600, 'Studio Grasso', nameSize, -0.01); const tagW = textWidth(inter600, 'TRIBUTARIO E SOCIETARIO', tagSize, 0.16); const width = Math.ceil(nameX + Math.max(nameW, tagW) + 2); const mark = markSvg({ dark, standalone: false }).replace( '', '', ); return ( `` + mark + `` + `` + `` ); } /** Icona quadrata per Apple (fondo pieno: iOS applica da sé gli angoli arrotondati). */ function appleTouchSvg() { return ( `` + `` + `` + `` + `` ); } /** Immagine Open Graph 1200 × 630: fondo navy, logo, payoff e città. */ function ogSvg() { const W = 1200; const H = 630; const left = 96; const markSize = 96; const scale = markSize / 44; const nameSize = 52; const tagSize = 20; const textX = left + markSize + 32; const payoff = 'Consulenza fiscale e tributaria'; const payoffSize = 64; const cities = 'Montegrosso d’Asti · Asti'; const citiesSize = 30; const mark = `` + `` + `` + ``; return ( `` + `` + `` + `` + mark + `` + `` + `` + `` + `` ); } /** File ICO con immagini PNG incapsulate (supportato da tutti i browser attuali). */ function buildIco(pngs) { const header = Buffer.alloc(6); header.writeUInt16LE(0, 0); header.writeUInt16LE(1, 2); header.writeUInt16LE(pngs.length, 4); const entries = []; let offset = 6 + 16 * pngs.length; for (const { size, data } of pngs) { const e = Buffer.alloc(16); e.writeUInt8(size >= 256 ? 0 : size, 0); e.writeUInt8(size >= 256 ? 0 : size, 1); e.writeUInt8(0, 2); e.writeUInt8(0, 3); e.writeUInt16LE(1, 4); e.writeUInt16LE(32, 6); e.writeUInt32LE(data.length, 8); e.writeUInt32LE(offset, 12); offset += data.length; entries.push(e); } return Buffer.concat([header, ...entries, ...pngs.map((p) => p.data)]); } const png = (svg, size) => sharp(Buffer.from(svg), { density: 72 * Math.max(1, size / 44) }) .resize(size, size) .png({ compressionLevel: 9 }) .toBuffer(); await mkdir(out('src/assets'), { recursive: true }); await mkdir(out('public'), { recursive: true }); const mark = markSvg(); const files = { 'src/assets/logo-mark.svg': mark, 'src/assets/logo-mark-dark.svg': markSvg({ dark: true }), 'src/assets/logo.svg': logoSvg(), 'src/assets/logo-dark.svg': logoSvg({ dark: true }), 'public/favicon.svg': mark, }; for (const [file, svg] of Object.entries(files)) { await writeFile(out(file), svg + '\n'); } const icoImages = []; for (const size of [16, 32, 48]) icoImages.push({ size, data: await png(mark, size) }); await writeFile(out('public/favicon.ico'), buildIco(icoImages)); await sharp(Buffer.from(appleTouchSvg())) .png({ compressionLevel: 9 }) .toFile(out('public/apple-touch-icon.png')); await sharp(Buffer.from(ogSvg()), { density: 72 }) .png({ compressionLevel: 9 }) .toFile(out('public/og-default.png')); console.log('Icone generate:'); for (const f of [ ...Object.keys(files), 'public/favicon.ico', 'public/apple-touch-icon.png', 'public/og-default.png', ]) { console.log(` ${f}`); }