54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
"""从 ToneOZ-Pinyin-WenKai-Regular.ttf 中提取拼音/字母/数字字符,生成新的字体文件。"""
|
|
|
|
from fontTools.ttLib import TTFont
|
|
from fontTools.subset import Subsetter
|
|
from pathlib import Path
|
|
|
|
BASE = Path(__file__).resolve().parent
|
|
INPUT = BASE / "input-fonts" / "ToneOZ-Pinyin-WenKai-Regular.ttf"
|
|
OUTPUT = BASE / "output-fonts" / "ToneOZ-Pinyin-Kai-Regular.ttf"
|
|
|
|
# Unicode 码点范围
|
|
RANGES = [
|
|
(0x0020, 0x007E), # 基本 ASCII(字母、数字、标点)
|
|
(0x00C0, 0x00FF), # 拉丁字母-1 补充
|
|
(0x0100, 0x017F), # 拉丁字母扩展 A
|
|
(0x01D6, 0x01DC), # ü 的四个声调变体
|
|
]
|
|
|
|
|
|
def build_unicodes():
|
|
codepoints = set()
|
|
for start, end in RANGES:
|
|
for cp in range(start, end + 1):
|
|
codepoints.add(cp)
|
|
return sorted(codepoints)
|
|
|
|
|
|
def main():
|
|
font = TTFont(INPUT)
|
|
unicodes = build_unicodes()
|
|
|
|
# 使用 fonttools Subsetter 提取指定字符
|
|
sub = Subsetter()
|
|
sub.populate(unicodes=unicodes)
|
|
sub.subset(font)
|
|
|
|
OUTPUT.parent.mkdir(parents=True, exist_ok=True)
|
|
font.save(OUTPUT)
|
|
print(f"提取完成: {OUTPUT}")
|
|
print(f"包含 {len(unicodes)} 个码点")
|
|
|
|
# 输出范围统计
|
|
for start, end in RANGES:
|
|
name = f"U+{start:04X}-U+{end:04X}"
|
|
glyphs_in_range = sum(
|
|
1 for cp in range(start, end + 1) if cp in set(unicodes)
|
|
)
|
|
print(f" {name}: {glyphs_in_range} 个字符")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|