diff --git a/src/misc/lv_text.c b/src/misc/lv_text.c index 4ea5534128ad..18306ce3141b 100644 --- a/src/misc/lv_text.c +++ b/src/misc/lv_text.c @@ -399,8 +399,40 @@ int32_t lv_text_get_width(const char * txt, uint32_t length, const lv_font_t * f uint32_t letter_next; lv_text_encoded_letter_next_2(txt, &letter, &letter_next, &i); - /*TODO: Check foor recolor flag*/ - if(0 /*(flag & LV_TEXT_FLAG_RECOLOR) != 0*/) { + int32_t char_width = lv_font_get_glyph_width(font, letter, letter_next); + if(char_width > 0) { + width += char_width; + width += letter_space; + } + } + + if(width > 0) { + width -= letter_space; /*Trim the last letter space. Important if the text is center + aligned*/ + } + } + + return width; +} + +int32_t lv_text_get_width_with_flags(const char * txt, uint32_t length, const lv_font_t * font, int32_t letter_space, + lv_text_flag_t flags) +{ + if(txt == NULL) return 0; + if(font == NULL) return 0; + if(txt[0] == '\0') return 0; + + uint32_t i = 0; + int32_t width = 0; + lv_text_cmd_state_t cmd_state = LV_TEXT_CMD_STATE_WAIT; + + if(length != 0) { + while(i < length) { + uint32_t letter; + uint32_t letter_next; + lv_text_encoded_letter_next_2(txt, &letter, &letter_next, &i); + + if((flags & LV_TEXT_FLAG_RECOLOR) != 0) { if(lv_text_is_cmd(&cmd_state, letter) != false) { continue; } diff --git a/src/misc/lv_text.h b/src/misc/lv_text.h index 3b57e5158ae9..f14297f83c0f 100644 --- a/src/misc/lv_text.h +++ b/src/misc/lv_text.h @@ -94,6 +94,19 @@ void lv_text_get_size(lv_point_t * size_res, const char * text, const lv_font_t */ int32_t lv_text_get_width(const char * txt, uint32_t length, const lv_font_t * font, int32_t letter_space); +/** + * Give the length of a text with a given font + * @param txt a '\0' terminate string + * @param length length of 'txt' in byte count and not characters (Á is 1 character but 2 bytes in + * UTF-8) + * @param font pointer to a font + * @param letter_space letter space + * @param flags settings for the text from ::lv_text_flag_t + * @return length of a char_num long text + */ +int32_t lv_text_get_width_with_flags(const char * txt, uint32_t length, const lv_font_t * font, int32_t letter_space, + lv_text_flag_t flags); + /** * Check if c is command state * @param state