luat_lib_ufont.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. @module ufont
  3. @summary 统一字体库(开发中)
  4. @version 1.0
  5. @date 2022.08.05
  6. @usage
  7. -- 尚处于开发阶段,暂不可用
  8. */
  9. #include "luat_base.h"
  10. #include "luat_malloc.h"
  11. #include "luat_fs.h"
  12. #include "string.h"
  13. #define LUAT_LOG_TAG "ufont"
  14. #include "luat_log.h"
  15. #include "luat_ufont.h"
  16. extern const luat_font_desc_t luat_font_sarasa_bold_16;
  17. extern const lv_font_t luat_font_sarasa_bold_16_lvgl;
  18. typedef struct ufont_reg
  19. {
  20. const char* name;
  21. const lv_font_t* font;
  22. }ufont_reg_t;
  23. const ufont_reg_t ufonts[] = {
  24. #ifdef LUAT_UFONT_FONTS_SARASA_BOLD_16
  25. {.name="sarasa_bold_16", .font=&luat_font_sarasa_bold_16_lvgl},
  26. #endif
  27. {.name = NULL, .font = (NULL)}
  28. };
  29. /*
  30. 获取字体
  31. @api ufont.get(name)
  32. @string 字体名称, 例如
  33. @return userdata 若字体存在,返回字体指针, 否则返回nil
  34. @usage
  35. -- TODO
  36. */
  37. static int l_ufont_get(lua_State *L) {
  38. const char* font_name = luaL_optstring(L, 1, "sarasa_bold_16");
  39. ufont_reg_t* reg = ufonts;
  40. while (reg->font != NULL) {
  41. //LLOGD("[%s] - [%s]", reg->name, font_name);
  42. if (strcmp(reg->name, font_name) == 0) {
  43. lua_pushlightuserdata(L, reg->font);
  44. return 1;
  45. }
  46. reg++;
  47. }
  48. LLOGW("font not exists [%s]", font_name);
  49. return 0;
  50. }
  51. /*
  52. 返回固件支持的字体列表
  53. @api ufont.list()
  54. @return table 字体列表
  55. @usage
  56. -- API新增于2022-08-05
  57. log.info("fonts", "u8g2", json.encode(ufont.list()))
  58. */
  59. static int l_ufont_list(lua_State *L) {
  60. ufont_reg_t* reg = ufonts;
  61. lua_createtable(L, 10, 0);
  62. int index = 1;
  63. while (reg->font != NULL) {
  64. lua_pushinteger(L, index);
  65. lua_pushstring(L, reg->name);
  66. lua_settable(L, -3);
  67. index ++;
  68. reg ++;
  69. }
  70. return 1;
  71. }
  72. #include "rotable2.h"
  73. static const rotable_Reg_t reg_ufont[] =
  74. {
  75. { "get" , ROREG_FUNC(l_ufont_get)},
  76. { "list" , ROREG_FUNC(l_ufont_list)},
  77. { NULL, ROREG_INT(0)},
  78. };
  79. LUAMOD_API int luaopen_ufont( lua_State *L ) {
  80. luat_newlib2(L, reg_ufont);
  81. return 1;
  82. }