1 /**
2  * @file lv_imgfont.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_imgfont.h"
10 
11 #if LV_USE_IMGFONT
12 
13 /*********************
14  *      DEFINES
15  *********************/
16 #define LV_IMGFONT_PATH_MAX_LEN 64
17 
18 /**********************
19  *      TYPEDEFS
20  **********************/
21 typedef struct {
22     lv_font_t * font;
23     lv_get_imgfont_path_cb_t path_cb;
24     char path[LV_IMGFONT_PATH_MAX_LEN];
25 } imgfont_dsc_t;
26 
27 /**********************
28  *  STATIC PROTOTYPES
29  **********************/
30 static const uint8_t * imgfont_get_glyph_bitmap(const lv_font_t * font, uint32_t unicode);
31 static bool imgfont_get_glyph_dsc(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out,
32                                   uint32_t unicode, uint32_t unicode_next);
33 
34 /**********************
35  *  STATIC VARIABLES
36  **********************/
37 
38 /**********************
39  * GLOBAL PROTOTYPES
40  **********************/
41 
42 /**********************
43  *      MACROS
44  **********************/
45 
46 /**********************
47  *   GLOBAL FUNCTIONS
48  **********************/
lv_imgfont_create(uint16_t height,lv_get_imgfont_path_cb_t path_cb)49 lv_font_t * lv_imgfont_create(uint16_t height, lv_get_imgfont_path_cb_t path_cb)
50 {
51     LV_ASSERT_MSG(LV_IMGFONT_PATH_MAX_LEN > sizeof(lv_img_dsc_t),
52                   "LV_IMGFONT_PATH_MAX_LEN must be greater than sizeof(lv_img_dsc_t)");
53 
54     size_t size = sizeof(imgfont_dsc_t) + sizeof(lv_font_t);
55     imgfont_dsc_t * dsc = (imgfont_dsc_t *)lv_mem_alloc(size);
56     if(dsc == NULL) return NULL;
57     lv_memset_00(dsc, size);
58 
59     dsc->font = (lv_font_t *)(((char *)dsc) + sizeof(imgfont_dsc_t));
60     dsc->path_cb = path_cb;
61 
62     lv_font_t * font = dsc->font;
63     font->dsc = dsc;
64     font->get_glyph_dsc = imgfont_get_glyph_dsc;
65     font->get_glyph_bitmap = imgfont_get_glyph_bitmap;
66     font->subpx = LV_FONT_SUBPX_NONE;
67     font->line_height = height;
68     font->base_line = 0;
69     font->underline_position = 0;
70     font->underline_thickness = 0;
71 
72     return dsc->font;
73 }
74 
lv_imgfont_destroy(lv_font_t * font)75 void lv_imgfont_destroy(lv_font_t * font)
76 {
77     if(font == NULL) {
78         return;
79     }
80 
81     imgfont_dsc_t * dsc = (imgfont_dsc_t *)font->dsc;
82     lv_mem_free(dsc);
83 }
84 
85 /**********************
86  *   STATIC FUNCTIONS
87  **********************/
88 
imgfont_get_glyph_bitmap(const lv_font_t * font,uint32_t unicode)89 static const uint8_t * imgfont_get_glyph_bitmap(const lv_font_t * font, uint32_t unicode)
90 {
91     LV_UNUSED(unicode);
92     LV_ASSERT_NULL(font);
93     imgfont_dsc_t * dsc = (imgfont_dsc_t *)font->dsc;
94     return (uint8_t *)dsc->path;
95 }
96 
imgfont_get_glyph_dsc(const lv_font_t * font,lv_font_glyph_dsc_t * dsc_out,uint32_t unicode,uint32_t unicode_next)97 static bool imgfont_get_glyph_dsc(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out,
98                                   uint32_t unicode, uint32_t unicode_next)
99 {
100     LV_ASSERT_NULL(font);
101 
102     imgfont_dsc_t * dsc = (imgfont_dsc_t *)font->dsc;
103     LV_ASSERT_NULL(dsc);
104     if(dsc->path_cb == NULL) return false;
105 
106     if(!dsc->path_cb(dsc->font, dsc->path, LV_IMGFONT_PATH_MAX_LEN, unicode, unicode_next)) {
107         return false;
108     }
109 
110     lv_img_header_t header;
111     if(lv_img_decoder_get_info(dsc->path, &header) != LV_RES_OK) {
112         return false;
113     }
114 
115     dsc_out->is_placeholder = 0;
116     dsc_out->adv_w = header.w;
117     dsc_out->box_w = header.w;
118     dsc_out->box_h = header.h;
119     dsc_out->bpp = LV_IMGFONT_BPP;   /* is image identifier */
120     dsc_out->ofs_x = 0;
121     dsc_out->ofs_y = 0;
122 
123     return true;
124 }
125 
126 #endif /*LV_USE_IMGFONT*/
127