feat(icons): 新增微软 Fluent Emoji 3D 位图图标源(1586 个,MIT),支持位图图标渲染与导出内联
This commit is contained in:
+13
-6
@@ -220,9 +220,13 @@ const HGB_Exporter = (function () {
|
||||
* @param {string|null} bgColor 背景色;null 表示透明
|
||||
*/
|
||||
function renderCanvas(size, bgColor, svgOverride) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const svgStr = svgOverride ||
|
||||
buildExportSvg(bgColor === undefined ? null : bgColor);
|
||||
const raw = svgOverride ||
|
||||
buildExportSvg(bgColor === undefined ? null : bgColor);
|
||||
// 位图图标(3D PNG)以相对 href 引用,转 blob 后相对路径失效 → 先内联为 data URI
|
||||
const ready = (typeof HGB_IconLib !== 'undefined' && HGB_IconLib.inlineRasters)
|
||||
? HGB_IconLib.inlineRasters(raw)
|
||||
: Promise.resolve(raw);
|
||||
return ready.then((svgStr) => new Promise((resolve, reject) => {
|
||||
const blob = new Blob([svgStr], { type: 'image/svg+xml;charset=utf-8' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
@@ -245,12 +249,15 @@ const HGB_Exporter = (function () {
|
||||
reject(new Error('SVG 渲染失败,请检查元素内容'));
|
||||
};
|
||||
img.src = url;
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
/* ============ 导出:SVG ============ */
|
||||
function exportSvg(svgOverride, bgColor) {
|
||||
const svgStr = svgOverride || buildExportSvg(bgColor || HGB_Store.get().bg || '#FFFFFF');
|
||||
async function exportSvg(svgOverride, bgColor) {
|
||||
let svgStr = svgOverride || buildExportSvg(bgColor || HGB_Store.get().bg || '#FFFFFF');
|
||||
if (typeof HGB_IconLib !== 'undefined' && HGB_IconLib.inlineRasters) {
|
||||
svgStr = await HGB_IconLib.inlineRasters(svgStr);
|
||||
}
|
||||
download(svgStr, 'image/svg+xml', safeName() + '.svg');
|
||||
return svgStr;
|
||||
}
|
||||
|
||||
+47
-2
@@ -8,7 +8,7 @@
|
||||
const HGB_IconLib = (function () {
|
||||
'use strict';
|
||||
|
||||
const VERSION = '20261105c'; // 与 index.html 中 builtin-icons.js?v= 版本联动
|
||||
const VERSION = '20261105d'; // 与 index.html 中 builtin-icons.js?v= 版本联动
|
||||
const cache = {}; // sid -> {icons:[...]}
|
||||
const pending = {}; // sid -> Promise
|
||||
|
||||
@@ -206,9 +206,54 @@ const HGB_IconLib = (function () {
|
||||
return String(fullId || '').split(':')[0];
|
||||
}
|
||||
|
||||
/* ---------- 位图图标内联(PNG 导出用) ---------- */
|
||||
// 3D 位图图标的数据文件以 <image href="js/data/sources/...png"> 引用原图,
|
||||
// 页面内联渲染可直接显示;但导出 PNG/PDF/SVG 时 SVG 会被转成 blob/data URI,
|
||||
// 相对路径失效 → 导出前把 href 内联成 data URI(按路径缓存,只取一次)。
|
||||
var rasterCache = {}; // 相对路径 -> data URI
|
||||
var RASTER_RE = /href="(js\/data\/sources\/[^"]+\.png)"/g;
|
||||
|
||||
/** 是否含位图图标引用 */
|
||||
function hasRaster(svg) {
|
||||
return typeof svg === 'string' && svg.indexOf('js/data/sources/') !== -1 && svg.indexOf('.png"') !== -1;
|
||||
}
|
||||
|
||||
function fetchDataURI(p) {
|
||||
if (rasterCache[p]) return Promise.resolve(rasterCache[p]);
|
||||
return fetch(p).then(function (r) {
|
||||
if (!r.ok) throw new Error('位图加载失败: ' + p);
|
||||
return r.blob();
|
||||
}).then(function (blob) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
var fr = new FileReader();
|
||||
fr.onload = function () { rasterCache[p] = fr.result; resolve(fr.result); };
|
||||
fr.onerror = reject;
|
||||
fr.readAsDataURL(blob);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/** 把 svg 里的位图相对引用内联为 data URI(导出前调用;无位图时原样返回) */
|
||||
function inlineRasters(svg) {
|
||||
if (!hasRaster(svg)) return Promise.resolve(svg);
|
||||
var paths = [], m;
|
||||
RASTER_RE.lastIndex = 0;
|
||||
while ((m = RASTER_RE.exec(svg))) {
|
||||
if (paths.indexOf(m[1]) === -1) paths.push(m[1]);
|
||||
}
|
||||
return Promise.all(paths.map(fetchDataURI)).then(function () {
|
||||
var out = svg;
|
||||
paths.forEach(function (p) {
|
||||
out = out.split('href="' + p + '"').join('href="' + rasterCache[p] + '"');
|
||||
});
|
||||
return out;
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
VERSION, loadSource, loadSources, preload, loadAll,
|
||||
getSource, loaded, loadedIds, findIcon, searchLoaded, searchIndex, sourceOf,
|
||||
expandKeyword, matchIcon: iconMatches, scoreIcon
|
||||
expandKeyword, matchIcon: iconMatches, scoreIcon,
|
||||
hasRaster, inlineRasters
|
||||
};
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user