From 8a7a966a7b5b692c3c4088ab74146540bfa30231 Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Wed, 11 Jul 2018 18:46:35 +0200 Subject: [PATCH 1/2] lv_txt_get_width: don't trim the closing spaces because it would result unwanted text wrap --- lv_misc/lv_txt.c | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lv_misc/lv_txt.c b/lv_misc/lv_txt.c index 4f7cba25e..76b886854 100644 --- a/lv_misc/lv_txt.c +++ b/lv_misc/lv_txt.c @@ -201,16 +201,6 @@ lv_coord_t lv_txt_get_width(const char * txt, uint16_t length, width -= letter_space; /*Trim the last letter space. Important if the text is center aligned */ - - /*Trim closing spaces. Important when the text is aligned to the middle */ - for(i = length - 1; i > 0; i--) { - if(txt[i] == ' ' || txt[i] == '\n' || txt[i] == '\r') { - width -= lv_font_get_width(font, txt[i]); - width -= letter_space; - } else { - break; - } - } } return width; From 9c0bb3d3929a432aca7249cc867e43fb3c8858ec Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Wed, 11 Jul 2018 18:56:54 +0200 Subject: [PATCH 2/2] lv_draw_label: trim the traling whitespaces when aligned to the middle --- lv_draw/lv_draw.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/lv_draw/lv_draw.c b/lv_draw/lv_draw.c index bcd176164..a4092ccd0 100644 --- a/lv_draw/lv_draw.c +++ b/lv_draw/lv_draw.c @@ -273,7 +273,7 @@ void lv_draw_label(const lv_area_t * coords, const lv_area_t * mask, const lv_st } /*Init variables for the first line*/ - lv_coord_t line_length = 0; + lv_coord_t line_widht = 0; uint32_t line_start = 0; uint32_t line_end = lv_txt_get_next_line(txt, font, style->text.letter_space, w, flag); @@ -283,9 +283,19 @@ void lv_draw_label(const lv_area_t * coords, const lv_area_t * mask, const lv_st /*Align the line to middle if enabled*/ if(flag & LV_TXT_FLAG_CENTER) { - line_length = lv_txt_get_width(&txt[line_start], line_end - line_start, + line_widht = lv_txt_get_width(&txt[line_start], line_end - line_start, font, style->text.letter_space, flag); - pos.x += (w - line_length) / 2; + /*Trim closing spaces.*/ + uint16_t i; + for(i = line_end - 1; i > 0; i--) { + if(txt[i] == ' ' || txt[i] == '\n' || txt[i] == '\r') { + line_widht -= lv_font_get_width(font, txt[i]); + line_widht -= style->text.letter_space; + } else { + break; + } + } + pos.x += (w - line_widht) / 2; } cmd_state_t cmd_state = CMD_STATE_WAIT; @@ -371,9 +381,19 @@ void lv_draw_label(const lv_area_t * coords, const lv_area_t * mask, const lv_st pos.x = coords->x1; /*Align to middle*/ if(flag & LV_TXT_FLAG_CENTER) { - line_length = lv_txt_get_width(&txt[line_start], line_end - line_start, + line_widht = lv_txt_get_width(&txt[line_start], line_end - line_start, font, style->text.letter_space, flag); - pos.x += (w - line_length) / 2; + /*Trim closing spaces.*/ + uint16_t i; + for(i = line_end - 1; i > line_start; i--) { + if(txt[i] == ' ' || txt[i] == '\n' || txt[i] == '\r') { + line_widht -= lv_font_get_width(font, txt[i]); + line_widht -= style->text.letter_space; + } else { + break; + } + } + pos.x += (w - line_widht) / 2; } /*Go the next line position*/ pos.y += lv_font_get_height(font);