feat(expand): P0-P3 素材扩库批次(发现器 + Google Fonts + Poly Haven + Noto Emoji 3D)
- P0: scripts/discover-sources.js 四通道候选发现,产出 candidates.json / registry.json - P1: build-posters 新增 fontsource 适配器,接入 Google Fonts 2766 个 woff2 - P2: 新增 polyhaven 适配器,接入 Poly Haven CC0 纹理 238 张 - P3: build-icons 新增 gh-raster listPath(Git Tree 逐级下钻),接入 Noto Emoji 3D 3797 个 PNG - 修复: GitHub Contents API 不支持目录分页导致重复/截断,改为 Git Tree 下钻并按路径去重;audit-files.json 同步重建 - 判定 noto-emoji-2d / twemoji-png 与既有 Iconify 源同图重复,不予收录 - 基线: 图标 264 源/448,551;海报 69 源/5,366;版本联动 20261108a
This commit is contained in:
@@ -315,6 +315,135 @@ async function downloadDirFiles(src) {
|
||||
return { items, ok, fail };
|
||||
}
|
||||
|
||||
/**
|
||||
* Fontsource 字体(Google Fonts 全字族自动发现)
|
||||
* 配置:{ format:'fontsource', subsets:['latin'], weights:[400,700], maxFamilies?, exclude? }
|
||||
* 说明:构建时拉取 https://api.fontsource.org/v1/fonts,按协议白名单 + 子集过滤,
|
||||
* 逐个从 jsDelivr 下载 woff2(体积小、浏览器可直用),新增字族无需改配置。
|
||||
*/
|
||||
const FONT_LICENSE_OK = /^(OFL-1\.1|Apache-2\.0|CC0-1\.0|Unlicense|MIT)$/i;
|
||||
|
||||
async function downloadFontsource(src) {
|
||||
const fonts = JSON.parse(await fetchText('https://api.fontsource.org/v1/fonts'));
|
||||
const subsets = Array.isArray(src.subsets) && src.subsets.length ? src.subsets : ['latin'];
|
||||
const weights = Array.isArray(src.weights) && src.weights.length ? src.weights : [400, 700];
|
||||
let list = fonts.filter(f =>
|
||||
f && f.id && FONT_LICENSE_OK.test(f.license || '') &&
|
||||
(f.type === 'google') &&
|
||||
Array.isArray(f.subsets) && f.subsets.some(s => subsets.includes(s))
|
||||
);
|
||||
if (src.exclude) {
|
||||
const re = new RegExp(src.exclude);
|
||||
list = list.filter(f => !re.test(f.id));
|
||||
}
|
||||
list = list.slice().sort((a, b) => String(a.id).localeCompare(String(b.id)));
|
||||
if (Number(src.maxFamilies) > 0) list = list.slice(0, Number(src.maxFamilies));
|
||||
|
||||
const targets = [];
|
||||
for (const f of list) {
|
||||
const subset = subsets.find(s => (f.subsets || []).includes(s)) || 'latin';
|
||||
const have = Array.isArray(f.weights) ? f.weights : [];
|
||||
let use = weights.filter(w => have.includes(w));
|
||||
if (!use.length && have.length) {
|
||||
// 没有 400/700 时退化为最接近的标准字重
|
||||
const near = w => have.reduce((a, b) => Math.abs(b - w) < Math.abs(a - w) ? b : a, have[0]);
|
||||
use = [...new Set([near(400), near(700)])];
|
||||
}
|
||||
if (!use.length) use = [400];
|
||||
for (const w of use) targets.push({ id: f.id, subset, weight: w });
|
||||
}
|
||||
|
||||
let done = 0;
|
||||
const results = await mapLimit(targets, 16, async (t) => {
|
||||
const url = `https://cdn.jsdelivr.net/npm/@fontsource/${t.id}/files/${t.id}-${t.subset}-${t.weight}-normal.woff2`;
|
||||
try {
|
||||
const buf = await fetchBuffer(url);
|
||||
if (!buf || buf.length < 400 || buf.slice(0, 4).toString('latin1') !== 'wOF2') throw new Error('bad woff2');
|
||||
done++;
|
||||
if (done % 500 === 0) console.log(` …已下载 ${done}/${targets.length}`);
|
||||
return { t, buf };
|
||||
} catch (e) { return null; }
|
||||
});
|
||||
|
||||
const items = [];
|
||||
let ok = 0, fail = 0;
|
||||
for (const r of results) {
|
||||
if (!r) { fail++; continue; }
|
||||
const name = `${r.t.id}-${r.t.weight}`;
|
||||
const it = await buildItem(src, name, name + '.woff2', r.buf, null);
|
||||
if (it) { items.push(it); ok++; } else fail++;
|
||||
}
|
||||
return { items, ok, fail, expected: targets.length };
|
||||
}
|
||||
|
||||
/**
|
||||
* Poly Haven 纹理(CC0 自动发现)
|
||||
* 配置:{ format:'polyhaven', maxItems:240, res:'1k', imageFormat:'jpg', categories?:[...], exclude? }
|
||||
* 说明:按分类轮转挑选,保证纹理多样性(不要同一类木纹刷屏)。
|
||||
*/
|
||||
async function downloadPolyhaven(src) {
|
||||
const all = JSON.parse(await fetchText('https://api.polyhaven.com/assets?t=textures'));
|
||||
const res = src.res || '1k';
|
||||
const imgFmt = src.imageFormat || 'jpg';
|
||||
let ids = Object.keys(all);
|
||||
if (Array.isArray(src.categories) && src.categories.length) {
|
||||
const want = new Set(src.categories);
|
||||
ids = ids.filter(id => (all[id].categories || []).some(c => want.has(c)));
|
||||
}
|
||||
if (src.exclude) {
|
||||
const re = new RegExp(src.exclude);
|
||||
ids = ids.filter(id => !re.test(id));
|
||||
}
|
||||
ids.sort();
|
||||
|
||||
// 分类轮转择优:每轮从各分类各取一个,直到达到上限
|
||||
const buckets = new Map();
|
||||
for (const id of ids) {
|
||||
const cats = (all[id].categories || []).filter(c => !c.startsWith('collection:'));
|
||||
const key = cats[0] || 'other';
|
||||
if (!buckets.has(key)) buckets.set(key, []);
|
||||
buckets.get(key).push(id);
|
||||
}
|
||||
const picked = [];
|
||||
const keys = [...buckets.keys()].sort();
|
||||
let round = 0;
|
||||
while (picked.length < (Number(src.maxItems) || 240)) {
|
||||
let added = 0;
|
||||
for (const k of keys) {
|
||||
const arr = buckets.get(k);
|
||||
if (round < arr.length) { picked.push(arr[round]); added++; }
|
||||
if (picked.length >= (Number(src.maxItems) || 240)) break;
|
||||
}
|
||||
if (!added) break;
|
||||
round++;
|
||||
}
|
||||
|
||||
let done = 0;
|
||||
const results = await mapLimit(picked, 8, async (id) => {
|
||||
try {
|
||||
const files = JSON.parse(await fetchText('https://api.polyhaven.com/files/' + id));
|
||||
const diffuse = files.Diffuse || files.diffuse || files.albedo;
|
||||
const variant = diffuse && (diffuse[res] || diffuse[Object.keys(diffuse)[0]]);
|
||||
const url = variant && variant[imgFmt] && variant[imgFmt].url;
|
||||
if (!url) throw new Error('no url');
|
||||
const buf = await fetchBuffer(url);
|
||||
if (!buf || buf.length < 2000) throw new Error('empty');
|
||||
done++;
|
||||
if (done % 40 === 0) console.log(` …已下载 ${done}/${picked.length}`);
|
||||
return { id, url, buf };
|
||||
} catch (e) { return null; }
|
||||
});
|
||||
|
||||
const items = [];
|
||||
let ok = 0, fail = 0;
|
||||
for (const r of results) {
|
||||
if (!r) { fail++; continue; }
|
||||
const it = await buildItem(src, r.id, r.id + '.' + imgFmt, r.buf, classifyPoster(r.id));
|
||||
if (it) { items.push(it); ok++; } else fail++;
|
||||
}
|
||||
return { items, ok, fail, expected: picked.length };
|
||||
}
|
||||
|
||||
/**
|
||||
* 本地源:读取 js/data/posters/{id}/files 下预先生成的素材(不联网)
|
||||
* 用于从 CSS 提取的图案、渐变与配色 JSON 等无独立文件的开源资源
|
||||
@@ -433,6 +562,11 @@ async function buildItem(src, name, filePath, buf, c) {
|
||||
} catch (e) { return null; }
|
||||
}
|
||||
|
||||
if (ext === '.woff2') {
|
||||
if (buf.slice(0, 4).toString('latin1') !== 'wOF2') return null; // woff2 魔数校验
|
||||
return saveBinary(src, base, filePath, buf, 'font/woff2', null);
|
||||
}
|
||||
|
||||
// 字体及其它二进制:保存 + 引用
|
||||
return saveBinary(src, base, filePath, buf, 'application/octet-stream', null);
|
||||
}
|
||||
@@ -535,6 +669,16 @@ async function main() {
|
||||
const r = await readLocalFiles(src);
|
||||
items = r.items; fail = r.fail; expected = r.ok + r.fail;
|
||||
usedUrl = 'local';
|
||||
} else if (src.format === 'fontsource') {
|
||||
console.log(`发现 ${src.name}(Fontsource 自动扫描字族)…`);
|
||||
const r = await downloadFontsource(src);
|
||||
items = r.items; fail = r.fail; expected = r.expected;
|
||||
usedUrl = 'cdn.jsdelivr.net/npm/@fontsource';
|
||||
} else if (src.format === 'polyhaven') {
|
||||
console.log(`发现 ${src.name}(Poly Haven CC0 纹理)…`);
|
||||
const r = await downloadPolyhaven(src);
|
||||
items = r.items; fail = r.fail; expected = r.expected;
|
||||
usedUrl = 'polyhaven.com';
|
||||
} else {
|
||||
throw new Error(`不支持的 format: ${src.format}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user