/** * 画个标 · 海报生成引擎:版式模板 + 配色 + 元素构造 * 决策(paletteId/layoutId/title/sub/icon)→ 元素数组,供 renderer 渲染。 */ const HGB_PosterGenerator = (function () { 'use strict'; const PALETTES = [ { id: 'indigo', name: '靛蓝', bg: '#1e1b4b', primary: '#ffffff', accent: '#f59e0b' }, { id: 'crimson', name: '深红', bg: '#7f1d1d', primary: '#ffffff', accent: '#fbbf24' }, { id: 'ocean', name: '海洋', bg: '#0c4a6e', primary: '#ffffff', accent: '#38bdf8' }, { id: 'forest', name: '森林', bg: '#14532d', primary: '#ffffff', accent: '#86efac' }, { id: 'minimal', name: '极简', bg: '#ffffff', primary: '#111827', accent: '#4f46e5' } ]; /** * 字体层级预设(白名单):AI 只能从中选择,保证标题/副标题的字号比与字重组合可控。 * 字重限制在 400 / 700 / 800 —— 渲染器只内嵌 Noto Sans SC Regular(400) 与 Bold(700-900), * 超出范围的权重会被浏览器合成,视觉不可控。 */ const TYPE_SCALES = [ { id: 'impact', name: '强冲击', titleWeight: 800, subWeight: 400, titleRatio: 0.075, subRatio: 0.38, note: '促销/活动主视觉,超大粗标题压场' }, { id: 'editorial', name: '编辑感', titleWeight: 700, subWeight: 400, titleRatio: 0.062, subRatio: 0.44, note: '文化/品牌叙事,标题收敛、副标题更可读' }, { id: 'steady', name: '稳健均衡', titleWeight: 700, subWeight: 700, titleRatio: 0.068, subRatio: 0.40, note: '政务/金融等信息型海报,标题副标题同粗同稳' }, { id: 'quiet', name: '克制留白', titleWeight: 400, subWeight: 400, titleRatio: 0.052, subRatio: 0.46, note: '高端/极简,细标题+大留白,靠空间而非字重建立层级' } ]; const DEFAULT_SCALE = 'impact'; function scaleOf(id) { return TYPE_SCALES.find(s => s.id === id) || TYPE_SCALES[0]; } function uid(p) { return p + '_' + Date.now().toString(36) + Math.random().toString(36).slice(2, 6); } function rect(id, x, y, w, h, fill) { return { id, type: 'rect', x, y, w, h, fill }; } function circle(id, cx, cy, r, fill) { return { id, type: 'circle', cx, cy, r, fill }; } function text(id, text, x, y, fontSize, fontWeight, fill, align) { return { id, type: 'text', text, x, y, fontSize, fontWeight, fill, align: align || 'start' }; } const LAYOUTS = [ { id: 'centered', name: '居中对称', emoji: '🎯', build(spec, o) { const cw = spec.w, ch = spec.h, els = []; els.push(rect(uid('bg'), 0, 0, cw, ch, o.palette.bg)); els.push(circle(uid('dot'), cw / 2, ch * 0.30, ch * 0.12, o.palette.accent)); const fs = Math.round(cw * o.scale.titleRatio); els.push(text(uid('title'), o.title, cw / 2, ch * 0.50, fs, o.scale.titleWeight, o.palette.primary, 'middle')); if (o.sub) els.push(text(uid('sub'), o.sub, cw / 2, ch * 0.56, Math.round(fs * o.scale.subRatio), o.scale.subWeight, o.palette.primary, 'middle')); els.push(rect(uid('bar'), cw / 2 - cw * 0.12, ch * 0.62, cw * 0.24, 8, o.palette.accent)); return els; } }, { id: 'left', name: '左对齐', emoji: '◀️', build(spec, o) { const cw = spec.w, ch = spec.h, els = []; els.push(rect(uid('bg'), 0, 0, cw, ch, o.palette.bg)); els.push(rect(uid('side'), 0, 0, cw * 0.06, ch, o.palette.accent)); const mx = cw * 0.13; const fs = Math.round(cw * o.scale.titleRatio); els.push(text(uid('title'), o.title, mx, ch * 0.44, fs, o.scale.titleWeight, o.palette.primary, 'start')); if (o.sub) els.push(text(uid('sub'), o.sub, mx, ch * 0.52, Math.round(fs * o.scale.subRatio), o.scale.subWeight, o.palette.primary, 'start')); els.push(rect(uid('bar'), mx, ch * 0.58, cw * 0.18, 8, o.palette.accent)); return els; } }, { id: 'banner', name: '顶部横幅', emoji: '📢', build(spec, o) { const cw = spec.w, ch = spec.h, els = []; els.push(rect(uid('bg'), 0, 0, cw, ch, o.palette.bg)); els.push(rect(uid('band'), 0, ch * 0.18, cw, ch * 0.16, o.palette.accent)); const fs = Math.round(cw * o.scale.titleRatio); els.push(text(uid('title'), o.title, cw / 2, ch * 0.26, fs, o.scale.titleWeight, o.palette.bg, 'middle')); if (o.sub) els.push(text(uid('sub'), o.sub, cw / 2, ch * 0.55, Math.round(fs * o.scale.subRatio), o.scale.subWeight, o.palette.primary, 'middle')); return els; } } ]; /** 决策 → 元素数组 */ function generate(spec, decision) { const palette = PALETTES.find(p => p.id === decision.paletteId) || PALETTES[0]; const layout = LAYOUTS.find(l => l.id === decision.layoutId) || LAYOUTS[0]; const scale = scaleOf(decision.typeScaleId); const o = { palette, scale, title: decision.title || '海报标题', sub: decision.sub || '' }; return layout.build(spec, o); } function defaultDecision(title, sub) { return { paletteId: 'indigo', layoutId: 'centered', typeScaleId: DEFAULT_SCALE, title: title || '海报标题', sub: sub || '' }; } return { PALETTES, LAYOUTS, TYPE_SCALES, scaleOf, generate, defaultDecision }; })();