图像字体 (imgfont)

使用 imgfont 在标签或 span 对象中绘制图像。 这通常用于在文本中显示 Unicode 表情符号图标。 支持的图像格式:由 LVGL 图像解码器决定。

使用方法

lv_conf.h 中启用 LV_USE_IMGFONT

使用 lv_imgfont_create(height, path_cb) 创建一个新的 imgfont。

height 用于指示 imgfont 的大小。 path_cb 用于获取指定 Unicode 的图像路径。

使用 lv_imgfont_destroy(imgfont) 销毁不再使用的 imgfont。

示例

在文本中使用表情符号

C code  

 GitHub
#include "../../lv_examples.h"
#include <stdio.h>

#if LV_BUILD_EXAMPLES
#if LV_USE_IMGFONT

LV_IMG_DECLARE(emoji_F617)
static bool get_imgfont_path(const lv_font_t * font, void * img_src,
                             uint16_t len, uint32_t unicode, uint32_t unicode_next)
{
    LV_UNUSED(font);
    LV_UNUSED(unicode_next);
    LV_ASSERT_NULL(img_src);

    if(unicode == 0xF617) {
        memcpy(img_src, &emoji_F617, sizeof(lv_img_dsc_t));
    }
    else {
        char * path = (char *)img_src;
        snprintf(path, len, "%s/%04X.%s", "A:lvgl/examples/assets/emoji", unicode, "png");
        path[len - 1] = '\0';
    }

    return true;
}

/**
 * draw img in label or span obj
 */
void lv_example_imgfont_1(void)
{
    lv_font_t * imgfont = lv_imgfont_create(80, get_imgfont_path);
    if(imgfont == NULL) {
        LV_LOG_ERROR("imgfont init error");
    }

    imgfont->fallback = LV_FONT_DEFAULT;

    lv_obj_t * label1 = lv_label_create(lv_scr_act());
    lv_label_set_text(label1, "12\uF600\uF617AB");
    lv_obj_set_style_text_font(label1, imgfont, LV_PART_MAIN);
    lv_obj_center(label1);
}
#else

void lv_example_imgfont_1(void)
{
    lv_obj_t * label = lv_label_create(lv_scr_act());
    lv_label_set_text(label, "imgfont is not installed");
    lv_obj_center(label);
}

#endif
#endif

MicroPython code  

 GitHub Simulator
Error encountered while trying to open D:\lv_port_pc_eclipse-release-v8.3\lvgl\examples\others\imgfont\lv_example_imgfont_1.py

API

Typedefs

typedef bool (*lv_get_imgfont_path_cb_t)(const lv_font_t *font, void *img_src, uint16_t len, uint32_t unicode, uint32_t unicode_next)

Functions

lv_font_t *lv_imgfont_create(uint16_t height, lv_get_imgfont_path_cb_t path_cb)

Creates a image font with info parameter specified.

Parameters
  • height -- font size

  • path_cb -- a function to get the image path name of character.

Returns

pointer to the new imgfont or NULL if create error.

void lv_imgfont_destroy(lv_font_t *font)

Destroy a image font that has been created.

Parameters

font -- pointer to image font handle.