1 /**
2 * @file lv_font.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9
10 #include "lv_font.h"
11 #include "../misc/lv_utils.h"
12 #include "../misc/lv_log.h"
13 #include "../misc/lv_assert.h"
14
15 /*********************
16 * DEFINES
17 *********************/
18
19 /**********************
20 * TYPEDEFS
21 **********************/
22
23 /**********************
24 * STATIC PROTOTYPES
25 **********************/
26
27 /**********************
28 * STATIC VARIABLES
29 **********************/
30
31 /**********************
32 * GLOBAL PROTOTYPES
33 **********************/
34
35 /**********************
36 * MACROS
37 **********************/
38
39 /**********************
40 * GLOBAL FUNCTIONS
41 **********************/
42
43 /**
44 * Return with the bitmap of a font.
45 * @param font_p pointer to a font
46 * @param letter a UNICODE character code
47 * @return pointer to the bitmap of the letter
48 */
lv_font_get_glyph_bitmap(const lv_font_t * font_p,uint32_t letter)49 const uint8_t * lv_font_get_glyph_bitmap(const lv_font_t * font_p, uint32_t letter)
50 {
51 LV_ASSERT_NULL(font_p);
52 return font_p->get_glyph_bitmap(font_p, letter);
53 }
54
55 /**
56 * Get the descriptor of a glyph
57 * @param font_p pointer to font
58 * @param dsc_out store the result descriptor here
59 * @param letter a UNICODE letter code
60 * @param letter_next the next letter after `letter`. Used for kerning
61 * @return true: descriptor is successfully loaded into `dsc_out`.
62 * false: the letter was not found, no data is loaded to `dsc_out`
63 */
lv_font_get_glyph_dsc(const lv_font_t * font_p,lv_font_glyph_dsc_t * dsc_out,uint32_t letter,uint32_t letter_next)64 bool lv_font_get_glyph_dsc(const lv_font_t * font_p, lv_font_glyph_dsc_t * dsc_out, uint32_t letter,
65 uint32_t letter_next)
66 {
67
68 LV_ASSERT_NULL(font_p);
69 LV_ASSERT_NULL(dsc_out);
70
71 #if LV_USE_FONT_PLACEHOLDER
72 const lv_font_t * placeholder_font = NULL;
73 #endif
74
75 const lv_font_t * f = font_p;
76
77 dsc_out->resolved_font = NULL;
78
79 while(f) {
80 bool found = f->get_glyph_dsc(f, dsc_out, letter, letter_next);
81 if(found) {
82 if(!dsc_out->is_placeholder) {
83 dsc_out->resolved_font = f;
84 return true;
85 }
86 #if LV_USE_FONT_PLACEHOLDER
87 else if(placeholder_font == NULL) {
88 placeholder_font = f;
89 }
90 #endif
91 }
92 f = f->fallback;
93 }
94
95 #if LV_USE_FONT_PLACEHOLDER
96 if(placeholder_font != NULL) {
97 placeholder_font->get_glyph_dsc(placeholder_font, dsc_out, letter, letter_next);
98 dsc_out->resolved_font = placeholder_font;
99 return true;
100 }
101 #endif
102
103 if(letter < 0x20 ||
104 letter == 0xf8ff || /*LV_SYMBOL_DUMMY*/
105 letter == 0x200c) { /*ZERO WIDTH NON-JOINER*/
106 dsc_out->box_w = 0;
107 dsc_out->adv_w = 0;
108 }
109 else {
110 #if LV_USE_FONT_PLACEHOLDER
111 dsc_out->box_w = font_p->line_height / 2;
112 dsc_out->adv_w = dsc_out->box_w + 2;
113 #else
114 dsc_out->box_w = 0;
115 dsc_out->adv_w = 0;
116 #endif
117 }
118
119 dsc_out->resolved_font = NULL;
120 dsc_out->box_h = font_p->line_height;
121 dsc_out->ofs_x = 0;
122 dsc_out->ofs_y = 0;
123 dsc_out->bpp = 1;
124 dsc_out->is_placeholder = true;
125
126 return false;
127 }
128
129 /**
130 * Get the width of a glyph with kerning
131 * @param font pointer to a font
132 * @param letter a UNICODE letter
133 * @param letter_next the next letter after `letter`. Used for kerning
134 * @return the width of the glyph
135 */
lv_font_get_glyph_width(const lv_font_t * font,uint32_t letter,uint32_t letter_next)136 uint16_t lv_font_get_glyph_width(const lv_font_t * font, uint32_t letter, uint32_t letter_next)
137 {
138 LV_ASSERT_NULL(font);
139 lv_font_glyph_dsc_t g;
140 lv_font_get_glyph_dsc(font, &g, letter, letter_next);
141 return g.adv_w;
142 }
143
144 /**********************
145 * STATIC FUNCTIONS
146 **********************/
147