Tiny TTF font engine(微小的TTF字体引擎)
Usage(用法)
显示原文
Allow using TrueType fonts in LVGL.
Detailed introduction: https://github.com/nothings/stb
When enabled in lv_conf.h
with LV_USE_TINY_TTF
lv_tiny_ttf_create_data(data, data_size, font_size) can be used to
create a TTF font instance at the specified line height. You can then
use that font anywhere lv_font_t
is accepted.
By default, the TTF or OTF file must be embedded as an array, either in a header, or loaded into RAM in order to function.
However, if LV_TINY_TTF_FILE_SUPPORT
is enabled,
lv_tiny_ttf_create_file(path, font_size) will also be available,
allowing tiny_ttf to stream from a file. The file must remain open the
entire time the font is being used.
After a font is created, you can change the font size in pixels by using lv_tiny_ttf_set_size(font, font_size).
By default, a font will cache data for upto 256 glyphs elements to speed up rendering. This maximum can be changed by using lv_tiny_ttf_create_data_ex(data, data_size, font_size, kerning, cache_size) or lv_tiny_ttf_create_file_ex(path, font_size, kerning, cache_size) (when available). The cache size is indicated in number of entries. Kerning is whether to allow if supported, or disable.
允许在 LVGL 中使用 TrueType 字体。
详细介绍:https://github.com/nothings/stb
在 lv_conf.h
中启用 LV_USE_TINY_TTF
时,lv_tiny_ttf_create_data(data, data_size, font_size) 可用于在指定的行高处创建 TTF 字体实例。然后,您可以在任何地方使用该字体都是可以接受的。
默认情况下,TTF 或 OTF 文件必须作为数组嵌入,或者标头,或加载到 RAM 中才能运行。
但是,如果启用了 LV_TINY_TTF_FILE_SUPPORT
,则 lv_tiny_ttf_create_file(path, font_size) 也将可用, 允许tiny_ttf从文件流式传输。该文件必须保持打开状态使用字体的整个过程。
创建字体后,可以使用 lv_tiny_ttf_set_size(font, font_size) 更改字体大小(以像素为单位)。
默认情况下,字体会缓存最多256个字形元素的数据以加快渲染速度。这个最大值可以通过使用 lv_tiny_ttf_create_data_ex(data, data_size, font_size, kerning, cache_size) 或 :cpp:expr:`lv_tiny_ttf_create_file_ex(path, font_size, kerning, cache_size)`(如果可用)来更改。缓存大小以条目数量表示。Kerning 表示是否启用(如果支持的话)或禁用。
Example
Open a font with Tiny TTF from data array
C code
View on GitHub#include "../../lv_examples.h"
#if LV_USE_TINY_TTF && LV_BUILD_EXAMPLES
/**
* Load a font with Tiny_TTF
*/
void lv_example_tiny_ttf_1(void)
{
extern const uint8_t ubuntu_font[];
extern const int ubuntu_font_size;
/*Create style with the new font*/
static lv_style_t style;
lv_style_init(&style);
lv_font_t * font = lv_tiny_ttf_create_data(ubuntu_font, ubuntu_font_size, 30);
lv_style_set_text_font(&style, font);
lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);
/*Create a label with the new style*/
lv_obj_t * label = lv_label_create(lv_screen_active());
lv_obj_add_style(label, &style, 0);
lv_label_set_text(label, "Hello world\nI'm a font\ncreated\nwith Tiny TTF");
lv_obj_center(label);
}
#endif
Load a font with Tiny_TTF from file
C code
View on GitHub#include "../../lv_examples.h"
#if LV_USE_TINY_TTF && LV_TINY_TTF_FILE_SUPPORT && LV_BUILD_EXAMPLES
/**
* Load a font with Tiny_TTF from file
*/
void lv_example_tiny_ttf_2(void)
{
/*Create style with the new font*/
static lv_style_t style;
lv_style_init(&style);
lv_font_t * font = lv_tiny_ttf_create_file("A:lvgl/examples/libs/tiny_ttf/Ubuntu-Medium.ttf", 30);
lv_style_set_text_font(&style, font);
lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);
/*Create a label with the new style*/
lv_obj_t * label = lv_label_create(lv_screen_active());
lv_obj_add_style(label, &style, 0);
lv_label_set_text(label, "Hello world\nI'm a font\ncreated\nwith Tiny TTF");
lv_obj_center(label);
}
#endif
Change font size with Tiny_TTF
C code
View on GitHub#include "../../lv_examples.h"
#if LV_USE_TINY_TTF && LV_BUILD_EXAMPLES && LV_USE_OBSERVER
static void font_size_observer_cb(lv_observer_t * observer, lv_subject_t * subject);
static lv_subject_t subject_font;
/**
* Change font size with Tiny_TTF
*/
void lv_example_tiny_ttf_3(void)
{
extern const uint8_t ubuntu_font[];
extern const int ubuntu_font_size;
lv_subject_init_int(&subject_font, 25);
/*Create style with the new font*/
static lv_style_t style;
lv_style_init(&style);
lv_font_t * font = lv_tiny_ttf_create_data(ubuntu_font, ubuntu_font_size, 25);
lv_style_set_text_font(&style, font);
lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);
lv_obj_t * slider = lv_slider_create(lv_screen_active());
lv_obj_center(slider);
lv_slider_set_range(slider, 5, 50);
lv_obj_align(slider, LV_ALIGN_BOTTOM_MID, 0, -50);
lv_slider_bind_value(slider, &subject_font);
lv_obj_t * slider_label = lv_label_create(lv_screen_active());
lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
lv_label_bind_text(slider_label, &subject_font, "%d");
/*Create a label with the new style*/
lv_obj_t * label = lv_label_create(lv_screen_active());
lv_obj_add_style(label, &style, 0);
lv_obj_set_size(label, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_label_set_text(label, "Hello world!");
lv_obj_center(label);
lv_subject_add_observer(&subject_font, font_size_observer_cb, &style);
}
static void font_size_observer_cb(lv_observer_t * observer, lv_subject_t * subject)
{
lv_style_t * style = lv_observer_get_user_data(observer);
lv_style_value_t v;
lv_style_get_prop(style, LV_STYLE_TEXT_FONT, &v);
lv_font_t * font = (lv_font_t *) v.ptr;
int32_t size = lv_subject_get_int(subject);
lv_tiny_ttf_set_size(font, size);
lv_obj_report_style_change(style);
}
#endif
API
stb_truetype_htcw.h .. Autogenerated