纯 MoonBit 的 TrueType 字体子集化引擎:字形闭包、cmap/loca/hmtx 重建、可验证的体积压缩,附 CLI。
Dependencies
| 样本 | 原始 | 子集后 | 字符 / 字形 | 外部校验 |
|---|---|---|---|---|
| VT323(拉丁) | 153,116 B | 8,964 B | 12 / 13 | GDI+ 逐像素差异 0 |
| DengXian(中文) | 16,324,168 B | 16,804 B | 27 / 29 | GDI+ 逐像素差异 0 |
| SimSun(TTC 第 1 面) | 18,070,492 B | 10,320 B | 14 / 15 | 本工具回读 + 校验和 |
moon add Mr-Houjie/mbt-fontsub# 查看字体信息(含 TTC 面数)
moon run cmd/main -- info tests/fixtures/VT323-Regular.ttf
# 按文本子集化
moon run cmd/main -- subset tests/fixtures/VT323-Regular.ttf \
--text "Hello, MoonBit!" -o out.ttf
# TTC 集合:指定面
moon run cmd/main -- subset C:/Windows/Fonts/simsun.ttc --face 1 \
--text "你好,世界" -o out.ttf
# 只做自检,不写文件
moon run cmd/main -- verify tests/fixtures/VT323-Regular.ttf --text "Hello"已写出 : out.ttf
体积 : 153116 → 8964 字节(省 94.15%)
字符 / 字形 : 12 / 13(原 587)
自检 : 通过///|
let font = @fontsub.read_font("tests/fixtures/VT323-Regular.ttf")
let text = "Hello, MoonBit!"
let chars : Array[Int] = []
for ch in text {
chars.push(ch.to_int())
}
let subset = @fontsub.subset_by_chars(font, chars)
let report = @fontsub.verify_subset(font, subset, chars)
assert_true(report.ok)| API | 说明 |
|---|---|
| parse_font(Bytes) -> Font | 解析单体 TrueType,TTC 默认第 0 面 |
| parse_font_face(Bytes, Int) -> Font | 解析指定面 |
| ttc_face_count(Bytes) -> Int | TTC 面数,单体字体返回 1 |
| read_font / read_font_face | 读文件并解析 |
| subset_by_text / subset_by_chars | 默认策略子集化 |
| subset_by_text_with / subset_by_chars_with | 带 SubsetOptions 的子集化 |
| verify_subset / verify_subset_with | 回读自检 |
| Font::glyph_of / has_glyph / advance_of | 查询映射与度量 |
///|
let options = @fontsub.SubsetOptions::default()
.drop_table("name") // 额外丢弃某张表
.keep_table("GSUB") // 强制保留某张表
.with_collector(fn(_font, _glyph) { [] }) // 为字形补充依赖(例如 GSUB 闭包)
.with_validator(fn(_original, _subset, _chars) { ["自定义规则"] })
let subset = @fontsub.subset_by_text_with(font, text, options)moon test # 单元测试
moon run cmd/verify # 独立自检(Windows 下 moon test 嵌套进程受限时使用)
moon run cmd/verify -- corpus/fonts/*.ttf # 对语料逐字体巡检moon run cmd/main -- subset tests/fixtures/VT323-Regular.ttf \
--text "Hello, MoonBit! 0123" -o /tmp/subset.ttf
python tools/crosscheck.py tests/fixtures/VT323-Regular.ttf \
/tmp/subset.ttf "Hello, MoonBit! 0123"python tools/fetch_corpus.py
moon run cmd/verify -- corpus/fonts/PressStart2P-Regular.ttf corpus/fonts/ZCOOLXiaoWei-Regular.ttfpub suberror FontError {
FontTruncated
FontNotSfnt
FontUnsupported(String)
FontMissingTable(String)
FontBadTable(String)
FontIo(String)
}fn SubsetOptions::with_collector(self : SubsetOptions, collector : (Font, Int) -> Array[Int]) -> SubsetOptionsfn SubsetOptions::with_validator(self : SubsetOptions, validator : (Font, Font, Array[Int]) -> Array[String]) -> SubsetOptionspub(all) struct TableRecord {
tag : String
checksum : UInt
offset : Int
length : Int
}pub(all) struct VerifyReport {
ok : Bool
chars_checked : Int
glyphs : Int
original_size : Int
subset_size : Int
savings_percent : Double
failures : Array[String]
}fn subset_by_chars_with(font : Font, chars : Array[Int], options : SubsetOptions) -> Bytes raise FontErrorfn subset_by_text_with(font : Font, text : String, options : SubsetOptions) -> Bytes raise FontErrorfn verify_subset_with(original : Font, subset_bytes : Bytes, chars : Array[Int], options : SubsetOptions) -> VerifyReportInstall
Download zip纯 MoonBit 的 TrueType 字体子集化引擎:字形闭包、cmap/loca/hmtx 重建、可验证的体积压缩,附 CLI。
Dependencies