/**
* 👤 人脸/人物头像风格样本预览页(18 种,每种 6 个样例,纯 SVG 无需渲染)
* 用户选定风格后,再批量生成 100 个。
*/
'use strict';
const fs = require('fs');
const path = require('path');
const vm = require('vm');
const ROOT = process.cwd();
global.window = global;
// 1. 加载引擎(const 声明需同一上下文,拼接赋值暴露到 globalThis)
const engine = fs.readFileSync(path.join(ROOT, 'js', 'core', 'dicebear.js'), 'utf8')
+ '\n;globalThis.HGB_Dicebear = HGB_Dicebear;';
vm.runInThisContext(engine, { filename: 'dicebear.js' });
const render = globalThis.HGB_Dicebear.renderAvatar;
// 2. 人脸/人物风格(排除 bottts 机器人、shapes/rings/identicon 抽象、initials 文字)
const STYLES = [
['avataaars', '阿凡达卡通人物'], ['adventurer', '冒险家人物'], ['adventurer-neutral', '冒险家·中性'],
['notionists', 'Notion 现代人物'], ['notionists-neutral', 'Notion·中性'], ['personas', '人物插画'],
['open-peeps', '手绘小人'], ['micah', '米迦人物'], ['lorelei', '洛雷莱人物'],
['lorelei-neutral', '洛雷莱·中性'], ['dylan', '迪伦人物'], ['miniavs', '迷你头像'],
['big-smile', '大笑脸'], ['fun-emoji', '趣味表情'], ['croodles', '卡通线条'],
['croodles-neutral', '卡通线条中性'], ['big-ears', '大耳朵人物'], ['big-ears-neutral', '大耳朵·中性']
];
let cards = '';
let ok = 0, fail = 0;
for (const [id, cn] of STYLES) {
const f = path.join(ROOT, 'js', 'data', 'sources', `dicebear-${id}.js`);
if (!fs.existsSync(f)) { console.warn('⚠️ 缺文件:', id); continue; }
try {
vm.runInThisContext(fs.readFileSync(f, 'utf8'), { filename: f });
const def = window[`HGB_Dicebear_${id.replace(/-/g, '_')}`];
if (!def) throw new Error('未注册 ' + id);
const cw = def.canvas.width, ch = def.canvas.height;
let samples = '';
for (let s = 1; s <= 6; s++) {
const r = render(id, `face-${s}`);
samples += ``;
}
cards += `