diff --git a/src/lv_misc/lv_txt.c b/src/lv_misc/lv_txt.c index f89671518..b7570b1cb 100644 --- a/src/lv_misc/lv_txt.c +++ b/src/lv_misc/lv_txt.c @@ -266,6 +266,9 @@ static uint16_t lv_txt_get_next_word(const char * txt, const lv_font_t * font, /** * Get the next line of text. Check line length and break chars too. + * + * A line of txt includes the \n character. + * * @param txt a '\0' terminated string * @param font pointer to a font * @param letter_space letter space @@ -295,18 +298,13 @@ uint16_t lv_txt_get_next_line(const char * txt, const lv_font_t * font, i += advance; - if(txt[i] == '\n') break; - } + if(txt[0] == '\n') break; + + if(txt[i] == '\n'){ + i++; /* Include the following newline in the current line */ + break; + } - /* If this is the last of the string, make sure pointer is at NULL-terminator. - * This catches the case, for example of a string ending in "\n" */ - if(txt[i] != '\0'){ - uint32_t i_next = i; - int tmp; - uint32_t letter_next = lv_txt_encoded_next(txt, &i_next); /*Gets current character*/ - tmp = i_next; - letter_next = lv_txt_encoded_next(txt, &i_next); /*Gets subsequent character*/ - if(letter_next == '\0') i = tmp; } /*Always step at least one to avoid infinite loops*/ diff --git a/src/lv_objx/lv_label.c b/src/lv_objx/lv_label.c index b52f06052..c9f3b81a1 100644 --- a/src/lv_objx/lv_label.c +++ b/src/lv_objx/lv_label.c @@ -661,7 +661,15 @@ uint16_t lv_label_get_letter_on(const lv_obj_t * label, lv_point_t * pos) while(txt[line_start] != '\0') { new_line_start += lv_txt_get_next_line(&txt[line_start], font, style->text.letter_space, max_w, flag); - if(pos->y <= y + letter_height) break; /*The line is found (stored in 'line_start')*/ + if(pos->y <= y + letter_height) { + /*The line is found (stored in 'line_start')*/ + /* Include the NULL terminator in the last line */ + uint32_t tmp = new_line_start; + uint32_t letter; + letter = lv_txt_encoded_prev(txt, &tmp); + if(letter != '\n' && txt[new_line_start] == '\0' ) new_line_start++; + break; + } y += letter_height + style->text.line_space; line_start = new_line_start; @@ -682,31 +690,29 @@ uint16_t lv_label_get_letter_on(const lv_obj_t * label, lv_point_t * pos) uint32_t letter; uint32_t letter_next; - if(new_line_start > 0) { - while(i < new_line_start) { - /* Get the current letter.*/ - letter = lv_txt_encoded_next(txt, &i); + while(i < new_line_start) { + /* Get the current letter.*/ + letter = lv_txt_encoded_next(txt, &i); - /*Get the next letter too for kerning*/ - letter_next = lv_txt_encoded_next(&txt[i], NULL); + /*Get the next letter too for kerning*/ + letter_next = lv_txt_encoded_next(&txt[i], NULL); - /*Handle the recolor command*/ - if((flag & LV_TXT_FLAG_RECOLOR) != 0) { - if(lv_txt_is_cmd(&cmd_state, txt[i]) != false) { - continue; /*Skip the letter is it is part of a command*/ - } + /*Handle the recolor command*/ + if((flag & LV_TXT_FLAG_RECOLOR) != 0) { + if(lv_txt_is_cmd(&cmd_state, txt[i]) != false) { + continue; /*Skip the letter is it is part of a command*/ } - - x += lv_font_get_glyph_width(font, letter, letter_next); - - /*Finish if the x position or the last char of the line is reached*/ - if(pos->x < x || i == new_line_start) { - i = i_act; - break; - } - x += style->text.letter_space; - i_act = i; } + + x += lv_font_get_glyph_width(font, letter, letter_next); + + /*Finish if the x position or the last char of the line is reached*/ + if(pos->x < x || i == new_line_start) { + i = i_act; + break; + } + x += style->text.letter_space; + i_act = i; } return lv_encoded_get_char_id(txt, i);