From 3179971b71de00c0241332a81f57bf1771e83e3d Mon Sep 17 00:00:00 2001 From: cjiio Date: Tue, 15 Sep 2026 13:52:32 +0800 Subject: [PATCH] docs: clarify flashing policy and Chinese font integration --- AGENTS.md | 3 + AGENTS.zh_CN.md | 3 + docs/development/README.md | 3 +- docs/development/README.zh_CN.md | 3 +- docs/development/ai-guide.md | 1 + docs/development/ai-guide.zh_CN.md | 1 + .../development/engineering/build-and-test.md | 7 + .../engineering/build-and-test.zh_CN.md | 5 + .../engineering/coding-conventions.md | 46 ++++- .../engineering/coding-conventions.zh_CN.md | 46 ++++- .../engineering/firmware-layout.md | 18 +- .../engineering/firmware-layout.zh_CN.md | 14 +- .../engineering/lvgl-chinese-fonts.md | 164 ++++++++++++++++++ .../engineering/lvgl-chinese-fonts.zh_CN.md | 164 ++++++++++++++++++ 14 files changed, 469 insertions(+), 9 deletions(-) create mode 100644 docs/development/engineering/lvgl-chinese-fonts.md create mode 100644 docs/development/engineering/lvgl-chinese-fonts.zh_CN.md diff --git a/AGENTS.md b/AGENTS.md index eb7fb07..7785385 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -14,9 +14,11 @@ This file is the only mandatory entry point for AI-assisted work in this reposit may deliberately change this layout; validate the resulting table and do not turn product-specific partitions into mandatory template contracts. - Preserve existing user changes. Start with `git status --short --branch`; never overwrite or clean unrelated files. +- Flashing new firmware does not require backing up the firmware already on the device; do not make a Flash readback a prerequisite. This does not guarantee preservation of user data or authorize a full-chip erase. Follow the [flashing and data policy](docs/development/engineering/firmware-layout.md#flashing-and-stored-data). - Hardware facts follow this priority: product specifications and measured results → `components/bsp/include/bsp_pins.h` → BSP headers and implementation → hardware guide → README/demo code. If a task requires a hardware detail not defined by these sources, ask the user instead of guessing. - Reusable board logic belongs in `components/bsp`; pages, state machines, animations, and application tasks belong in `main`. - LVGL is not thread-safe. Code outside the LVGL task must hold `bsp_lvgl_lock()` while accessing LVGL objects. +- Before adding Chinese UI text, follow the [font checklist](docs/development/engineering/coding-conventions.md#chinese-fonts-and-missing-glyphs). The default Montserrat fonts have no Chinese glyphs; UTF-8 and a successful build do not prove display support. Verify glyph coverage, widget font selection, and on-device rendering. - Button callbacks must stay non-blocking. Audio, storage, networking, and other slow operations belong in worker tasks. - A demo must stop every task, timer, callback, and event handler that can access its UI before deleting the screen. - Keep testable state machines, protocols, timing, and layout calculations independent from ESP-IDF/LVGL and cover them with host tests. @@ -31,6 +33,7 @@ This file is the only mandatory entry point for AI-assisted work in this reposit | Environment bootstrap or missing toolchain | `docs/development/engineering/environment-setup.md` | | BSP, pins, buses, display, audio, battery | `docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.md`, `components/bsp/include/bsp_pins.h` | | Demo or menu | `main/demo.h`, `main/main.c`, the nearest `main/demo_*.c` implementation | +| Chinese UI text or fonts | `docs/development/engineering/lvgl-chinese-fonts.md`, the application's font assets, configuration, and widget styles | | Build, test, dependencies, partitions | `docs/development/engineering/build-and-test.md`, `docs/development/engineering/firmware-layout.md`, `sdkconfig.defaults`, `partitions.csv` | | CI or release | the matching file in `docs/development/ci/CI-*.md` and `.github/workflows/` | | Project completion | `docs/development/release/project-completion.md` (then the `issue-suggestions` or `experience-pr` skill) | diff --git a/AGENTS.zh_CN.md b/AGENTS.zh_CN.md index 4a91da2..7bc31bb 100644 --- a/AGENTS.zh_CN.md +++ b/AGENTS.zh_CN.md @@ -13,9 +13,11 @@ 剩余空间的单个 factory app。用户固件可以按需求明确调整布局;修改后必须 验证结果,不得把产品专用分区变成模板的强制契约。 - 保留用户已有修改。先执行 `git status --short --branch`,不得覆盖或清理无关文件。 +- 下载(烧录)新固件无需备份设备内原有固件,不得把读取 Flash 备份作为前置条件。这不保证保留用户数据,也不授权全片擦除;遵循[烧录与数据说明](docs/development/engineering/firmware-layout.zh_CN.md#烧录与已存数据)。 - 硬件事实优先级:产品规格与实测结果 → `components/bsp/include/bsp_pins.h` → BSP 头文件与实现 → 硬件指南 → README/demo。任务所需硬件细节未在这些来源中定义时,直接询问用户,不得猜测。 - 可复用板级逻辑放入 `components/bsp`;页面、状态机、动画和应用任务放入 `main`。 - LVGL 非线程安全。LVGL 任务之外访问 LVGL 对象时必须持有 `bsp_lvgl_lock()`。 +- 添加中文 UI 前必须遵循[字体检查清单](docs/development/engineering/coding-conventions.zh_CN.md#中文字体与缺字排查)。默认 Montserrat 字体不含中文字形;UTF-8 正确、编译成功均不代表能够显示中文。必须核对字形覆盖、控件实际字体并完成真机显示验收。 - 按键回调不得阻塞。音频、存储、网络等慢操作必须放入工作任务。 - demo 删除 screen 前,必须停止所有可能访问其 UI 的任务、定时器、回调和事件处理器。 - 可测试的状态机、协议、计时和布局计算应与 ESP-IDF/LVGL 解耦,并由 host tests 覆盖。 @@ -30,6 +32,7 @@ | 环境引导或缺少工具链 | `docs/development/engineering/environment-setup.zh_CN.md` | | BSP、引脚、总线、显示、音频、电池 | `docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.zh_CN.md`、`components/bsp/include/bsp_pins.h` | | Demo 或菜单 | `main/demo.h`、`main/main.c`、最近的 `main/demo_*.c` 实现 | +| 中文 UI 或字体 | `docs/development/engineering/lvgl-chinese-fonts.zh_CN.md`、应用字体素材、配置与控件样式 | | 构建、测试、依赖、分区 | `docs/development/engineering/build-and-test.zh_CN.md`、`docs/development/engineering/firmware-layout.zh_CN.md`、`sdkconfig.defaults`、`partitions.csv` | | CI 或发布 | `docs/development/ci/CI-*.zh_CN.md` 中的对应文件与 `.github/workflows/` | | 项目开发完成 | `docs/development/release/project-completion.zh_CN.md`(再进入 `issue-suggestions` 或 `experience-pr` skill) | diff --git a/docs/development/README.md b/docs/development/README.md index 4869392..7ad3fc7 100644 --- a/docs/development/README.md +++ b/docs/development/README.md @@ -15,7 +15,8 @@ This directory contains AI Passport engineering rules and reusable workflows, gr - [environment-setup.md](engineering/environment-setup.md): clean-machine bootstrap for AI agents, including international and mainland China download routes. - [build-and-test.md](engineering/build-and-test.md): ESP-IDF build and validation. - [firmware-layout.md](engineering/firmware-layout.md): default and user-defined partition layouts and merged-artifact validation. -- [coding-conventions.md](engineering/coding-conventions.md): source-code and resource conventions. +- [coding-conventions.md](engineering/coding-conventions.md): source-code and resource conventions, including Chinese font integration, blank/boxed text troubleshooting, and display acceptance. +- [lvgl-chinese-fonts.md](engineering/lvgl-chinese-fonts.md): step-by-step CJK configuration, font generation/linking, fallback examples, glyph checks, and troubleshooting. ## CI diff --git a/docs/development/README.zh_CN.md b/docs/development/README.zh_CN.md index 7f820c6..b026b0d 100644 --- a/docs/development/README.zh_CN.md +++ b/docs/development/README.zh_CN.md @@ -24,7 +24,8 @@ - [environment-setup.zh_CN.md](engineering/environment-setup.zh_CN.md):AI 在全新机器上的环境引导,包含国际与中国大陆下载线路。 - [build-and-test.zh_CN.md](engineering/build-and-test.zh_CN.md):构建与验证(ESP-IDF 命令、逻辑测试、改动验证要求)。 - [firmware-layout.zh_CN.md](engineering/firmware-layout.zh_CN.md):默认/用户自定义分区布局与合并产物验证。 -- [coding-conventions.zh_CN.md](engineering/coding-conventions.zh_CN.md):代码约定(语言风格、复用、注释、测试同步、资源约束等)。 +- [coding-conventions.zh_CN.md](engineering/coding-conventions.zh_CN.md):代码约定(语言风格、复用、注释、测试同步、资源约束等),包含中文字体接入、空白/方框排查与显示验收。 +- [lvgl-chinese-fonts.zh_CN.md](engineering/lvgl-chinese-fonts.zh_CN.md):CJK 配置、字体生成/链接、fallback 示例、缺字检查和故障排查的分步指南。 ## CI(ci) diff --git a/docs/development/ai-guide.md b/docs/development/ai-guide.md index 93a5871..faa760c 100644 --- a/docs/development/ai-guide.md +++ b/docs/development/ai-guide.md @@ -50,6 +50,7 @@ Only reusable hardware capabilities belong in the BSP. Document blocking behavio - The `main` menu, `demo_*.c` pages, and `ui_pixel` visuals are a board-capability demonstration, not a production application shell. A newly developed application must redesign and implement its own screens and interaction flow from its product requirements instead of directly shipping or copying the current demo UI. Reuse BSP interfaces and applicable implementation patterns; inherit the demo menu or visual theme only when the developer explicitly requests it. A hardware-validation page that intentionally remains part of the baseline demo should still use `ui_pixel_screen_create()` / `ui_pixel_panel_create()` for consistency. - By default show the battery level in the top-right corner of a user interface (read via `bsp_battery_soc()`), unless the developer specifies a different placement or explicitly does not want it. Degrade gracefully when the reading is `-1` (unavailable) instead of drawing a number. Place it so it does not overlap the cloud decoration (`add_cloud`, around `x≈188, y≈8`) in the top-right; use the clear sky space rather than covering the cloud. - Budget internal RAM for images, fonts, networking, audio, LVGL, and task stacks; this board has no PSRAM. +- Treat Chinese text as a font-integration task, not just a string translation. Follow the [Chinese font checklist](engineering/coding-conventions.md#chinese-fonts-and-missing-glyphs) when changing text, fonts, sizes, themes, or dynamic content; never hide missing-glyph boxes as a fix. - Isolate testable state machines, protocols, timing, and layout calculations from ESP-IDF/LVGL and cover them with host tests. ## Material placement diff --git a/docs/development/ai-guide.zh_CN.md b/docs/development/ai-guide.zh_CN.md index 424221b..c410c93 100644 --- a/docs/development/ai-guide.zh_CN.md +++ b/docs/development/ai-guide.zh_CN.md @@ -60,6 +60,7 @@ Natural-language requirement - 当前 `main` 菜单、`demo_*.c` 页面和 `ui_pixel` 视觉仅用于展示与验证板级能力,不是成品应用界面。新开发应用必须根据产品需求重新设计并实现页面与交互流程,不能直接交付或照搬当前 demo UI;可以复用 BSP 接口和适用的实现模式,只有开发者明确要求时才沿用 demo 菜单或视觉主题。若新增页面本身就是继续留在基础 demo 中的硬件验证页,则仍应使用 `ui_pixel_screen_create()` / `ui_pixel_panel_create()` 保持一致。 - 默认情况下,在用户界面的**右上角显示电量信息**(读取 `bsp_battery_soc()`),除非开发者明确指定其它位置或明确不需要。读值为 `-1`(不可用)时优雅降级,而不是画一个数字。位置要避开右上角已有的**白云装饰**(`add_cloud`,约 `x≈188, y≈8`),用空闲的蓝天区,不要盖住白云。 - 新图片、字体、网络栈、音频缓存、LVGL buffer 或任务栈都要评估内部 RAM;总空闲堆足够不代表存在足够大的连续内存块。 +- 中文显示属于字体集成任务,不只是翻译字符串。修改文案、字体、字号、主题或动态内容时,必须执行[中文字体检查清单](engineering/coding-conventions.zh_CN.md#中文字体与缺字排查);不得通过隐藏缺字方框来假装修复显示问题。 - 可测试的状态机、协议、计时和布局计算应与 ESP-IDF/LVGL 分离,优先加入主机逻辑测试。 ## 5. 素材放置(Material placement) diff --git a/docs/development/engineering/build-and-test.md b/docs/development/engineering/build-and-test.md index f012002..68cb77d 100644 --- a/docs/development/engineering/build-and-test.md +++ b/docs/development/engineering/build-and-test.md @@ -7,6 +7,13 @@ Use ESP-IDF 5.5.3. On a clean machine or when the toolchain is missing, follow the [environment bootstrap](environment-setup.md) first. +> **No original-firmware backup is required before downloading (flashing) new +> firmware to the device.** Reading out the installed firmware is not a +> prerequisite. Flashing replaces the installed firmware and does not provide +> automatic restoration of it. This does not mean user data is preserved: if +> you need existing settings or records, export or otherwise save them first. +> See [flashing and stored data](firmware-layout.md#flashing-and-stored-data). + > Prefer `./tools/validate.sh --firmware` for firmware builds. Flash its > verified `build/FoloToy-AI-Passport-full.bin` at offset `0x0` for a blank > device or an intentional complete refresh. The merged image may reset NVS; diff --git a/docs/development/engineering/build-and-test.zh_CN.md b/docs/development/engineering/build-and-test.zh_CN.md index ae19be0..9d4a427 100644 --- a/docs/development/engineering/build-and-test.zh_CN.md +++ b/docs/development/engineering/build-and-test.zh_CN.md @@ -7,6 +7,11 @@ 使用 ESP-IDF 5.5.3。全新机器或缺少工具链时,先按 [环境引导](environment-setup.zh_CN.md)完成安装。 +> **向设备下载(烧录)新固件前,无需备份设备内部原有固件。** 不要求先读出 +> 原固件,也不把原固件备份作为烧录前置条件。烧录会覆盖原固件,不会自动恢复 +> 原固件。这不代表用户数据会被保留:如果需要保留已有设置或记录,应事先 +> 导出或另行保存。详见[烧录与已存数据](firmware-layout.zh_CN.md#烧录与已存数据)。 + > 固件编译优先运行 `./tools/validate.sh --firmware`。空白设备初始化或有意完整 > 刷新时,把验证通过的 `build/FoloToy-AI-Passport-full.bin` 从 `0x0` 写入; > 合并镜像可能重置 NVS,需要保留已有 NVS 状态时使用分段 `idf.py flash`。 diff --git a/docs/development/engineering/coding-conventions.md b/docs/development/engineering/coding-conventions.md index 8886de3..14ec178 100644 --- a/docs/development/engineering/coding-conventions.md +++ b/docs/development/engineering/coding-conventions.md @@ -5,8 +5,8 @@ # Coding Conventions - Write C with four-space indentation and K&R braces, following neighboring files. Use `snake_case`, `BSP_*` public constants, `s_` file-local state, `bsp_` public BSP APIs, and `demo__` demo entry points. Prefer `static` for internal symbols. -- Keep UI text and default documentation in English. Explanatory source comments may use Chinese while retaining established English technical terms. -- The baseline enables only LVGL Montserrat 14 and 20, which do not contain CJK glyphs. Chinese UTF-8 text therefore renders as missing-glyph boxes; changing source-file encoding does not fix it. Before adding Chinese UI text, compile and select a CJK font that covers every displayed character, prefer a glyph subset over a full font, configure a suitable fallback for mixed-language text, budget Flash and internal RAM, and verify the result on the device. +- Keep baseline-demo UI text and default documentation in English. Application UI may use Chinese when its font support is implemented and verified. Explanatory source comments may use Chinese while retaining established English technical terms. +- Before adding Chinese UI text, complete the [font checklist](#chinese-fonts-and-missing-glyphs); changing strings to UTF-8 alone does not add missing glyphs. - Put reusable hardware behavior in `components/bsp`; keep menus, animations, product interaction, and validation pages in `main`. - The `ui_pixel` theme (sky background, grass, title plate, mascot, ink-outlined panels) is part of the user interface, not a removable component. When trimming components or routing straight to a feature screen, keep the theme and build the screen through `ui_pixel_screen_create()` / `ui_pixel_panel_create()`. - Show the battery level in the top-right corner of a user interface by default, unless the developer specifies a different placement or explicitly does not want it. Read it from `bsp_battery_soc()` (and `bsp_battery_mv()` where useful); render it as a small battery indicator or percentage in the top-right area of the screen, and degrade gracefully when it reads `-1` (unavailable). Place it where it does not overlap the existing cloud decoration (`add_cloud`, around `x≈188, y≈8`): use the clear sky space beside or below the cloud, or the very top-right edge, rather than covering the cloud. @@ -15,3 +15,45 @@ - If adding a cache, define expiration and cleanup unless durable retention is explicitly justified. - The ESP32-C3 has no PSRAM. Review internal RAM and largest-contiguous-block impact before increasing LVGL buffers, audio allocations, network state, or task stacks. - **Watch power consumption.** This is a wearable powered by a small battery; keep it efficient. Avoid keeping the screen lit for long periods: dim or turn off the backlight, and return to a low-power state (light/deep sleep) whenever the screen is idle, so the device is not left displaying a bright screen while doing nothing. See the guidance on sleep in [`../hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.md`](../../hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.md). + +## Chinese fonts and missing glyphs + +For configuration, converter commands, CMake integration, safe fallback code, and glyph-check examples, follow the [detailed LVGL Chinese text guide](lvgl-chinese-fonts.md). + +The baseline enables Montserrat 14 and 20 in [`sdkconfig.defaults`](../../../sdkconfig.defaults); neither contains Chinese glyphs. A correct UTF-8 string can therefore appear as boxes or blank text. Serial-console output, an installed computer font, and a successful firmware build do not prove that LVGL can render that text. + +### Before implementing or changing Chinese UI + +1. **Define coverage.** Collect all displayed text: titles, buttons, hints, empty/error states, units, punctuation, and dynamic content. Include Latin letters, digits, full-width punctuation, and any `LV_SYMBOL_*` icons in the chosen font or fallback chain. Regenerate each affected size/subset when text changes. A built-in CJK subset or a subset extracted from fixed UI strings does not guarantee arbitrary Chinese names, Wi-Fi SSIDs, or server/user input; define the supported character set and an explicit unsupported-character policy. +2. **Generate reproducible assets.** Select a licensed font whose source actually contains the required glyphs. Prefer a subset at the sizes needed by the application instead of embedding an entire font family. Keep reusable sources/generated files under `assets/fonts/` and record the license, converter version, command, character inventory, size, and bit depth in the [assets documentation](../../../assets/README.md). Use output compatible with the LVGL version pinned in [`dependencies.lock`](../../../dependencies.lock) (currently 9.5.0). See the [official font converter](https://github.com/lvgl/lv_font_conv) for generation options; this baseline does not bundle an application-specific Chinese font. +3. **Compile and select the font.** Add the generated `.c` file to the consuming component's CMake `SRCS`, declare its exported symbol with `LV_FONT_DECLARE`, and assign it to the actual text widget/part. Copying a TTF/OTF or `.c` into `assets/` does not automatically compile, load, or select it. Check the generated `sdkconfig` has `CONFIG_LV_TXT_ENC_UTF8=y`; changing defaults alone does not necessarily update an existing configuration (see [build and test](build-and-test.md)). +4. **Check style overrides and fallback.** Changing the default font or a screen's inherited font does not override explicit child fonts. [`main/ui_pixel.c`](../../../main/ui_pixel.c) sets a local font in `ui_pixel_label()` and explicitly uses Montserrat 20 for its title; menu/demo labels also select fonts directly. Audit titles, labels, widget parts, and focused/pressed/disabled states. For mixed text, use a font containing all required glyphs or a deliberate fallback chain. Do not cast away `const` to modify Flash-resident font descriptors, create fallback cycles, or free a font while widgets still use it. +5. **Budget resources.** Inspect Flash/static RAM after generation and free heap/largest free block at runtime, including when networking is active. Constant bitmap font data can reside in Flash; runtime font loading, decoding, and caches may need RAM. Do not blindly enlarge the LVGL heap or display buffers to treat missing glyphs; more memory cannot supply glyphs absent from the font. + +For example, after generating and linking `app_font_20` with the required glyphs, select it explicitly. This symbol is illustrative, not a font supplied by this repository. Run object operations in the LVGL task or while holding `bsp_lvgl_lock()`: + +```c +LV_FONT_DECLARE(app_font_20); + +/* Inside the application's UI creation function. */ +lv_obj_t *label = lv_label_create(parent); +lv_obj_set_style_text_font(label, &app_font_20, LV_PART_MAIN | LV_STATE_DEFAULT); +lv_label_set_text(label, "\u4E2D\u6587 ABC 123"); +``` + +### Troubleshooting order + +| Symptom | Check first | +| --- | --- | +| Latin text works; Chinese is blank or boxed | Actual widget font and its fallback coverage, UTF-8 input, and `CONFIG_LV_TXT_ENC_UTF8`; disabling `CONFIG_LV_USE_FONT_PLACEHOLDER` only hides the missing-glyph indicator, not the defect | +| Only some characters or new text fail | Missing subset entries, punctuation, each font size, and dynamic text outside the supported set; regenerate and relink the affected assets | +| Body text works but titles/buttons or selected states fail | Explicit local fonts, theme/state overrides, and the font on the widget part that draws the text | +| Glyph coverage is correct but text is invisible or cut off | Text color/opacity, hidden flags, label size, line height, parent clipping, and draw/allocation errors; do not assume every blank label is a font defect | + +### Required acceptance + +- Check every required printable Unicode code point against each font/fallback chain actually used. With the pinned LVGL API, `lv_font_get_glyph_dsc()` must succeed and `is_placeholder` must be false; a placeholder is not coverage. Decode UTF-8 into code points rather than testing individual bytes. Record uncovered code points and add a repeatable coverage check alongside the application's font-generation pipeline. +- On the device, verify all pages and error/empty states, Chinese/Latin/digit/punctuation/icon mixtures, different sizes and interaction states, long text, and dynamic content both inside and outside the declared supported set. Check for boxes, blanks, clipping, and fallback baseline/line-height mismatches; do not accept one successful sample label as whole-application validation. +- Report the tested text/character set, font sizes, and resource changes. The baseline's host tests do not validate a derivative application's font coverage or screen output. Without a physical-device check, report `Device tests: NOT RUN` and explicitly list Chinese rendering under `Unverified`. + +Font selection and fallback semantics: [LVGL 9.5 font overview](https://docs.lvgl.io/9.5/main-modules/fonts/overview.html). Missing-glyph handling: [LVGL 9.5 implementation](https://github.com/lvgl/lvgl/blob/v9.5.0/src/font/lv_font.c). diff --git a/docs/development/engineering/coding-conventions.zh_CN.md b/docs/development/engineering/coding-conventions.zh_CN.md index 5c5f8c3..39946da 100644 --- a/docs/development/engineering/coding-conventions.zh_CN.md +++ b/docs/development/engineering/coding-conventions.zh_CN.md @@ -4,8 +4,8 @@ # 代码约定(Coding Conventions) -- **语言/风格**:C 用四空格缩进 + K&R 大括号,跟随相邻文件;函数/局部变量 `snake_case`,公开硬件常量 `BSP_*`,文件内状态 `s_` 前缀;BSP API 用 `bsp_` 前缀,demo 入口 `demo__`;内部符号优先 `static`。UI 文案用英文,解释性注释可用中文;保留记录硬件寄存器值与内存约束的注释。 -- **中文方框字陷阱**:当前基线只启用 LVGL Montserrat 14 和 20,这些字体不包含 CJK glyph。即使源码和字符串均为 UTF-8,中文也会显示为缺字方框,修改源文件编码不能解决。添加中文 UI 前,必须编译并选用覆盖全部实际字符的 CJK 字体,优先生成 glyph 子集而非引入完整字库;中英文混排时配置合适的 fallback,同时核对 Flash、内部 RAM 占用并在真机逐字验证。 +- **语言/风格**:C 用四空格缩进 + K&R 大括号,跟随相邻文件;函数/局部变量 `snake_case`,公开硬件常量 `BSP_*`,文件内状态 `s_` 前缀;BSP API 用 `bsp_` 前缀,demo 入口 `demo__`;内部符号优先 `static`。基础 demo UI 文案与默认文档用英文;应用 UI 可以使用中文,但必须实现并验证字体支持。解释性注释可用中文并保留通用英文技术术语;保留记录硬件寄存器值与内存约束的注释。 +- **中文空白/方框陷阱**:添加中文 UI 前必须完成[字体检查清单](#中文字体与缺字排查);仅把字符串改成 UTF-8 不会补齐缺失的字形。 - **复用既有组件**:可复用硬件逻辑放 `components/bsp`;菜单/动画/业务交互/验证页面放 `main`。不要另造轮子,遵循既有 BSP API。 - **主题不是可删组件**:`ui_pixel` 主题体系(天空底色、草地、标题牌、吉祥物、墨色描边面板)属于用户界面的一部分,不是可删除的"不必要组件"。精简组件或直接载入功能界面时,保留该主题,并通过 `ui_pixel_screen_create()` / `ui_pixel_panel_create()` 建屏。 - **默认右上角电量**:用户界面在没有默认要求的情况下,默认在**右上角显示电量信息**;除非开发者明确指定其它位置或明确不需要。电量读取 `bsp_battery_soc()`(需要时用 `bsp_battery_mv()`),以小型电池图标或百分比呈现在屏幕右上区域;读值为 `-1`(不可用)时优雅降级,不要画一个数字。**位置不能和右上角已有的白云装饰冲突**(`add_cloud`,约 `x≈188, y≈8`):放在白云旁边或下方的空闲蓝天区,或屏幕最右上边角,不要盖住白云。 @@ -18,3 +18,45 @@ - **缓存组件**:若引入缓存组件(如 NVS 缓存、内存缓存等),默认设置过期时间;长期缓存需说明保留原因、过期风险与清理机制。当前固件不含需要 TTL 约束的缓存,无需特别处理。 - **资源约束**:ESP32-C3 无 PSRAM,不增加 LVGL buffer / 音频分配 / 任务栈前先核对内部 RAM;足够的总空闲堆不保证有足够大连续块。 - **关注功耗**:本设备是小电池供电的可穿戴产品,编写固件时要留意功耗。避免让屏幕长时间点亮:屏幕空闲时调暗或关闭背光,并及时回到低功耗状态(light/deep sleep),不要让它无事可做地一直亮着。睡眠相关指引见 [`../hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.md`](../../hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.zh_CN.md)。 + +## 中文字体与缺字排查 + +配置项、转换命令、CMake 接入、安全 fallback 代码和缺字检查示例见 [LVGL 中文显示详细指南](lvgl-chinese-fonts.zh_CN.md)。 + +基线在 [`sdkconfig.defaults`](../../../sdkconfig.defaults) 中启用 Montserrat 14 和 20,两者都不包含中文字形。因此,即使字符串是正确的 UTF-8,中文仍可能显示为方框或空白。串口能打印中文、电脑安装了中文字体、固件编译成功,都不能证明 LVGL 能显示这些文字。 + +### 开发或修改中文 UI 前 + +1. **明确字符范围。** 收集所有显示文案,包括标题、按钮、提示、空状态/错误状态、单位、标点和动态内容。所选字体或 fallback 链必须覆盖实际使用的英文字母、数字、全角标点以及 `LV_SYMBOL_*` 图标。文案变化时,重新生成受影响的各字号/子集。内置 CJK 子集或从固定 UI 文案提取的子集不保证覆盖任意中文姓名、Wi-Fi SSID、服务器/用户输入;必须声明支持的字符范围和明确的超范围字符处理策略。 +2. **生成可复现的素材。** 选用授权允许且源字库确实包含所需字形的字体。优先按应用实际字号生成子集,不直接塞入整套字体家族。可复用源文件/生成文件放在 `assets/fonts/`,并在[素材文档](../../../assets/README.zh_CN.md)记录授权、转换工具版本、命令、字符清单、字号和位深。生成结果须兼容 [`dependencies.lock`](../../../dependencies.lock) 锁定的 LVGL 版本(当前为 9.5.0)。生成选项见[官方字体转换工具](https://github.com/lvgl/lv_font_conv);本基线不自带应用专用中文字库。 +3. **编译并实际选用字体。** 把生成的 `.c` 加入使用方组件的 CMake `SRCS`,用 `LV_FONT_DECLARE` 声明导出的字体符号,再绑定到实际绘制文字的控件/部件。只把 TTF/OTF 或 `.c` 复制进 `assets/`,不会自动完成编译、加载或选用。检查生成的 `sdkconfig` 中 `CONFIG_LV_TXT_ENC_UTF8=y`;仅修改默认值不一定更新已有配置(见[构建与验证](build-and-test.zh_CN.md))。 +4. **检查样式覆盖与 fallback。** 修改默认字体或 screen 上可继承的字体,并不会覆盖子控件显式指定的字体。[`main/ui_pixel.c`](../../../main/ui_pixel.c) 的 `ui_pixel_label()` 会设置局部字体,标题明确使用 Montserrat 20;菜单/demo 标签也有直接选用字体的代码。逐项检查标题、标签、控件部件及焦点/按下/禁用状态。混排可使用覆盖全部所需字形的字体,或明确配置 fallback 链。不得通过强制去掉 `const` 来修改 Flash 中的字体描述符,不得形成 fallback 环路,也不得在控件仍使用字体时释放它。 +5. **评估资源。** 生成后检查 Flash/静态 RAM,运行时检查 free heap/largest free block,并包含网络同时启用的场景。常量位图字体数据可以驻留 Flash;动态加载、解码和缓存仍可能消耗 RAM。不要为解决缺字而盲目扩大 LVGL 内存池或显示缓冲;更多内存不会生成字库中不存在的字形。 + +例如,生成并链接包含所需字形的 `app_font_20` 后,显式选用它。此符号仅作示例,不是仓库已经提供的字体。对象操作必须在 LVGL 任务中执行,或持有 `bsp_lvgl_lock()`: + +```c +LV_FONT_DECLARE(app_font_20); + +/* Inside the application's UI creation function. */ +lv_obj_t *label = lv_label_create(parent); +lv_obj_set_style_text_font(label, &app_font_20, LV_PART_MAIN | LV_STATE_DEFAULT); +lv_label_set_text(label, "\u4E2D\u6587 ABC 123"); +``` + +### 排查顺序 + +| 现象 | 优先检查 | +| --- | --- | +| 英文正常,中文空白或方框 | 控件实际字体及 fallback 覆盖、UTF-8 输入和 `CONFIG_LV_TXT_ENC_UTF8`;关闭 `CONFIG_LV_USE_FONT_PLACEHOLDER` 只会隐藏缺字提示,不会修复缺字 | +| 只有部分字符或新文案异常 | 子集漏字、标点、各字号字库及超出支持范围的动态内容;重新生成并链接受影响的素材 | +| 正文正常,标题/按钮或选中状态异常 | 局部显式字体、主题/状态覆盖,以及真正绘制文字的控件部件所用字体 | +| 字形覆盖正确,但文字不可见或被截断 | 文字颜色/透明度、隐藏标志、标签尺寸、行高、父容器裁剪及绘制/内存分配错误;不能把所有空白标签都认定为字库问题 | + +### 必须完成的验收 + +- 对实际使用的每个字体/fallback 链检查全部所需可打印 Unicode 码点。在当前锁定的 LVGL API 中,`lv_font_get_glyph_dsc()` 必须成功且 `is_placeholder` 为 false;占位字形不算覆盖。应先将 UTF-8 解码为码点,不能逐字节检查。记录未覆盖的码点,并在应用的字体生成流程中加入可重复执行的覆盖检查。 +- 真机检查所有页面、错误/空状态,中英数/标点/图标混排,不同字号和交互状态、长文本,以及支持范围内外的动态内容。确认无方框、空白、裁剪和 fallback 基线/行高错位;不能只凭一个测试标签显示成功就认定整个应用通过。 +- 交付时记录测试文案/字符范围、字号与资源变化。基线 host tests 不会验证衍生应用的字库覆盖或屏幕输出。没有进行真机检查时,报告 `Device tests: NOT RUN`,并在 `Unverified` 中明确列出中文显示。 + +字体选择与 fallback 语义见 [LVGL 9.5 字体概览](https://docs.lvgl.io/9.5/main-modules/fonts/overview.html),缺字处理见 [LVGL 9.5 实现](https://github.com/lvgl/lvgl/blob/v9.5.0/src/font/lv_font.c)。 diff --git a/docs/development/engineering/firmware-layout.md b/docs/development/engineering/firmware-layout.md index adf6ce0..ad07ef6 100644 --- a/docs/development/engineering/firmware-layout.md +++ b/docs/development/engineering/firmware-layout.md @@ -50,8 +50,24 @@ table. ## Flashing and stored data +> **No backup of the firmware already installed on the device is required +> before downloading (flashing) new firmware.** Do not make reading out the +> original firmware or saving a full-Flash dump a prerequisite for this +> workflow. The new firmware replaces the original firmware; this workflow +> does not retain an automatic rollback copy or promise that the original +> firmware can be restored. + +Firmware and user data are different. If existing NVS settings, application +records, or files must be kept, export or otherwise save them before flashing +using a method supported by that application. Not requiring an original-firmware +backup does not guarantee data preservation or authorize a full-chip erase. + The verified merged image is written from `0x0`. Because the merged file pads the gaps between images, flashing it can reset the NVS and PHY data regions. Use the merged image for blank-device provisioning or an intentional complete refresh. During normal development, use segmented `idf.py flash` when existing -NVS state should be preserved. `idf.py erase-flash` erases all user data. +NVS state should be preserved; this also requires a compatible partition layout +and flash targets that do not overwrite those data regions. `idf.py erase-flash` +erases all user data. Do not add it as a routine prerequisite: use it only when +a complete erase is explicitly intended and any data that must be kept has +been saved. diff --git a/docs/development/engineering/firmware-layout.zh_CN.md b/docs/development/engineering/firmware-layout.zh_CN.md index f4da39c..7cb7071 100644 --- a/docs/development/engineering/firmware-layout.zh_CN.md +++ b/docs/development/engineering/firmware-layout.zh_CN.md @@ -43,7 +43,17 @@ ESP32-C3。默认布局不预留产品专用身份、OTA 或未使用的数据 ## 烧录与已存数据 +> **向设备下载(烧录)新固件前,无需备份设备内部原有固件。** 本流程不要求 +> 先读出原固件,也不把保存整片 Flash 镜像作为前置条件。新固件会覆盖原固件; +> 本流程不会保留可自动回滚的副本,也不承诺能够恢复原固件。 + +固件本体与用户数据不同。如果需要保留已有 NVS 设置、应用记录或文件,应在 +烧录前通过原应用支持的方式导出或另行保存。无需备份原固件,不代表用户数据 +一定保留,也不代表可以默认执行全片擦除。 + 验证通过的合并镜像从 `0x0` 写入。合并文件会对各镜像之间的空隙做填充,因此 烧录时可能重置 NVS 与 PHY data 区域。空白设备初始化或有意完整刷新时使用合并 -镜像;日常开发若要保留已有 NVS 状态,应使用分段 `idf.py flash`。 -`idf.py erase-flash` 会擦除全部用户数据。 +镜像;日常开发若要保留已有 NVS 状态,应使用分段 `idf.py flash`,并且要求分区 +布局兼容、烧录目标不会覆盖这些数据区域。`idf.py erase-flash` 会擦除全部用户 +数据,不应作为常规前置步骤;仅在明确需要全片擦除且已保存需要保留的数据时 +执行。 diff --git a/docs/development/engineering/lvgl-chinese-fonts.md b/docs/development/engineering/lvgl-chinese-fonts.md new file mode 100644 index 0000000..9e4b613 --- /dev/null +++ b/docs/development/engineering/lvgl-chinese-fonts.md @@ -0,0 +1,164 @@ +

+ 简体中文 · English +

+ +# LVGL Chinese Text: Integration and Troubleshooting + +Use this guide when adding Chinese text, changing fonts/sizes, or displaying user/network content. The [coding checklist](coding-conventions.md#chinese-fonts-and-missing-glyphs) defines acceptance; this page provides the integration procedure for the LVGL 9.5.0 version pinned in this repository. These are application examples, not changes already enabled in the baseline. + +## 1. Keep encoding, coverage, and style selection separate + +Correct rendering requires all of the following: + +| Layer | Required condition | Typical failure | +| --- | --- | --- | +| Text input | Valid UTF-8, with no truncated multibyte characters | Corrupted text even with the correct font | +| Font asset | Each requested Unicode code point exists in the font or fallback chain | Chinese is blank/boxed while Latin text works | +| Build integration | Font source is compiled, linked, and enabled | Undefined font symbol or the intended font never being used | +| Widget style | The text-drawing part actually selects that font in the active state | Only titles, popups, or selected items fail | +| Layout/rendering | Visible color/opacity, sufficient space, valid font lifetime and draw memory | Clipped or invisible text despite correct coverage | + +Montserrat 14/20 in the default configuration has no Chinese glyphs. UTF-8 only describes how to decode text; it does not install a font. Source Han Sans SC built into this LVGL version is also a subset, not the complete Chinese character set. The font asset and widget style must both be checked; see the [LVGL font overview](https://docs.lvgl.io/9.5/main-modules/fonts/overview.html). + +## 2. Verify the configuration actually used by the application + +In `idf.py menuconfig`, search for the following symbols and check the generated `sdkconfig`: + +```text +CONFIG_LV_TXT_ENC_UTF8=y +CONFIG_LV_USE_FONT_PLACEHOLDER=y +``` + +Keep placeholders enabled while developing so missing glyphs remain visible. Disabling them can turn missing text into blanks; it does not repair coverage. Do not edit generated headers or files under `managed_components/` to force these settings. + +Save source files and text resources as UTF-8. Decode JSON escapes before display, and convert any legacy-encoded input at the application boundary. Do not truncate UTF-8 by an arbitrary byte count; leave space for the terminating NUL and cut only at a character boundary. `lv_label_set_text()` copies its string, while `lv_label_set_text_static()` requires the supplied buffer to remain valid; a stack-local buffer must not outlive its scope through the latter API. + +For local builds, inspect the generated root `sdkconfig`, not just `sdkconfig.defaults`. The repository's firmware gate instead generates an isolated configuration from tracked defaults. Persist intentional settings in your application's tracked defaults and verify both configurations as described in [build and test](build-and-test.md). + +## 3. Option A: validate a built-in CJK subset + +For a small initial experiment, enable this font in your application's configuration: + +```text +CONFIG_LV_FONT_SOURCE_HAN_SANS_SC_16_CJK=y +``` + +Rebuild, then explicitly select it on a label. The following function is called only after LVGL/display initialization, in the LVGL task or while holding `bsp_lvgl_lock()`. Supply a visible parent with adequate space: + +```c +#include "lvgl.h" + +lv_obj_t *app_create_cjk_probe(lv_obj_t *parent) +{ + lv_obj_t *label = lv_label_create(parent); + lv_obj_set_style_text_font(label, &lv_font_source_han_sans_sc_16_cjk, + LV_PART_MAIN | LV_STATE_DEFAULT); + lv_label_set_text(label, "\u4E2D\u6587 ABC 123"); + return label; +} +``` + +The escapes identify U+4E2D and U+6587 and keep the example identical in both document languages. Valid UTF-8 source literals work too. This probe tests a few characters only. Before choosing this font for a product, check every actual label, punctuation mark, name, and supported input. Enabling the font does not select it globally; setting a theme/default font still does not replace explicit fonts in existing widgets. + +## 4. Option B: generate a reproducible application subset + +Prepare a licensed TTF/OTF font that contains the required glyphs. Store reusable materials under `assets/fonts/` and record their source/license in the [assets README](../../../assets/README.md). Install a pinned version of the [official converter](https://github.com/lvgl/lv_font_conv), record its version, and confirm the options with `lv_font_conv --help`. + +Run from the repository root, after creating the asset directory and replacing the example input path with your font: + +```bash +lv_font_conv \ + --font /path/to/licensed-cjk-font.otf \ + --range 0x20-0x7E,0x4E2D,0x6587,0xFF0C,0x3002 \ + --size 20 --bpp 4 --format lvgl --no-compress \ + --lv-font-name app_font_20 --lv-include lvgl.h \ + --output assets/fonts/app_font_20.c +``` + +This example requests printable ASCII, two Chinese characters, a full-width comma, and an ideographic full stop. It is not a production character inventory. Replace the range with every required code point, or use the converter's `--symbols` input with your real text. An absent glyph in the source font cannot be created by requesting its code point. Check conversion diagnostics and the generated coverage rather than assuming success means completeness. + +Keep the character inventory and conversion command under version control. Generate each required size separately, using distinct names such as `app_font_16` and `app_font_20`. Start uncompressed to avoid a decoder/configuration mismatch; if compression is introduced, enable `CONFIG_LV_USE_FONT_COMPRESSED=y` and measure its rendering and memory impact. Avoid enabling every size or a full CJK family merely to fix one missing character. + +### Compile and declare the generated source + +For this repository's `main` component, append this after the existing `idf_component_register(...)` in `main/CMakeLists.txt`; do not replace its source or dependency lists: + +```cmake +target_sources(${COMPONENT_LIB} PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/../assets/fonts/app_font_20.c" +) +``` + +Alternatively, add that source to the component's existing `SRCS` list. Use only one method: do not compile the same font twice or `#include` its `.c` in another source. Include `lvgl.h` and use `LV_FONT_DECLARE(app_font_20)` in the application file or shared font header. The declaration must match the generated symbol; it does not define or link the font by itself. A separate font component must declare its LVGL dependency. + +## 5. Bind text and icons without modifying read-only fonts + +If `app_font_20` covers Chinese and ASCII but lacks the application's LVGL icons, it can fall back to the already enabled Montserrat 20. For these static generated fonts, keep a writable descriptor with application lifetime and initialize it once before any widget uses it: + +```c +#include "lvgl.h" + +LV_FONT_DECLARE(app_font_20); +static lv_font_t s_app_font_with_symbols; + +void app_fonts_init(void) +{ + s_app_font_with_symbols = app_font_20; + s_app_font_with_symbols.fallback = &lv_font_montserrat_20; +} + +lv_obj_t *app_create_font_probe(lv_obj_t *parent) +{ + lv_obj_t *label = lv_label_create(parent); + lv_obj_set_style_text_font(label, &s_app_font_with_symbols, + LV_PART_MAIN | LV_STATE_DEFAULT); + lv_label_set_text(label, "\u4E2D\u6587\uFF0C ABC 123\u3002 " LV_SYMBOL_OK); + return label; +} +``` + +Call `app_fonts_init()` first, after LVGL initialization and before creating labels. Run UI operations in the LVGL task or under `bsp_lvgl_lock()`. This shallow descriptor copy is for the static bitmap font in this example, not a general cloning method for dynamically loaded font engines. Keep the descriptor and referenced data alive for all users; never cast away `const` on an existing font or create a fallback loop. + +Fallback is directional: here the application font is tried first and Montserrat supplies only missing glyphs. It cannot supply additional Chinese characters absent from both fonts. Equal nominal sizes do not guarantee matching baselines or line heights; inspect mixed lines and reserve enough height, or regenerate compatible metrics. For Latin-first typography, deliberately reverse the primary/fallback choice using an application-owned descriptor and recheck metrics and coverage. + +Audit actual widget bindings, not just the screen/default font. `ui_pixel_label()` sets local fonts, `ui_pixel_screen_create()` uses Montserrat 20 for its title, and menu/demo labels select Montserrat explicitly. New applications should implement their own UI/font helpers. For compound widgets, inspect the part that draws text and any separately created popup list; check focused, pressed, checked, and disabled states too. + +## 6. Prove glyph coverage and identify the active font + +This helper checks a Unicode code point, not a UTF-8 byte or a multicharacter C literal: + +```c +#include "lvgl.h" + +bool app_font_has_glyph(const lv_font_t *font, uint32_t codepoint) +{ + if (font == NULL) return false; + lv_font_glyph_dsc_t glyph = {0}; + return lv_font_get_glyph_dsc(font, &glyph, codepoint, 0) + && !glyph.is_placeholder; +} +``` + +For an existing label, get `lv_obj_get_style_text_font(label, LV_PART_MAIN)` under the LVGL lock, then check that font rather than an unrelated global symbol. This getter resolves the current state; repeat after each relevant state transition. A positive result checks descriptors/fallback coverage, not successful bitmap decoding or final screen output. + +Decode all fixed text into Unicode code points and check the ones required by each size/style; omit control characters such as newline from printable-glyph checks. Log missing values as `U+XXXX`. For the generated subset above, U+4E2D/U+6587/U+FF0C/U+3002 should pass if the source provides them; U+9F98 is intentionally absent and should fail with the shown Montserrat fallback. Include a known-missing negative case so the coverage test cannot pass unconditionally. + +Dynamic content needs an explicit contract: accept a documented character set, add verified glyph coverage, or show a deliberate supported replacement/unsupported-text message. Do not silently drop unknown characters or claim that a fixed menu subset supports arbitrary names and network text. When the supported inventory changes, regenerate fonts and rerun coverage checks. + +## 7. Diagnose failures in order + +| Symptom | Next action | +| --- | --- | +| Undefined reference to a font | Check symbol spelling, CMake source registration, font enable macro, and a C-compatible declaration when consumed by C++ | +| Font file exists but nothing changes | Check the actual selected font; copying assets or declaring a symbol is insufficient | +| ASCII works, Chinese is blank/boxed | Check encoding, per-code-point coverage, and the actual fallback chain; do not disable placeholders | +| Body text works, title/popup fails | Inspect local fonts, widget parts, separately created children/popups, and state overrides | +| Glyph check passes but text is blank | Check the bitmap format/decoder, font lifetime, text color/opacity, hidden flags, and LVGL allocation errors | +| Characters overlap or lose their top/bottom | Check line height, baseline, parent clipping, label width/height, and fallback metrics | +| New strings fail after an otherwise successful build | Regenerate all affected subsets/sizes, inspect the actual configuration, and confirm the device is running the intended build | + +## 8. Validate without overstating the result + +Run the [complete repository gate](build-and-test.md) and your application's character-coverage tests. Inspect `idf.py size-components`/`idf.py size-files` for Flash/static RAM, and record runtime free heap and largest free block with the real UI and networking active. A font compiled into read-only Flash is not equivalent to allocating the whole font on the heap, but drawing/loading/caching can still require internal RAM. + +On the device, inspect every page and state, mixed text/icons, long lines, different sizes, and dynamic input at both sides of the supported-set boundary. Test after page recreation and restart, not just the first label. Record the firmware version, font/converter versions, tested character inventory, screenshots where useful, and any missing characters or layout defects. Without this check, Chinese rendering remains `Unverified`; neither the baseline host tests nor a successful firmware build verifies the examples' visual output. diff --git a/docs/development/engineering/lvgl-chinese-fonts.zh_CN.md b/docs/development/engineering/lvgl-chinese-fonts.zh_CN.md new file mode 100644 index 0000000..fb1099a --- /dev/null +++ b/docs/development/engineering/lvgl-chinese-fonts.zh_CN.md @@ -0,0 +1,164 @@ +

+ 简体中文 · English +

+ +# LVGL 中文显示:接入与排查 + +添加中文文案、修改字体/字号或显示用户/网络内容时使用本指南。[代码检查清单](coding-conventions.zh_CN.md#中文字体与缺字排查)定义验收要求,本文提供当前仓库锁定的 LVGL 9.5.0 的具体接入步骤。以下都是应用开发示例,不代表基线已经启用了这些配置。 + +## 1. 区分编码、字形覆盖和样式选择 + +正确显示需要同时满足以下条件: + +| 层次 | 必须满足 | 常见错误表现 | +| --- | --- | --- | +| 文本输入 | 有效 UTF-8,不能截断多字节字符 | 即使字体正确也显示乱码 | +| 字体素材 | 每个所需 Unicode 码点都在字体或 fallback 链中 | 英文正常,中文空白/方框 | +| 编译接入 | 字体源文件已编译、链接并启用 | 字体符号未定义,或预期字体根本没用上 | +| 控件样式 | 当前状态下,实际绘制文字的部件选用了该字体 | 只有标题、弹窗或选中项异常 | +| 布局/绘制 | 颜色/透明度可见、空间足够,字体生命周期和绘制内存有效 | 字形存在,但文字被裁剪或不可见 | + +默认配置中的 Montserrat 14/20 不含中文字形。UTF-8 只规定怎样解码文本,不会安装字库。此版本 LVGL 内置的 Source Han Sans SC 也只是子集,不是完整中文字库。必须同时核对字体素材和控件样式;参见 [LVGL 字体概览](https://docs.lvgl.io/9.5/main-modules/fonts/overview.html)。 + +## 2. 核对应用实际使用的配置 + +在 `idf.py menuconfig` 中搜索以下符号,并检查生成的 `sdkconfig`: + +```text +CONFIG_LV_TXT_ENC_UTF8=y +CONFIG_LV_USE_FONT_PLACEHOLDER=y +``` + +开发时保留缺字占位,让问题可见。关闭占位可能把缺字变成空白,并没有补齐字形。不要修改生成的头文件或 `managed_components/` 内的文件来强行覆盖这些配置。 + +源码和文本资源保存为 UTF-8。JSON 转义应先解码再显示,旧编码输入应在应用边界转换。不要按任意字节数截断 UTF-8;既要给结尾 NUL 留空间,也要确保截断位置在完整字符边界。`lv_label_set_text()` 会复制字符串,`lv_label_set_text_static()` 则要求传入缓冲持续有效,不能让它在栈局部缓冲离开作用域后继续使用该地址。 + +本地构建检查生成的根目录 `sdkconfig`,不能只看 `sdkconfig.defaults`。仓库固件门禁会从已跟踪的 defaults 生成隔离配置。把应用有意使用的设置写入该应用已跟踪的 defaults,并按[构建与验证](build-and-test.zh_CN.md)核对两种配置。 + +## 3. 方案 A:用内置 CJK 子集快速验证 + +小范围初步实验可以在应用配置中启用: + +```text +CONFIG_LV_FONT_SOURCE_HAN_SANS_SC_16_CJK=y +``` + +重新编译,然后在标签上显式指定字体。以下函数只在 LVGL/显示初始化后调用,必须处于 LVGL 任务或持有 `bsp_lvgl_lock()`;传入可见且有足够空间的父对象: + +```c +#include "lvgl.h" + +lv_obj_t *app_create_cjk_probe(lv_obj_t *parent) +{ + lv_obj_t *label = lv_label_create(parent); + lv_obj_set_style_text_font(label, &lv_font_source_han_sans_sc_16_cjk, + LV_PART_MAIN | LV_STATE_DEFAULT); + lv_label_set_text(label, "\u4E2D\u6587 ABC 123"); + return label; +} +``` + +转义表示 U+4E2D、U+6587,即“中文”,用来保持中英文文档示例一致;合法 UTF-8 源码也可直接写中文。这个探针只测试少量字符。用于产品前,必须检查实际文案、标点、姓名和支持的输入。启用字体不等于全局选用它;设置主题/默认字体也不会替换现有控件显式指定的字体。 + +## 4. 方案 B:生成可复现的应用子集 + +准备授权允许且覆盖所需字形的 TTF/OTF 字体。可复用素材放在 `assets/fonts/`,来源/授权记录在[素材 README](../../../assets/README.zh_CN.md)。安装固定版本的[官方字体转换工具](https://github.com/lvgl/lv_font_conv),记录版本,并通过 `lv_font_conv --help` 核对参数。 + +先创建素材目录,把输入路径换成实际字体,再从仓库根目录运行: + +```bash +lv_font_conv \ + --font /path/to/licensed-cjk-font.otf \ + --range 0x20-0x7E,0x4E2D,0x6587,0xFF0C,0x3002 \ + --size 20 --bpp 4 --format lvgl --no-compress \ + --lv-font-name app_font_20 --lv-include lvgl.h \ + --output assets/fonts/app_font_20.c +``` + +示例只请求可打印 ASCII、“中文”、全角逗号和中文句号,不是正式应用的字符清单。请把范围替换为全部所需码点,或使用转换器的 `--symbols` 输入实际文案。源字体没有的字形,不能靠填写码点生成;必须检查转换提示及生成结果的覆盖范围,不能把命令成功等同于字形齐全。 + +将字符清单和转换命令纳入版本管理。每个实际字号分别生成,使用 `app_font_16`、`app_font_20` 等不同名称。首次接入先不压缩,避免解码器/配置不匹配;后续若使用压缩字体,启用 `CONFIG_LV_USE_FONT_COMPRESSED=y` 并测量绘制与内存影响。不要为了补一个字就启用所有字号或整套 CJK 字体家族。 + +### 编译并声明生成的源文件 + +对于当前仓库的 `main` 组件,在 `main/CMakeLists.txt` 现有 `idf_component_register(...)` 之后追加以下内容,不要替换原有源文件或依赖列表: + +```cmake +target_sources(${COMPONENT_LIB} PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/../assets/fonts/app_font_20.c" +) +``` + +也可以把源文件加入组件已有的 `SRCS` 列表,两种方式选一种即可。不要重复编译同一字体,也不要在其他源码里 `#include` 字体 `.c`。在应用源文件或共享字体头文件中包含 `lvgl.h` 并使用 `LV_FONT_DECLARE(app_font_20)`;声明必须匹配生成的符号,声明本身不会定义或链接字体。若独立建立字体组件,该组件必须声明 LVGL 依赖。 + +## 5. 绑定文字与图标,不修改只读字体 + +若 `app_font_20` 包含中文和 ASCII,但没有应用使用的 LVGL 图标,可以 fallback 到默认已经启用的 Montserrat 20。对于这里的静态生成字体,保留一个与应用同寿命的可写描述符,并在任何控件使用它之前初始化一次: + +```c +#include "lvgl.h" + +LV_FONT_DECLARE(app_font_20); +static lv_font_t s_app_font_with_symbols; + +void app_fonts_init(void) +{ + s_app_font_with_symbols = app_font_20; + s_app_font_with_symbols.fallback = &lv_font_montserrat_20; +} + +lv_obj_t *app_create_font_probe(lv_obj_t *parent) +{ + lv_obj_t *label = lv_label_create(parent); + lv_obj_set_style_text_font(label, &s_app_font_with_symbols, + LV_PART_MAIN | LV_STATE_DEFAULT); + lv_label_set_text(label, "\u4E2D\u6587\uFF0C ABC 123\u3002 " LV_SYMBOL_OK); + return label; +} +``` + +先完成 LVGL 初始化,再调用 `app_fonts_init()`,最后创建标签。UI 操作仍须在 LVGL 任务或 `bsp_lvgl_lock()` 内执行。此处描述符浅拷贝只针对示例的静态位图字体,不是动态加载字体引擎的通用复制方式。描述符及引用的数据必须覆盖全部使用期;不得强制去掉原字体的 `const`,也不得形成 fallback 环路。 + +Fallback 有方向:这里先查应用字体,仅在缺字时使用 Montserrat。两边都没有的中文仍然无法显示。标称字号相同不保证基线、行高相同;必须检查混排行并留足高度,必要时重新生成度量兼容的字体。若希望英文优先使用另一种字体,应通过应用持有的描述符明确调整主字体/fallback 方向,并重新核对度量与覆盖。 + +检查实际控件绑定,不能只看 screen/默认字体。`ui_pixel_label()` 设置局部字体,`ui_pixel_screen_create()` 的标题使用 Montserrat 20,菜单/demo 标签也有显式 Montserrat 配置。新应用应实现自己的 UI/字体辅助函数。复合控件需检查真正绘制文字的部件,以及单独创建的弹出列表;焦点、按下、选中和禁用状态都要检查。 + +## 6. 验证字形覆盖,查明当前字体 + +以下辅助函数检查 Unicode 码点,而不是 UTF-8 单字节或 C 的多字符字符常量: + +```c +#include "lvgl.h" + +bool app_font_has_glyph(const lv_font_t *font, uint32_t codepoint) +{ + if (font == NULL) return false; + lv_font_glyph_dsc_t glyph = {0}; + return lv_font_get_glyph_dsc(font, &glyph, codepoint, 0) + && !glyph.is_placeholder; +} +``` + +对于已有标签,在 LVGL 锁内调用 `lv_obj_get_style_text_font(label, LV_PART_MAIN)`,对获取的实际字体进行检查,而不是验证一个无关的全局字体符号。这个 getter 解析当前状态,各相关状态切换后都要重复检查。返回成功只说明描述符/fallback 字形覆盖,不证明位图解码和屏幕输出成功。 + +把所有固定文案解码为 Unicode 码点,按各字号/样式真正需要的字符检查;换行等控制字符不纳入可打印字形检查。缺字记录为 `U+XXXX`。对于上述生成子集,若源字体包含所请求字形,U+4E2D/U+6587/U+FF0C/U+3002 应通过;U+9F98 刻意未收录,使用示例中的 Montserrat fallback 时应失败。加入已知缺字的反例,避免覆盖测试无条件通过。 + +动态内容必须有明确约定:只接受已声明字符集、补齐经过验证的字形,或显示有意设计且能正确显示的替代字符/不支持提示。不要悄悄丢弃未知字符,也不要把固定菜单子集描述成支持任意姓名和网络文本。支持范围变化后,重新生成字体并运行覆盖检查。 + +## 7. 按顺序定位故障 + +| 现象 | 下一步 | +| --- | --- | +| 字体 undefined reference | 检查符号拼写、CMake 源文件注册、字体启用宏,以及 C++ 使用时是否有兼容 C 的声明 | +| 字体文件存在但界面没变化 | 检查实际选中的字体;复制素材或声明符号都不够 | +| ASCII 正常,中文空白/方框 | 检查编码、逐码点覆盖和实际 fallback 链;不要关闭占位 | +| 正文正常,标题/弹窗异常 | 检查局部字体、控件部件、单独创建的子对象/弹窗及状态覆盖 | +| 字形检查通过但仍空白 | 检查位图格式/解码器、字体生命周期、文字颜色/透明度、隐藏标志和 LVGL 分配错误 | +| 字符重叠或上下被切掉 | 检查行高、基线、父容器裁剪、标签宽高和 fallback 度量 | +| 编译成功但新文案异常 | 重新生成受影响的所有子集/字号,检查实际配置,并确认设备运行的是目标构建 | + +## 8. 验证与交付边界 + +运行[完整仓库门禁](build-and-test.zh_CN.md)和应用自己的字符覆盖测试。用 `idf.py size-components`/`idf.py size-files` 检查 Flash/静态 RAM,同时记录真实 UI 和网络启用时的 free heap、largest free block。只读 Flash 中的字体并不等于把整个字库分配到堆中,但绘制/加载/缓存仍可能需要内部 RAM。 + +真机逐页逐状态检查混排/图标、长文本、不同字号和支持范围内外的动态输入。页面重建、重启后也要测,不能只看第一个标签。记录固件版本、字体/转换器版本、测试字符清单,必要时留截图,列出任何缺字与排版问题。未完成这些检查时,中文显示仍属于 `Unverified`;基线 host tests 或固件编译通过都不能验证这些示例的实际显示效果。