Proper word wrapping taking into account letter_space
@@ -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 */
+ if( last_break != NO_BREAK_FOUND ) {
uint32_t i_tmp = i;
- cur_w = 0;
+ 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;
+ 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) {
- return i;
- }
+ /* Current letter already exceeds, return previous */
+ lv_txt_encoded_prev(txt, &i);
+ break;
}
-
- /*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*/
+ cur_w += letter_space;
}
- /*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;
}
}
