From 4b2cd9030c062c5c2871c2f309fb0f2847ec379c Mon Sep 17 00:00:00 2001 From: Brian Pugh Date: Sun, 6 Jan 2019 17:43:57 -0800 Subject: [PATCH] Correctly count characters. Also remove letter spaces from 0-width characters --- lv_misc/lv_txt.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lv_misc/lv_txt.c b/lv_misc/lv_txt.c index 0b16c17d1..11ae7e559 100644 --- a/lv_misc/lv_txt.c +++ b/lv_misc/lv_txt.c @@ -224,13 +224,13 @@ lv_coord_t lv_txt_get_width(const char * txt, uint16_t length, if(txt == NULL) return 0; if(font == NULL) return 0; - uint32_t i = 0; + uint32_t i = 0, j; lv_coord_t width = 0; lv_txt_cmd_state_t cmd_state = LV_TXT_CMD_STATE_WAIT; uint32_t letter; if(length != 0) { - while(i < length) { + for(j=0; j< length; j++){ letter = lv_txt_encoded_next(txt, &i); if((flag & LV_TXT_FLAG_RECOLOR) != 0) { if(lv_txt_is_cmd(&cmd_state, letter) != false) { @@ -238,11 +238,16 @@ lv_coord_t lv_txt_get_width(const char * txt, uint16_t length, } } - width += lv_font_get_width(font, letter); - width += letter_space; + lv_coord_t char_width = lv_font_get_width(font, letter); + if(char_width > 0){ + width += lv_font_get_width(font, letter); + width += letter_space; + } } - width -= letter_space; /*Trim the last letter space. Important if the text is center aligned */ + if(width > 0) { + width -= letter_space; /*Trim the last letter space. Important if the text is center aligned */ + } } return width;