1 /**
2  * @file lv_font.h
3  *
4  */
5 
6 #ifndef LV_FONT_H
7 #define LV_FONT_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #include "../lv_conf_internal.h"
17 #include <stdint.h>
18 #include <stddef.h>
19 #include <stdbool.h>
20 
21 #include "lv_symbol_def.h"
22 #include "../misc/lv_area.h"
23 
24 /*********************
25  *      DEFINES
26  *********************/
27 
28 /**********************
29  *      TYPEDEFS
30  **********************/
31 
32 /*------------------
33  * General types
34  *-----------------*/
35 
36 struct _lv_font_t;
37 /** Describes the properties of a glyph.*/
38 typedef struct {
39     const struct _lv_font_t *
40         resolved_font; /**< Pointer to a font where the gylph was actually found after handling fallbacks*/
41     uint16_t adv_w; /**< The glyph needs this space. Draw the next glyph after this width.*/
42     uint16_t box_w; /**< Width of the glyph's bounding box*/
43     uint16_t box_h; /**< Height of the glyph's bounding box*/
44     int16_t ofs_x;  /**< x offset of the bounding box*/
45     int16_t ofs_y;  /**< y offset of the bounding box*/
46     uint8_t bpp: 4;  /**< Bit-per-pixel: 1, 2, 4, 8*/
47     uint8_t is_placeholder: 1; /** Glyph is missing. But placeholder will still be displayed */
48 } lv_font_glyph_dsc_t;
49 
50 /** The bitmaps might be upscaled by 3 to achieve subpixel rendering.*/
51 enum {
52     LV_FONT_SUBPX_NONE,
53     LV_FONT_SUBPX_HOR,
54     LV_FONT_SUBPX_VER,
55     LV_FONT_SUBPX_BOTH,
56 };
57 
58 typedef uint8_t lv_font_subpx_t;
59 
60 /** Describe the properties of a font*/
61 typedef struct _lv_font_t {
62     /** Get a glyph's descriptor from a font*/
63     bool (*get_glyph_dsc)(const struct _lv_font_t *, lv_font_glyph_dsc_t *, uint32_t letter, uint32_t letter_next);
64 
65     /** Get a glyph's bitmap from a font*/
66     const uint8_t * (*get_glyph_bitmap)(const struct _lv_font_t *, uint32_t);
67 
68     /*Pointer to the font in a font pack (must have the same line height)*/
69     lv_coord_t line_height;         /**< The real line height where any text fits*/
70     lv_coord_t base_line;           /**< Base line measured from the top of the line_height*/
71     uint8_t subpx  : 2;             /**< An element of `lv_font_subpx_t`*/
72 
73     int8_t underline_position;      /**< Distance between the top of the underline and base line (< 0 means below the base line)*/
74     int8_t underline_thickness;     /**< Thickness of the underline*/
75 
76     const void * dsc;               /**< Store implementation specific or run_time data or caching here*/
77     const struct _lv_font_t * fallback;   /**< Fallback font for missing glyph. Resolved recursively */
78 #if LV_USE_USER_DATA
79     void * user_data;               /**< Custom user data for font.*/
80 #endif
81 } lv_font_t;
82 
83 /**********************
84  * GLOBAL PROTOTYPES
85  **********************/
86 
87 /**
88  * Return with the bitmap of a font.
89  * @param font_p pointer to a font
90  * @param letter an UNICODE character code
91  * @return pointer to the bitmap of the letter
92  */
93 const uint8_t * lv_font_get_glyph_bitmap(const lv_font_t * font_p, uint32_t letter);
94 
95 /**
96  * Get the descriptor of a glyph
97  * @param font_p pointer to font
98  * @param dsc_out store the result descriptor here
99  * @param letter an UNICODE letter code
100  * @param letter_next the next letter after `letter`. Used for kerning
101  * @return true: descriptor is successfully loaded into `dsc_out`.
102  *         false: the letter was not found, no data is loaded to `dsc_out`
103  */
104 bool lv_font_get_glyph_dsc(const lv_font_t * font_p, lv_font_glyph_dsc_t * dsc_out, uint32_t letter,
105                            uint32_t letter_next);
106 
107 /**
108  * Get the width of a glyph with kerning
109  * @param font pointer to a font
110  * @param letter an UNICODE letter
111  * @param letter_next the next letter after `letter`. Used for kerning
112  * @return the width of the glyph
113  */
114 uint16_t lv_font_get_glyph_width(const lv_font_t * font, uint32_t letter, uint32_t letter_next);
115 
116 /**
117  * Get the line height of a font. All characters fit into this height
118  * @param font_p pointer to a font
119  * @return the height of a font
120  */
lv_font_get_line_height(const lv_font_t * font_p)121 static inline lv_coord_t lv_font_get_line_height(const lv_font_t * font_p)
122 {
123     return font_p->line_height;
124 }
125 
126 /**********************
127  *      MACROS
128  **********************/
129 
130 #define LV_FONT_DECLARE(font_name) extern const lv_font_t font_name;
131 
132 #if LV_FONT_MONTSERRAT_8
133 LV_FONT_DECLARE(lv_font_montserrat_8)
134 #endif
135 
136 #if LV_FONT_MONTSERRAT_10
LV_FONT_DECLARE(lv_font_montserrat_10)137 LV_FONT_DECLARE(lv_font_montserrat_10)
138 #endif
139 
140 #if LV_FONT_MONTSERRAT_12
141 LV_FONT_DECLARE(lv_font_montserrat_12)
142 #endif
143 
144 #if LV_FONT_MONTSERRAT_14
145 LV_FONT_DECLARE(lv_font_montserrat_14)
146 #endif
147 
148 #if LV_FONT_MONTSERRAT_16
149 LV_FONT_DECLARE(lv_font_montserrat_16)
150 #endif
151 
152 #if LV_FONT_MONTSERRAT_18
153 LV_FONT_DECLARE(lv_font_montserrat_18)
154 #endif
155 
156 #if LV_FONT_MONTSERRAT_20
157 LV_FONT_DECLARE(lv_font_montserrat_20)
158 #endif
159 
160 #if LV_FONT_MONTSERRAT_22
161 LV_FONT_DECLARE(lv_font_montserrat_22)
162 #endif
163 
164 #if LV_FONT_MONTSERRAT_24
165 LV_FONT_DECLARE(lv_font_montserrat_24)
166 #endif
167 
168 #if LV_FONT_MONTSERRAT_26
169 LV_FONT_DECLARE(lv_font_montserrat_26)
170 #endif
171 
172 #if LV_FONT_MONTSERRAT_28
173 LV_FONT_DECLARE(lv_font_montserrat_28)
174 #endif
175 
176 #if LV_FONT_MONTSERRAT_30
177 LV_FONT_DECLARE(lv_font_montserrat_30)
178 #endif
179 
180 #if LV_FONT_MONTSERRAT_32
181 LV_FONT_DECLARE(lv_font_montserrat_32)
182 #endif
183 
184 #if LV_FONT_MONTSERRAT_34
185 LV_FONT_DECLARE(lv_font_montserrat_34)
186 #endif
187 
188 #if LV_FONT_MONTSERRAT_36
189 LV_FONT_DECLARE(lv_font_montserrat_36)
190 #endif
191 
192 #if LV_FONT_MONTSERRAT_38
193 LV_FONT_DECLARE(lv_font_montserrat_38)
194 #endif
195 
196 #if LV_FONT_MONTSERRAT_40
197 LV_FONT_DECLARE(lv_font_montserrat_40)
198 #endif
199 
200 #if LV_FONT_MONTSERRAT_42
201 LV_FONT_DECLARE(lv_font_montserrat_42)
202 #endif
203 
204 #if LV_FONT_MONTSERRAT_44
205 LV_FONT_DECLARE(lv_font_montserrat_44)
206 #endif
207 
208 #if LV_FONT_MONTSERRAT_46
209 LV_FONT_DECLARE(lv_font_montserrat_46)
210 #endif
211 
212 #if LV_FONT_MONTSERRAT_48
213 LV_FONT_DECLARE(lv_font_montserrat_48)
214 #endif
215 
216 #if LV_FONT_MONTSERRAT_12_SUBPX
217 LV_FONT_DECLARE(lv_font_montserrat_12_subpx)
218 #endif
219 
220 #if LV_FONT_MONTSERRAT_28_COMPRESSED
221 LV_FONT_DECLARE(lv_font_montserrat_28_compressed)
222 #endif
223 
224 #if LV_FONT_DEJAVU_16_PERSIAN_HEBREW
225 LV_FONT_DECLARE(lv_font_dejavu_16_persian_hebrew)
226 #endif
227 
228 #if LV_FONT_SIMSUN_16_CJK
229 LV_FONT_DECLARE(lv_font_simsun_16_cjk)
230 #endif
231 
232 #if LV_FONT_UNSCII_8
233 LV_FONT_DECLARE(lv_font_unscii_8)
234 #endif
235 
236 #if LV_FONT_UNSCII_16
237 LV_FONT_DECLARE(lv_font_unscii_16)
238 #endif
239 
240 /*Declare the custom (user defined) fonts*/
241 #ifdef LV_FONT_CUSTOM_DECLARE
242 LV_FONT_CUSTOM_DECLARE
243 #endif
244 
245 /**
246  * Just a wrapper around LV_FONT_DEFAULT because it might be more convenient to use a function is some cases
247  * @return  pointer to LV_FONT_DEFAULT
248  */
249 static inline const lv_font_t * lv_font_default(void)
250 {
251     return LV_FONT_DEFAULT;
252 }
253 
254 #ifdef __cplusplus
255 } /*extern "C"*/
256 #endif
257 
258 #endif /*USE_FONT*/
259