feat(webui): 展示英文字体子集化结果 (close #14)

英文字体自带中文字形时, apply.sh 会先裁成纯拉丁子集再安装 (issue #10), 但
WebUI 上仍显示原字体大小、也没有任何标记, 看起来像裁切没生效。

- 新增英文子集大小读取: 读 FONTS/.en-subset.ttf, 且要求它比 en.ttf 新
  (换过英文字体但还没重新应用时, 旧子集不该继续展示)
- 英文槽位卡片改为「原大小 → 子集大小」并加「已子集化」徽标; 子集常在
  百 KB 量级, 小于 1MB 时改用 KB, 否则会显示成 0.2 MB 看不出量级
- 应用完成后自动刷新该状态; 重新选择英文字体时先撤下徽标 (它描述的是
  上次应用的结果, 不是当前选择的)
- apply.sh 未使用子集时清掉残留的子集文件, 使该文件的存在即代表本次应用
  确实用了子集
This commit is contained in:
YuleBest committed 2026-09-16 15:42:50 +08:00
1 parent 211277d9ea
commit 929ae33ff2
7 files changed
+64 -5

No files matched your search

+5 -1
View File
@@ -125,9 +125,13 @@ if [ ! -f "$FONTS_DIR/en.ttf" ]; then
# 简体仅在缺字形时兜底 (issue #6)
install_from "$FONTS_DIR/hans.ttf" "$en_fonts"
else
# 子集化成功则改用子集文件, 否则沿用原字体 (失败不阻断安装)
# 子集化成功则改用子集文件, 否则沿用原字体 (失败不阻断安装)。
# 未用子集时清掉上次留下的文件, 使 FONTS/.en-subset.ttf 的存在即代表
# 「本次应用确实用了子集」—— WebUI 据此展示裁切结果 (issue #14)
if prepare_english_font "$FONTS_DIR/en.ttf" "$EN_SUBSET"; then
EN_SRC="$EN_SUBSET"
else
rm -f "$EN_SUBSET"
fi
install_from "$EN_SRC" "$en_fonts"
fi
+3 -1
View File
@@ -1,5 +1,5 @@
import { exec, shellQuote, toast } from './ksu';
import { copyFont, pickWghtRange, renderSlots, setApplying, slots } from './slots';
import { copyFont, pickWghtRange, refreshEnSubset, renderSlots, setApplying, slots } from './slots';
import { applyWghtOverride, getSelectedWghtMode, writeWghtMode } from './wght';
import { invalidateTestFonts } from './testFonts';
import { applyBtn } from './dom';
@@ -77,6 +77,8 @@ async function apply() {
toast(`应用失败: ${String(e)}`);
} finally {
setApplying(false);
// 应用后刷新英文子集结果 (issue #14): 子集大小只有跑过 apply.sh 才知道
await refreshEnSubset();
renderSlots();
}
}
+23
View File
@@ -1,5 +1,6 @@
import { exec, shellQuote } from './ksu';
import { FONTS_DIR, TEST_FONT_DIR } from './constants';
import { formatSize } from './fontInfo';
// 复制 FONT/ 到 webroot/fonts-test, 供页面加载与名称解析
// (WebView 无法直接访问模块外部目录, 符号链接在真机也不可靠, 直接复制)
@@ -46,3 +47,25 @@ export async function listFontsDir(): Promise<string[]> {
return [];
}
}
// 英文字体子集文件: apply.sh 子集化成功时写入 FONTS/.en-subset.ttf (issue #10),
// 安装到 system/fonts 的就是它, 大小通常比原字体小两个数量级 (issue #14)。
export const EN_SUBSET_FILE = `${FONTS_DIR}/.en-subset.ttf`;
// 读取英文子集大小 (无子集返回 null)。
// 要求子集比 en.ttf 新 —— 换过英文字体但还没重新应用时, 旧子集不该继续展示。
export async function getEnSubsetSizeText(): Promise<string | null> {
// dev 假数据: 真机没应用过就没有该文件, 徽标无从预览
if (import.meta.env.DEV) return '218 KB';
try {
const subset = shellQuote(EN_SUBSET_FILE);
const { errno, stdout } = await exec(
`[ -f ${subset} ] && [ ${subset} -nt ${shellQuote(`${FONTS_DIR}/en.ttf`)} ] && wc -c < ${subset}`,
);
if (errno !== 0) return null;
const bytes = Number(stdout.trim());
return Number.isFinite(bytes) && bytes > 0 ? formatSize(bytes) : null;
} catch {
return null;
}
}
+7 -1
View File
@@ -31,6 +31,12 @@ function extractWghtRange(font: any): string | undefined {
return `${Math.round(wght.minValue)}-${Math.round(wght.maxValue)}`;
}
// 字体体积展示: 1MB 以下用 KB (子集化结果常在百 KB 量级, 用 MB 会显示成 0.2 MB 看不出量级)
export function formatSize(bytes: number): string {
if (bytes < 1024 * 1024) return `${Math.max(1, Math.round(bytes / 1024))} KB`;
return `${(bytes / 1024 / 1024).toFixed(1)} MB`;
}
// 读取 fonts-test/ 下字体文件的名称与大小 (WebView 仅能访问 webroot 内文件)
export async function readFontInfo(file: string): Promise<FontInfo> {
// dev 假数据: 模拟各槽位字体的名称/大小/可变标识
@@ -61,7 +67,7 @@ export async function readFontInfo(file: string): Promise<FontInfo> {
const buf = await res.arrayBuffer();
const font = opentype.parse(buf);
const name = extractFamilyName(font);
const sizeText = `${(buf.byteLength / 1024 / 1024).toFixed(1)} MB`;
const sizeText = formatSize(buf.byteLength);
return {
name: name ?? undefined,
sizeText,
+18 -2
View File
@@ -1,7 +1,7 @@
import { fontPicker } from './fontPicker';
import { exec, shellQuote } from './ksu';
import { readFontInfo } from './fontInfo';
import { ensureFontsCopy } from './fontFiles';
import { ensureFontsCopy, getEnSubsetSizeText } from './fontFiles';
import { FONTS_DIR, TEST_FONT_DIR } from './constants';
import { applyBtn } from './dom';
import type { FontSlot } from './types';
@@ -100,6 +100,14 @@ export function renderSlots() {
const nameText = required ? '未选择字体,此项为必选项' : slotPlaceholder(slot);
// 扩展名大写 (TTF/OTF...), 从源文件路径动态提取, 不硬编码
const ext = slot.path ? (slot.path.split('.').pop() ?? '').toUpperCase() : '';
// 英文字体被裁成拉丁子集后, 卡片上同时给出「原大小 → 子集大小」与徽标,
// 否则用户看到原字体大小, 会以为裁切没生效 (issue #14)
const sizeText = slot.subsetSizeText
? `${slot.sizeText || '…'} → ${slot.subsetSizeText}`
: slot.sizeText || '…';
const subsetBadge = slot.subsetSizeText
? `<span class="slot-meta-badge slot-meta-badge--subset" title="英文字体自带中文字形, 安装时已裁切为纯拉丁子集; 装入系统的是这个子集">已子集化</span>`
: '';
return `
<md-filled-card class="slot-card ${slot.key === 'hans' ? 'slot-hans' : ''} ${slot.path ? 'selected' : ''}" data-key="${slot.key}">
<div class="slot-header">
@@ -121,7 +129,7 @@ export 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">可变字体${slot.wghtRange ? ` ${slot.wghtRange}` : ''}</span>` : ''}</div>`
? `<div class="slot-meta"><span class="slot-meta-badge">${sizeText}</span>${ext ? `<span class="slot-meta-badge">${ext}</span>` : ''}${subsetBadge}${slot.isVariable ? `<span class="slot-meta-badge slot-meta-badge--variable">可变字体${slot.wghtRange ? ` ${slot.wghtRange}` : ''}</span>` : ''}</div>`
: ''
}
</div>
@@ -185,6 +193,8 @@ export function pickWghtRange(): { min: number; max: number } | null {
// 选择新字体后: 复制到 fonts-test 供解析, 更新卡片显示的名称/大小
async function refreshSlotInfo(slot: FontSlot) {
if (!slot.path) return;
// 换了英文字体: 旧的子集标记描述的是「上次应用」的状态, 先撤下, 应用后再刷新
if (slot.key === 'en') slot.subsetSizeText = undefined;
try {
await exec(`cp -f ${shellQuote(slot.path)} ${shellQuote(`${TEST_FONT_DIR}/${slot.key}.ttf`)}`);
const info = await readFontInfo(`${slot.key}.ttf`);
@@ -199,6 +209,11 @@ async function refreshSlotInfo(slot: FontSlot) {
}
}
// 刷新英文槽位的子集信息 (打开 WebUI 时, 以及每次应用后)。不负责重绘。
export async function refreshEnSubset(): Promise<void> {
slots.en.subsetSizeText = (await getEnSubsetSizeText()) ?? undefined;
}
// 打开 WebUI 时: 复制 FONT/ 到 fonts-test, 读取已有字体的名称/大小填充卡片
async function loadExistingFonts() {
const available = await ensureFontsCopy();
@@ -213,6 +228,7 @@ async function loadExistingFonts() {
slot.isVariable = Boolean(info.isVariable);
slot.wghtRange = info.wghtRange;
}
await refreshEnSubset();
renderSlots();
}
+6
View File
@@ -222,6 +222,12 @@ md-filled-card.slot-card {
background-color: var(--md-sys-color-primary);
color: var(--md-sys-color-on-primary);
}
// 子集化徽标: 与可变字体徽标同级的实色提示 (仅英文字体被裁切时出现)
&.slot-meta-badge--subset {
background-color: var(--md-sys-color-tertiary);
color: var(--md-sys-color-on-tertiary);
}
}
}
}
+2
View File
@@ -9,4 +9,6 @@ export interface FontSlot {
isVariable?: boolean;
/** wght 轴范围 (如 "100-900"), 可变字体时有 */
wghtRange?: string;
/** 英文字体裁切后的子集大小 (如 "218 KB"), 仅英文槽位且子集有效时存在 (issue #14) */
subsetSizeText?: string;
}