/* 从 fabric 模板 JSON 渲染预览图(在 cover-studio 镜像里跑,复用 satori + resvg + 中文字体) * 用法:docker run --rm -v :/data -e NODE_PATH=/app/node_modules -w /app cover-studio:1.0 node /data/render_preview.js * 好处:预览和模板同源,避免"缩略图与载入后不一致"。 */ const fs = require('fs'); const path = require('path'); const _satori = require('satori'); const satori = _satori.default || _satori; const _resvg = require('@resvg/resvg-js'); const Resvg = _resvg.Resvg || _resvg.default.Resvg; const DATA = process.env.DATA || '/data'; const FONT_DIR = process.env.FONT_DIR || '/app/fonts'; const FONTS = [ { name: 'Noto Sans SC', data: fs.readFileSync(path.join(FONT_DIR, 'NotoSansSC-Regular.otf')), weight: 400, style: 'normal' }, { name: 'Noto Sans SC', data: fs.readFileSync(path.join(FONT_DIR, 'NotoSansSC-Bold.otf')), weight: 700, style: 'normal' }, ]; const FAMILY = 'Noto Sans SC'; const isTransparent = (c) => !c || c === 'transparent' || /rgba\([^)]*,\s*0\s*\)/.test(c); function rectNode(o) { const s = { position: 'absolute', left: `${o.left}px`, top: `${o.top}px`, width: `${o.width}px`, height: `${o.height}px`, background: isTransparent(o.fill) ? 'transparent' : o.fill, opacity: o.opacity === undefined ? 1 : o.opacity, display: 'flex', }; if (o.rx) s.borderRadius = `${o.rx}px`; if (o.angle) { s.transform = `rotate(${o.angle}deg)`; s.transformOrigin = 'top left'; } if (o.stroke && !isTransparent(o.stroke)) { const w = Math.max(1, Math.round(o.strokeWidth || 1)); s.border = o.strokeDashArray ? `${w}px dashed ${o.stroke}` : `${w}px solid ${o.stroke}`; } return { type: 'div', props: { style: s } }; } function textNode(o) { const outline = isTransparent(o.fill) && o.stroke && !isTransparent(o.stroke); const s = { position: 'absolute', left: `${o.left}px`, top: `${o.top}px`, width: `${o.width}px`, fontFamily: FAMILY, fontSize: `${o.fontSize}px`, fontWeight: o.fontWeight === 'bold' ? 700 : 400, color: outline ? o.stroke : o.fill, textAlign: o.textAlign || 'left', lineHeight: o.lineHeight || 1.3, letterSpacing: o.charSpacing ? `${o.charSpacing}px` : 0, whiteSpace: 'pre-wrap', opacity: o.opacity === undefined ? 1 : o.opacity, display: 'flex', }; return { type: 'div', props: { style: s, children: String(o.text).replace(/\n/g, ' ') } }; } async function renderOne(m) { const doc = JSON.parse(fs.readFileSync(path.join(DATA, 'tmpl', `${m.id}.json`), 'utf8')); const nodes = doc.objects.filter(o => o.id !== 'workspace') .map(o => (o.type === 'rect' ? rectNode(o) : textNode(o))); const root = { type: 'div', props: { style: { width: `${m.w}px`, height: `${m.h}px`, display: 'flex', position: 'relative', background: '#ffffff', overflow: 'hidden' }, children: nodes, }, }; const svg = await satori(root, { width: m.w, height: m.h, fonts: FONTS }); const png = new Resvg(svg, { fitTo: { mode: 'width', value: Math.min(m.w, 1100) } }).render().asPng(); fs.writeFileSync(path.join(DATA, 'preview', `${m.id}.png`), png); return png.length; } (async () => { const index = JSON.parse(fs.readFileSync(path.join(DATA, 'index.json'), 'utf8')); let ok = 0, fail = []; for (const m of index) { try { await renderOne(m); ok++; } catch (e) { fail.push(`${m.id}:${e.message}`); } } console.log(`预览图完成 ${ok}/${index.length}`); if (fail.length) console.log('失败:', fail.join(' | ')); })();