mirror of
https://github.com/YuleBest/FontMM.git
synced 2026-12-04 03:11:48 +08:00
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:
1 parent
78abbd8a5d
commit
418ea6e7c6
5 files changed
+144
-10
No files matched your search
@@ -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 <font.ttf>" >&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
|
||||
@@ -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
@@ -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
@@ -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;
|
||||
|
||||
@@ -7,4 +7,6 @@ export interface FontSlot {
|
||||
sizeText?: string;
|
||||
/** 是否为可变字体 (fvar 表存在), 异步获取 */
|
||||
isVariable?: boolean;
|
||||
/** wght 轴范围 (如 "100-900"), 可变字体时有 */
|
||||
wghtRange?: string;
|
||||
}
|
||||
Reference in new issue
Block a user