| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- /*
- @module hzfont
- @summary HzFont字体库
- @version 1.0
- @date 2025.10.16
- @tag LUAT_USE_HZFONT
- @usage
- -- 使用HzFont渲染TTF字体
- -- 需要准备TTF字体文件
- -- 初始化字体
- hzfont.init("/sd/font.ttf")
- */
- #include "luat_base.h"
- #include "luat_hzfont.h"
- #include "ttf_parser.h"
- #include "luat_lcd.h"
- #define LUAT_LOG_TAG "hzfont"
- #include "luat_log.h"
- #include "rotable2.h"
- #include "luat_conf_bsp.h"
- /**
- 初始化HzFont字体库
- @api hzfont.init([ttf_path][, cache_size][, load_to_psram])
- @string ttf_path TTF字体文件路径,可选;留空则回退到内置字库(若启用)
- @int cache_size 可选,位图与码点缓存容量(支持常量 HZFONT_CACHE_128/256/512/1024/2048),默认 HZFONT_CACHE_256
- @bool load_to_psram 可选,true 时将字库整包拷贝到 PSRAM 后再解析,减少后续 IO
- @return boolean 成功返回true,失败返回false
- @usage
- -- 从文件加载,使用默认缓存 256
- hzfont.init("/sd/font.ttf")
- -- 从文件加载,指定缓存 1024
- hzfont.init("/sd/font.ttf", hzfont.HZFONT_CACHE_1024)
- -- 从luadb文件系统加载
- hzfont.init("/luadb/font.ttf")
- -- 回退内置字库(启用 固件配置项 LUAT_CONF_USE_HZFONT_BUILTIN_TTF 时生效)
- hzfont.init()
- */
- static int l_hzfont_init(lua_State* L) {
- const char* ttf_path = NULL;
- size_t len = 0;
- if (!lua_isnoneornil(L, 1)) {
- ttf_path = luaL_checklstring(L, 1, &len);
- if (len == 0) {
- ttf_path = NULL;
- }
- }
- uint32_t cache_size = 0;
- if (!lua_isnoneornil(L, 2)) {
- cache_size = (uint32_t)luaL_checkinteger(L, 2);
- }
- int load_to_psram = 0;
- if (!lua_isnoneornil(L, 3)) {
- load_to_psram = lua_toboolean(L, 3);
- }
- int result = luat_hzfont_init(ttf_path, cache_size, load_to_psram);
- lua_pushboolean(L, result);
- return 1;
- }
- /* drawUtf8 已移至 lcd 模块的 lcd.drawHzfontUtf8 */
- /**
- 调试开关
- @api hzfont.debug(enable)
- @boolean enable true 开启,false 关闭
- @return boolean 总是返回true
- */
- static int l_hzfont_debug(lua_State* L) {
- int enable = lua_toboolean(L, 1);
- (void)ttf_set_debug(enable);
- lua_pushboolean(L, 1);
- return 1;
- }
- static const rotable_Reg_t reg_hzfont[] = {
- { "init", ROREG_FUNC(l_hzfont_init)},
- { "debug", ROREG_FUNC(l_hzfont_debug)},
- { "HZFONT_CACHE_128", ROREG_INT(128)},
- { "HZFONT_CACHE_256", ROREG_INT(256)},
- { "HZFONT_CACHE_512", ROREG_INT(512)},
- { "HZFONT_CACHE_1024", ROREG_INT(1024)},
- { "HZFONT_CACHE_2048", ROREG_INT(2048)},
- { NULL, ROREG_INT(0)}
- };
- LUAMOD_API int luaopen_hzfont(lua_State *L) {
- luat_newlib2(L, reg_hzfont);
- return 1;
- }
|