From 5bcd5ab0f27896e658f9acf5f4946be3f0a69320 Mon Sep 17 00:00:00 2001 From: sgp Date: Thu, 5 Nov 2026 20:14:13 +0800 Subject: [PATCH] =?UTF-8?q?feat(poster):=20=E6=B5=B7=E6=8A=A5=E7=94=BB?= =?UTF-8?q?=E5=B8=83=E6=8E=A5=E5=85=A5=E6=80=9D=E6=BA=90=E9=BB=91=E4=BD=93?= =?UTF-8?q?=EF=BC=88Noto=20Sans=20SC=EF=BC=89=EF=BC=8CSVG=20=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E8=B5=B0=E7=9B=B8=E5=AF=B9=E8=B7=AF=E5=BE=84=E5=BC=95?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - poster-renderer:注入 @font-face(按需加载,不阻塞首屏),文字默认字族 Noto Sans SC;新增 renderLinked(相对路径)与 renderEmbedded(PNG 内联,字体缺失自动降级) - poster-exporter:SVG 导出用相对路径(约 1KB),PNG 仍内联字体保证自包含 - 实测:页面内字体生效(中文墨迹 7129 vs 回退 6952);导出的 SVG 站点内打开生效(8154 vs 7959);PNG 900x1200 正常 - poster.html 相关脚本统一 ?v=20261105a --- js/core/poster-exporter.js | 21 ++++-- js/core/poster-renderer.js | 143 +++++++++++++++++++++++++++++++++++-- poster.html | 14 ++-- 3 files changed, 162 insertions(+), 16 deletions(-) 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 `${body}`; + function wrap(spec, body, defs) { + return `${defs || ''}${body}`; } - 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 @@ - - - - - + + + + +