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:
6
examples/others/imgfont/index.rst
Normal file
6
examples/others/imgfont/index.rst
Normal file
@@ -0,0 +1,6 @@
|
||||
Use emojis in a text.
|
||||
"""""""""""""""""""""""""""""""""""""""""""""""
|
||||
|
||||
.. lv_example:: others/imgfont/lv_example_imgfont_1
|
||||
:language: c
|
||||
|
||||
38
examples/others/imgfont/lv_example_imgfont.h
Normal file
38
examples/others/imgfont/lv_example_imgfont.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* @file lv_example_imgfont.h
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LV_EXAMPLE_IMGFONT_H
|
||||
#define LV_EXAMPLE_IMGFONT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
void lv_example_imgfont_1(void);
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /*extern "C"*/
|
||||
#endif
|
||||
|
||||
#endif /*LV_EXAMPLE_IMGFONT_H*/
|
||||
54
examples/others/imgfont/lv_example_imgfont_1.c
Normal file
54
examples/others/imgfont/lv_example_imgfont_1.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#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
|
||||
@@ -17,6 +17,7 @@ extern "C" {
|
||||
#include "monkey/lv_example_monkey.h"
|
||||
#include "gridnav/lv_example_gridnav.h"
|
||||
#include "fragment/lv_example_fragment.h"
|
||||
#include "imgfont/lv_example_imgfont.h"
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
|
||||
Reference in New Issue
Block a user