96 lines
2.7 KiB
Python
96 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""提取工程 UI 源码中字符串字面量里的非 ASCII 字符(实际显示的汉字),
|
|
输出 <仓库根>/ui_cn_chars.txt,供 make_font.py 生成中文字体子集。
|
|
|
|
用法(在仓库根目录执行,或任意目录均可,脚本自动定位仓库根):
|
|
python tools/font_gen/extract_cn.py
|
|
"""
|
|
import os
|
|
import glob
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
UI_DIR = os.path.join(ROOT, 'Drivers', 'BSP', 'UI')
|
|
TASK_DIR = os.path.join(ROOT, 'App', 'task')
|
|
SCAN = (glob.glob(os.path.join(UI_DIR, '*.c')) +
|
|
glob.glob(os.path.join(UI_DIR, '*.h')) +
|
|
[os.path.join(TASK_DIR, 'ui_task.c'), os.path.join(TASK_DIR, 'ui_task.h')])
|
|
|
|
|
|
def collect(text):
|
|
"""跳过 /* */ 与 // 注释、字符常量,只收集双引号字符串内的非 ASCII 字符。"""
|
|
out, i, n = set(), 0, len(text)
|
|
blk = line = sstr = cchr = False
|
|
while i < n:
|
|
c = text[i]
|
|
nx = text[i + 1] if i + 1 < n else ''
|
|
if blk:
|
|
if c == '*' and nx == '/':
|
|
blk = False
|
|
i += 2
|
|
continue
|
|
i += 1
|
|
continue
|
|
if line:
|
|
if c == '\n':
|
|
line = False
|
|
i += 1
|
|
continue
|
|
if sstr:
|
|
if c == '\\':
|
|
i += 2
|
|
continue
|
|
if c == '"':
|
|
sstr = False
|
|
i += 1
|
|
continue
|
|
if ord(c) > 0x7F:
|
|
out.add(c)
|
|
i += 1
|
|
continue
|
|
if cchr:
|
|
if c == '\\':
|
|
i += 2
|
|
continue
|
|
if c == "'":
|
|
cchr = False
|
|
i += 1
|
|
continue
|
|
if c == '/' and nx == '*':
|
|
blk = True
|
|
i += 2
|
|
continue
|
|
if c == '/' and nx == '/':
|
|
line = True
|
|
i += 2
|
|
continue
|
|
if c == '"':
|
|
sstr = True
|
|
i += 1
|
|
continue
|
|
if c == "'":
|
|
cchr = True
|
|
i += 1
|
|
continue
|
|
i += 1
|
|
return out
|
|
|
|
|
|
def main():
|
|
chars = set()
|
|
for f in SCAN:
|
|
if not os.path.exists(f):
|
|
print(' skip (not found):', os.path.relpath(f, ROOT))
|
|
continue
|
|
chars |= collect(open(f, encoding='utf-8-sig', errors='ignore').read())
|
|
s = ''.join(sorted(chars))
|
|
out_path = os.path.join(ROOT, 'ui_cn_chars.txt')
|
|
with open(out_path, 'w', encoding='utf-8') as fp:
|
|
fp.write(s)
|
|
print('scanned %d files; unique non-ASCII chars: %d' % (len(SCAN), len(s)))
|
|
print('->', out_path)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|