Correctly count characters. Also remove letter spaces from 0-width characters

This commit is contained in:
Brian Pugh
2019-01-06 17:43:57 -08:00
parent 61bb01e06d
commit 4b2cd9030c
+10 -5
View File
@@ -224,13 +224,13 @@ lv_coord_t lv_txt_get_width(const char * txt, uint16_t length,
if(txt == NULL) return 0; if(txt == NULL) return 0;
if(font == NULL) return 0; if(font == NULL) return 0;
uint32_t i = 0; uint32_t i = 0, j;
lv_coord_t width = 0; lv_coord_t width = 0;
lv_txt_cmd_state_t cmd_state = LV_TXT_CMD_STATE_WAIT; lv_txt_cmd_state_t cmd_state = LV_TXT_CMD_STATE_WAIT;
uint32_t letter; uint32_t letter;
if(length != 0) { if(length != 0) {
while(i < length) { for(j=0; j< length; j++){
letter = lv_txt_encoded_next(txt, &i); letter = lv_txt_encoded_next(txt, &i);
if((flag & LV_TXT_FLAG_RECOLOR) != 0) { if((flag & LV_TXT_FLAG_RECOLOR) != 0) {
if(lv_txt_is_cmd(&cmd_state, letter) != false) { 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); lv_coord_t char_width = lv_font_get_width(font, letter);
width += letter_space; 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; return width;