Files
MeowOS-For-R3P2/patch_v2/sdk/test_pinyin.c
T
AskaEth 54857bc0fd sdk(ui): 字符串资源外置(i18n 雏形 §13)—— meow_str_* + 内嵌双语表 + TF 覆盖表 + 控件接入
公开 SDK 前把 SDK/App 硬编码文案外置:新增 meow_str/meow_strf/meow_str_set_locale/
meow_str_locale,按当前 locale 取串(永不 NULL),第三方开发者可做多语言。

- 新增 include/meow_ui_str.h + meow_ui_str.c:取串优先级 = TF 覆盖表 > 内嵌当前语言
  (en_us→en 其余→zh) > 内嵌内置默认(zh_cn) > 原样返回 id。内嵌双语表(common.* 21 条 +
  ime.* 9 条);热路径零 malloc(编译期静态表 + ≤256 静态覆盖数组);纯 libc 宿主可单测。
- locale 存系统设置 settings.conf 的 ui.locale(默认 zh_cn,首次调用惰性读取);
  meow_str_set_locale 即时切换 + 重载覆盖表 + best-effort 持久化(流式改写保留其它行)。
- TF 覆盖表 MeowData/<pkg>/strings/<locale>.txt(id=text 行,UTF-8,'=' 两侧去空白);
  无卡/无文件/无该 id → 回落内嵌。meow_strf 用调用方缓冲(snprintf 语义,重入安全)。
- SDK 控件接入:dialog 默认按钮 OK/CANCEL + INPUT 占位、IME 键面/徽标(空格/确定/拼英/
  ?123/清/拼音/(无候选)/中)改走 meow_str;空态/加载态给 App 提供 common.no_data/loading。
- 测试:新增 test_meow_ui_str.c(内嵌回退/TF 覆盖优先级/未知 id 回退/切 locale 即时生效/
  查找正确性/meow_strf,tmp+env 两模式);全量宿主回归(widgets/dispatch/box/anim/scroll/
  listvirt/theme/ime_fix/pinyin/overlay/notif/sdk_input_keys/p1p2p3/config/vol/sb_collapse/
  notify_*) 0 回归;交叉 zig-mipsel -Wall -Wextra 0 warning。
- 文档:MEOW_APP_SDK.md 新增 §13(字符串资源/i18n,遗留顺延为 §14);docs/APP_DEV_GUIDE.md
  新增 §7.11。各宿主 test_*.c 头注释补 meow_ui_str.c 编译依赖。

边界(§13.7):仅 zh_cn/en_us 整串取用;不做复数规则 / BiDi / RTL / 动态字体回退(雏形)。
2026-09-02 02:37:38 +08:00

