1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00

Proper word wrapping taking into account letter_space

This commit is contained in:
Brian Pugh 2019-01-04 09:09:41 -08:00
parent 0a0df60f77
commit b7c08e8aed

View File

@ -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;
}
}