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