126 lines
5.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* test_pinyin.c — 中文拼音 IME 宿主验证(480x720 离屏,直接 handle_tap 确定语义)。
* ① 拼音组合:type "nihao" → 组合串="nihao"、字段=""(组合不上屏、on_change 不触发);
* 点候选(你好)→ 字段="你好"、组合清空、on_change="你好";
* ② 候选翻页:type "shi" → nc>6;点 ▶ 翻页末首候选变化、点 ◀ 回;
* ③ UTF-8 删字段:上屏"你好"后点退格 → 字段="你"(按 codepoint 回退,不出现乱码);
* ④ 中/英切换:拼音点 [拼/英] → ASCII;ASCII 点顶条「中」热区 → 拼音;
* ⑤ 空格/回车上屏首候选:type "ni" + 空格 → 字段="你"。
*
* 编译(宿主 gcc,同 test_ime_fix):
* gcc -Wall -Wextra -o test_pinyin test_pinyin.c test_host_stub.c \
* meow_ui_gfx.c meow_ui_sys.c meow_ui_theme.c meow_ui_vol.c meow_ui_log.c \
* meow_ui_widget.c meow_ui_input.c meow_ui_activity.c meow_ui_dialog.c meow_ui_ime.c meow_ui_str.c \
* meow_pinyin.c meow_ui_draw.c meow_ui_view.c meow_ui_img.c meow_ui_media.c \
* meow_notify_ipc.c stb_image.c -I. -I../meow_launcherd -DMEOW_UI_HOST_TEST -pthread -lm
*/
#include "meow_ui.h"
#include "meow_ui_internal.h"
#include "meow_pinyin.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
/* 拼音键位(build_kb PINYIN:行 y=504/552/600/648,GCOL=48,行左缘 x=2;键 44×36):
* row1 y=504 "qwertyuiop";row2 y=552 "asdfghjkl"+[拼/英]@col9;
* row3 y=600 "zxcvbnm"+[←退格]@col7;row4 y=648 [?123]+[空格]+[OK]。 */
#define KEY_XY(col, y) (((2 + (col) * 48) + 22), ((y) + 18))
static void t(char ch) {
/* 按字符找其键中心坐标(行/列换算) */
int row = 0, col = 0;
const char *rows[3] = { "qwertyuiop", "asdfghjkl", "zxcvbnm" };
for (int r = 0; r < 3; r++) {
const char *p = strchr(rows[r], ch);
if (p) { row = r; col = (int)(p - rows[r]); break; }
}
int x = 2 + col * 48 + 22;
int y = 504 + row * 48 + 18;
meow_ime_handle_tap(x, y);
}
static char g_echo[128] = "";
static void ime_cb(const char *t, void *ctx) { (void)ctx; snprintf(g_echo, sizeof g_echo, "%s", t); }
static void tap(int x, int y) { meow_ime_handle_tap(x, y); }
static meow_ime_ctx g_c;
static void open_py(void) {
memset(&g_c, 0, sizeof g_c);
g_c.mode = MEOW_IME_PINYIN; g_c.maxlen = 64; g_c.on_change = ime_cb;
memset(g_echo, 0, sizeof g_echo);
meow_ui_text_input_async(&g_c);
assert(meow_ime_active() == 1);
}
static void close_py(void) { meow_ime_close(0); assert(meow_ime_active() == 0); }
int main(void) {
setenv("MEOW_UI_DUMP", "/tmp/pinyin.ppm", 1);
meow_ui_opts o = { 480, 720 };
assert(meow_ui_init(&o) == 0);
/* ① 拼音组合 + 点上屏 */
open_py();
t('n'); t('i'); t('h'); t('a'); t('o');
assert(strcmp(meow_ime_composition(), "nihao") == 0);
assert(meow_ime_text()[0] == 0); /* 组合不上屏 */
assert(g_echo[0] == 0); /* 未上屏不回调 */
/* 点候选 0(你好,x=44 起;宿主 stub width=strlen*6) */
tap(60, 480);
assert(strcmp(meow_ime_text(), "你好") == 0);
assert(strcmp(g_echo, "你好") == 0);
assert(meow_ime_composition()[0] == 0); /* 组合已清 */
close_py();
printf("PASS pinyin: nihao -> 你好 (compose+commit+on_change)\n"); fflush(stdout);
/* ② 候选翻页(shi 候选 >6) */
open_py();
t('s'); t('h'); t('i');
char all[32][64];
int n = py_predict("shi", all, 32);
assert(n > 6);
assert(strcmp(all[0], "是") == 0);
/* 点 ▶(下一页)→ 页内首候选应为 all[6](页 0 首候选是 all[0]) */
tap(457, 480); /* ▶:页 0 → 页 1 */
tap(60, 480); /* 点页 1 候选 0(x=44 起) */
assert(strcmp(meow_ime_text(), all[6]) == 0);
close_py();
printf("PASS pagination: shi page_next then commit page1[0] == all[6]\n"); fflush(stdout);
/* ③ UTF-8 删字段:上屏"你好"后点退格 → "你" */
open_py();
t('n'); t('i'); t('h'); t('a'); t('o');
tap(60, 480); /* 上屏"你好" */
assert(strcmp(meow_ime_text(), "你好") == 0);
tap(410, 618); /* ←退格(row3 col7 宽键中心) */
assert(strcmp(meow_ime_text(), "你") == 0);
assert(strcmp(g_echo, "你") == 0);
close_py();
printf("PASS utf8 field backspace: 你好 -> 你\n"); fflush(stdout);
/* ④ 中/英切换 */
open_py();
tap(456, 570); /* [拼/英] row2 col9 → ASCII */
assert(meow_ime_active() == 1);
/* 顶条「中」热区(430..474, KB_TOP..KB_TOP+42)→ 切回拼音;组合已被丢弃 */
tap(452, 438);
assert(meow_ime_active() == 1);
t('n'); t('i');
assert(strcmp(meow_ime_composition(), "ni") == 0);
tap(456, 570); /* 又回 ASCII */
assert(strcmp(meow_ime_composition(), "") == 0); /* 组合态已丢弃 */
close_py();
printf("PASS zh/en switch (pinyin->ascii->pinyin, combo dropped)\n"); fflush(stdout);
/* ⑤ 空格上屏首候选(ni → 你) */
open_py();
t('n'); t('i');
tap(194, 666); /* 空格 */
assert(strcmp(meow_ime_text(), "你") == 0);
assert(strcmp(g_echo, "你") == 0);
close_py();
printf("PASS space commits first candidate: ni -> 你\n"); fflush(stdout);
printf("ALL PASS\n");
meow_ui_shutdown();
return 0;
}