/**
* 画个标 · 海报渲染引擎:元素数组 → SVG 字符串
* 中文字体:思源黑体(海报素材库 noto-sans-sc,OFL-1.1,OTF 直载)
* - 页面预览:注入 @font-face 后由浏览器直接渲染
* - 导出:SVG 作为
渲染时拿不到页面 @font-face,
* 故把实际用到的字重内联进 SVG(';
}
/** 按元素实际用到的字重生成内联 @font-face(PNG 导出用);无文字元素返回空串 */
function embedFontDefs(elements) {
const used = fontsUsed(elements);
if (!used.needRegular && !used.needBold) return Promise.resolve('');
const jobs = [];
if (used.needRegular) {
jobs.push(fontBase64(FONT.regular).then(function (b) {
return faceRule('data:font/otf;base64,' + b, 400);
}));
}
if (used.needBold) {
jobs.push(fontBase64(FONT.bold).then(function (b) {
return faceRule('data:font/otf;base64,' + b, '700 900');
}));
}
return Promise.all(jobs).then(function (rules) {
return '';
});
}
/* ---------- 元素 → SVG ---------- */
function elToSvg(el) {
if (el.type === 'rect') {
return ``;
}
if (el.type === 'circle') {
return ``;
}
if (el.type === 'text') {
const anchor = el.align === 'middle' ? 'middle' : 'start';
// 字族用单引号包裹,避免与属性双引号冲突
const family = String(el.fontFamily || FONT_STACK).replace(/"/g, "'");
return `${esc(el.text)}`;
}
if (el.type === 'icon') {
return `${el.svg}`;
}
return '';
}
function wrap(spec, body, defs) {
return ``;
}
/** 页面预览用(同步,依赖已加载的 @font-face) */
function render(spec, elements) {
return wrap(spec, (elements || []).map(elToSvg).join(''), '');
}
/** SVG 导出用(同步、文件小):@font-face 相对路径引用站点内字体 */
function renderLinked(spec, elements) {
return wrap(spec, (elements || []).map(elToSvg).join(''), linkFontDefs(elements));
}
/** 导出用(异步,内联字体,离线可用);字体缺失时降级为系统字体栈,不阻断导出 */
function renderEmbedded(spec, elements) {
const body = (elements || []).map(elToSvg).join('');
return embedFontDefs(elements)
.then(function (defs) { return wrap(spec, body, defs); })
.catch(function () { return wrap(spec, body, ''); });
}
return { render, renderLinked, renderEmbedded, installFontFaces, ensureFonts, elToSvg, FONT, FONT_STACK };
})();