From b7c08e8aedd0414af5f41b1fc9391b34bac1e974 Mon Sep 17 00:00:00 2001 From: Brian Pugh Date: Fri, 4 Jan 2019 09:09:41 -0800 Subject: [PATCH] Proper word wrapping taking into account letter_space --- lv_misc/lv_txt.c | 56 +++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/lv_misc/lv_txt.c b/lv_misc/lv_txt.c index fc8ebec0c..a185f12fe 100644 --- a/lv_misc/lv_txt.c +++ b/lv_misc/lv_txt.c @@ -148,6 +148,7 @@ uint16_t lv_txt_get_next_line(const char * txt, const lv_font_t * font, uint32_t i = 0; lv_coord_t cur_w = 0; + lv_coord_t w_at_last_break = 0; uint32_t last_break = NO_BREAK_FOUND; lv_txt_cmd_state_t cmd_state = LV_TXT_CMD_STATE_WAIT; uint32_t letter = 0; @@ -176,36 +177,32 @@ uint16_t lv_txt_get_next_line(const char * txt, const lv_font_t * font, /*If the txt is too long then finish, this is the line end*/ if(cur_w > max_width) { /* Continue searching for next breakable character to see if the next word will fit */ - uint32_t i_tmp = i; - cur_w = 0; - while(txt[i_tmp] != 0) { - letter = lv_txt_encoded_next(txt, &i_tmp); - /*Check for new line chars*/ - if(letter == '\n' || letter == '\r') { - uint32_t i_tmp2 = i; - uint32_t letter_next = lv_txt_encoded_next(txt, &i_tmp2); - if(letter == '\r' && letter_next == '\n') i = i_tmp2; - break; - } - else if (is_break_char(letter)) { - break; + if( last_break != NO_BREAK_FOUND ) { + uint32_t i_tmp = i; + cur_w -= w_at_last_break; + while(txt[i_tmp] != 0) { + letter = lv_txt_encoded_next(txt, &i_tmp); + /*Check for new line chars*/ + if(letter == '\n' || letter == '\r') { + uint32_t i_tmp2 = i_tmp; + uint32_t letter_next = lv_txt_encoded_next(txt, &i_tmp2); + if(letter == '\r' && letter_next == '\n') i = i_tmp2; + i = last_break; + break; + } + else if (is_break_char(letter)) { + i = last_break; + break; + } + lv_coord_t letter_width = lv_font_get_width(font, letter); + cur_w += letter_width; + if(cur_w > max_width) { + /* Current letter already exceeds, return previous */ + lv_txt_encoded_prev(txt, &i); + break; + } + cur_w += letter_space; } - lv_coord_t letter_width = lv_font_get_width(font, letter); - cur_w += letter_width; - if(cur_w > max_width) { - return i; - } - } - - /*If this a break char then break here.*/ - if(is_break_char(letter)) { - /* Now 'i' points to the next char because of txt_utf8_next() - * But we need the first char of the next line so keep it. - * Hence do nothing here*/ - } - /*If already a break character is found, then break there*/ - if(last_break != NO_BREAK_FOUND) { - i = last_break; } else { /* Now this character is out of the area so it will be first character of the next line*/ /* But 'i' already points to the next character (because of lv_txt_utf8_next) step beck one*/ @@ -222,6 +219,7 @@ uint16_t lv_txt_get_next_line(const char * txt, const lv_font_t * font, * txt can be broken here later */ else if(is_break_char(letter)) { last_break = i; /*Save the first char index after break*/ + w_at_last_break = cur_w; } }