Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/libs/lvgl/lv_misc/lv_font.c
1476 views
1
/**
2
* @file lv_font.c
3
*
4
*/
5
6
/*********************
7
* INCLUDES
8
*********************/
9
#include <stddef.h>
10
#include "lv_font.h"
11
#include "lv_log.h"
12
13
/*********************
14
* DEFINES
15
*********************/
16
17
/**********************
18
* TYPEDEFS
19
**********************/
20
21
/**********************
22
* STATIC PROTOTYPES
23
**********************/
24
25
/**********************
26
* STATIC VARIABLES
27
**********************/
28
29
/**********************
30
* GLOBAL PROTOTYPES
31
**********************/
32
33
/**********************
34
* MACROS
35
**********************/
36
37
/**********************
38
* GLOBAL FUNCTIONS
39
**********************/
40
41
/**
42
* Initialize the fonts
43
*/
44
void lv_font_init(void)
45
{
46
lv_font_builtin_init();
47
}
48
49
/**
50
* Add a font to an other to extend the character set.
51
* @param child the font to add
52
* @param parent this font will be extended. Using it later will contain the characters from `child`
53
*/
54
void lv_font_add(lv_font_t * child, lv_font_t * parent)
55
{
56
if(parent == NULL) return;
57
58
while(parent->next_page != NULL) {
59
parent = parent->next_page; /*Got to the last page and add the new font there*/
60
}
61
62
parent->next_page = child;
63
64
}
65
66
/**
67
* Remove a font from a character set.
68
* @param child the font to remove
69
* @param parent remove `child` from here
70
*/
71
void lv_font_remove(lv_font_t * child, lv_font_t * parent)
72
{
73
if(parent == NULL) return;
74
if(child == NULL) return;
75
76
while(parent->next_page != child) {
77
parent = parent->next_page; /*Got to the last page and add the new font there*/
78
}
79
80
parent->next_page = child->next_page;
81
}
82
83
84
/**
85
* Tells if font which contains `letter` is monospace or not
86
* @param font_p point to font
87
* @param letter an UNICODE character code
88
* @return true: the letter is monospace; false not monospace
89
*/
90
bool lv_font_is_monospace(const lv_font_t * font_p, uint32_t letter)
91
{
92
const lv_font_t * font_i = font_p;
93
int16_t w;
94
while(font_i != NULL) {
95
w = font_i->get_width(font_i, letter);
96
if(w >= 0) {
97
/*Glyph found*/
98
if(font_i->monospace) return true;
99
return false;
100
}
101
102
font_i = font_i->next_page;
103
}
104
105
return 0;
106
}
107
108
/**
109
* Return with the bitmap of a font.
110
* @param font_p pointer to a font
111
* @param letter an UNICODE character code
112
* @return pointer to the bitmap of the letter
113
*/
114
const uint8_t * lv_font_get_bitmap(const lv_font_t * font_p, uint32_t letter)
115
{
116
const lv_font_t * font_i = font_p;
117
while(font_i != NULL) {
118
const uint8_t * bitmap = font_i->get_bitmap(font_i, letter);
119
if(bitmap) return bitmap;
120
121
font_i = font_i->next_page;
122
}
123
124
return NULL;
125
}
126
127
/**
128
* Get the width of a letter in a font. If `monospace` is set then return with it.
129
* @param font_p pointer to a font
130
* @param letter an UNICODE character code
131
* @return the width of a letter
132
*/
133
uint8_t lv_font_get_width(const lv_font_t * font_p, uint32_t letter)
134
{
135
const lv_font_t * font_i = font_p;
136
int16_t w;
137
while(font_i != NULL) {
138
w = font_i->get_width(font_i, letter);
139
if(w >= 0) {
140
/*Glyph found*/
141
uint8_t m = font_i->monospace;
142
if(m) w = m;
143
return w;
144
}
145
146
font_i = font_i->next_page;
147
}
148
149
return 0;
150
151
}
152
153
/**
154
* Get the width of the letter without overwriting it with the `monospace` attribute
155
* @param font_p pointer to a font
156
* @param letter an UNICODE character code
157
* @return the width of a letter
158
*/
159
uint8_t lv_font_get_real_width(const lv_font_t * font_p, uint32_t letter)
160
{
161
const lv_font_t * font_i = font_p;
162
int16_t w;
163
while(font_i != NULL) {
164
w = font_i->get_width(font_i, letter);
165
if(w >= 0) return w;
166
167
font_i = font_i->next_page;
168
}
169
170
return 0;
171
}
172
173
/**
174
* Get the bit-per-pixel of font
175
* @param font pointer to font
176
* @param letter a letter from font (font extensions can have different bpp)
177
* @return bpp of the font (or font extension)
178
*/
179
uint8_t lv_font_get_bpp(const lv_font_t * font, uint32_t letter)
180
{
181
const lv_font_t * font_i = font;
182
while(font_i != NULL) {
183
if(letter >= font_i->unicode_first && letter <= font_i->unicode_last) {
184
return font_i->bpp;
185
}
186
font_i = font_i->next_page;
187
}
188
189
return 0;
190
191
}
192
193
/**
194
* Generic bitmap get function used in 'font->get_bitmap' when the font contains all characters in the range
195
* @param font pointer to font
196
* @param unicode_letter an unicode letter which bitmap should be get
197
* @return pointer to the bitmap or NULL if not found
198
*/
199
const uint8_t * lv_font_get_bitmap_continuous(const lv_font_t * font, uint32_t unicode_letter)
200
{
201
/*Check the range*/
202
if(unicode_letter < font->unicode_first || unicode_letter > font->unicode_last) return NULL;
203
204
uint32_t index = (unicode_letter - font->unicode_first);
205
return &font->glyph_bitmap[font->glyph_dsc[index].glyph_index];
206
}
207
208
/**
209
* Generic bitmap get function used in 'font->get_bitmap' when the font NOT contains all characters in the range (sparse)
210
* @param font pointer to font
211
* @param unicode_letter an unicode letter which bitmap should be get
212
* @return pointer to the bitmap or NULL if not found
213
*/
214
const uint8_t * lv_font_get_bitmap_sparse(const lv_font_t * font, uint32_t unicode_letter)
215
{
216
/*Check the range*/
217
if(unicode_letter < font->unicode_first || unicode_letter > font->unicode_last) return NULL;
218
219
uint32_t i;
220
for(i = 0; font->unicode_list[i] != 0; i++) {
221
if(font->unicode_list[i] == unicode_letter) {
222
return &font->glyph_bitmap[font->glyph_dsc[i].glyph_index];
223
}
224
}
225
226
return NULL;
227
}
228
229
/**
230
* Generic glyph width get function used in 'font->get_width' when the font contains all characters in the range
231
* @param font pointer to font
232
* @param unicode_letter an unicode letter which width should be get
233
* @return width of the gylph or -1 if not found
234
*/
235
int16_t lv_font_get_width_continuous(const lv_font_t * font, uint32_t unicode_letter)
236
{
237
/*Check the range*/
238
if(unicode_letter < font->unicode_first || unicode_letter > font->unicode_last) {
239
return -1;
240
}
241
242
uint32_t index = (unicode_letter - font->unicode_first);
243
return font->glyph_dsc[index].w_px;
244
}
245
246
/**
247
* Generic glyph width get function used in 'font->get_bitmap' when the font NOT contains all characters in the range (sparse)
248
* @param font pointer to font
249
* @param unicode_letter an unicode letter which width should be get
250
* @return width of the glyph or -1 if not found
251
*/
252
int16_t lv_font_get_width_sparse(const lv_font_t * font, uint32_t unicode_letter)
253
{
254
/*Check the range*/
255
if(unicode_letter < font->unicode_first || unicode_letter > font->unicode_last) return -1;
256
257
uint32_t i;
258
for(i = 0; font->unicode_list[i] != 0; i++) {
259
if(font->unicode_list[i] == unicode_letter) {
260
return font->glyph_dsc[i].w_px;
261
}
262
}
263
264
return -1;
265
}
266
267
/**********************
268
* STATIC FUNCTIONS
269
**********************/
270
271