// PNH · generador de carteles con el marco oficial. spec.height: 1080 (feed, por defecto) o 1920 (historia 9:16).
// Uso: node render.mjs spec.json
// spec = { out: "dir", prefix: "nombre", footerLeft: "29N · VOTO EN BLANCO", numbered: true,
// slides: [ { html: "
...
", cross: 0.13, halo: false, footer: true } ] }
// En el html se pueden usar {{LOGO}}, {{HORSE}} y {{IMG:archivo}} (data URIs de assets/) y las clases CSS de abajo.
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { chromium } from 'playwright-core';
const K = path.dirname(fileURLToPath(import.meta.url));
const spec = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
const asset = f => fs.readFileSync(path.join(K, 'assets', f)).toString('base64');
const font = f => fs.readFileSync(path.join(K, 'node_modules/@fontsource', f)).toString('base64');
const HORSE = 'data:image/png;base64,' + asset('horse.png');
const LOGO = 'data:image/png;base64,' + asset('logo_full_hd.png');
function raggedBar(x0, y0, x1, y1, w) {
const dx = x1 - x0, dy = y1 - y0, L = Math.hypot(dx, dy), ux = dx / L, uy = dy / L, nx = -uy, ny = ux;
const pts = [], n = 9;
for (let s = 0; s <= 1; s++) {
const side = s ? -1 : 1, seq = [];
for (let k = 0; k <= n; k++) {
const t = k / n * L, knot = (k > 0 && k < n && k % 2 === 1) ? 26 : 0;
seq.push([x0 + ux * t + nx * side * (w / 2 + knot), y0 + uy * t + ny * side * (w / 2 + knot)]);
if (knot) seq.push([x0 + ux * (t + 18) + nx * side * w / 2, y0 + uy * (t + 18) + ny * side * w / 2]);
}
pts.push(...(s ? seq.reverse() : seq));
}
return 'M' + pts.map(p => p.map(v => v.toFixed(1)).join(',')).join('L') + 'Z';
}
const H = spec.height || 1080;
const CROSS = raggedBar(-60, -60, 1140, H + 60, 110) + raggedBar(1140, -60, -60, H + 60, 110);
const css = `
@font-face{font-family:Cinzel;font-weight:700;src:url(data:font/woff2;base64,${font('cinzel/files/cinzel-latin-700-normal.woff2')})}
@font-face{font-family:Cinzel;font-weight:900;src:url(data:font/woff2;base64,${font('cinzel/files/cinzel-latin-900-normal.woff2')})}
@font-face{font-family:Garamond;font-weight:500;src:url(data:font/woff2;base64,${font('eb-garamond/files/eb-garamond-latin-500-normal.woff2')})}
@font-face{font-family:Garamond;font-weight:600;src:url(data:font/woff2;base64,${font('eb-garamond/files/eb-garamond-latin-600-normal.woff2')})}
@font-face{font-family:Garamond;font-style:italic;font-weight:500;src:url(data:font/woff2;base64,${font('eb-garamond/files/eb-garamond-latin-500-italic.woff2')})}
@font-face{font-family:Garamond;font-style:italic;font-weight:600;src:url(data:font/woff2;base64,${font('eb-garamond/files/eb-garamond-latin-600-italic.woff2')})}
html,body{margin:0;background:#2a0508}
.s{position:relative;width:1080px;height:${H}px;overflow:hidden;color:#FBF3E4}
.s>svg{position:absolute;inset:0}
/* bloque de texto estándar (zona útil 96..984 x 215..930) */
.txt{position:absolute;left:96px;right:96px;top:215px;bottom:150px;display:flex;flex-direction:column;justify-content:center;gap:34px}
.center{position:absolute;left:0;right:0;text-align:center}
.kick{font-family:Cinzel;font-weight:700;font-size:24px;letter-spacing:6px;color:#D9A63A}
.date{font-family:Cinzel;font-weight:700;font-size:32px;letter-spacing:8px;color:#F2C64B}
.big{font-family:Cinzel;font-weight:900;font-size:120px;line-height:1.02;color:#FBF3E4;text-shadow:0 4px 12px rgba(0,0,0,.5)}
h1{margin:0;font-family:Cinzel;font-weight:900;font-size:68px;line-height:1.05;color:#F2C64B;text-wrap:balance}
p{margin:0;font-family:Garamond;font-weight:500;font-size:37px;line-height:1.32;color:#FBF3E4}
p b,.gold{font-weight:600;color:#F2C64B}
.motto{font-family:Garamond;font-style:italic;font-size:40px;color:#F2C64B}
.quote{font-family:Garamond;font-style:italic;font-size:44px;line-height:1.25;color:#F2C64B;border-left:4px solid #D9A63A;padding-left:28px}
.bigquote{font-family:Garamond;font-style:italic;font-weight:500;font-size:56px;line-height:1.18;color:#FBF3E4;text-wrap:balance;text-shadow:0 3px 10px rgba(0,0,0,.45)}
.bigquote b{font-weight:600;color:#F2C64B}
.rows{display:flex;flex-direction:column;gap:22px}
.row{display:grid;grid-template-columns:270px 1fr;gap:28px;align-items:baseline;border-top:1.5px solid rgba(217,166,58,.55);padding-top:20px}
.row .k{font-family:Cinzel;font-weight:900;font-size:34px;color:#F2C64B;letter-spacing:1px}
.row .v{font-family:Garamond;font-weight:500;font-size:33px;line-height:1.28}
.row.hl .k{color:#FBF3E4;background:#A10F2B;padding:2px 12px;justify-self:start;white-space:nowrap;font-size:30px}
`;
function frame(i, n, sl) {
const footer = sl.footer === false ? '' : `
${sl.footerText || spec.footerLeft || ''}
${spec.numbered && n > 1 ? `${i} / ${n}${i < n ? ' →' : ''}` : ''}`;
return ``;
}
const out = path.resolve(path.dirname(process.argv[2]), spec.out || '.');
fs.mkdirSync(out, { recursive: true });
const browser = await chromium.launch({ executablePath: process.env.CHROMIUM || '/opt/pw-browsers/chromium' });
const pg = await browser.newPage({ viewport: { width: 1080, height: H }, deviceScaleFactor: spec.scale || 1 });
const n = spec.slides.length, files = [];
for (let i = 0; i < n; i++) {
const sl = spec.slides[i];
const html = (sl.html || '').replaceAll('{{LOGO}}', LOGO).replaceAll('{{HORSE}}', HORSE)
.replace(/\{\{IMG:([^}]+)\}\}/g, (_, f) => `data:image/${f.endsWith('.png') ? 'png' : 'jpeg'};base64,` + asset(f));
await pg.setContent(`${frame(i + 1, n, sl)}${html}
`);
await pg.evaluate(() => document.fonts.ready); await pg.waitForTimeout(150);
const f = path.join(out, `${spec.prefix || 'pnh'}${n > 1 ? '_' + (i + 1) : ''}.png`);
await pg.screenshot({ path: f, clip: { x: 0, y: 0, width: 1080, height: H } });
files.push(f);
}
await browser.close();
console.log(files.join('\n'));