Skip to content

Commit

Permalink
feat(text) Add lv_text_get_width_with_flags
Browse files Browse the repository at this point in the history
  • Loading branch information
C47D committed Oct 3, 2024
1 parent 48cb59b commit 394af86
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/misc/lv_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,40 @@ int32_t lv_text_get_width(const char * txt, uint32_t length, const lv_font_t * f
uint32_t letter_next;
lv_text_encoded_letter_next_2(txt, &letter, &letter_next, &i);

/*TODO: Check foor recolor flag*/
if(0 /*(flag & LV_TEXT_FLAG_RECOLOR) != 0*/) {
int32_t char_width = lv_font_get_glyph_width(font, letter, letter_next);
if(char_width > 0) {
width += char_width;
width += letter_space;
}
}

if(width > 0) {
width -= letter_space; /*Trim the last letter space. Important if the text is center
aligned*/
}
}

return width;
}

int32_t lv_text_get_width_with_flags(const char * txt, uint32_t length, const lv_font_t * font, int32_t letter_space,
lv_text_flag_t flags)
{
if(txt == NULL) return 0;
if(font == NULL) return 0;
if(txt[0] == '\0') return 0;

uint32_t i = 0;
int32_t width = 0;
lv_text_cmd_state_t cmd_state = LV_TEXT_CMD_STATE_WAIT;

if(length != 0) {
while(i < length) {
uint32_t letter;
uint32_t letter_next;
lv_text_encoded_letter_next_2(txt, &letter, &letter_next, &i);

if((flags & LV_TEXT_FLAG_RECOLOR) != 0) {
if(lv_text_is_cmd(&cmd_state, letter) != false) {
continue;
}
Expand Down
13 changes: 13 additions & 0 deletions src/misc/lv_text.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ void lv_text_get_size(lv_point_t * size_res, const char * text, const lv_font_t
*/
int32_t lv_text_get_width(const char * txt, uint32_t length, const lv_font_t * font, int32_t letter_space);

/**
* Give the length of a text with a given font
* @param txt a '\0' terminate string
* @param length length of 'txt' in byte count and not characters (Á is 1 character but 2 bytes in
* UTF-8)
* @param font pointer to a font
* @param letter_space letter space
* @param flags settings for the text from ::lv_text_flag_t
* @return length of a char_num long text
*/
int32_t lv_text_get_width_with_flags(const char * txt, uint32_t length, const lv_font_t * font, int32_t letter_space,
lv_text_flag_t flags);

/**
* Check if c is command state
* @param state
Expand Down

0 comments on commit 394af86

Please sign in to comment.