mirror of
https://github.com/lvgl/lvgl.git
synced 2025-01-28 07:03:00 +08:00
long text wrapping cleanup
This commit is contained in:
parent
44b2197721
commit
2f5ec6befe
@ -175,16 +175,8 @@ static uint16_t lv_txt_get_next_word(const char * txt, const lv_font_t * font,
|
|||||||
letter = lv_txt_encoded_next(txt, &i_next);
|
letter = lv_txt_encoded_next(txt, &i_next);
|
||||||
i_next_next = i_next;
|
i_next_next = i_next;
|
||||||
|
|
||||||
if(letter == '\n' || letter == '\r' || is_break_char(letter)) {
|
|
||||||
//printf("pre_while_loop: %d\n", i_next);
|
|
||||||
return i_next;
|
|
||||||
}
|
|
||||||
|
|
||||||
while(txt[i] != '\0') {
|
while(txt[i] != '\0') {
|
||||||
//printf("top of while %d\n", i);
|
|
||||||
letter_next = lv_txt_encoded_next(txt, &i_next_next);
|
letter_next = lv_txt_encoded_next(txt, &i_next_next);
|
||||||
//printf("i: %d i_next: %d i_next_next: %d txt[i]:\"%c\" letter:\"%c\"\n", i, i_next, i_next_next, txt[i], letter);
|
|
||||||
//assert(txt[i]==letter);
|
|
||||||
word_len++;
|
word_len++;
|
||||||
|
|
||||||
/*Handle the recolor command*/
|
/*Handle the recolor command*/
|
||||||
@ -197,13 +189,12 @@ static uint16_t lv_txt_get_next_word(const char * txt, const lv_font_t * font,
|
|||||||
/*Check the transversed string pixel width*/
|
/*Check the transversed string pixel width*/
|
||||||
letter_w = lv_font_get_glyph_width(font, letter, letter_next);
|
letter_w = lv_font_get_glyph_width(font, letter, letter_next);
|
||||||
cur_w += letter_w;
|
cur_w += letter_w;
|
||||||
//printf("%d BLOOP\n", __LINE__);
|
|
||||||
|
|
||||||
/* Test if this character fits within max_width */
|
/* Test if this character fits within max_width */
|
||||||
if( break_index == NO_BREAK_FOUND && cur_w > max_width) {
|
if( break_index == NO_BREAK_FOUND && cur_w > max_width) {
|
||||||
// break_index is now pointing at the character that doesn't fit
|
/* break_index is now pointing at the character that doesn't fit */
|
||||||
break_index = i;
|
break_index = i;
|
||||||
if(break_index > 0) { // zero is possible if first character doesn't fit in width
|
if(break_index > 0) { /* zero is possible if first character doesn't fit in width */
|
||||||
lv_txt_encoded_prev(txt, &break_index);
|
lv_txt_encoded_prev(txt, &break_index);
|
||||||
}
|
}
|
||||||
break_letter_count = word_len - 1;
|
break_letter_count = word_len - 1;
|
||||||
@ -217,13 +208,13 @@ static uint16_t lv_txt_get_next_word(const char * txt, const lv_font_t * font,
|
|||||||
/*Check for new line chars and breakchars*/
|
/*Check for new line chars and breakchars*/
|
||||||
if(letter == '\n' || letter == '\r' || is_break_char(letter)) {
|
if(letter == '\n' || letter == '\r' || is_break_char(letter)) {
|
||||||
word_len--;
|
word_len--;
|
||||||
//printf("meow %d %d\n", i, i_next);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(letter_w > 0) {
|
if(letter_w > 0) {
|
||||||
cur_w += letter_space;
|
cur_w += letter_space;
|
||||||
}
|
}
|
||||||
|
|
||||||
i = i_next;
|
i = i_next;
|
||||||
i_next = i_next_next;
|
i_next = i_next_next;
|
||||||
letter = letter_next;
|
letter = letter_next;
|
||||||
@ -231,9 +222,7 @@ static uint16_t lv_txt_get_next_word(const char * txt, const lv_font_t * font,
|
|||||||
|
|
||||||
/* Entire Word fits in the provided space */
|
/* Entire Word fits in the provided space */
|
||||||
if( break_index == NO_BREAK_FOUND ) {
|
if( break_index == NO_BREAK_FOUND ) {
|
||||||
//if( word_w_ptr != NULL && (letter == '\r' || letter == '\n')) *word_w_ptr = 0;
|
if( word_len == 0 || (letter == '\r' && letter_next == '\n') ) i = i_next;
|
||||||
if(letter == '\r' && letter_next == '\n') return i_next;
|
|
||||||
//printf("(%d): txt[i] \"%c\"\n", __LINE__, txt[i]);
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -249,25 +238,16 @@ static uint16_t lv_txt_get_next_word(const char * txt, const lv_font_t * font,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
i = break_index;
|
|
||||||
|
|
||||||
/* Word is a "long", but letters may need to be better distributed */
|
/* Word is a "long", but letters may need to be better distributed */
|
||||||
#if 0
|
|
||||||
{
|
{
|
||||||
#if 1
|
i = break_index;
|
||||||
printf("redistributing\n");
|
|
||||||
while(i!=break_index){
|
|
||||||
lv_txt_encoded_prev(txt, &i);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
int32_t n_move = LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN - (word_len - break_letter_count);
|
int32_t n_move = LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN - (word_len - break_letter_count);
|
||||||
|
/* Move pointer "i" backwards */
|
||||||
for(;n_move>0; n_move--){
|
for(;n_move>0; n_move--){
|
||||||
lv_txt_encoded_prev(txt, &i);
|
lv_txt_encoded_prev(txt, &i);
|
||||||
// todo: it would be appropriate to update the returned word width here
|
// todo: it would be appropriate to update the returned word width here
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
@ -291,35 +271,33 @@ uint16_t lv_txt_get_next_line(const char * txt, const lv_font_t * font,
|
|||||||
|
|
||||||
uint32_t i = 0; /* Iterating index into txt */
|
uint32_t i = 0; /* Iterating index into txt */
|
||||||
|
|
||||||
//printf("\n\n(%d): FULL_TEXT (len %d) \"%s\"\n", __LINE__, strlen(txt), txt);
|
|
||||||
|
|
||||||
while(txt[i] != '\0' && max_width > 0) {
|
while(txt[i] != '\0' && max_width > 0) {
|
||||||
//printf("(%d): MAX_WIDTH: %d\n", __LINE__, max_width);
|
|
||||||
uint32_t word_w = 0;
|
uint32_t word_w = 0;
|
||||||
uint32_t advance = lv_txt_get_next_word(&txt[i], font, letter_space,
|
uint32_t advance = lv_txt_get_next_word(&txt[i], font, letter_space,
|
||||||
max_width, flag, &word_w);
|
max_width, flag, &word_w);
|
||||||
max_width -= word_w;
|
max_width -= word_w;
|
||||||
|
|
||||||
{ //debug
|
|
||||||
printf("Word is %d long: \"", advance);
|
|
||||||
for(uint32_t j = 0; j < advance; j++){
|
|
||||||
printf("%c", txt[i+j]);
|
|
||||||
}
|
|
||||||
printf("\"\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if( advance == 0 ){
|
if( advance == 0 ){
|
||||||
if(i == 0) {
|
if(i == 0) lv_txt_encoded_next(txt, &i); // prevent inf loops
|
||||||
lv_txt_encoded_next(txt, &i); // prevent inf loops
|
|
||||||
printf("inf loop prevented\n");
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
i += advance;
|
i += advance;
|
||||||
|
|
||||||
if(txt[i] == '\n') { printf("nl_break\n");break;}
|
if(txt[i] == '\n') 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 letter
|
||||||
|
tmp = i_next;
|
||||||
|
letter_next = lv_txt_encoded_next(txt, &i_next); // Gets subsequent
|
||||||
|
if(letter_next == '\0') return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user