feat(icons): 新增微软 Fluent Emoji 3D 位图图标源(1586 个,MIT),支持位图图标渲染与导出内联

This commit is contained in:
sgp
2026-11-05 11:33:36 +08:00
parent 07d2a79a60
commit fcb70e8c7f
1606 changed files with 266 additions and 55 deletions
+89 -3
View File
@@ -25,16 +25,16 @@ const path = require('path');
const SOURCES = require('../js/data/icon-sources.js');
/* ================= 网络 ================= */
function fetch(url, redirects) {
function fetch(url, redirects, headers) {
redirects = redirects || 0;
return new Promise((resolve, reject) => {
const req = https.get(url, { timeout: 60000 }, (res) => {
const req = https.get(url, { timeout: 60000, headers: headers || {} }, (res) => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
res.resume();
if (redirects >= 3) return reject(new Error('redirect loop ' + url));
let next = res.headers.location;
if (next.startsWith('/')) next = 'https://unpkg.com' + next;
return resolve(fetch(next, redirects + 1));
return resolve(fetch(next, redirects + 1, headers));
}
if (res.statusCode !== 200) {
res.resume();
@@ -49,6 +49,31 @@ function fetch(url, redirects) {
});
}
/** 二进制下载(位图图标用) */
function fetchBuffer(url, redirects) {
redirects = redirects || 0;
return new Promise((resolve, reject) => {
const req = https.get(url, { timeout: 60000 }, (res) => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
res.resume();
if (redirects >= 3) return reject(new Error('redirect loop ' + url));
let next = res.headers.location;
if (next.startsWith('/')) next = 'https://cdn.jsdelivr.net' + next;
return resolve(fetchBuffer(next, redirects + 1));
}
if (res.statusCode !== 200) {
res.resume();
return reject(new Error(`${res.statusCode} ${url}`));
}
const chunks = [];
res.on('data', c => chunks.push(c));
res.on('end', () => resolve(Buffer.concat(chunks)));
});
req.on('error', reject);
req.on('timeout', () => { req.destroy(new Error('timeout ' + url)); });
});
}
/** 简易并发池(remix 单文件批量下载用) */
async function mapLimit(items, limit, fn) {
const results = new Array(items.length);
@@ -649,6 +674,61 @@ async function downloadGhFiles(src) {
return { icons, ok, fail };
}
/* ================= GitHub 位图图标(3D 渲染 PNG:文件树枚举 + 二进制下载) ================= */
/**
* 用于只提供位图(PNG)的开源图标集(如 Fluent Emoji 3D)。
* - pathRe:文件树路径正则,筛出目标素材(如 assets/<Name>/3D/xxx.png 或 Default 肤色)
* - exclude:路径正则排除(如非 Default 肤色变体),避免同图多肤色堆量
* 产物:PNG 落盘 js/data/sources/{id}/files/,图标 svg 为 <image href="..."> 引用
* (渲染时相对页面路径可直接显示;导出时由 HGB_IconLib.inlineRasters 内联为 data URI)
*/
async function downloadGhRaster(src) {
const branch = src.ghBranch || 'main';
const api = `https://api.github.com/repos/${src.gh}/git/trees/${branch}?recursive=1`;
const tree = JSON.parse(await fetch(api, 0, {
'User-Agent': 'drawlogo-build',
Accept: 'application/vnd.github+json'
}));
const re = new RegExp(src.pathRe);
let picked = (tree.tree || []).filter(e => e.type === 'blob' && re.test(e.path));
if (src.exclude) {
const ex = new RegExp(src.exclude);
picked = picked.filter(e => !ex.test(e.path));
}
const cap = Number(src.maxItems) > 0 ? Math.min(Number(src.maxItems), picked.length) : picked.length;
picked = picked.slice(0, cap);
const expected = picked.length;
const dir = path.join(process.cwd(), 'js/data/sources', src.id, 'files');
fs.mkdirSync(dir, { recursive: true });
const base = `https://cdn.jsdelivr.net/gh/${src.gh}@${branch}/`;
let fail = 0;
const results = await mapLimit(picked, 12, async (e) => {
// 文件名 slug → 图标名:去掉 _3d / _3d_default 后缀,下划线转连字符
const slug = path.basename(e.path, '.png')
.replace(/_3d(?:_default)?$/i, '')
.replace(/_/g, '-');
try {
const buf = await fetchBuffer(base + e.path.split('/').map(encodeURIComponent).join('/'));
if (!buf || buf.length < 500) throw new Error('empty');
fs.writeFileSync(path.join(dir, slug + '.png'), buf);
const c = classify(slug);
return {
id: `${src.id}:${slug}`,
name: slug,
svg: `<image href="js/data/sources/${src.id}/files/${slug}.png" x="0" y="0" width="24" height="24" preserveAspectRatio="xMidYMid meet"/>`,
category: c.cat,
categoryCn: c.cn,
raster: 1
};
} catch (err) {
fail++;
return null;
}
});
return { icons: results.filter(Boolean), fail, expected };
}
/* ================= Iconify 集合(@iconify-json npm 包,单文件 icons.json) ================= */
/**
* IconifyJSON 格式:{prefix, icons:{name:{body,width,height}}, aliases:{alias:{parent}}, width, height}
@@ -943,6 +1023,12 @@ async function main() {
const r = await downloadGhFiles(src);
icons = r.icons; fail = r.fail;
usedUrl = 'cdn.jsdelivr.net/gh/' + src.gh;
} else if (src.format === 'gh-raster') {
console.log(`获取 ${src.name}(GitHub 位图 ${src.gh})…`);
downloaded = true;
const r = await downloadGhRaster(src);
icons = r.icons; fail = r.fail; expCount = r.expected;
usedUrl = 'cdn.jsdelivr.net/gh/' + src.gh;
} else if (src.format === 'iconify') {
console.log(`获取 ${src.name}(Iconify 集合)…`);
downloaded = true;