From 929ae33ff2d94d0c9e8fa5eddb0b5beff776b420 Mon Sep 17 00:00:00 2001 From: YuleBest Date: Wed, 16 Sep 2026 15:42:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(webui):=20=E5=B1=95=E7=A4=BA=E8=8B=B1?= =?UTF-8?q?=E6=96=87=E5=AD=97=E4=BD=93=E5=AD=90=E9=9B=86=E5=8C=96=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=20(close=20#14)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 英文字体自带中文字形时, apply.sh 会先裁成纯拉丁子集再安装 (issue #10), 但 WebUI 上仍显示原字体大小、也没有任何标记, 看起来像裁切没生效。 - 新增英文子集大小读取: 读 FONTS/.en-subset.ttf, 且要求它比 en.ttf 新 (换过英文字体但还没重新应用时, 旧子集不该继续展示) - 英文槽位卡片改为「原大小 → 子集大小」并加「已子集化」徽标; 子集常在 百 KB 量级, 小于 1MB 时改用 KB, 否则会显示成 0.2 MB 看不出量级 - 应用完成后自动刷新该状态; 重新选择英文字体时先撤下徽标 (它描述的是 上次应用的结果, 不是当前选择的) - apply.sh 未使用子集时清掉残留的子集文件, 使该文件的存在即代表本次应用 确实用了子集 --- src/apply.sh | 6 +++++- web/src/apply.ts | 4 +++- web/src/fontFiles.ts | 23 +++++++++++++++++++++++ web/src/fontInfo.ts | 8 +++++++- web/src/slots.ts | 20 ++++++++++++++++++-- web/src/style.scss | 6 ++++++ web/src/types.ts | 2 ++ 7 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/apply.sh b/src/apply.sh index f007859..4f02d80 100644 --- a/src/apply.sh +++ b/src/apply.sh @@ -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 diff --git a/web/src/apply.ts b/web/src/apply.ts index 59cecb3..72be108 100644 --- a/web/src/apply.ts +++ b/web/src/apply.ts @@ -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(); } } diff --git a/web/src/fontFiles.ts b/web/src/fontFiles.ts index 88480ab..b028f96 100644 --- a/web/src/fontFiles.ts +++ b/web/src/fontFiles.ts @@ -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 { 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 { + // 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; + } +} diff --git a/web/src/fontInfo.ts b/web/src/fontInfo.ts index 2b18ebb..d2d530a 100644 --- a/web/src/fontInfo.ts +++ b/web/src/fontInfo.ts @@ -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 { // dev 假数据: 模拟各槽位字体的名称/大小/可变标识 @@ -61,7 +67,7 @@ export async function readFontInfo(file: string): Promise { 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, diff --git a/web/src/slots.ts b/web/src/slots.ts index 00668d1..bfebdff 100644 --- a/web/src/slots.ts +++ b/web/src/slots.ts @@ -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 + ? `已子集化` + : ''; return `
@@ -121,7 +129,7 @@ export function renderSlots() {
${nameText}
${ slot.path - ? `
${slot.sizeText || '…'}${ext ? `${ext}` : ''}${slot.isVariable ? `可变字体${slot.wghtRange ? ` ${slot.wghtRange}` : ''}` : ''}
` + ? `
${sizeText}${ext ? `${ext}` : ''}${subsetBadge}${slot.isVariable ? `可变字体${slot.wghtRange ? ` ${slot.wghtRange}` : ''}` : ''}
` : '' }
@@ -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 { + 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(); } diff --git a/web/src/style.scss b/web/src/style.scss index a871734..608502b 100644 --- a/web/src/style.scss +++ b/web/src/style.scss @@ -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); + } } } } diff --git a/web/src/types.ts b/web/src/types.ts index 69c0680..cb2f7fd 100644 --- a/web/src/types.ts +++ b/web/src/types.ts @@ -9,4 +9,6 @@ export interface FontSlot { isVariable?: boolean; /** wght 轴范围 (如 "100-900"), 可变字体时有 */ wghtRange?: string; + /** 英文字体裁切后的子集大小 (如 "218 KB"), 仅英文槽位且子集有效时存在 (issue #14) */ + subsetSizeText?: string; }