lvgl_bitmap_font.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <os_common_api.h>
  5. #include <lvgl/lvgl_bitmap_font.h>
  6. #include "font_mempool.h"
  7. bool bitmap_font_get_glyph_dsc_cb(const lv_font_t * lv_font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode, uint32_t unicode_next)
  8. {
  9. lv_font_fmt_bitmap_dsc_t* font_dsc;
  10. bitmap_font_t* font;
  11. bitmap_cache_t* cache;
  12. glyph_metrics_t* metric;
  13. if (unicode == '\n' || unicode == '\r')
  14. return false;
  15. if((lv_font == NULL) || (dsc_out == NULL))
  16. {
  17. SYS_LOG_ERR("null font info, %p, %p\n", lv_font, dsc_out);
  18. return false;
  19. }
  20. font_dsc = (lv_font_fmt_bitmap_dsc_t*)lv_font->user_data;
  21. if(font_dsc == NULL)
  22. {
  23. SYS_LOG_ERR("null bitmap font for font %p\n", lv_font);
  24. return false;
  25. }
  26. font = font_dsc->font;
  27. cache = font_dsc->cache;
  28. if(unicode >= 0x1F300)
  29. {
  30. if(bitmap_font_get_max_emoji_num() == 0)
  31. {
  32. metric = bitmap_font_get_glyph_dsc(font, cache, 0x20);
  33. }
  34. else
  35. {
  36. metric = bitmap_font_get_emoji_glyph_dsc(font_dsc->emoji_font, unicode, true);
  37. }
  38. }
  39. else
  40. {
  41. metric = bitmap_font_get_glyph_dsc(font, cache, unicode);
  42. }
  43. if(metric == NULL)
  44. {
  45. SYS_LOG_ERR("null metric for glyph 0x%x\n", unicode);
  46. return false;
  47. }
  48. dsc_out->adv_w = metric->advance;
  49. dsc_out->ofs_x = metric->bbx;
  50. dsc_out->ofs_y = metric->bby - font->descent;
  51. dsc_out->box_w = metric->bbw;
  52. dsc_out->box_h = metric->bbh;
  53. dsc_out->bpp = font->bpp;
  54. // SYS_LOG_INF("dsc out %d x %d y %d w %d h %d\n\n", dsc_out->adv_w, dsc_out->ofs_x, dsc_out->ofs_y, dsc_out->box_w, dsc_out->box_h);
  55. return true;
  56. }
  57. const uint8_t * bitmap_font_get_bitmap_cb(const lv_font_t * lv_font, uint32_t unicode)
  58. {
  59. uint8_t* data;
  60. lv_font_fmt_bitmap_dsc_t* font_dsc;
  61. bitmap_font_t* font;
  62. bitmap_cache_t* cache;
  63. if(lv_font == NULL)
  64. {
  65. SYS_LOG_ERR("null font info, %p\n", lv_font);
  66. return NULL;
  67. }
  68. font_dsc = (lv_font_fmt_bitmap_dsc_t*)lv_font->user_data;
  69. if(font_dsc == NULL)
  70. {
  71. SYS_LOG_ERR("null bitmap font for font %p\n", lv_font);
  72. return false;
  73. }
  74. font = font_dsc->font;
  75. cache = font_dsc->cache;
  76. if(unicode >= 0x1F300)
  77. {
  78. if(bitmap_font_get_max_emoji_num() == 0)
  79. {
  80. data = bitmap_font_get_bitmap(font, cache, 0x20);
  81. }
  82. else
  83. {
  84. data = bitmap_font_get_emoji_bitmap(font_dsc->emoji_font, unicode);
  85. }
  86. }
  87. else
  88. {
  89. data = bitmap_font_get_bitmap(font, cache, unicode);
  90. }
  91. return data;
  92. }
  93. int lvgl_bitmap_font_init(const char *def_font_path)
  94. {
  95. bitmap_font_init();
  96. if(bitmap_font_get_high_freq_enabled()==1)
  97. {
  98. if (def_font_path)
  99. {
  100. bitmap_font_load_high_freq_chars(def_font_path);
  101. }
  102. }
  103. return 0;
  104. }
  105. int lvgl_bitmap_font_deinit(void)
  106. {
  107. bitmap_font_deinit();
  108. return 0;
  109. }
  110. int lvgl_bitmap_font_set_emoji_font(lv_font_t* lv_font, const char* emoji_font_path)
  111. {
  112. lv_font_fmt_bitmap_dsc_t * dsc = (lv_font_fmt_bitmap_dsc_t*)lv_font->user_data;
  113. if(dsc == NULL)
  114. {
  115. SYS_LOG_ERR("null bitmap font for font %p\n", lv_font);
  116. return -1;
  117. }
  118. if(bitmap_font_get_max_emoji_num() == 0)
  119. {
  120. SYS_LOG_ERR("emoji not supported\n");
  121. return -1;
  122. }
  123. dsc->emoji_font = bitmap_emoji_font_open(emoji_font_path);
  124. if(dsc->emoji_font == NULL)
  125. {
  126. SYS_LOG_ERR("open bitmap emoji font failed\n");
  127. return -1;
  128. }
  129. return 0;
  130. }
  131. int lvgl_bitmap_font_get_emoji_dsc(const lv_font_t* lv_font, uint32_t unicode, lv_img_dsc_t* dsc, lv_point_t* pos, bool force_retrieve)
  132. {
  133. lv_font_fmt_bitmap_dsc_t* font_dsc;
  134. glyph_metrics_t* metric;
  135. if(bitmap_font_get_max_emoji_num() == 0)
  136. {
  137. SYS_LOG_ERR("emoji not supported\n");
  138. return -1;
  139. }
  140. if(dsc == NULL)
  141. {
  142. SYS_LOG_ERR("invalid img dsc for emoji 0x%x\n", unicode);
  143. return -1;
  144. }
  145. if(unicode < 0x1F300)
  146. {
  147. SYS_LOG_ERR("invalid emoji code 0x%x\n", unicode);
  148. return -1;
  149. }
  150. font_dsc = (lv_font_fmt_bitmap_dsc_t*)lv_font->user_data;
  151. if(font_dsc == NULL)
  152. {
  153. SYS_LOG_ERR("null bitmap font for font %p\n", lv_font);
  154. return -1;
  155. }
  156. metric = bitmap_font_get_emoji_glyph_dsc(font_dsc->emoji_font, unicode, force_retrieve);
  157. if(!metric)
  158. {
  159. return -1;
  160. }
  161. dsc->header.cf = LV_IMG_CF_ARGB_6666;
  162. dsc->header.w = metric->bbw;
  163. dsc->header.h = metric->bbh;
  164. dsc->header.always_zero = 0;
  165. dsc->data_size = metric->bbw*metric->bbh*3;
  166. // SYS_LOG_INF("emoji metric %d %d, dsc %d %d, size %d\n", metric->bbw, metric->bbh, dsc->header.w, dsc->header.h, dsc->data_size);
  167. dsc->data = bitmap_font_get_emoji_bitmap(font_dsc->emoji_font, unicode);
  168. if(pos)
  169. {
  170. pos->y += metric->bby;
  171. }
  172. return 0;
  173. }
  174. int lvgl_bitmap_font_set_default_code(lv_font_t* font, uint32_t word_code, uint32_t emoji_code)
  175. {
  176. lv_font_fmt_bitmap_dsc_t* font_dsc;
  177. if(font == NULL)
  178. {
  179. SYS_LOG_ERR("null font");
  180. return -1;
  181. }
  182. font_dsc = (lv_font_fmt_bitmap_dsc_t*)font->user_data;
  183. if(font_dsc == NULL)
  184. {
  185. SYS_LOG_ERR("cant set default code to an unopend font %p", font);
  186. return -1;
  187. }
  188. bitmap_font_set_default_code(font_dsc->font, word_code);
  189. if(bitmap_font_get_max_emoji_num() > 0)
  190. {
  191. bitmap_font_set_default_emoji_code(font_dsc->emoji_font, emoji_code);
  192. }
  193. return 0;
  194. }
  195. int lvgl_bitmap_font_set_default_bitmap(lv_font_t* font, uint8_t* bitmap, uint32_t width, uint32_t height, uint32_t gap, uint32_t bpp)
  196. {
  197. lv_font_fmt_bitmap_dsc_t* font_dsc;
  198. if(font == NULL)
  199. {
  200. SYS_LOG_ERR("null font");
  201. return -1;
  202. }
  203. font_dsc = (lv_font_fmt_bitmap_dsc_t*)font->user_data;
  204. if(font_dsc == NULL)
  205. {
  206. SYS_LOG_ERR("cant set default code to an unopend font %p", font);
  207. return -1;
  208. }
  209. bitmap_font_set_default_bitmap(font_dsc->font, bitmap, width, height, gap, bpp);
  210. return 0;
  211. }
  212. int lvgl_bitmap_font_cache_preset(bitmap_font_cache_preset_t* preset, int count)
  213. {
  214. return bitmap_font_cache_preset(preset, count);
  215. }
  216. int lvgl_bitmap_font_open(lv_font_t* font, const char * font_path)
  217. {
  218. lv_font_fmt_bitmap_dsc_t * dsc;
  219. if(font == NULL)
  220. {
  221. SYS_LOG_ERR("null font pointer");
  222. return -1;
  223. }
  224. if(font_path == NULL)
  225. {
  226. SYS_LOG_ERR("null path\n");
  227. return -1;
  228. }
  229. dsc = mem_malloc(sizeof(lv_font_fmt_bitmap_dsc_t));
  230. if(dsc == NULL)
  231. {
  232. SYS_LOG_ERR("malloc font dsc data failed\n");
  233. return -1;
  234. }
  235. memset(dsc, 0, sizeof(lv_font_fmt_bitmap_dsc_t));
  236. dsc->font = bitmap_font_open(font_path);
  237. if(dsc->font == NULL)
  238. {
  239. SYS_LOG_ERR("open bitmap font failed\n");
  240. goto ERR_EXIT;
  241. }
  242. dsc->cache = bitmap_font_get_cache(dsc->font);
  243. font->get_glyph_dsc = bitmap_font_get_glyph_dsc_cb; /*Set a callback to get info about gylphs*/
  244. font->get_glyph_bitmap = bitmap_font_get_bitmap_cb; /*Set a callback to get bitmap of gylphs*/
  245. font->user_data = dsc;
  246. if(dsc->font->ascent <= dsc->font->font_size)
  247. {
  248. font->line_height = dsc->font->ascent - dsc->font->descent;
  249. font->base_line = 0; //(dsc->font->ascent); //Base line measured from the top of line_height
  250. }
  251. else
  252. {
  253. font->line_height = dsc->font->ascent;
  254. font->base_line = dsc->font->descent/2; //(dsc->font->ascent); /*Base line measured from the top of line_height*/
  255. }
  256. font->subpx = LV_FONT_SUBPX_NONE;
  257. return 0;
  258. ERR_EXIT:
  259. if(dsc->font != NULL)
  260. {
  261. bitmap_font_close(dsc->font);
  262. }
  263. mem_free(dsc);
  264. return -1;
  265. }
  266. void lvgl_bitmap_font_close(lv_font_t* font)
  267. {
  268. lv_font_fmt_bitmap_dsc_t * dsc;
  269. if(font == NULL)
  270. {
  271. SYS_LOG_ERR("null font pointer\n");
  272. return;
  273. }
  274. dsc = (lv_font_fmt_bitmap_dsc_t*)font->user_data;
  275. if(dsc == NULL)
  276. {
  277. SYS_LOG_ERR("null font dsc pointer for font %p\n", font);
  278. return;
  279. }
  280. bitmap_font_close(dsc->font);
  281. if(dsc->emoji_font != NULL)
  282. {
  283. bitmap_emoji_font_close(dsc->emoji_font);
  284. }
  285. mem_free(dsc);
  286. font->user_data = NULL;
  287. }