108 lines
7.7 KiB
JavaScript
108 lines
7.7 KiB
JavaScript
// PNH · generador de carteles 1080x1080 con el marco oficial.
|
|
// Uso: node render.mjs spec.json
|
|
// spec = { out: "dir", prefix: "nombre", footerLeft: "29N · VOTO EN BLANCO", numbered: true,
|
|
// slides: [ { html: "<div class='txt'>...</div>", cross: 0.13, halo: false, footer: true } ] }
|
|
// En el html se pueden usar {{LOGO}} y {{HORSE}} (data URIs) 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 CROSS = raggedBar(-60, -60, 1140, 1140, 110) + raggedBar(1140, -60, -60, 1140, 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:1080px;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 ? '' : `
|
|
<line x1="90" y1="968" x2="990" y2="968" stroke="#D9A63A" stroke-width="1"/>
|
|
<text x="${spec.footerCenter ? 540 : 96}" y="1008" ${spec.footerCenter ? 'text-anchor="middle"' : ''} font-family="Cinzel" font-weight="700" font-size="${spec.footerCenter ? 22 : 20}" fill="#D9A63A" letter-spacing="4">${sl.footerText || spec.footerLeft || ''}</text>
|
|
${spec.numbered && n > 1 ? `<text x="984" y="1008" text-anchor="end" font-family="Cinzel" font-weight="700" font-size="20" fill="#D9A63A" letter-spacing="3">${i} / ${n}${i < n ? ' →' : ''}</text>` : ''}`;
|
|
return `<svg width="1080" height="1080" viewBox="0 0 1080 1080" xmlns="http://www.w3.org/2000/svg">
|
|
<defs>
|
|
<radialGradient id="bg" cx="50%" cy="55%" r="75%"><stop offset="0" stop-color="#6a0a14"/><stop offset=".55" stop-color="#4B0000"/><stop offset="1" stop-color="#220306"/></radialGradient>
|
|
<linearGradient id="gold" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#FFE9A6"/><stop offset=".45" stop-color="#F2C64B"/><stop offset="1" stop-color="#C9962C"/></linearGradient>
|
|
<radialGradient id="halo"><stop offset="0" stop-color="#F2C64B" stop-opacity=".35"/><stop offset="1" stop-color="#F2C64B" stop-opacity="0"/></radialGradient>
|
|
<clipPath id="inner"><rect x="44" y="44" width="992" height="992"/></clipPath>
|
|
</defs>
|
|
<rect width="1080" height="1080" fill="url(#bg)"/>
|
|
<g clip-path="url(#inner)"><path d="${CROSS}" fill="#A10F2B" opacity="${sl.cross ?? .16}"/></g>
|
|
${sl.halo ? `<circle cx="${sl.halo[0]}" cy="${sl.halo[1]}" r="${sl.halo[2]}" fill="url(#halo)"/>` : ''}
|
|
${sl.svg || ''}
|
|
<rect x="26" y="26" width="1028" height="1028" fill="none" stroke="#D9A63A" stroke-width="4"/>
|
|
<rect x="38" y="38" width="1004" height="1004" fill="none" stroke="#D9A63A" stroke-width="1.5"/>
|
|
<g font-family="Cinzel" font-weight="700" font-size="27" fill="url(#gold)" letter-spacing="3">
|
|
<text x="460" y="104" text-anchor="end">PARTIDO NACIONAL</text>
|
|
<text x="620" y="104" text-anchor="start">HISPANISTA</text>
|
|
</g>
|
|
<image href="${HORSE}" x="483" y="62" width="114" height="72"/>
|
|
<line x1="90" y1="148" x2="990" y2="148" stroke="#D9A63A" stroke-width="1.5"/>
|
|
<line x1="90" y1="154" x2="990" y2="154" stroke="#D9A63A" stroke-width="1"/>
|
|
${footer}
|
|
</svg>`;
|
|
}
|
|
|
|
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: 1080 }, 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);
|
|
await pg.setContent(`<!doctype html><html><head><meta charset="utf-8"><style>${css}${spec.css || ''}</style></head><body><div class="s">${frame(i + 1, n, sl)}${html}</div></body></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: 1080 } });
|
|
files.push(f);
|
|
}
|
|
await browser.close();
|
|
console.log(files.join('\n'));
|