lv_font_ex.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*******************************************************************************
  2. * tool: lvgl_conv_tool V1.0.1
  3. * author: Dozingfiretruck
  4. * author_github_url: https://github.com/Dozingfiretruck
  5. * author_gitee_url: https://gitee.com/Dozingfiretruck
  6. * qq: 1041390013
  7. ******************************************************************************/
  8. #ifdef __has_include
  9. #if __has_include("lvgl.h")
  10. #ifndef LV_LVGL_H_INCLUDE_SIMPLE
  11. #define LV_LVGL_H_INCLUDE_SIMPLE
  12. #endif
  13. #endif
  14. #endif
  15. #ifdef LV_LVGL_H_INCLUDE_SIMPLE
  16. #include "lvgl.h"
  17. #else
  18. #include "lvgl/lvgl.h"
  19. #endif
  20. #include "luat_mem.h"
  21. #include "luat_fs.h"
  22. /*-----------------
  23. * STRUCT
  24. *----------------*/
  25. typedef struct{
  26. char head[5];
  27. uint8_t bpp;
  28. int8_t underline_position;
  29. int8_t underline_thickness;
  30. int16_t line_height;
  31. int16_t base_line;
  32. uint16_t codes_min;
  33. uint16_t codes_max;
  34. }custom_font_header_t;
  35. typedef struct{
  36. uint32_t adv_w;
  37. uint16_t box_w;
  38. uint16_t box_h;
  39. int16_t ofs_x;
  40. int16_t ofs_y;
  41. uint32_t glyph_data_len;
  42. }custom_glyph_dsc_t;
  43. typedef struct{
  44. custom_font_header_t custom_font_header;
  45. FILE* font_file;
  46. uint8_t glyph_data[2048];
  47. }custom_font_data_t;
  48. static void *custom_font_getdata(const lv_font_t * font, uint32_t offset, uint32_t size){
  49. custom_font_data_t* custom_font_data = font->dsc;
  50. FILE* font_file = custom_font_data->font_file;
  51. uint8_t* glyph_data = custom_font_data->glyph_data;
  52. memset(glyph_data, 0, sizeof(custom_font_data->glyph_data));
  53. luat_fs_fseek(font_file, offset, SEEK_SET);
  54. luat_fs_fread(glyph_data, 1, size, font_file);
  55. return glyph_data;
  56. }
  57. /*-----------------
  58. * GET DSC
  59. *----------------*/
  60. static bool custom_font_get_glyph_dsc_fmt_txt(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter, uint32_t unicode_letter_next) {
  61. custom_font_data_t* custom_font_data = font->dsc;
  62. custom_font_header_t* custom_font_header = &custom_font_data->custom_font_header;
  63. bool is_tab = false;
  64. if(unicode_letter == '\t') {
  65. unicode_letter = ' ';
  66. is_tab = true;
  67. }
  68. if( unicode_letter>custom_font_header->codes_max || unicode_letter<custom_font_header->codes_min ) {
  69. return false;
  70. }
  71. uint32_t unicode_offset = sizeof(custom_font_header_t)+(unicode_letter-custom_font_header->codes_min)*sizeof(uint32_t);
  72. uint32_t *p_pos = (uint32_t *)custom_font_getdata(font, unicode_offset, sizeof(uint32_t));
  73. if(*p_pos) {
  74. custom_glyph_dsc_t * gdsc = (custom_glyph_dsc_t*)custom_font_getdata(font, *p_pos, sizeof(custom_glyph_dsc_t));
  75. dsc_out->box_h = gdsc->box_h;
  76. dsc_out->box_w = gdsc->box_w;
  77. dsc_out->ofs_x = gdsc->ofs_x;
  78. dsc_out->ofs_y = gdsc->ofs_y;
  79. dsc_out->bpp = custom_font_header->bpp;
  80. uint32_t adv_w = gdsc->adv_w;
  81. if(is_tab) adv_w *= 2;
  82. dsc_out->adv_w = (adv_w + (1 << 3)) >> 4;
  83. if(is_tab) dsc_out->box_w = dsc_out->box_w * 2;
  84. return true;
  85. }
  86. return false;
  87. }
  88. /*-----------------
  89. * GET BITMAP
  90. *----------------*/
  91. static const uint8_t* custom_font_get_bitmap_fmt_txt(const lv_font_t * font, uint32_t unicode_letter) {
  92. custom_font_data_t* custom_font_data = font->dsc;
  93. custom_font_header_t* custom_font_header = &custom_font_data->custom_font_header;
  94. if(unicode_letter == '\t') unicode_letter = ' ';
  95. if( unicode_letter>custom_font_header->codes_max || unicode_letter<custom_font_header->codes_min ) {
  96. return NULL;
  97. }
  98. uint32_t unicode_offset = sizeof(custom_font_header_t )+(unicode_letter-custom_font_header->codes_min)*sizeof(uint32_t);
  99. uint32_t *p_pos = (uint32_t *)custom_font_getdata(font, unicode_offset, sizeof(uint32_t));
  100. uint32_t dsc_offset = p_pos[0];
  101. if(dsc_offset) {
  102. custom_glyph_dsc_t* gdsc = (custom_glyph_dsc_t*)custom_font_getdata(font, dsc_offset, sizeof(custom_glyph_dsc_t));
  103. return custom_font_getdata(font, dsc_offset + sizeof(custom_glyph_dsc_t), gdsc->glyph_data_len);
  104. }
  105. return NULL;
  106. }
  107. /*-----------------
  108. * PUBLIC FONT
  109. *----------------*/
  110. lv_font_t* custom_get_font(FILE* font_file){
  111. lv_font_t* custom_font = luat_heap_malloc(sizeof(lv_font_t));
  112. if (custom_font == NULL){
  113. return NULL;
  114. }
  115. memset(custom_font, 0, sizeof(lv_font_t));
  116. custom_font->get_glyph_dsc = custom_font_get_glyph_dsc_fmt_txt;
  117. custom_font->get_glyph_bitmap = custom_font_get_bitmap_fmt_txt;
  118. custom_font_data_t* custom_font_data = luat_heap_malloc(sizeof(custom_font_data_t));
  119. memset(custom_font_data, 0, sizeof(custom_font_data_t));
  120. custom_font_data->font_file = font_file;
  121. custom_font->dsc = custom_font_data;
  122. uint32_t *p_pos = (uint32_t *)custom_font_getdata(custom_font, 0, sizeof(custom_font_header_t));
  123. memcpy(&custom_font_data->custom_font_header, p_pos, sizeof(custom_font_header_t));
  124. if (strncmp(custom_font_data->custom_font_header.head, "head", 4)){
  125. luat_heap_free(custom_font);
  126. return NULL;
  127. }
  128. custom_font->base_line = custom_font_data->custom_font_header.base_line;
  129. custom_font->line_height = custom_font_data->custom_font_header.line_height;
  130. #if LV_VERSION_CHECK(7, 4, 0) || LVGL_VERSION_MAJOR >= 8
  131. custom_font->underline_position = custom_font_data->custom_font_header.underline_position;
  132. custom_font->underline_thickness = custom_font_data->custom_font_header.underline_thickness;
  133. #endif
  134. return custom_font;
  135. }
  136. void custom_free_font(lv_font_t* custom_font){
  137. if (custom_font){
  138. luat_heap_free(custom_font->dsc);
  139. luat_heap_free(custom_font);
  140. }
  141. }