UI 生成文件按工程约定产出:生成器补文件头、K&R 大括号与版本守卫压平

- gen_menu.py:ui_menu_tab.h/.c 输出工程标准文件头,5 个函数定义改 K&R 大括号
- make_font.py:新增 normalize_project_conventions 后处理——工程文件头
  (替换内嵌绝对路径的 lv_font_conv 头注释)、包含收敛为 #include "lvgl.h"、
  LVGL 9.3.0 版本守卫静态压平(含守卫宏定义清理)
- 重生成 ui_font_cn_12.c / ui_menu_tab.c / ui_menu_tab.h:仅头部/大括号/守卫
  行变化,字形数据与菜单表逐字节不变
- 两文件随之满足规范检查,pre-commit 中无需再排除(配合上一提交生效)

验证:pre-commit 全钩子通过、模拟器 0 警、菜单/内容页像素回归 PASS、Keil 0 错 0 警
This commit is contained in:
2026-09-13 19:38:30 +08:00
parent 34528a9798
commit 508577bbf0
5 changed files with 237 additions and 66 deletions
+12 -38
View File
@@ -1,20 +1,19 @@
/*******************************************************************************
* Size: 12 px
* Bpp: 1
* Opts: --font C:\Users\ADMINI~1\AppData\Local\Temp\simsun_regular.ttf --size 12 --bpp 1 -r 0x20-0x7E --symbols ▶三主事交件作保信值共关制前动化区压参双口号启告地址型复子字存定实密序式强归当录息户手抖护报据控掩操故数整无日时期本板查校检模死波流测清点版用界直看码硬程端第类系线终维网置自行装警认讯记设询软运通遥量钟钥防除面页馈验默,: --no-compress --format lvgl --lv-font-name ui_font_cn_12 -o D:\Code\DTU Code\STM32F4-Base\Drivers\BSP\UI\ui_font_cn_12.c
******************************************************************************/
/*
* 模块名称:UI 中文位图字体(ui_font_cn_12,自动生成)
* 模块功能:中文子集 12px 位图字体(宋体;含 ▶ 箭头与全角逗号补回字形)
* 适用平台:STM32F407ZGTx / PC 模拟器(共用源码)
* 作者:王建锋
* 创建日期:2026-08-30
* 修改记录:
* 2026-09-13 王建锋 工程约定规范化(文件头/包含路径/版本守卫压平),生成器同步更新
* 自动生成:本文件由 tools/font_gen/make_font.py(lv_font_conv --format lvgl)自动生成,请勿手改。
* 字符集:ui_cn_chars.txt(extract_cn.py);改字符集或字体参数后重跑 make_font.py。
* 生成参数:宋体 (simsun.ttc face0), size=12, bpp=1, --no-compress。
*/
#ifdef LV_LVGL_H_INCLUDE_SIMPLE
#include "lvgl.h"
#else
#include "lvgl/lvgl.h"
#endif
#ifndef UI_FONT_CN_12
#define UI_FONT_CN_12 1
#endif
#if UI_FONT_CN_12
/*-----------------
* BITMAPS
@@ -1110,16 +1109,8 @@ static const lv_font_fmt_txt_cmap_t cmaps[] =
* ALL CUSTOM DATA
*--------------------*/
#if LVGL_VERSION_MAJOR == 8
/*Store all the custom data of the font*/
static lv_font_fmt_txt_glyph_cache_t cache;
#endif
#if LVGL_VERSION_MAJOR >= 8
static const lv_font_fmt_txt_dsc_t font_dsc = {
#else
static lv_font_fmt_txt_dsc_t font_dsc = {
#endif
.glyph_bitmap = glyph_bitmap,
.glyph_dsc = glyph_dsc,
.cmaps = cmaps,
@@ -1129,9 +1120,6 @@ static lv_font_fmt_txt_dsc_t font_dsc = {
.bpp = 1,
.kern_classes = 0,
.bitmap_format = 0,
#if LVGL_VERSION_MAJOR == 8
.cache = &cache
#endif
};
@@ -1141,29 +1129,15 @@ static lv_font_fmt_txt_dsc_t font_dsc = {
*----------------*/
/*Initialize a public general font descriptor*/
#if LVGL_VERSION_MAJOR >= 8
const lv_font_t ui_font_cn_12 = {
#else
lv_font_t ui_font_cn_12 = {
#endif
.get_glyph_dsc = lv_font_get_glyph_dsc_fmt_txt, /*Function pointer to get glyph's data*/
.get_glyph_bitmap = lv_font_get_bitmap_fmt_txt, /*Function pointer to get glyph's bitmap*/
.line_height = 13, /*The maximum line height required by the font*/
.base_line = 2, /*Baseline measured from the bottom of the line*/
#if !(LVGL_VERSION_MAJOR == 6 && LVGL_VERSION_MINOR == 0)
.subpx = LV_FONT_SUBPX_NONE,
#endif
#if LV_VERSION_CHECK(7, 4, 0) || LVGL_VERSION_MAJOR >= 8
.underline_position = -1,
.underline_thickness = 1,
#endif
.dsc = &font_dsc, /*The custom font data. Will be accessed by `get_glyph_bitmap/dsc` */
#if LV_VERSION_CHECK(8, 2, 0) || LVGL_VERSION_MAJOR >= 9
.fallback = NULL,
#endif
.user_data = NULL,
};
#endif /*#if UI_FONT_CN_12*/
+15 -12
View File
@@ -1,5 +1,13 @@
/* 本文件由 tools/menu_gen/gen_menu.py 从 ui_menu_tree.txt 自动生成,请勿手改。
* 修改菜单:编辑 ui_menu_tree.txt 后重跑 `python tools/menu_gen/gen_menu.py`。
/*
* 模块名称:UI 菜单节点表(自动生成)
* 模块功能:菜单节点扁平父子索引表 + 5 个表查询 API(导航逻辑层与显示层共用)
* 适用平台:STM32F407ZGTx / PC 模拟器(共用源码)
* 作者:王建锋
* 创建日期:2026-09-07
* 修改记录:
* 2026-09-13 王建锋 工程约定规范化(文件头 / K&R 大括号),内容由生成器自动更新
* 自动生成:本文件由 tools/menu_gen/gen_menu.py 从 ui_menu_tree.txt 自动生成,请勿手改。
* 修改菜单:编辑 ui_menu_tree.txt 后重跑 `python tools/menu_gen/gen_menu.py`。
*/
#include "ui_menu_tab.h"
@@ -57,8 +65,7 @@ const ui_menu_node_t g_menu_tab[UI_MENU_TAB_COUNT] = {
* 限定条件:node < UI_MENU_TAB_COUNT
* 函数说明:子项 = 表中所有 parent == 本下标的行(表序即显示序);建页核心查询
*/
uint8_t ui_menu_tab_children(uint8_t node, uint8_t out[], uint8_t max)
{
uint8_t ui_menu_tab_children(uint8_t node, uint8_t out[], uint8_t max) {
uint8_t n = 0u;
for (uint8_t i = 0u; i < UI_MENU_TAB_COUNT && n < max; i++) {
if (g_menu_tab[i].parent == node) {
@@ -75,8 +82,7 @@ uint8_t ui_menu_tab_children(uint8_t node, uint8_t out[], uint8_t max)
* 限定条件:node < UI_MENU_TAB_COUNT
* 函数说明:菜单/叶子由"是否有子项"自动推导,无单独类型字段
*/
bool ui_menu_tab_has_children(uint8_t node)
{
bool ui_menu_tab_has_children(uint8_t node) {
for (uint8_t i = 0u; i < UI_MENU_TAB_COUNT; i++) {
if (g_menu_tab[i].parent == node) {
return true;
@@ -92,8 +98,7 @@ bool ui_menu_tab_has_children(uint8_t node)
* 限定条件:node < UI_MENU_TAB_COUNT
* 函数说明:行文本一律经此取静态字符串,不 strdup
*/
const char *ui_menu_tab_name(uint8_t node)
{
const char *ui_menu_tab_name(uint8_t node) {
return g_menu_tab[node].name;
}
@@ -104,8 +109,7 @@ const char *ui_menu_tab_name(uint8_t node)
* 限定条件:无
* 说明:表 pwd 列取自源 ui_menu_tree.txt 行尾 #P 注记;导航逻辑层据此决定是否先经密码页
*/
bool ui_menu_tab_has_pwd(uint8_t node)
{
bool ui_menu_tab_has_pwd(uint8_t node) {
return (g_menu_tab[node].pwd != 0u);
}
@@ -116,8 +120,7 @@ bool ui_menu_tab_has_pwd(uint8_t node)
* 限定条件:无
* 函数说明:每个非根节点的 parent 必须为 MENU_ROOT 或有效下标;在 ui_menu_init 调用
*/
bool ui_menu_tab_valid(void)
{
bool ui_menu_tab_valid(void) {
for (uint8_t i = 0u; i < UI_MENU_TAB_COUNT; i++) {
if (i == 0u) {
continue; /* 根 */
+10 -2
View File
@@ -1,5 +1,13 @@
/* 本文件由 tools/menu_gen/gen_menu.py 从 ui_menu_tree.txt 自动生成,请勿手改。
* 修改菜单:编辑 ui_menu_tree.txt 后重跑 `python tools/menu_gen/gen_menu.py`。
/*
* 模块名称:UI 菜单节点表头文件(自动生成)
* 模块功能:菜单节点结构、表规模护栏与表查询 API 声明
* 适用平台:STM32F407ZGTx / PC 模拟器(共用源码)
* 作者:王建锋
* 创建日期:2026-09-07
* 修改记录:
* 2026-09-13 王建锋 工程约定规范化(文件头),内容由生成器自动更新
* 自动生成:本文件由 tools/menu_gen/gen_menu.py 从 ui_menu_tree.txt 自动生成,请勿手改。
* 修改菜单:编辑 ui_menu_tree.txt 后重跑 `python tools/menu_gen/gen_menu.py`。
*/
#ifndef UI_MENU_TAB_H
#define UI_MENU_TAB_H
+175
View File
@@ -6,12 +6,20 @@
npm i -g lv_font_conv # 离线 TTF/OTF -> LVGL 字体 转换器
pip install fonttools # 仅当字体源是 .ttc 集合时需要(用于抽取单字重)
后处理(本脚本自动完成,均幂等;产物可直接入库):
merge_arrow_glyph 补回宋体缺失的 ▶ 箭头字形
merge_extra_symbols 补回 lv_font_conv 静默丢弃的低码位符号(全角逗号)
verify_coverage 硬校验:ui_cn_chars.txt 全部字符必须进字体
fix_trailing_newline 规范化文件末尾为单个换行
normalize_project_conventions 工程约定规范化(文件头 / 包含路径 / 版本守卫压平)
用法:
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 re
import sys
import argparse
import tempfile
@@ -127,6 +135,172 @@ def fix_trailing_newline(path):
fp.write(fixed)
# ---------------------------------------------------------------------------
# 工程约定规范化(lv_font_conv 产物后处理最后一步,幂等)
# 1) lv_font_conv 头注释(内嵌本机绝对路径)替换为工程文件头(模块名称/作者)
# 2) LV_LVGL_H_INCLUDE_SIMPLE 双分支包含收敛为单一 #include "lvgl.h"
# (规范 2.3.5 禁止相对/绝对路径 include,如 "lvgl/lvgl.h")
# 3) 版本守卫(#if LVGL_VERSION_*/LV_VERSION_CHECK)压平:仓库 LVGL 固定 9.3.0,
# 守卫可静态求值,保留真分支、删除守卫(同时消除 cppcheck syntaxError)
# ---------------------------------------------------------------------------
LVGL_VERSION = (9, 3, 0)
def _eval_cond(cond, symbols):
"""静态求值 #if 条件(仅支持 lv_font_conv 产出的版本表达式)。
返回 True/False;无法求值时返回 None(调用方保留原块不压平)。
"""
def ver_check(m):
a, b, c = (int(x) for x in m.groups())
return '1' if LVGL_VERSION >= (a, b, c) else '0'
e = re.sub(r'LV_VERSION_CHECK\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)', ver_check, cond)
for name, val in (('LVGL_VERSION_MAJOR', LVGL_VERSION[0]),
('LVGL_VERSION_MINOR', LVGL_VERSION[1]),
('LVGL_VERSION_PATCH', LVGL_VERSION[2])):
e = re.sub(r'\b%s\b' % name, str(val), e)
for name, val in symbols.items():
e = re.sub(r'\b%s\b' % name, str(val), e)
if re.search(r'[A-Za-z_]\w*', e): # 仍有标识符 → 无法静态求值(先于 &&/|| 转换,避免误判 and/or)
return None
e = e.replace('&&', ' and ').replace('||', ' or ')
e = re.sub(r'!(?!=)', ' not ', e)
try:
return bool(eval(e, {'__builtins__': {}}))
except Exception:
return None
def _active(stack):
"""当前行是否处于"保留分支"(不可求值块视为整体保留)。"""
for cond, in_else, evalable in stack:
if evalable and in_else == cond:
return False
return True
def _flatten_guards(lines, symbols):
"""压平可静态求值的版本守卫;不可求值的块原样保留(并告警)。"""
out = []
stack = [] # [cond, in_else, evalable];cond 为 None = 不可求值
for raw in lines:
s = raw.strip()
if not s.startswith('#'):
if _active(stack):
out.append(raw)
continue
mi = re.match(r'#\s*if\s+(\S.*)$', s)
mid = re.match(r'#\s*ifdef\s+(\S+)', s)
minn = re.match(r'#\s*ifndef\s+(\S+)', s)
if mi or mid or minn:
if mid:
cond, evalable = mid.group(1) in symbols, True
elif minn:
cond, evalable = minn.group(1) not in symbols, True
else:
cond = _eval_cond(mi.group(1), symbols)
evalable = cond is not None
if not evalable:
print('WARNING: 无法静态求值,原样保留: %s' % s)
stack.append([cond, False, evalable])
if not evalable:
out.append(raw) # 不可求值块保留 #if 行
continue
if re.match(r'#\s*else\b', s):
if stack and stack[-1][2]:
stack[-1][1] = not stack[-1][1]
continue # 可求值:丢弃 #else 行
out.append(raw) # 不可求值(或无配对 #if):原样保留
continue
if re.match(r'#\s*endif\b', s):
if stack:
if stack[-1][2]:
stack.pop()
continue # 可求值:丢弃 #endif 行
stack.pop()
out.append(raw)
continue
if s.startswith('#define'):
m = re.match(r'#\s*define\s+([A-Za-z_]\w*)\s+(\S.*)$', s)
if m:
try:
symbols[m.group(1)] = int(m.group(2).strip())
except ValueError:
pass
if _active(stack):
out.append(raw)
continue
if _active(stack): # 其余指令(#include 等)
out.append(raw)
return out
def _drop_unused_guard(lines, guard_name):
"""压平后 lv_font_conv 遗留的 #define 守卫宏(如 UI_FONT_CN_12)若无人再引用则删除
(.c 文件不需要头文件保护宏)。"""
n = len(re.findall(r'\b%s\b' % re.escape(guard_name), '\n'.join(lines)))
if n != 1:
return lines
return [l for l in lines
if not re.match(r'#\s*define\s+%s\b' % re.escape(guard_name), l.strip())]
def normalize_project_conventions(font_path, size, bpp):
"""lv_font_conv 产物对齐工程约定(在 verify_coverage / fix_trailing_newline 之后调用)。"""
raw = open(font_path, 'rb').read()
has_bom = raw.startswith(b'\xef\xbb\xbf')
text = raw.decode('utf-8-sig')
eol = '\r\n' if '\r\n' in text else '\n'
lines = [l.rstrip('\r') for l in text.split('\n')]
while lines and lines[-1] == '':
lines.pop()
# 1) lv_font_conv 头注释(内嵌本机绝对路径)替换为工程文件头
if lines and lines[0].startswith('/**'):
end = None
for i in range(1, len(lines)):
if re.fullmatch(r'\s*\*+/\s*', lines[i]):
end = i
break
if end is not None:
header = [
'/*',
' * 模块名称:UI 中文位图字体(%s,自动生成)' % FONT_NAME,
' * 模块功能:中文子集 %dpx 位图字体(宋体;含 ▶ 箭头与全角逗号补回字形)' % size,
' * 适用平台:STM32F407ZGTx / PC 模拟器(共用源码)',
' * 作者:王建锋',
' * 创建日期:2026-08-30',
' * 修改记录:',
' * 2026-09-13 王建锋 工程约定规范化(文件头/包含路径/版本守卫压平),生成器同步更新',
' * 自动生成:本文件由 tools/font_gen/make_font.py(lv_font_conv --format lvgl)自动生成,请勿手改。',
' * 字符集:ui_cn_chars.txt(extract_cn.py);改字符集或字体参数后重跑 make_font.py。',
' * 生成参数:宋体 (simsun.ttc face0), size=%d, bpp=%d, --no-compress。' % (size, bpp),
' */',
]
lines = header + lines[end + 1:]
# 2) 双分支包含收敛为单一 #include "lvgl.h"
for i, l in enumerate(lines):
if l.strip() == '#ifdef LV_LVGL_H_INCLUDE_SIMPLE':
j = i
while j < len(lines) and lines[j].strip() != '#endif':
j += 1
lines[i:j + 1] = ['#include "lvgl.h"']
break
# 3) 压平版本守卫(保留真分支;本仓库 LVGL 为 9.3.0)
symbols = {}
lines = _flatten_guards(lines, symbols)
lines = _drop_unused_guard(lines, FONT_NAME.upper())
while lines and lines[-1] == '':
lines.pop()
data = eol.join(lines) + eol
open(font_path, 'wb').write((b'\xef\xbb\xbf' if has_bom else b'') + data.encode('utf-8'))
print('工程约定规范化完成:文件头/包含路径/版本守卫压平(LVGL %d.%d.%d)'
% LVGL_VERSION)
# ---------------------------------------------------------------------------
# 低码位符号补回(与箭头字形合并同一模式)
# lv_font_conv 的稀疏 cmap 从符号集中最小码位起算,低于该起点的符号被静默丢弃
@@ -290,6 +464,7 @@ def main():
merge_extra_symbols(OUT, args.size, args.bpp) # 补回 lv_font_conv 丢弃的低码位符号(幂等)
verify_coverage(OUT) # 生成后校验:字符集全部码位必须进字体(防静默丢字)
fix_trailing_newline(OUT) # 规范化末尾换行(pre-commit end-of-file-fixer)
normalize_project_conventions(OUT, args.size, args.bpp) # 对齐工程约定(文件头/包含路径/版本守卫压平)
print('OK %d bytes -> %s' % (os.path.getsize(OUT), OUT))
+25 -14
View File
@@ -83,8 +83,16 @@ def emit(nodes):
max_depth = max(d for _, _, d, _ in nodes)
h = [
'/* 本文件由 tools/menu_gen/gen_menu.py 从 ui_menu_tree.txt 自动生成,请勿手改。',
' * 修改菜单:编辑 ui_menu_tree.txt 后重跑 `python tools/menu_gen/gen_menu.py`。',
'/*',
' * 模块名称:UI 菜单节点表头文件(自动生成)',
' * 模块功能:菜单节点结构、表规模护栏与表查询 API 声明',
' * 适用平台:STM32F407ZGTx / PC 模拟器(共用源码)',
' * 作者:王建锋',
' * 创建日期:2026-09-07',
' * 修改记录:',
' * 2026-09-13 王建锋 工程约定规范化(文件头),内容由生成器自动更新',
' * 自动生成:本文件由 tools/menu_gen/gen_menu.py 从 ui_menu_tree.txt 自动生成,请勿手改。',
' * 修改菜单:编辑 ui_menu_tree.txt 后重跑 `python tools/menu_gen/gen_menu.py`。',
' */',
'#ifndef UI_MENU_TAB_H',
'#define UI_MENU_TAB_H',
@@ -124,8 +132,16 @@ def emit(nodes):
]
c = [
'/* 本文件由 tools/menu_gen/gen_menu.py 从 ui_menu_tree.txt 自动生成,请勿手改。',
' * 修改菜单:编辑 ui_menu_tree.txt 后重跑 `python tools/menu_gen/gen_menu.py`。',
'/*',
' * 模块名称:UI 菜单节点表(自动生成)',
' * 模块功能:菜单节点扁平父子索引表 + 5 个表查询 API(导航逻辑层与显示层共用)',
' * 适用平台:STM32F407ZGTx / PC 模拟器(共用源码)',
' * 作者:王建锋',
' * 创建日期:2026-09-07',
' * 修改记录:',
' * 2026-09-13 王建锋 工程约定规范化(文件头 / K&R 大括号),内容由生成器自动更新',
' * 自动生成:本文件由 tools/menu_gen/gen_menu.py 从 ui_menu_tree.txt 自动生成,请勿手改。',
' * 修改菜单:编辑 ui_menu_tree.txt 后重跑 `python tools/menu_gen/gen_menu.py`。',
' */',
'#include "ui_menu_tab.h"',
'',
@@ -147,8 +163,7 @@ def emit(nodes):
' * 限定条件:node < UI_MENU_TAB_COUNT',
' * 函数说明:子项 = 表中所有 parent == 本下标的行(表序即显示序);建页核心查询',
' */',
'uint8_t ui_menu_tab_children(uint8_t node, uint8_t out[], uint8_t max)',
'{',
'uint8_t ui_menu_tab_children(uint8_t node, uint8_t out[], uint8_t max) {',
' uint8_t n = 0u;',
' for (uint8_t i = 0u; i < UI_MENU_TAB_COUNT && n < max; i++) {',
' if (g_menu_tab[i].parent == node) {',
@@ -165,8 +180,7 @@ def emit(nodes):
' * 限定条件:node < UI_MENU_TAB_COUNT',
' * 函数说明:菜单/叶子由"是否有子项"自动推导,无单独类型字段',
' */',
'bool ui_menu_tab_has_children(uint8_t node)',
'{',
'bool ui_menu_tab_has_children(uint8_t node) {',
' for (uint8_t i = 0u; i < UI_MENU_TAB_COUNT; i++) {',
' if (g_menu_tab[i].parent == node) {',
' return true;',
@@ -182,8 +196,7 @@ def emit(nodes):
' * 限定条件:node < UI_MENU_TAB_COUNT',
' * 函数说明:行文本一律经此取静态字符串,不 strdup',
' */',
'const char *ui_menu_tab_name(uint8_t node)',
'{',
'const char *ui_menu_tab_name(uint8_t node) {',
' return g_menu_tab[node].name;',
'}',
'',
@@ -194,8 +207,7 @@ def emit(nodes):
' * 限定条件:无',
' * 说明:表 pwd 列取自源 ui_menu_tree.txt 行尾 #P 注记;导航逻辑层据此决定是否先经密码页',
' */',
'bool ui_menu_tab_has_pwd(uint8_t node)',
'{',
'bool ui_menu_tab_has_pwd(uint8_t node) {',
' return (g_menu_tab[node].pwd != 0u);',
'}',
'',
@@ -206,8 +218,7 @@ def emit(nodes):
' * 限定条件:无',
' * 函数说明:每个非根节点的 parent 必须为 MENU_ROOT 或有效下标;在 ui_menu_init 调用',
' */',
'bool ui_menu_tab_valid(void)',
'{',
'bool ui_menu_tab_valid(void) {',
' for (uint8_t i = 0u; i < UI_MENU_TAB_COUNT; i++) {',
' if (i == 0u) {',
' continue; /* 根 */',