diff --git a/js/core/poster-exporter.js b/js/core/poster-exporter.js
index aabcc90..fe3b6cf 100644
--- a/js/core/poster-exporter.js
+++ b/js/core/poster-exporter.js
@@ -1,5 +1,7 @@
/**
* 画个标 · 海报导出:SVG / PNG(按画布规格导出)
+ * - SVG:@font-face 用相对路径引用站点内思源黑体(文件小,留在站点内打开即正确渲染)
+ * - PNG:SVG 作为
渲染时拿不到页面 @font-face,故内联字重后再栅格化
*/
const HGB_PosterExporter = (function () {
'use strict';
@@ -8,6 +10,16 @@ const HGB_PosterExporter = (function () {
return HGB_PosterRenderer.render(spec, elements);
}
+ /** SVG 导出用(相对路径字体,文件小) */
+ function buildSvgLinked(spec, elements) {
+ return HGB_PosterRenderer.renderLinked(spec, elements);
+ }
+
+ /** 内联字体后的 SVG(Promise,PNG 栅格化用) */
+ function buildSvgEmbedded(spec, elements) {
+ return HGB_PosterRenderer.renderEmbedded(spec, elements);
+ }
+
function download(name, blob) {
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
@@ -18,14 +30,13 @@ const HGB_PosterExporter = (function () {
}
function exportSvg(spec, elements, base) {
- const svg = buildSvg(spec, elements);
+ const svg = buildSvgLinked(spec, elements);
download((base || '海报') + '.svg', new Blob([svg], { type: 'image/svg+xml' }));
}
/** 渲染 SVG 到位图并下载(scale 控制放大倍数,默认 1) */
function svgToCanvas(spec, elements, scale) {
- return new Promise((resolve, reject) => {
- const svg = buildSvg(spec, elements);
+ return buildSvgEmbedded(spec, elements).then(svg => new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
const w = Math.round(spec.w * scale), h = Math.round(spec.h * scale);
@@ -37,7 +48,7 @@ const HGB_PosterExporter = (function () {
};
img.onerror = () => reject(new Error('SVG 渲染失败'));
img.src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg);
- });
+ }));
}
async function exportPng(spec, elements, base, scale) {
@@ -46,5 +57,5 @@ const HGB_PosterExporter = (function () {
download((base || '海报') + '.png', blob);
}
- return { buildSvg, exportSvg, exportPng, svgToCanvas };
+ return { buildSvg, buildSvgLinked, buildSvgEmbedded, exportSvg, exportPng, svgToCanvas };
})();
diff --git a/js/core/poster-renderer.js b/js/core/poster-renderer.js
index d64a010..91e699d 100644
--- a/js/core/poster-renderer.js
+++ b/js/core/poster-renderer.js
@@ -1,13 +1,127 @@
/**
* 画个标 · 海报渲染引擎:元素数组 → 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 ``;
@@ -17,7 +131,9 @@ const HGB_PosterRenderer = (function () {
}
if (el.type === 'text') {
const anchor = el.align === 'middle' ? 'middle' : 'start';
- return `${esc(el.text)}`;
+ // 字族用单引号包裹,避免与属性双引号冲突
+ const family = String(el.fontFamily || FONT_STACK).replace(/"/g, "'");
+ return `${esc(el.text)}`;
}
if (el.type === 'icon') {
return `${el.svg}`;
@@ -25,10 +141,27 @@ const HGB_PosterRenderer = (function () {
return '';
}
- function render(spec, elements) {
- const body = (elements || []).map(elToSvg).join('');
- return ``;
+ function wrap(spec, body, defs) {
+ return ``;
}
- return { render };
+ /** 页面预览用(同步,依赖已加载的 @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 };
})();
diff --git a/poster.html b/poster.html
index 5b6e6e7..5f8675b 100644
--- a/poster.html
+++ b/poster.html
@@ -93,11 +93,11 @@
-
-
-
-
-
+
+
+
+
+