luat_lib_hzfont.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. @module hzfont
  3. @summary HzFont字体库
  4. @version 1.0
  5. @date 2025.10.16
  6. @tag LUAT_USE_HZFONT
  7. @usage
  8. -- 使用HzFont渲染TTF字体
  9. -- 需要准备TTF字体文件
  10. -- 初始化字体
  11. hzfont.init("/sd/font.ttf")
  12. -- 获取字符串宽度
  13. local width = hzfont.getStrWidth("Hello世界", 24)
  14. print("字符串宽度:", width)
  15. lcd.drawfreefontUtf8(10, 50, "Hello世界", 24, 0xFFFFFF)
  16. */
  17. #include "luat_base.h"
  18. #include "luat_hzfont.h"
  19. #include "ttf_parser.h"
  20. #include "luat_lcd.h"
  21. #define LUAT_LOG_TAG "hzfont"
  22. #include "luat_log.h"
  23. #include "rotable2.h"
  24. #include "luat_conf_bsp.h"
  25. /**
  26. 初始化HzFont字体库
  27. @api hzfont.init(ttf_path)
  28. @string ttf_path TTF字体文件路径
  29. @return boolean 成功返回true,失败返回false
  30. @usage
  31. hzfont.init("/sd/font.ttf")
  32. */
  33. static int l_hzfont_init(lua_State* L) {
  34. size_t len = 0;
  35. const char* ttf_path = luaL_checklstring(L, 1, &len);
  36. if (!ttf_path || len == 0) {
  37. LLOGE("TTF path is empty");
  38. lua_pushboolean(L, 0);
  39. return 1;
  40. }
  41. int result = luat_hzfont_init(ttf_path);
  42. lua_pushboolean(L, result);
  43. return 1;
  44. }
  45. /**
  46. 反初始化HzFont字体库
  47. @api hzfont.deinit()
  48. @usage
  49. hzfont.deinit()
  50. */
  51. static int l_hzfont_deinit(lua_State* L) {
  52. (void)L;
  53. luat_hzfont_deinit();
  54. return 0;
  55. }
  56. /**
  57. 获取当前初始化状态
  58. @api hzfont.state()
  59. @return int 状态值:0-未初始化,1-已初始化,2-错误
  60. @usage
  61. local state = hzfont.state()
  62. if state == 1 then
  63. print("HzFont已初始化")
  64. end
  65. */
  66. static int l_hzfont_state(lua_State* L) {
  67. luat_hzfont_state_t state = luat_hzfont_get_state();
  68. lua_pushinteger(L, (int)state);
  69. return 1;
  70. }
  71. /**
  72. 获取UTF-8字符串宽度
  73. @api hzfont.getStrWidth(str, fontSize)
  74. @string str UTF-8字符串
  75. @int fontSize 字体大小(像素)
  76. @return int 字符串宽度(像素)
  77. @usage
  78. local width = hzfont.getStrWidth("Hello世界", 24)
  79. print("字符串宽度:", width)
  80. */
  81. static int l_hzfont_get_str_width(lua_State* L) {
  82. size_t len = 0;
  83. const char* str = luaL_checklstring(L, 1, &len);
  84. int fontSize = luaL_checkinteger(L, 2);
  85. if (fontSize <= 0 || fontSize > 255) {
  86. LLOGE("Invalid font size: %d", fontSize);
  87. lua_pushinteger(L, 0);
  88. return 1;
  89. }
  90. unsigned int width = luat_hzfont_get_str_width(str, (unsigned char)fontSize);
  91. lua_pushinteger(L, width);
  92. return 1;
  93. }
  94. /**
  95. 绘制UTF-8字符串到LCD
  96. @api hzfont.drawUtf8(x, y, str, fontSize, color)
  97. @int x X坐标
  98. @int y Y坐标(左下角为基准)
  99. @string str UTF-8字符串
  100. @int fontSize 字体大小(像素)
  101. @int color 颜色值(RGB565格式)
  102. @return boolean 成功返回true,失败返回false
  103. @usage
  104. -- 绘制白色文本
  105. hzfont.drawUtf8(10, 50, "Hello世界", 24, 0xFFFF)
  106. -- 绘制红色文本
  107. hzfont.drawUtf8(10, 80, "红色文本", 24, 0xF800)
  108. */
  109. static int l_hzfont_draw_utf8(lua_State* L) {
  110. int x = luaL_checkinteger(L, 1);
  111. int y = luaL_checkinteger(L, 2);
  112. size_t len = 0;
  113. const char* str = luaL_checklstring(L, 3, &len);
  114. int fontSize = luaL_checkinteger(L, 4);
  115. uint32_t color = luaL_checkinteger(L, 5);
  116. if (fontSize <= 0 || fontSize > 255) {
  117. LLOGE("Invalid font size: %d", fontSize);
  118. lua_pushboolean(L, 0);
  119. return 1;
  120. }
  121. int result = luat_hzfont_draw_utf8(x, y, str, (unsigned char)fontSize, color);
  122. lua_pushboolean(L, result == 0 ? 1 : 0);
  123. return 1;
  124. }
  125. /**
  126. 调试开关
  127. @api hzfont.debug(enable)
  128. @boolean enable true 开启,false 关闭
  129. @return boolean 总是返回true
  130. */
  131. static int l_hzfont_debug(lua_State* L) {
  132. int enable = lua_toboolean(L, 1);
  133. (void)ttf_set_debug(enable);
  134. lua_pushboolean(L, 1);
  135. return 1;
  136. }
  137. static const rotable_Reg_t reg_hzfont[] = {
  138. { "init", ROREG_FUNC(l_hzfont_init)},
  139. { "deinit", ROREG_FUNC(l_hzfont_deinit)},
  140. { "state", ROREG_FUNC(l_hzfont_state)},
  141. { "getStrWidth", ROREG_FUNC(l_hzfont_get_str_width)},
  142. { "drawUtf8", ROREG_FUNC(l_hzfont_draw_utf8)},
  143. { "debug", ROREG_FUNC(l_hzfont_debug)},
  144. { NULL, ROREG_INT(0)}
  145. };
  146. LUAMOD_API int luaopen_hzfont(lua_State *L) {
  147. luat_newlib2(L, reg_hzfont);
  148. return 1;
  149. }