knob_os/tools/collect_chars.py

97 lines
3.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
"""collect_chars.py —— 扫描源码,收集所有会上屏的非 ASCII 字符。
中文字体是按子集打包的(app 分区只有 3MB,整套 CJK 要 8MB+),
所以"哪些字要进固件"完全取决于源码里出现过哪些字面量。
用法:
collect_chars.py <工程根目录> 打印字符集,并写入 tools/symbols.txt
collect_chars.py <工程根目录> --check 只检查 symbols.txt 是否已过期
为什么要手写词法扫描而不是正则:正则处理 "http://..." 这类字面量时会把 //
当注释起点,连带吃掉后面的内容,结果就是漏字——屏幕上是方框,日志里却毫无线索。
"""
import os
import sys
# 标点与符号常直接写进文案,先兜底一批,免得每次都靠代码里出现过
BASE = '·°℃←→↑↓…—「」【】、。,!?:;()¥±×÷≈%~'
EXTS = ('.c', '.cpp', '.h', '.ino')
SKIP_PREFIX = ('lv_font', 'web_')
SKIP_DIRS = {'build', '.git', 'node_modules', 'tools', 'assets'}
def literals(code):
"""逐个吐出 C/C++ 字符串(含字符常量)的内容。"""
i, n = 0, len(code)
while i < n:
c = code[i]
if c == '/' and i + 1 < n and code[i + 1] == '/':
j = code.find('\n', i)
i = n if j < 0 else j + 1
elif c == '/' and i + 1 < n and code[i + 1] == '*':
j = code.find('*/', i + 2)
i = n if j < 0 else j + 2
elif c in '"\'':
q = c
i += 1
start = i
while i < n:
if code[i] == '\\':
i += 2
continue
if code[i] == q or code[i] == '\n':
break
i += 1
yield code[start:i]
i += 1
else:
i += 1
def collect(src):
chars = set(BASE)
for dp, dirs, fs in os.walk(src):
dirs[:] = [d for d in dirs if d not in SKIP_DIRS]
for fn in fs:
if not fn.endswith(EXTS) or fn.startswith(SKIP_PREFIX):
continue
with open(os.path.join(dp, fn), encoding='utf-8', errors='ignore') as f:
code = f.read()
for lit in literals(code):
chars.update(ch for ch in lit if ord(ch) > 0x7F)
return ''.join(sorted(chars))
def main():
src = sys.argv[1]
check = '--check' in sys.argv
found = collect(src)
symbols_path = os.path.join(os.path.abspath(src), 'tools', 'symbols.txt')
if not check:
with open(symbols_path, 'w', encoding='utf-8') as f:
f.write(found)
print(f'>> {len(found)} non-ascii chars collected -> {symbols_path}')
return 0
try:
with open(symbols_path, encoding='utf-8') as f:
have = set(f.read())
except FileNotFoundError:
print('!! symbols.txt 不存在,请先跑 tools/gen_font.sh')
return 1
missing = [c for c in found if c not in have]
if missing:
print('!! 字体子集缺 %d 个字:%s' % (len(missing), ''.join(missing)))
print(' 请跑 tools/gen_font.sh 重新生成,否则这些字在屏幕上是方框。')
return 1
print('>> 字体字符集是最新的')
return 0
if __name__ == '__main__':
sys.exit(main())