#!/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 (
``
);
}
/** 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(
'