94 lines
3.6 KiB
Python
94 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""生成中文子集位图字体 ui_font_cn_12.c(LVGL 格式,输出到 Drivers/BSP/UI/)。
|
||
|
||
前置依赖:
|
||
npm i -g lv_font_conv # 离线 TTF/OTF -> LVGL 字体 转换器
|
||
pip install fonttools # 仅当字体源是 .ttc 集合时需要(用于抽取单字重)
|
||
|
||
用法:
|
||
python tools/font_gen/make_font.py # 默认:宋体, 12px, 1bpp
|
||
python tools/font_gen/make_font.py --bpp 2 # 宋体细笔画发虚时改用 2bpp
|
||
python tools/font_gen/make_font.py --font C:/path/to/font.ttf
|
||
"""
|
||
import os
|
||
import sys
|
||
import argparse
|
||
import tempfile
|
||
import subprocess
|
||
|
||
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||
OUT = os.path.join(ROOT, 'Drivers', 'BSP', 'UI', 'ui_font_cn_12.c')
|
||
CHARS_FILE = os.path.join(ROOT, 'ui_cn_chars.txt')
|
||
FONT_NAME = 'ui_font_cn_12'
|
||
|
||
|
||
def resolve_lv_font_conv():
|
||
# 1) 全局 node 模块路径(npm i -g)
|
||
try:
|
||
groot = subprocess.check_output(['npm', 'root', '-g'], text=True).strip()
|
||
p = os.path.join(groot, 'lv_font_conv', 'lv_font_conv.js')
|
||
if os.path.exists(p):
|
||
return ['node', p]
|
||
except Exception:
|
||
pass
|
||
# 2) PATH 上的可执行
|
||
for cmd in ('lv_font_conv.cmd', 'lv_font_conv'):
|
||
if subprocess.run(['where', cmd], capture_output=True, shell=True).returncode == 0:
|
||
return [cmd]
|
||
sys.exit('ERROR: 找不到 lv_font_conv。请先执行: npm i -g lv_font_conv')
|
||
|
||
|
||
def resolve_font(path):
|
||
if path:
|
||
if not os.path.exists(path):
|
||
sys.exit('ERROR: --font 指定的文件不存在: ' + path)
|
||
return path
|
||
# 默认字体:Windows 宋体集合 simsun.ttc(lv_font_conv 不支持 .ttc,需先抽取 face0)
|
||
ttc = r'C:\Windows\Fonts\simsun.ttc'
|
||
if os.path.exists(ttc):
|
||
try:
|
||
from fontTools.ttLib import TTCollection
|
||
coll = TTCollection(ttc)
|
||
tmp = os.path.join(tempfile.gettempdir(), 'simsun_regular.ttf')
|
||
coll.fonts[0].save(tmp)
|
||
print('extracted SimSun regular (face0) ->', tmp)
|
||
return tmp
|
||
except Exception as e:
|
||
sys.exit('ERROR: 抽取 simsun.ttc 失败,请 pip install fonttools,或用 --font 指定 ttf: ' + str(e))
|
||
sys.exit('ERROR: 未找到 C:\\Windows\\Fonts\\simsun.ttc,请用 --font 指定字体 ttf/otf 路径')
|
||
|
||
|
||
def main():
|
||
ap = argparse.ArgumentParser()
|
||
ap.add_argument('--bpp', type=int, default=1, help='字体位深 1/2/3(默认 1)')
|
||
ap.add_argument('--size', type=int, default=12, help='字号(默认 12)')
|
||
ap.add_argument('--font', default=None, help='自定义字体 ttf/otf 路径(默认宋体)')
|
||
args = ap.parse_args()
|
||
|
||
if not os.path.exists(CHARS_FILE):
|
||
sys.exit('ERROR: 请先运行 extract_cn.py 生成 ui_cn_chars.txt')
|
||
|
||
chars = open(CHARS_FILE, encoding='utf-8').read().strip()
|
||
cmd = resolve_lv_font_conv()
|
||
font = resolve_font(args.font)
|
||
cmd += ['--font', font,
|
||
'--size', str(args.size),
|
||
'--bpp', str(args.bpp),
|
||
'-r', '0x20-0x7E', # 含 ASCII,使字体自包含
|
||
'--symbols', chars, # 本工程实际显示的汉字子集
|
||
'--no-compress', # 关闭 RLE 压缩,保证兼容
|
||
'--format', 'lvgl',
|
||
'--lv-font-name', FONT_NAME,
|
||
'-o', OUT]
|
||
|
||
print('RUN:', ' '.join(cmd))
|
||
r = subprocess.run(cmd)
|
||
if r.returncode != 0:
|
||
sys.exit('FAILED rc=%d' % r.returncode)
|
||
print('OK %d bytes -> %s' % (os.path.getsize(OUT), OUT))
|
||
|
||
|
||
if __name__ == '__main__':
|
||
main()
|