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 "../misc/lv_types.h"
18 
19 #include "lv_symbol_def.h"
20 #include "../draw/lv_draw_buf.h"
21 #include "../misc/lv_area.h"
22 
23 /*********************
24  *      DEFINES
25  *********************/
26 
27 /**********************
28  *      TYPEDEFS
29  **********************/
30 
31 /*------------------
32  * General types
33  *-----------------*/
34 
35 /** The font format.*/
36 typedef enum {
37     LV_FONT_GLYPH_FORMAT_NONE   = 0, /**< Maybe not visible*/
38 
39     /**< Legacy simple formats with no byte padding at end of the lines*/
40     LV_FONT_GLYPH_FORMAT_A1     = 0x01, /**< 1 bit per pixel*/
41     LV_FONT_GLYPH_FORMAT_A2     = 0x02, /**< 2 bit per pixel*/
42     LV_FONT_GLYPH_FORMAT_A3     = 0x03, /**< 3 bit per pixel*/
43     LV_FONT_GLYPH_FORMAT_A4     = 0x04, /**< 4 bit per pixel*/
44     LV_FONT_GLYPH_FORMAT_A8     = 0x08, /**< 8 bit per pixel*/
45 
46     /**< Legacy simple formats with byte padding at end of the lines*/
47     LV_FONT_GLYPH_FORMAT_A1_ALIGNED = 0x011, /**< 1 bit per pixel*/
48     LV_FONT_GLYPH_FORMAT_A2_ALIGNED = 0x012, /**< 2 bit per pixel*/
49     LV_FONT_GLYPH_FORMAT_A4_ALIGNED = 0x014, /**< 4 bit per pixel*/
50     LV_FONT_GLYPH_FORMAT_A8_ALIGNED = 0x018, /**< 8 bit per pixel*/
51 
52     LV_FONT_GLYPH_FORMAT_IMAGE  = 0x19, /**< Image format*/
53 
54     /**< Advanced formats*/
55     LV_FONT_GLYPH_FORMAT_VECTOR = 0x1A, /**< Vectorial format*/
56     LV_FONT_GLYPH_FORMAT_SVG    = 0x1B, /**< SVG format*/
57     LV_FONT_GLYPH_FORMAT_CUSTOM = 0xFF, /**< Custom format*/
58 } lv_font_glyph_format_t;
59 
60 /** Describes the properties of a glyph.*/
61 typedef struct {
62     const lv_font_t *
63     resolved_font;  /**< Pointer to a font where the glyph was actually found after handling fallbacks*/
64     uint16_t adv_w; /**< The glyph needs this space. Draw the next glyph after this width.*/
65     uint16_t box_w; /**< Width of the glyph's bounding box*/
66     uint16_t box_h; /**< Height of the glyph's bounding box*/
67     int16_t ofs_x;  /**< x offset of the bounding box*/
68     int16_t ofs_y;  /**< y offset of the bounding box*/
69     lv_font_glyph_format_t format;  /**< Font format of the glyph see lv_font_glyph_format_t */
70     uint8_t is_placeholder: 1;      /**< Glyph is missing. But placeholder will still be displayed*/
71 
72     /** 0: Get bitmap should return an A8 or ARGB8888 image.
73      * 1: return the bitmap as it is (Maybe A1/2/4 or any proprietary formats). */
74     uint8_t req_raw_bitmap: 1;
75 
76     union {
77         uint32_t index;       /**< Unicode code point*/
78         const void * src;     /**< Pointer to the source data used by image fonts*/
79     } gid;                    /**< The index of the glyph in the font file. Used by the font cache*/
80     lv_cache_entry_t * entry; /**< The cache entry of the glyph draw data. Used by the font cache*/
81 } lv_font_glyph_dsc_t;
82 
83 /** The bitmaps might be upscaled by 3 to achieve subpixel rendering.*/
84 typedef enum {
85     LV_FONT_SUBPX_NONE,
86     LV_FONT_SUBPX_HOR,
87     LV_FONT_SUBPX_VER,
88     LV_FONT_SUBPX_BOTH,
89 } lv_font_subpx_t;
90 
91 /** Adjust letter spacing for specific character pairs.*/
92 typedef enum {
93     LV_FONT_KERNING_NORMAL,
94     LV_FONT_KERNING_NONE,
95 } lv_font_kerning_t;
96 
97 /** Describe the properties of a font*/
98 struct _lv_font_t {
99     /** Get a glyph's descriptor from a font*/
100     bool (*get_glyph_dsc)(const lv_font_t *, lv_font_glyph_dsc_t *, uint32_t letter, uint32_t letter_next);
101 
102     /** Get a glyph's bitmap from a font*/
103     const void * (*get_glyph_bitmap)(lv_font_glyph_dsc_t *, lv_draw_buf_t *);
104 
105     /** Release a glyph*/
106     void (*release_glyph)(const lv_font_t *, lv_font_glyph_dsc_t *);
107 
108     /*Pointer to the font in a font pack (must have the same line height)*/
109     int32_t line_height;         /**< The real line height where any text fits*/
110     int32_t base_line;           /**< Base line measured from the bottom of the line_height*/
111     uint8_t subpx   : 2;            /**< An element of `lv_font_subpx_t`*/
112     uint8_t kerning : 1;            /**< An element of `lv_font_kerning_t`*/
113 
114     int8_t underline_position;      /**< Distance between the top of the underline and base line (< 0 means below the base line)*/
115     int8_t underline_thickness;     /**< Thickness of the underline*/
116 
117     const void * dsc;               /**< Store implementation specific or run_time data or caching here*/
118     const lv_font_t * fallback;   /**< Fallback font for missing glyph. Resolved recursively */
119     void * user_data;               /**< Custom user data for font.*/
120 };
121 
122 /**********************
123  * GLOBAL PROTOTYPES
124  **********************/
125 
126 /**
127  * Return with the bitmap of a font.
128  * @note You must call lv_font_get_glyph_dsc() to get `g_dsc` (lv_font_glyph_dsc_t) before you can call this function.
129  * @param g_dsc         the glyph descriptor including which font to use, which supply the glyph_index and the format.
130  * @param draw_buf      a draw buffer that can be used to store the bitmap of the glyph, it's OK not to use it.
131  * @return pointer to the glyph's data. It can be a draw buffer for bitmap fonts or an image source for imgfonts.
132  */
133 const void * lv_font_get_glyph_bitmap(lv_font_glyph_dsc_t * g_dsc, lv_draw_buf_t * draw_buf);
134 
135 /**
136  * Get the descriptor of a glyph
137  * @param font          pointer to font
138  * @param dsc_out       store the result descriptor here
139  * @param letter        a UNICODE letter code
140  * @param letter_next   the next letter after `letter`. Used for kerning
141  * @return true: descriptor is successfully loaded into `dsc_out`.
142  *         false: the letter was not found, no data is loaded to `dsc_out`
143  */
144 bool lv_font_get_glyph_dsc(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t letter,
145                            uint32_t letter_next);
146 
147 /**
148  * Release the bitmap of a font.
149  * @note You must call lv_font_get_glyph_dsc() to get `g_dsc` (lv_font_glyph_dsc_t) before you can call this function.
150  * @param g_dsc         the glyph descriptor including which font to use, which supply the glyph_index and the format.
151  */
152 void lv_font_glyph_release_draw_data(lv_font_glyph_dsc_t * g_dsc);
153 
154 /**
155  * Get the width of a glyph with kerning
156  * @param font          pointer to a font
157  * @param letter        a UNICODE letter
158  * @param letter_next   the next letter after `letter`. Used for kerning
159  * @return the width of the glyph
160  */
161 uint16_t lv_font_get_glyph_width(const lv_font_t * font, uint32_t letter, uint32_t letter_next);
162 
163 /**
164  * Get the line height of a font. All characters fit into this height
165  * @param font      pointer to a font
166  * @return the height of a font
167  */
168 int32_t lv_font_get_line_height(const lv_font_t * font);
169 
170 /**
171  * Configure the use of kerning information stored in a font
172  * @param font    pointer to a font
173  * @param kerning `LV_FONT_KERNING_NORMAL` (default) or `LV_FONT_KERNING_NONE`
174  */
175 void lv_font_set_kerning(lv_font_t * font, lv_font_kerning_t kerning);
176 
177 /**********************
178  *      MACROS
179  **********************/
180 
181 #define LV_FONT_DECLARE(font_name) LV_ATTRIBUTE_EXTERN_DATA extern const lv_font_t font_name;
182 
183 #if LV_FONT_MONTSERRAT_8
184 LV_FONT_DECLARE(lv_font_montserrat_8)
185 #endif
186 
187 #if LV_FONT_MONTSERRAT_10
188 LV_FONT_DECLARE(lv_font_montserrat_10)
189 #endif
190 
191 #if LV_FONT_MONTSERRAT_12
192 LV_FONT_DECLARE(lv_font_montserrat_12)
193 #endif
194 
195 #if LV_FONT_MONTSERRAT_14
196 LV_FONT_DECLARE(lv_font_montserrat_14)
197 #endif
198 
199 #if LV_FONT_MONTSERRAT_16
200 LV_FONT_DECLARE(lv_font_montserrat_16)
201 #endif
202 
203 #if LV_FONT_MONTSERRAT_18
204 LV_FONT_DECLARE(lv_font_montserrat_18)
205 #endif
206 
207 #if LV_FONT_MONTSERRAT_20
208 LV_FONT_DECLARE(lv_font_montserrat_20)
209 #endif
210 
211 #if LV_FONT_MONTSERRAT_22
212 LV_FONT_DECLARE(lv_font_montserrat_22)
213 #endif
214 
215 #if LV_FONT_MONTSERRAT_24
216 LV_FONT_DECLARE(lv_font_montserrat_24)
217 #endif
218 
219 #if LV_FONT_MONTSERRAT_26
220 LV_FONT_DECLARE(lv_font_montserrat_26)
221 #endif
222 
223 #if LV_FONT_MONTSERRAT_28
224 LV_FONT_DECLARE(lv_font_montserrat_28)
225 #endif
226 
227 #if LV_FONT_MONTSERRAT_30
228 LV_FONT_DECLARE(lv_font_montserrat_30)
229 #endif
230 
231 #if LV_FONT_MONTSERRAT_32
232 LV_FONT_DECLARE(lv_font_montserrat_32)
233 #endif
234 
235 #if LV_FONT_MONTSERRAT_34
236 LV_FONT_DECLARE(lv_font_montserrat_34)
237 #endif
238 
239 #if LV_FONT_MONTSERRAT_36
240 LV_FONT_DECLARE(lv_font_montserrat_36)
241 #endif
242 
243 #if LV_FONT_MONTSERRAT_38
244 LV_FONT_DECLARE(lv_font_montserrat_38)
245 #endif
246 
247 #if LV_FONT_MONTSERRAT_40
248 LV_FONT_DECLARE(lv_font_montserrat_40)
249 #endif
250 
251 #if LV_FONT_MONTSERRAT_42
252 LV_FONT_DECLARE(lv_font_montserrat_42)
253 #endif
254 
255 #if LV_FONT_MONTSERRAT_44
256 LV_FONT_DECLARE(lv_font_montserrat_44)
257 #endif
258 
259 #if LV_FONT_MONTSERRAT_46
260 LV_FONT_DECLARE(lv_font_montserrat_46)
261 #endif
262 
263 #if LV_FONT_MONTSERRAT_48
264 LV_FONT_DECLARE(lv_font_montserrat_48)
265 #endif
266 
267 #if LV_FONT_MONTSERRAT_28_COMPRESSED
268 LV_FONT_DECLARE(lv_font_montserrat_28_compressed)
269 #endif
270 
271 #if LV_FONT_DEJAVU_16_PERSIAN_HEBREW
272 LV_FONT_DECLARE(lv_font_dejavu_16_persian_hebrew)
273 #endif
274 
275 #if LV_FONT_SIMSUN_14_CJK
276 LV_FONT_DECLARE(lv_font_simsun_14_cjk)
277 #endif
278 
279 #if LV_FONT_SIMSUN_16_CJK
280 LV_FONT_DECLARE(lv_font_simsun_16_cjk)
281 #endif
282 
283 #if LV_FONT_UNSCII_8
284 LV_FONT_DECLARE(lv_font_unscii_8)
285 #endif
286 
287 #if LV_FONT_UNSCII_16
288 LV_FONT_DECLARE(lv_font_unscii_16)
289 #endif
290 
291 /*Declare the custom (user defined) fonts*/
292 #ifdef LV_FONT_CUSTOM_DECLARE
293 LV_FONT_CUSTOM_DECLARE
294 #endif
295 
296 /**
297  * Just a wrapper around LV_FONT_DEFAULT because it might be more convenient to use a function in some cases
298  * @return  pointer to LV_FONT_DEFAULT
299  */
300 const lv_font_t * lv_font_default(void);
301 
302 #ifdef __cplusplus
303 } /*extern "C"*/
304 #endif
305 
306 #endif /*USE_FONT*/
307