diff --git a/src/tools/wght-range.sh b/src/tools/wght-range.sh new file mode 100644 index 0000000..122eb4a --- /dev/null +++ b/src/tools/wght-range.sh @@ -0,0 +1,90 @@ +#!/bin/sh +# wght-range.sh - 读取可变字体 wght 轴范围,输出 "min max" +# 用法: sh wght-range.sh <字体.ttf> + +font=$1 + +[ -n "$font" ] || { + echo "usage: sh $0 " >&2 + exit 1 +} +[ -f "$font" ] || { + echo "error: file not found: $font" >&2 + exit 1 +} + +# 读 1 字节 +byte() { + od -An -tu1 -j "$1" -N1 "$font" 2>/dev/null | tr -d ' ' +} + +# 读 2 字节大端无符号整数 +u16() { + b0=$(byte "$1") || return 1 + b1=$(byte "$(($1 + 1))") || return 1 + [ -n "$b0" ] && [ -n "$b1" ] || return 1 + echo $(((b0 << 8) | b1)) +} + +# 读 4 字节大端无符号整数 +u32() { + b0=$(byte "$1") || return 1 + b1=$(byte "$(($1 + 1))") || return 1 + b2=$(byte "$(($1 + 2))") || return 1 + b3=$(byte "$(($1 + 3))") || return 1 + [ -n "$b0" ] && [ -n "$b1" ] && [ -n "$b2" ] && [ -n "$b3" ] || return 1 + echo $(((b0 << 24) | (b1 << 16) | (b2 << 8) | b3)) +} + +# 读 4 字节 ASCII tag +tag4() { + dd if="$font" bs=1 skip="$1" count=4 2>/dev/null +} + +num_tables=$(u16 4) || { + echo "error: cannot read font: $font" >&2 + exit 1 +} + +# 在 sfnt 表目录里找 fvar 表 +# offset 12 起,每条 16 字节:tag/checksum/offset/length +i=0 +fvar_off= +while [ "$i" -lt "$num_tables" ]; do + pos=$((12 + i * 16)) + if [ "$(tag4 "$pos")" = "fvar" ]; then + fvar_off=$(u32 "$((pos + 8))") + break + fi + i=$((i + 1)) +done + +[ -n "$fvar_off" ] || { + echo "error: no fvar table (not a variable font?)" >&2 + exit 1 +} + +axis_off=$((fvar_off + $(u16 "$((fvar_off + 4))"))) +axis_count=$(u16 "$((fvar_off + 8))") +axis_size=$(u16 "$((fvar_off + 10))") + +# 遍历轴找 wght +# min/max 是 Signed Fixed 16.16,整数部分 = 高 16 位 +j=0 +while [ "$j" -lt "$axis_count" ]; do + p=$((axis_off + j * axis_size)) + if [ "$(tag4 "$p")" = "wght" ]; then + hi=$(u16 "$((p + 4))") # minValue 高 16 位 + [ "$hi" -ge 32768 ] && hi=$((hi - 65536)) + min=$hi + hi=$(u16 "$((p + 12))") # maxValue 高 16 位 + [ "$hi" -ge 32768 ] && hi=$((hi - 65536)) + max=$hi + echo "$min $max" + exit 0 + fi + j=$((j + 1)) +done + +echo "error: no wght axis in font" >&2 +exit 1 diff --git a/web/src/fontWorker.ts b/web/src/fontWorker.ts index 01de371..b014bf7 100644 --- a/web/src/fontWorker.ts +++ b/web/src/fontWorker.ts @@ -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, }); diff --git a/web/src/main.ts b/web/src/main.ts index f59c517..489eed6 100644 --- a/web/src/main.ts +++ b/web/src/main.ts @@ -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 = { - '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() {
${nameText}
${ slot.path - ? `
${slot.sizeText || '…'}${ext ? `${ext}` : ''}${slot.isVariable ? '可变字体' : ''}
` + ? `
${slot.sizeText || '…'}${ext ? `${ext}` : ''}${slot.isVariable ? `可变字体${slot.wghtRange ? ` ${slot.wghtRange}` : ''}` : ''}
` : '' } @@ -640,7 +666,12 @@ async function loadTestFonts(): Promise { 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); diff --git a/web/src/tools.ts b/web/src/tools.ts index 70491f3..083e636 100644 --- a/web/src/tools.ts +++ b/web/src/tools.ts @@ -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; diff --git a/web/src/types.ts b/web/src/types.ts index 017b11f..69c0680 100644 --- a/web/src/types.ts +++ b/web/src/types.ts @@ -7,4 +7,6 @@ export interface FontSlot { sizeText?: string; /** 是否为可变字体 (fvar 表存在), 异步获取 */ isVariable?: boolean; + /** wght 轴范围 (如 "100-900"), 可变字体时有 */ + wghtRange?: string; }