FreeType 支持

FreeType 接口,用于在运行时生成字体位图。

安装 FreeType

  • 这里下载 FreeType

  • make

  • sudo make install

将 FreeType 添加到您的项目

  • 添加包含路径:/usr/include/freetype2(对于 GCC:-I/usr/include/freetype2 -L/usr/local/lib

  • 添加库:freetype(对于 GCC:-L/usr/local/lib -lfreetype

使用方法

lv_conf.h 中启用 LV_USE_FREETYPE

要缓存已打开字体的字形,请设置 LV_FREETYPE_CACHE_SIZE >= 0,然后使用以下宏进行详细配置:

  1. LV_FREETYPE_CACHE_SIZE:用于缓存字体位图、轮廓、字符映射等的最大内存(字节)。0 表示使用系统默认值,小于 0 表示禁用缓存。注意:此值不包括管理的 FT_Face 和 FT_Size 对象。

  2. LV_FREETYPE_CACHE_FT_FACES:此缓存实例管理的最大打开 FT_Face 对象数。0 表示使用系统默认值。仅当 LV_FREETYPE_CACHE_SIZE >= 0 时有用。

  3. LV_FREETYPE_CACHE_FT_SIZES:此缓存实例管理的最大打开 FT_Size 对象数。0 表示使用系统默认值。仅当 LV_FREETYPE_CACHE_SIZE >= 0 时有用。

当您确定所有使用的字体大小不会超过 256 时,可以启用 LV_FREETYPE_SBIT_CACHE,这对于小位图来说更加节省内存。

您可以使用 lv_ft_font_init() 创建 FreeType 字体。返回 true 表示成功,同时,lv_ft_info_tfont 成员将被填充为指向 LVGL 字体的指针,您可以像使用任何 LVGL 字体一样使用它。

字体样式支持加粗和斜体,您可以使用以下宏进行设置:

  1. FT_FONT_STYLE_NORMAL:默认样式。

  2. FT_FONT_STYLE_ITALIC:斜体样式。

  3. FT_FONT_STYLE_BOLD:加粗样式。

它们可以组合使用。例如:FT_FONT_STYLE_BOLD | FT_FONT_STYLE_ITALIC

注意,FreeType 扩展不使用 LVGL 的文件系统。 您可以直接传递字体的路径,就像在您的操作系统或平台上通常使用的一样。

示例

使用 FreeType 打开字体

C code  

 GitHub
#include "../../lv_examples.h"
#if LV_BUILD_EXAMPLES
#if LV_USE_FREETYPE

/**
 * Load a font with FreeType
 */
void lv_example_freetype_1(void)
{
    /*Create a font*/
    static lv_ft_info_t info;
    /*FreeType uses C standard file system, so no driver letter is required.*/
    info.name = "./lvgl/examples/libs/freetype/Lato-Regular.ttf";
    info.weight = 24;
    info.style = FT_FONT_STYLE_NORMAL;
    info.mem = NULL;
    if(!lv_ft_font_init(&info)) {
        LV_LOG_ERROR("create failed.");
    }

    /*Create style with the new font*/
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_text_font(&style, info.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_scr_act());
    lv_obj_add_style(label, &style, 0);
    lv_label_set_text(label, "Hello world\nI'm a font created with FreeType");
    lv_obj_center(label);
}
#else

void lv_example_freetype_1(void)
{
    /*TODO
     *fallback for online examples*/

    lv_obj_t * label = lv_label_create(lv_scr_act());
    lv_label_set_text(label, "FreeType is not installed");
    lv_obj_center(label);
}

#endif
#endif

MicroPython code  

 GitHub Simulator
#!/opt/bin/lv_micropython -i
import lvgl as lv
import display_driver
import fs_driver

info = lv.ft_info_t()
info.name ="./Lato-Regular.ttf"
info.weight = 24
info.style = lv.FT_FONT_STYLE.NORMAL
info.font_init()

# Create style with the new font
style = lv.style_t()
style.init()
style.set_text_font(info.font)
style.set_text_align(lv.TEXT_ALIGN.CENTER)

# Create a label with the new style
label = lv.label(lv.scr_act())
label.add_style(style, 0)
label.set_text("Hello world\nI'm a font created with FreeType")
label.center()

了解更多

API

Enums

enum LV_FT_FONT_STYLE

Values:

enumerator FT_FONT_STYLE_NORMAL
enumerator FT_FONT_STYLE_ITALIC
enumerator FT_FONT_STYLE_BOLD

Functions

bool lv_freetype_init(uint16_t max_faces, uint16_t max_sizes, uint32_t max_bytes)

init freetype library

Parameters
  • max_faces -- Maximum number of opened FT_Face objects managed by this cache instance. Use 0 for defaults.

  • max_sizes -- Maximum number of opened FT_Size objects managed by this cache instance. Use 0 for defaults.

  • max_bytes -- Maximum number of bytes to use for cached data nodes. Use 0 for defaults. Note that this value does not account for managed FT_Face and FT_Size objects.

Returns

true on success, otherwise false.

void lv_freetype_destroy(void)

Destroy freetype library

bool lv_ft_font_init(lv_ft_info_t *info)

Creates a font with info parameter specified.

Parameters

info -- See lv_ft_info_t for details. when success, lv_ft_info_t->font point to the font you created.

Returns

true on success, otherwise false.

void lv_ft_font_destroy(lv_font_t *font)

Destroy a font that has been created.

Parameters

font -- pointer to font.

struct lv_ft_info_t

Public Members

const char *name
const void *mem
size_t mem_size
lv_font_t *font
uint16_t weight
uint16_t style