fix(draw): center imgfont's drawing position (#3668)
Signed-off-by: pengyiqiang <pengyiqiang@xiaomi.com> Co-authored-by: pengyiqiang <pengyiqiang@xiaomi.com> Co-authored-by: Gabor Kiss-Vamosi <kisvegabor@gmail.com> Co-authored-by: W-Mai <1341398182@qq.com>
This commit is contained in:
@@ -7,10 +7,11 @@
|
||||
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,
|
||||
void * user_data)
|
||||
lv_coord_t * offset_y, void * user_data)
|
||||
{
|
||||
LV_UNUSED(font);
|
||||
LV_UNUSED(unicode_next);
|
||||
LV_UNUSED(offset_y);
|
||||
LV_UNUSED(user_data);
|
||||
LV_ASSERT_NULL(img_src);
|
||||
|
||||
@@ -22,11 +23,10 @@ static bool get_imgfont_path(const lv_font_t * font, void * img_src,
|
||||
else {
|
||||
char * path = (char *)img_src;
|
||||
#if LV_USE_FFMPEG
|
||||
snprintf(path, len, "%s/%04X.%s", "lvgl/examples/assets/emoji", unicode, "png");
|
||||
lv_snprintf(path, len, "%s/%04X.png", "lvgl/examples/assets/emoji", unicode);
|
||||
#elif LV_USE_PNG
|
||||
snprintf(path, len, "%s/%04X.%s", "A:lvgl/examples/assets/emoji", unicode, "png");
|
||||
lv_snprintf(path, len, "%s/%04X.png", "A:lvgl/examples/assets/emoji", unicode);
|
||||
#endif
|
||||
path[len - 1] = '\0';
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -40,6 +40,7 @@ void lv_example_imgfont_1(void)
|
||||
lv_font_t * imgfont = lv_imgfont_create(80, get_imgfont_path, NULL);
|
||||
if(imgfont == NULL) {
|
||||
LV_LOG_ERROR("imgfont init error");
|
||||
return;
|
||||
}
|
||||
|
||||
imgfont->fallback = LV_FONT_DEFAULT;
|
||||
|
||||
@@ -14,7 +14,7 @@ try:
|
||||
except NameError:
|
||||
script_path = ''
|
||||
|
||||
def get_imgfont_path(font, img_src, length, unicode, unicode_next,user_data) :
|
||||
def get_imgfont_path(font, img_src, length, unicode, unicode_next, offset_y, user_data) :
|
||||
if unicode < 0xf600:
|
||||
return
|
||||
if LV_USE_FFMPEG:
|
||||
@@ -31,6 +31,7 @@ def get_imgfont_path(font, img_src, length, unicode, unicode_next,user_data) :
|
||||
imgfont = lv.imgfont_create(80, get_imgfont_path, None)
|
||||
if imgfont == None:
|
||||
print("imgfont init error")
|
||||
sys.exit(1)
|
||||
|
||||
imgfont.fallback = LV_FONT_DEFAULT
|
||||
|
||||
|
||||
@@ -130,9 +130,21 @@ void lv_draw_sw_letter(lv_draw_ctx_t * draw_ctx, const lv_draw_label_dsc_t * dsc
|
||||
/*Don't draw anything if the character is empty. E.g. space*/
|
||||
if((g.box_h == 0) || (g.box_w == 0)) return;
|
||||
|
||||
lv_coord_t real_h;
|
||||
#if LV_USE_IMGFONT
|
||||
if(g.bpp == LV_IMGFONT_BPP) {
|
||||
/*Center imgfont's drawing position*/
|
||||
real_h = (dsc->font->line_height - g.box_h) / 2;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
real_h = (dsc->font->line_height - dsc->font->base_line) - g.box_h;
|
||||
}
|
||||
|
||||
lv_point_t gpos;
|
||||
gpos.x = pos_p->x + g.ofs_x;
|
||||
gpos.y = pos_p->y + (dsc->font->line_height - dsc->font->base_line) - g.box_h - g.ofs_y;
|
||||
gpos.y = pos_p->y + real_h - g.ofs_y;
|
||||
|
||||
/*If the letter is completely out of mask don't draw it*/
|
||||
if(gpos.x + g.box_w < draw_ctx->clip_area->x1 ||
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
**********************/
|
||||
typedef struct {
|
||||
lv_font_t * font;
|
||||
lv_get_imgfont_path_cb_t path_cb;
|
||||
lv_imgfont_get_path_cb_t path_cb;
|
||||
void * user_data;
|
||||
char path[LV_IMGFONT_PATH_MAX_LEN];
|
||||
} imgfont_dsc_t;
|
||||
@@ -46,7 +46,7 @@ static bool imgfont_get_glyph_dsc(const lv_font_t * font, lv_font_glyph_dsc_t *
|
||||
/**********************
|
||||
* GLOBAL FUNCTIONS
|
||||
**********************/
|
||||
lv_font_t * lv_imgfont_create(uint16_t height, lv_get_imgfont_path_cb_t path_cb, void * user_data)
|
||||
lv_font_t * lv_imgfont_create(uint16_t height, lv_imgfont_get_path_cb_t path_cb, void * user_data)
|
||||
{
|
||||
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)");
|
||||
@@ -75,9 +75,7 @@ lv_font_t * lv_imgfont_create(uint16_t height, lv_get_imgfont_path_cb_t path_cb,
|
||||
|
||||
void lv_imgfont_destroy(lv_font_t * font)
|
||||
{
|
||||
if(font == NULL) {
|
||||
return;
|
||||
}
|
||||
LV_ASSERT_NULL(font);
|
||||
|
||||
imgfont_dsc_t * dsc = (imgfont_dsc_t *)font->dsc;
|
||||
lv_free(dsc);
|
||||
@@ -104,7 +102,9 @@ static bool imgfont_get_glyph_dsc(const lv_font_t * font, lv_font_glyph_dsc_t *
|
||||
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, dsc->user_data)) {
|
||||
lv_coord_t offset_y = 0;
|
||||
|
||||
if(!dsc->path_cb(dsc->font, dsc->path, LV_IMGFONT_PATH_MAX_LEN, unicode, unicode_next, &offset_y, dsc->user_data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -134,7 +134,7 @@ static bool imgfont_get_glyph_dsc(const lv_font_t * font, lv_font_glyph_dsc_t *
|
||||
dsc_out->box_h = img_header->h;
|
||||
dsc_out->bpp = LV_IMGFONT_BPP; /* is image identifier */
|
||||
dsc_out->ofs_x = 0;
|
||||
dsc_out->ofs_y = 0;
|
||||
dsc_out->ofs_y = offset_y;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -26,9 +26,9 @@ extern "C" {
|
||||
**********************/
|
||||
|
||||
/* gets the image path name of this character */
|
||||
typedef bool (*lv_get_imgfont_path_cb_t)(const lv_font_t * font, void * img_src,
|
||||
typedef bool (*lv_imgfont_get_path_cb_t)(const lv_font_t * font, void * img_src,
|
||||
uint16_t len, uint32_t unicode, uint32_t unicode_next,
|
||||
void * user_data);
|
||||
lv_coord_t * offset_y, void * user_data);
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
@@ -40,7 +40,7 @@ typedef bool (*lv_get_imgfont_path_cb_t)(const lv_font_t * font, void * img_src,
|
||||
* @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, void * user_data);
|
||||
lv_font_t * lv_imgfont_create(uint16_t height, lv_imgfont_get_path_cb_t path_cb, void * user_data);
|
||||
|
||||
/**
|
||||
* Destroy a image font that has been created.
|
||||
|
||||
Reference in New Issue
Block a user