feat(poster): 海报画布接入思源黑体(Noto Sans SC),SVG 导出走相对路径引用
- 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
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
/**
|
||||
* 画个标 · 海报导出:SVG / PNG(按画布规格导出)
|
||||
* - SVG:@font-face 用相对路径引用站点内思源黑体(文件小,留在站点内打开即正确渲染)
|
||||
* - PNG:SVG 作为 <img> 渲染时拿不到页面 @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<string>,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 };
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user