BinFont Loader
lv_binfont_create can be used to load a font from a file. The font needs to have a special binary format.
Overview
lv_binfont_create can be used to load a font from a file. The font needs
to have a special binary format. (Not TTF or WOFF). Use
lv_font_conv with the
--format bin option to generate an LVGL compatible font file.
Loading from File
To load a font LVGL's filesystem needs to be enabled and a driver must be added.
This is how you can use it in practice:
lv_font_t *my_font = lv_binfont_create("A:/path/to/my_font.bin");
if(my_font == NULL) return;
/* Use the font */
/* Free the font if not required anymore */
lv_binfont_destroy(my_font);Loading from Memory
lv_binfont_create_from_buffer can be used to load a font from a memory buffer.
This function may be useful to load a font from an external file system, which is not
supported by LVGL. The font needs to be in the same format as if it were loaded from a file.
To load a font from a buffer LVGL's filesystem needs to be enabled and the MEMFS driver must be added.
This is how you can use it in practice:
lv_font_t *my_font;
uint8_t *buf;
uint32_t bufsize;
/* Read font file into the buffer from the external file system */
...
/* Load font from the buffer */
my_font = lv_binfont_create_from_buffer((void *)buf, buf));
if(my_font == NULL) return;
/* Use the font */
/* Free the font if not required anymore */
lv_binfont_destroy(my_font);Dynamic Glyph Loading
Overview
By default lv_binfont_create() reads the whole font into RAM, glyph bitmaps included. For a
large font the bitmaps are by far the biggest part of it.
lv_binfont_create_ex can load the same font with dynamic_glyph_load set,
in which case only the glyph descriptors, the character maps and the kerning data are kept in
RAM. The glyph bitmaps stay in the font file and are read when a glyph is actually drawn, into
a scratch buffer that is only as large as the biggest glyph of the font. Depending on the font
this typically cuts the RAM usage by an order of magnitude.
This is decided per font, so a font that is drawn constantly can be loaded normally while a rarely used one is loaded dynamically. It works with both plain and compressed fonts.
The font file is kept open for as long as the font is alive. lv_binfont_destroy() closes it.
Every drawn glyph costs a seek and a read in the font file, so the file system should be reasonably fast.
This is how you can use it in practice:
const lv_binfont_dsc_t dsc = {
.path = "A:/path/to/my_font.bin",
.dynamic_glyph_load = true,
};
lv_font_t * my_font = lv_binfont_create_ex(&dsc);
if(my_font == NULL) return;
/* Use the font */
/* Free the font and close the font file */
lv_binfont_destroy(my_font);The same works when loading from memory, by setting buffer and buffer_size instead of
path, and when a font is registered in the font manager,
by setting dynamic_glyph_load in lv_binfont_font_src_t.
Last updated on