feat(font): add imgfont - can be used to add emojis to label/span (#3160)

* feature(font): draw img in label/span

* add to tests

Co-authored-by: Gabor Kiss-Vamosi <kisvegabor@gmail.com>
This commit is contained in:
guoweilkd
2022-03-17 17:18:14 +08:00
committed by GitHub
parent 7153e3f8b7
commit 7713327d61
16 changed files with 676 additions and 0 deletions

View File

@@ -172,6 +172,24 @@ LV_ATTRIBUTE_FAST_MEM static void draw_letter_normal(lv_draw_ctx_t * draw_ctx, c
uint32_t shades;
if(bpp == 3) bpp = 4;
#if LV_USE_IMGFONT
if(bpp == LV_IMGFONT_BPP) { //is imgfont
lv_area_t fill_area;
fill_area.x1 = pos->x;
fill_area.y1 = pos->y;
fill_area.x2 = pos->x + g->box_w - 1;
fill_area.y2 = pos->y + g->box_h - 1;
lv_draw_img_dsc_t img_dsc;
lv_draw_img_dsc_init(&img_dsc);
img_dsc.angle = 0;
img_dsc.zoom = LV_IMG_ZOOM_NONE;
img_dsc.opa = dsc->opa;
img_dsc.blend_mode = dsc->blend_mode;
lv_draw_img(draw_ctx, &img_dsc, &fill_area, map_p);
return;
}
#endif
switch(bpp) {
case 1:
bpp_opa_table_p = _lv_bpp1_opa_table;

View File

@@ -0,0 +1,126 @@
/**
* @file lv_imgfont.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_imgfont.h"
#if LV_USE_IMGFONT
/*********************
* DEFINES
*********************/
#define LV_IMGFONT_PATH_MAX_LEN 64
/**********************
* TYPEDEFS
**********************/
typedef struct {
lv_font_t * font;
lv_get_imgfont_path_cb_t path_cb;
char path[LV_IMGFONT_PATH_MAX_LEN];
} imgfont_dsc_t;
/**********************
* STATIC PROTOTYPES
**********************/
static const uint8_t * imgfont_get_glyph_bitmap(const lv_font_t * font, uint32_t unicode);
static bool imgfont_get_glyph_dsc(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out,
uint32_t unicode, uint32_t unicode_next);
/**********************
* STATIC VARIABLES
**********************/
/**********************
* GLOBAL PROTOTYPES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
lv_font_t * lv_imgfont_create(uint16_t height, lv_get_imgfont_path_cb_t path_cb)
{
LV_ASSERT_MSG(LV_IMGFONT_PATH_MAX_LEN > sizeof(lv_img_dsc_t),
"LV_IMGFONT_PATH_MAX_LEN must be greater than sizeof(lv_img_dsc_t)");
size_t size = sizeof(imgfont_dsc_t) + sizeof(lv_font_t);
imgfont_dsc_t * dsc = (imgfont_dsc_t *)lv_mem_alloc(size);
if(dsc == NULL) return NULL;
lv_memset_00(dsc, size);
dsc->font = (lv_font_t *)(((char *)dsc) + sizeof(imgfont_dsc_t));
dsc->path_cb = path_cb;
lv_font_t * font = dsc->font;
font->dsc = dsc;
font->get_glyph_dsc = imgfont_get_glyph_dsc;
font->get_glyph_bitmap = imgfont_get_glyph_bitmap;
font->subpx = LV_FONT_SUBPX_NONE;
font->line_height = height;
font->base_line = 0;
font->underline_position = 0;
font->underline_thickness = 0;
return dsc->font;
}
void lv_imgfont_destroy(lv_font_t * font)
{
if(font == NULL) {
return;
}
imgfont_dsc_t * dsc = (imgfont_dsc_t *)font->dsc;
lv_mem_free(dsc);
}
/**********************
* STATIC FUNCTIONS
**********************/
static const uint8_t * imgfont_get_glyph_bitmap(const lv_font_t * font, uint32_t unicode)
{
LV_UNUSED(unicode);
LV_ASSERT_NULL(font);
imgfont_dsc_t * dsc = (imgfont_dsc_t *)font->dsc;
return (uint8_t *)dsc->path;
}
static bool imgfont_get_glyph_dsc(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out,
uint32_t unicode, uint32_t unicode_next)
{
LV_ASSERT_NULL(font);
imgfont_dsc_t * dsc = (imgfont_dsc_t *)font->dsc;
LV_ASSERT_NULL(dsc);
if(dsc->path_cb == NULL) return false;
if(!dsc->path_cb(dsc->font, dsc->path, LV_IMGFONT_PATH_MAX_LEN, unicode, unicode_next)) {
return false;
}
lv_img_header_t header;
if(lv_img_decoder_get_info(dsc->path, &header) != LV_RES_OK) {
return false;
}
dsc_out->is_placeholder = 0;
dsc_out->adv_w = header.w;
dsc_out->box_w = header.w;
dsc_out->box_h = header.h;
dsc_out->bpp = LV_IMGFONT_BPP; /* is image identifier */
dsc_out->ofs_x = 0;
dsc_out->ofs_y = 0;
return true;
}
#endif /*LV_USE_IMGFONT*/

View File

@@ -0,0 +1,60 @@
/**
* @file lv_imgfont.h
*
*/
#ifndef LV_IMGFONT_H
#define LV_IMGFONT_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************
* INCLUDES
*********************/
#include "../../../lvgl.h"
#if LV_USE_IMGFONT
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/* gets the image path name of this character */
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);
/**********************
* GLOBAL PROTOTYPES
**********************/
/**
* Creates a image font with info parameter specified.
* @param height font size
* @param path_cb a function to get the image path name of character.
* @return pointer to the new imgfont or NULL if create error.
*/
lv_font_t * lv_imgfont_create(uint16_t height, lv_get_imgfont_path_cb_t path_cb);
/**
* Destroy a image font that has been created.
* @param font pointer to image font handle.
*/
void lv_imgfont_destroy(lv_font_t * font);
/**********************
* MACROS
**********************/
#endif /*LV_USE_IMGFONT*/
#ifdef __cplusplus
} /*extern "C"*/
#endif
#endif /* LV_IMGFONT_H */

View File

@@ -17,6 +17,7 @@ extern "C" {
#include "monkey/lv_monkey.h"
#include "gridnav/lv_gridnav.h"
#include "fragment/lv_fragment.h"
#include "imgfont/lv_imgfont.h"
/*********************
* DEFINES

View File

@@ -25,6 +25,9 @@ extern "C" {
* DEFINES
*********************/
/* imgfont identifier */
#define LV_IMGFONT_BPP 9
/**********************
* TYPEDEFS
**********************/

View File

@@ -2167,6 +2167,15 @@
#endif
#endif
/*draw img in label or span obj */
#ifndef LV_USE_IMGFONT
#ifdef CONFIG_LV_USE_IMGFONT
#define LV_USE_IMGFONT CONFIG_LV_USE_IMGFONT
#else
#define LV_USE_IMGFONT 0
#endif
#endif
/*==================
* EXAMPLES
*==================*/