mirror of
https://github.com/YuleBest/FontMM.git
synced 2026-12-04 03:11:48 +08:00
feat: 固定行距/字距, 用系统字体挖出度量载体 (close #17)
安卓的行高取 Paint 度量与「该行实际使用字体」度量的并集, 而 Paint 度量来自 typeface 家族里与请求字重最接近的条目 (hwui Paint::getMetricsInternal -> Minikin FontCollection::baseFontFaked; StaticLayout 再取 min(ascent)/max(descent))。 本模块 fonts.xml 中 sans-serif 首位正是英文槽位字体, 因此换英文字体时连中文行距 都会跟着变。 - fontmm-subset 新增 -metrics: 把现成字体「挖空」, 只留 hhea/OS2 度量与 U+0020/U+00A0/U+3000 空格字形。实测 Noto CJK 19.6MB -> 2.2KB, 度量 1160/-288 原样保留, 字符集只剩三个空格码点 - apply.sh: 开关打开时按优先级取系统自带且未被本模块覆盖的字体 (Noto CJK, 退回 Roboto) 挖空成 system/fonts/FontMM-Metrics.ttf, 再调 fontmm-wght 插入条目; 关闭时删除载体; 找不到来源字体则跳过 (功能降级, 不影响字体安装) - fontmm-wght 新增 -metrics on|off|auto: 在 4 个生效家族首位插入 9 档字重的载体 条目 —— 必须覆盖全部字重, 否则粗体等字重仍会落到用户字体上。幂等 (先移除再插入), 关闭能把 XML 逐字节还原, 载体文件不存在时不插入 (避免引用缺失字体) - WebUI: 高级配置新增「固定行距 & 字距」开关, 开关存 FONTS/metrics.txt, 应用时下发 - customize.sh 顺带继承 wght-mode.txt / wght-map.txt / metrics.txt: 模块更新会重建 整个模块目录, 此前字重模式与自定义映射每次都要重设 - dev/ci.mjs 增加载体文件名与开关文件名的跨文件一致性检查 验证: Go 单测覆盖插入/移除/幂等/与字重覆写叠加/auto 解析/缺文件跳过; 用同一份 源码编出宿主端工具跑通 apply.sh 全流程 (生成 2.2KB 载体、36 条条目写入 XML、 关闭后与原始 XML 逐字节一致); 浏览器实测开关切换与读写往返。
This commit is contained in:
1 parent
929ae33ff2
commit
176263e400
14 files changed
+568
-15
No files matched your search
@@ -145,6 +145,17 @@
|
||||
</md-outlined-select>
|
||||
</div>
|
||||
|
||||
<!-- 固定行距/字距 (issue #17) -->
|
||||
<div class="wght-mode-row">
|
||||
<div class="wght-mode-info">
|
||||
<div class="wght-mode-title">固定行距 & 字距</div>
|
||||
<div class="wght-mode-sub" id="metrics-sub">
|
||||
把系统字体的行距字距度量挖出来当基准,换字体后行距不再跟着变。开启后需重新「应用」。
|
||||
</div>
|
||||
</div>
|
||||
<md-switch id="metrics-switch" aria-label="固定行距字距"></md-switch>
|
||||
</div>
|
||||
|
||||
<!-- 字重映射 (各模式实时预览, 修改即切为自定义) -->
|
||||
<div class="wght-map" id="wght-map">
|
||||
<div class="wght-map-inner">
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { exec, shellQuote, toast } from './ksu';
|
||||
import { copyFont, pickWghtRange, refreshEnSubset, renderSlots, setApplying, slots } from './slots';
|
||||
import { applyWghtOverride, getSelectedWghtMode, writeWghtMode } from './wght';
|
||||
import { getSelectedMetrics, writeMetrics } from './metrics';
|
||||
import { invalidateTestFonts } from './testFonts';
|
||||
import { applyBtn } from './dom';
|
||||
import { FONTS_DIR } from './constants';
|
||||
@@ -20,6 +21,8 @@ async function apply() {
|
||||
// 字重范围覆写: 用 UI 当前模式 (持久化到 FONTS/wght-mode.txt), 仅当存在可变字体且模式非 0 时处理
|
||||
const wghtMode = getSelectedWghtMode();
|
||||
await writeWghtMode(wghtMode);
|
||||
// 固定行距字距: 开关同样先落盘 —— 载体生成与条目插入都在 apply.sh 里按它进行
|
||||
await writeMetrics(getSelectedMetrics());
|
||||
const wghtPick = pickWghtRange();
|
||||
if (wghtMode !== 0 && wghtPick) {
|
||||
// Go 程序自行读取 wght-map.txt (mode 3)
|
||||
|
||||
@@ -39,6 +39,9 @@ const FAKE_FS: Record<string, { dirs: string[]; fonts: string[]; files: string[]
|
||||
'/storage/emulated/0/字体': { dirs: [], fonts: ['思源黑体.ttf', '霞鹜文楷.ttf'], files: [] },
|
||||
};
|
||||
|
||||
// dev 模式下的固定行距字距开关 (FONTS/metrics.txt 的内存态)
|
||||
let mockMetrics = '0';
|
||||
|
||||
async function mockExec(command: string): Promise<ExecResult> {
|
||||
const fake = (stdout = ''): ExecResult => ({ errno: 0, stdout, stderr: '' });
|
||||
const fail = (stderr: string): ExecResult => ({ errno: 1, stdout: '', stderr });
|
||||
@@ -75,6 +78,15 @@ async function mockExec(command: string): Promise<ExecResult> {
|
||||
if (command.includes('wght-mode.txt')) {
|
||||
return command.includes('echo') ? fake('') : slow('1');
|
||||
}
|
||||
// 固定行距字距开关: 内存态往返 (echo 写, cat 读), 便于 dev 下验证持久化
|
||||
if (command.includes('metrics.txt')) {
|
||||
if (command.includes('echo')) {
|
||||
const m = command.match(/echo '(\d)'/);
|
||||
if (m) mockMetrics = m[1];
|
||||
return fake('');
|
||||
}
|
||||
return slow(mockMetrics);
|
||||
}
|
||||
// 字重覆写: Go 程序 fontmm-wght (模拟成功日志)
|
||||
if (command.includes('fontmm-wght')) {
|
||||
return slow(
|
||||
|
||||
@@ -2,6 +2,7 @@ import './material';
|
||||
// 各功能模块在导入时完成自身初始化 (绑定事件/加载数据)
|
||||
import './slots';
|
||||
import './wght';
|
||||
import './metrics';
|
||||
import './apply';
|
||||
import './testFonts';
|
||||
import './home';
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
// metrics.ts — 固定行距/字距 (issue #17)
|
||||
//
|
||||
// 背景: Android 的行高取「paint 度量」与「该行实际使用字体度量」的并集, 而 paint
|
||||
// 度量来自 typeface 家族里与请求字重最接近的那个条目 —— 本模块的 fonts.xml 中
|
||||
// sans-serif 首个条目正是英文槽位字体, 于是换英文字体 (进而中文行距) 都会跟着变。
|
||||
//
|
||||
// 做法: 让 apply.sh 把系统自带的现成字体挖空 (只留度量与空格字形), 作为家族首位
|
||||
// 的度量载体; 开关记在 FONTS/metrics.txt, 由 apply.sh 里的 fontmm-wght 按开关
|
||||
// 插入/移除载体条目。
|
||||
import '@material/web/switch/switch.js';
|
||||
import { exec, toast } from './ksu';
|
||||
import { FONTS_DIR } from './constants';
|
||||
|
||||
const metricsSwitch = document.getElementById('metrics-switch') as any;
|
||||
const metricsSub = document.getElementById('metrics-sub');
|
||||
|
||||
/** 当前 UI 开关状态 */
|
||||
export function getSelectedMetrics(): boolean {
|
||||
return Boolean(metricsSwitch?.selected);
|
||||
}
|
||||
|
||||
/** 写入开关到 FONTS/metrics.txt (apply.sh 读取) */
|
||||
export async function writeMetrics(enabled: boolean): Promise<void> {
|
||||
try {
|
||||
await exec(`echo '${enabled ? 1 : 0}' > '${FONTS_DIR}/metrics.txt'`);
|
||||
} catch {
|
||||
// 忽略写入失败
|
||||
}
|
||||
}
|
||||
|
||||
/** 读取开关 (FONTS/metrics.txt), 默认关闭 */
|
||||
async function readMetrics(): Promise<boolean> {
|
||||
try {
|
||||
const { errno, stdout } = await exec(`cat '${FONTS_DIR}/metrics.txt' 2>/dev/null || true`);
|
||||
if (errno === 0) return stdout.trim() === '1';
|
||||
} catch {
|
||||
// 忽略
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 开关只在应用时生效 (载体由 apply.sh 生成), 切换后要提示
|
||||
metricsSwitch?.addEventListener('change', () => {
|
||||
const on = getSelectedMetrics();
|
||||
toast(on ? '已启用固定行距字距,点「应用」后生效' : '已关闭固定行距字距,点「应用」后生效');
|
||||
if (metricsSub) metricsSub.classList.toggle('metrics-sub--on', on);
|
||||
});
|
||||
|
||||
async function initMetricsUI(): Promise<void> {
|
||||
const enabled = await readMetrics();
|
||||
if (metricsSwitch) metricsSwitch.selected = enabled;
|
||||
if (metricsSub) metricsSub.classList.toggle('metrics-sub--on', enabled);
|
||||
}
|
||||
|
||||
void initMetricsUI();
|
||||
Reference in new issue
Block a user