feat(webui): 可变字体 wght 轴范围展示与集成

- 新增 src/tools/wght-range.sh: shell 读取可变字体 fvar wght 轴范围 (od/dd 解析, 无依赖)
- WebUI 前端直读 fvar (opentype.js / fontTools) 展示 wght 范围:
  - 配置页徽标: "可变字体" -> "可变字体 100-900" (无 wght 轴时仍显示"可变字体")
  - 字体测试页: var-tag 加 "(可变字体 wght 100-900)" 提示
  - 字体编辑信息卡: Variable Font -> "是 (wght 100-900)"
- FontSlot 类型扩展 wghtRange 字段
This commit is contained in:
YuleBest committed 2026-08-08 01:00:15 +08:00
1 parent 78abbd8a5d
commit 418ea6e7c6
5 files changed
+144 -10

No files matched your search

+8 -1
View File
@@ -26,9 +26,15 @@ def analyze(data):
glyphs = f['maxp'].numGlyphs
units = f['head'].unitsPerEm
variable = 'fvar' in f
wght_range = None
if variable:
for a in f['fvar'].axes:
if a.axisTag == 'wght':
wght_range = '%d-%d' % (round(a.minValue), round(a.maxValue))
break
return {'ok': True, 'family': family, 'subfamily': subfamily,
'glyphs': glyphs, 'units': units, 'variable': variable,
'names': read_all_names(f)}
'wghtRange': wght_range, 'names': read_all_names(f)}
except Exception as e:
return {'ok': False, 'error': str(e)}
`;
@@ -288,6 +294,7 @@ self.onmessage = async (e: MessageEvent) => {
glyphs: r.glyphs,
units: r.units,
variable: r.variable,
wghtRange: r.wghtRange,
names: r.names,
error: r.error,
});
+39 -8
View File
@@ -108,20 +108,39 @@ function extractFamilyName(font: any): string | null {
}
// 读取 fonts-test/ 下字体文件的名称与大小 (WebView 仅能访问 webroot 内文件)
// 可变字体时同时返回 wght 轴范围
function extractWghtRange(font: any): string | undefined {
const fvar = font?.tables?.fvar;
if (!fvar?.axes) return undefined;
const wght = fvar.axes.find((a: any) => a.tag === 'wght');
if (!wght) return undefined;
return `${Math.round(wght.minValue)}-${Math.round(wght.maxValue)}`;
}
async function readFontInfo(
file: string,
): Promise<{ name?: string; sizeText?: string; isVariable?: boolean }> {
): Promise<{ name?: string; sizeText?: string; isVariable?: boolean; wghtRange?: string }> {
// dev 假数据: 模拟各槽位字体的名称/大小/可变标识
if (import.meta.env.DEV) {
const MOCK_FONT_INFO: Record<string, { name: string; size: string; variable: boolean }> = {
'hans.ttf': { name: 'HarmonyOS Sans SC', size: '13.5 MB', variable: true },
const MOCK_FONT_INFO: Record<
string,
{ name: string; size: string; variable: boolean; wght?: string }
> = {
'hans.ttf': { name: 'HarmonyOS Sans SC', size: '13.5 MB', variable: true, wght: '100-900' },
'hant.ttf': { name: '源樣明體', size: '21.2 MB', variable: false },
'en.ttf': { name: 'Inter Variable', size: '0.8 MB', variable: true },
'en.ttf': { name: 'Inter Variable', size: '0.8 MB', variable: true, wght: '100-900' },
'mono.ttf': { name: 'JetBrains Mono', size: '1.2 MB', variable: false },
'emoji.ttf': { name: 'Noto Color Emoji', size: '9.8 MB', variable: false },
};
const mock = MOCK_FONT_INFO[file];
return mock ? { name: mock.name, sizeText: mock.size, isVariable: mock.variable } : {};
return mock
? {
name: mock.name,
sizeText: mock.size,
isVariable: mock.variable,
wghtRange: mock.wght,
}
: {};
}
try {
const res = await fetch(`fonts-test/${file}`);
@@ -130,7 +149,12 @@ async function readFontInfo(
const font = opentype.parse(buf);
const name = extractFamilyName(font);
const sizeText = `${(buf.byteLength / 1024 / 1024).toFixed(1)} MB`;
return { name: name ?? undefined, sizeText, isVariable: Boolean(font.tables?.fvar) };
return {
name: name ?? undefined,
sizeText,
isVariable: Boolean(font.tables?.fvar),
wghtRange: extractWghtRange(font),
};
} catch {
return {};
}
@@ -146,6 +170,7 @@ async function refreshSlotInfo(slot: FontSlot) {
if (info.sizeText) slot.sizeText = info.sizeText;
// 无条件同步: 防止换字体后旧的可变标记残留
slot.isVariable = Boolean(info.isVariable);
slot.wghtRange = info.wghtRange;
renderSlots();
} catch {
// 忽略解析失败, 保持选择器返回的文件名
@@ -164,6 +189,7 @@ async function loadExistingFonts() {
slot.fileName = info.name ?? file;
if (info.sizeText) slot.sizeText = info.sizeText;
slot.isVariable = Boolean(info.isVariable);
slot.wghtRange = info.wghtRange;
}
renderSlots();
}
@@ -212,7 +238,7 @@ function renderSlots() {
<div class="slot-name ${!slot.path ? 'placeholder' : ''} ${required ? 'required' : ''}">${nameText}</div>
${
slot.path
? `<div class="slot-meta"><span class="slot-meta-badge">${slot.sizeText || '…'}</span>${ext ? `<span class="slot-meta-badge">${ext}</span>` : ''}${slot.isVariable ? '<span class="slot-meta-badge slot-meta-badge--variable">可变字体</span>' : ''}</div>`
? `<div class="slot-meta"><span class="slot-meta-badge">${slot.sizeText || '…'}</span>${ext ? `<span class="slot-meta-badge">${ext}</span>` : ''}${slot.isVariable ? `<span class="slot-meta-badge slot-meta-badge--variable">可变字体${slot.wghtRange ? ` ${slot.wghtRange}` : ''}</span>` : ''}</div>`
: ''
}
</div>
@@ -640,7 +666,12 @@ async function loadTestFonts(): Promise<void> {
hasFont('en') ? slotLabel('en') : hansLabel ? `回退 ${hansLabel}` : '未设置, 使用系统字体',
);
setTag('weight-tag', `使用 ${hansLabel ?? '系统字体'}`);
setTag('var-tag', `使用 ${hansLabel ?? '系统字体'}`);
// 可变字体提示: 若所选字体为可变字体, 显示 wght 轴范围
const varSlot = slots.hans.isVariable ? slots.hans : null;
setTag(
'var-tag',
`使用 ${hansLabel ?? '系统字体'}${varSlot?.wghtRange ? ` (可变字体 wght ${varSlot.wghtRange})` : ''}`,
);
const setFamily = (id: string, family: string, fallback: string) => {
const el = document.getElementById(id);
+5 -1
View File
@@ -496,7 +496,11 @@ class FontEditorPage {
this.renderNameFields(info.names ?? {});
this.page.querySelector('#ft-glyphs')!.textContent = String(info.glyphs);
this.page.querySelector('#ft-units')!.textContent = String(info.units);
this.page.querySelector('#ft-variable')!.textContent = info.variable ? '是' : '否';
this.page.querySelector('#ft-variable')!.textContent = info.variable
? info.wghtRange
? `是 (wght ${info.wghtRange})`
: '是'
: '否';
this.page.querySelector('#ft-file-name')!.textContent = name;
this.infoCard.hidden = false;
+2
View File
@@ -7,4 +7,6 @@ export interface FontSlot {
sizeText?: string;
/** 是否为可变字体 (fvar 表存在), 异步获取 */
isVariable?: boolean;
/** wght 轴范围 (如 "100-900"), 可变字体时有 */
wghtRange?: string;
}