/**1* @file lv_text.c2*3*/45/*********************6* INCLUDES7*********************/8#include "lv_txt.h"9#include "lv_math.h"1011/*********************12* DEFINES13*********************/14#define NO_BREAK_FOUND UINT32_MAX1516#ifndef LV_TXT_LINE_BREAK_LONG_LEN17#define LV_TXT_LINE_BREAK_LONG_LEN 12 /* If a character is at least this long, will break wherever "prettiest" */18#endif1920#ifndef LV_TXT_LINE_BREAK_LONG_PRE_MIN_LEN21#define LV_TXT_LINE_BREAK_LONG_PRE_MIN_LEN 3 /* Minimum number of characters of a word to put on a line before a break */22#endif2324#ifndef LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN25#define LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN 1 /* Minimum number of characters of a word to put on a line after a break */26#endif2728/**********************29* TYPEDEFS30**********************/3132/**********************33* STATIC PROTOTYPES34**********************/35static bool is_break_char(uint32_t letter);3637#if LV_TXT_UTF838static uint8_t lv_txt_utf8_size(const char * str);39static uint32_t lv_txt_unicode_to_utf8(uint32_t letter_uni);40static uint32_t lv_txt_utf8_conv_wc(uint32_t c);41static uint32_t lv_txt_utf8_next(const char * txt, uint32_t * i);42static uint32_t lv_txt_utf8_prev(const char * txt, uint32_t * i_start);43static uint32_t lv_txt_utf8_get_byte_id(const char * txt, uint32_t utf8_id);44static uint32_t lv_txt_utf8_get_char_id(const char * txt, uint32_t byte_id);45static uint32_t lv_txt_utf8_get_length(const char * txt);46#else47static uint8_t lv_txt_ascii_size(const char * str);48static uint32_t lv_txt_unicode_to_ascii(uint32_t letter_uni);49static uint32_t lv_txt_ascii_conv_wc(uint32_t c);50static uint32_t lv_txt_ascii_next(const char * txt, uint32_t * i);51static uint32_t lv_txt_ascii_prev(const char * txt, uint32_t * i_start);52static uint32_t lv_txt_ascii_get_byte_id(const char * txt, uint32_t utf8_id);53static uint32_t lv_txt_ascii_get_char_id(const char * txt, uint32_t byte_id);54static uint32_t lv_txt_ascii_get_length(const char * txt);55#endif5657/**********************58* STATIC VARIABLES59**********************/606162/**********************63* GLOBAL VARIABLES64**********************/65#if LV_TXT_UTF866uint8_t (*lv_txt_encoded_size)(const char *) = lv_txt_utf8_size;67uint32_t (*lv_txt_unicode_to_encoded)(uint32_t) = lv_txt_unicode_to_utf8;68uint32_t (*lv_txt_encoded_conv_wc)(uint32_t) = lv_txt_utf8_conv_wc;69uint32_t (*lv_txt_encoded_next)(const char *, uint32_t *) = lv_txt_utf8_next;70uint32_t (*lv_txt_encoded_prev)(const char *, uint32_t *) = lv_txt_utf8_prev;71uint32_t (*lv_txt_encoded_get_byte_id)(const char *, uint32_t) = lv_txt_utf8_get_byte_id;72uint32_t (*lv_encoded_get_char_id)(const char *, uint32_t) = lv_txt_utf8_get_char_id;73uint32_t (*lv_txt_get_encoded_length)(const char *) = lv_txt_utf8_get_length;74#else75uint8_t (*lv_txt_encoded_size)(const char *) = lv_txt_ascii_size;76uint32_t (*lv_txt_unicode_to_encoded)(uint32_t) = lv_txt_unicode_to_ascii;77uint32_t (*lv_txt_encoded_conv_wc)(uint32_t) = lv_txt_ascii_conv_wc;78uint32_t (*lv_txt_encoded_next)(const char *, uint32_t *) = lv_txt_ascii_next;79uint32_t (*lv_txt_encoded_prev)(const char *, uint32_t *) = lv_txt_ascii_prev;80uint32_t (*lv_txt_encoded_get_byte_id)(const char *, uint32_t) = lv_txt_ascii_get_byte_id;81uint32_t (*lv_encoded_get_char_id)(const char *, uint32_t) = lv_txt_ascii_get_char_id;82uint32_t (*lv_txt_get_encoded_length)(const char *) = lv_txt_ascii_get_length;83#endif84/**********************85* MACROS86**********************/8788/**********************89* GLOBAL FUNCTIONS90**********************/9192/**93* Get size of a text94* @param size_res pointer to a 'point_t' variable to store the result95* @param text pointer to a text96* @param font pinter to font of the text97* @param letter_space letter space of the text98* @param txt.line_space line space of the text99* @param flags settings for the text from 'txt_flag_t' enum100* @param max_width max with of the text (break the lines to fit this size) Set CORD_MAX to avoid line breaks101*/102void lv_txt_get_size(lv_point_t * size_res, const char * text, const lv_font_t * font,103lv_coord_t letter_space, lv_coord_t line_space, lv_coord_t max_width, lv_txt_flag_t flag)104{105size_res->x = 0;106size_res->y = 0;107108if(text == NULL) return;109if(font == NULL) return;110111if(flag & LV_TXT_FLAG_EXPAND) max_width = LV_COORD_MAX;112113uint32_t line_start = 0;114uint32_t new_line_start = 0;115lv_coord_t act_line_length;116uint8_t letter_height = lv_font_get_height(font);117118/*Calc. the height and longest line*/119while(text[line_start] != '\0') {120new_line_start += lv_txt_get_next_line(&text[line_start], font, letter_space, max_width, flag);121size_res->y += letter_height ;122size_res->y += line_space;123124/*Calculate the the longest line*/125act_line_length = lv_txt_get_width(&text[line_start], new_line_start - line_start,126font, letter_space, flag);127128size_res->x = LV_MATH_MAX(act_line_length, size_res->x);129line_start = new_line_start;130}131132/*Make the text one line taller if the last character is '\n' or '\r'*/133if((line_start != 0) && (text[line_start - 1] == '\n' || text[line_start - 1] == '\r')) {134size_res->y += letter_height + line_space;135}136137/*Correction with the last line space or set the height manually if the text is empty*/138if(size_res->y == 0) size_res->y = letter_height;139else size_res->y -= line_space;140141}142143/**144* Get the next line of text. Check line length and break chars too.145* @param txt a '\0' terminated string146* @param font pointer to a font147* @param letter_space letter space148* @param max_width max with of the text (break the lines to fit this size) Set CORD_MAX to avoid line breaks149* @param flags settings for the text from 'txt_flag_type' enum150* @return the index of the first char of the new line (in byte index not letter index. With UTF-8 they are different)151*/152uint16_t lv_txt_get_next_line(const char * txt, const lv_font_t * font,153lv_coord_t letter_space, lv_coord_t max_width, lv_txt_flag_t flag)154{155if(txt == NULL) return 0;156if(font == NULL) return 0;157158if(flag & LV_TXT_FLAG_EXPAND) max_width = LV_COORD_MAX;159160uint32_t i = 0;161lv_coord_t cur_w = 0;162lv_coord_t w_at_last_break = 0;163uint32_t n_char_since_last_break = 0; /* Used count word length of long words */164uint32_t last_break = NO_BREAK_FOUND;165lv_txt_cmd_state_t cmd_state = LV_TXT_CMD_STATE_WAIT;166uint32_t letter = 0;167168while(txt[i] != '\0') {169lv_coord_t letter_width;170letter = lv_txt_encoded_next(txt, &i);171172/*Handle the recolor command*/173if((flag & LV_TXT_FLAG_RECOLOR) != 0) {174if(lv_txt_is_cmd(&cmd_state, letter) != false) {175continue; /*Skip the letter is it is part of a command*/176}177}178179180/*Check for new line chars*/181if(letter == '\n' || letter == '\r') {182uint32_t i_tmp = i;183uint32_t letter_next = lv_txt_encoded_next(txt, &i_tmp);184if(letter == '\r' && letter_next == '\n') i = i_tmp;185186return i; /*Return with the first letter of the next line*/187188} else { /*Check the actual length*/189n_char_since_last_break++;190letter_width = lv_font_get_width(font, letter);191cur_w += letter_width;192193/* Get the length of the current work and determine best place194* to break the line. */195if(cur_w > max_width) {196if( last_break != NO_BREAK_FOUND ) {197/* Continue searching for next breakable character to see if the next word will fit */198uint32_t n_char_fit = n_char_since_last_break - 1;199if( n_char_since_last_break <= LV_TXT_LINE_BREAK_LONG_PRE_MIN_LEN ) {200i = last_break;201}202else {203uint32_t i_tmp = i;204cur_w -= w_at_last_break + letter_space; /*ignore the first letter_space after the break char */205bool other = true;206while(txt[i_tmp] != '\0') {207letter = lv_txt_encoded_next(txt, &i_tmp);208209/*Handle the recolor command*/210if((flag & LV_TXT_FLAG_RECOLOR) != 0) {211if(lv_txt_is_cmd(&cmd_state, letter) != false) {212continue; /*Skip the letter is it is part of a command*/213}214}215216/*Check for new line chars*/217if(letter == '\n' || letter == '\r' || is_break_char(letter)) {218if(n_char_since_last_break >= LV_TXT_LINE_BREAK_LONG_LEN) {219/* Figure out the prettiest place to break */220uint32_t char_remain;221lv_txt_encoded_prev(txt, &i);222for(char_remain=n_char_since_last_break - n_char_fit;223char_remain < LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN;224char_remain++) {225lv_txt_encoded_prev(txt, &i);226}227}228else{229i = last_break;230}231other = false;232break;233}234n_char_since_last_break++;235lv_coord_t letter_width2 = lv_font_get_width(font, letter);236cur_w += letter_width2;237if(cur_w > max_width) {238/* Current letter already exceeds, return previous */239lv_txt_encoded_prev(txt, &i);240other = false;241break;242}243if(letter_width2 > 0){244cur_w += letter_space;245}246}247if( other ) {248if(n_char_since_last_break >= LV_TXT_LINE_BREAK_LONG_LEN) {249/* Figure out the prettiest place to break */250uint32_t char_remain;251lv_txt_encoded_prev(txt, &i);252for(char_remain=n_char_since_last_break - n_char_fit;253char_remain < LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN;254char_remain++){255lv_txt_encoded_prev(txt, &i);256}257}258else{259i = last_break;260}261}262}263} else {264/* Now this character is out of the area so it will be first character of the next line*/265/* But 'i' already points to the next character (because of lv_txt_utf8_next) step beck one*/266lv_txt_encoded_prev(txt, &i);267}268269/* Do not let to return without doing nothing.270* Find at least one character (Avoid infinite loop )*/271if(i == 0) lv_txt_encoded_next(txt, &i);272273return i;274}275/*If this char still can fit to this line then check if276* txt can be broken here later */277else if(is_break_char(letter)) {278last_break = i; /*Save the first char index after break*/279w_at_last_break = cur_w;280if(letter_width > 0) {281w_at_last_break += letter_space;282}283n_char_since_last_break = 0;284}285}286287if(letter_width > 0) {288cur_w += letter_space;289}290}291292return i;293}294295/**296* Give the length of a text with a given font297* @param txt a '\0' terminate string298* @param length length of 'txt' in byte count and not characters (Á is 1 character but 2 bytes in UTF-8)299* @param font pointer to a font300* @param letter_space letter space301* @param flags settings for the text from 'txt_flag_t' enum302* @return length of a char_num long text303*/304lv_coord_t lv_txt_get_width(const char * txt, uint16_t length,305const lv_font_t * font, lv_coord_t letter_space, lv_txt_flag_t flag)306{307if(txt == NULL) return 0;308if(font == NULL) return 0;309310uint32_t i = 0;311lv_coord_t width = 0;312lv_txt_cmd_state_t cmd_state = LV_TXT_CMD_STATE_WAIT;313uint32_t letter;314315if(length != 0) {316while(i< length){317letter = lv_txt_encoded_next(txt, &i);318if((flag & LV_TXT_FLAG_RECOLOR) != 0) {319if(lv_txt_is_cmd(&cmd_state, letter) != false) {320continue;321}322}323324lv_coord_t char_width = lv_font_get_width(font, letter);325if(char_width > 0){326width += char_width;327width += letter_space;328}329}330331if(width > 0) {332width -= letter_space; /*Trim the last letter space. Important if the text is center aligned */333}334}335336return width;337}338339/**340* Check next character in a string and decide if the character is part of the command or not341* @param state pointer to a txt_cmd_state_t variable which stores the current state of command processing342* (Initied. to TXT_CMD_STATE_WAIT )343* @param c the current character344* @return true: the character is part of a command and should not be written,345* false: the character should be written346*/347bool lv_txt_is_cmd(lv_txt_cmd_state_t * state, uint32_t c)348{349bool ret = false;350351if(c == (uint32_t)LV_TXT_COLOR_CMD[0]) {352if(*state == LV_TXT_CMD_STATE_WAIT) { /*Start char*/353*state = LV_TXT_CMD_STATE_PAR;354ret = true;355} else if(*state == LV_TXT_CMD_STATE_PAR) { /*Other start char in parameter is escaped cmd. char */356*state = LV_TXT_CMD_STATE_WAIT;357} else if(*state == LV_TXT_CMD_STATE_IN) { /*Command end */358*state = LV_TXT_CMD_STATE_WAIT;359ret = true;360}361}362363/*Skip the color parameter and wait the space after it*/364if(*state == LV_TXT_CMD_STATE_PAR) {365if(c == ' ') {366*state = LV_TXT_CMD_STATE_IN; /*After the parameter the text is in the command*/367}368ret = true;369}370371return ret;372}373374/**375* Insert a string into an other376* @param txt_buf the original text (must be big enough for the result text)377* @param pos position to insert. Expressed in character index and not byte index (Different in UTF-8)378* 0: before the original text, 1: after the first char etc.379* @param ins_txt text to insert380*/381void lv_txt_ins(char * txt_buf, uint32_t pos, const char * ins_txt)382{383uint32_t old_len = strlen(txt_buf);384uint32_t ins_len = strlen(ins_txt);385uint32_t new_len = ins_len + old_len;386#if LV_TXT_UTF8 != 0387pos = lv_txt_encoded_get_byte_id(txt_buf, pos); /*Convert to byte index instead of letter index*/388#endif389/*Copy the second part into the end to make place to text to insert*/390uint32_t i;391for(i = new_len; i >= pos + ins_len; i--) {392txt_buf[i] = txt_buf[i - ins_len];393}394395/* Copy the text into the new space*/396memcpy(txt_buf + pos, ins_txt, ins_len);397}398399/**400* Delete a part of a string401* @param txt string to modify402* @param pos position where to start the deleting (0: before the first char, 1: after the first char etc.)403* @param len number of characters to delete404*/405void lv_txt_cut(char * txt, uint32_t pos, uint32_t len)406{407408uint32_t old_len = strlen(txt);409#if LV_TXT_UTF8 != 0410pos = lv_txt_encoded_get_byte_id(txt, pos); /*Convert to byte index instead of letter index*/411len = lv_txt_encoded_get_byte_id(&txt[pos], len);412#endif413414/*Copy the second part into the end to make place to text to insert*/415uint32_t i;416for(i = pos; i <= old_len - len; i++) {417txt[i] = txt[i + len];418}419}420421422/*******************************423* UTF-8 ENCODER/DECOER424******************************/425426#if LV_TXT_UTF8427428/**429* Give the size of an UTF-8 coded character430* @param str pointer to a character in a string431* @return length of the UTF-8 character (1,2,3 or 4). O on invalid code432*/433static uint8_t lv_txt_utf8_size(const char * str)434{435if((str[0] & 0x80) == 0) return 1;436else if((str[0] & 0xE0) == 0xC0) return 2;437else if((str[0] & 0xF0) == 0xE0) return 3;438else if((str[0] & 0xF8) == 0xF0) return 4;439return 0;440}441442443/**444* Convert an Unicode letter to UTF-8.445* @param letter_uni an Unicode letter446* @return UTF-8 coded character in Little Endian to be compatible with C chars (e.g. 'Á', 'Ű')447*/448static uint32_t lv_txt_unicode_to_utf8(uint32_t letter_uni)449{450if(letter_uni < 128) return letter_uni;451uint8_t bytes[4];452453if(letter_uni < 0x0800) {454bytes[0] = ((letter_uni >> 6) & 0x1F) | 0xC0;455bytes[1] = ((letter_uni >> 0) & 0x3F) | 0x80;456bytes[2] = 0;457bytes[3] = 0;458} else if(letter_uni < 0x010000) {459bytes[0] = ((letter_uni >> 12) & 0x0F) | 0xE0;460bytes[1] = ((letter_uni >> 6) & 0x3F) | 0x80;461bytes[2] = ((letter_uni >> 0) & 0x3F) | 0x80;462bytes[3] = 0;463} else if(letter_uni < 0x110000) {464bytes[0] = ((letter_uni >> 18) & 0x07) | 0xF0;465bytes[1] = ((letter_uni >> 12) & 0x3F) | 0x80;466bytes[2] = ((letter_uni >> 6) & 0x3F) | 0x80;467bytes[3] = ((letter_uni >> 0) & 0x3F) | 0x80;468}469470uint32_t * res_p = (uint32_t *)bytes;471return *res_p;472}473474/**475* Convert a wide character, e.g. 'Á' little endian to be UTF-8 compatible476* @param c a wide character or a Little endian number477* @return `c` in big endian478*/479static uint32_t lv_txt_utf8_conv_wc(uint32_t c)480{481/*Swap the bytes (UTF-8 is big endian, but the MCUs are little endian)*/482if((c & 0x80) != 0) {483uint32_t swapped;484uint8_t c8[4];485memcpy(c8, &c, 4);486swapped = (c8[0] << 24) + (c8[1] << 16) + (c8[2] << 8) + (c8[3]);487uint8_t i;488for(i = 0; i < 4; i++) {489if((swapped & 0xFF) == 0) swapped = (swapped >> 8); /*Ignore leading zeros (they were in the end originally)*/490}491c = swapped;492}493494return c;495}496497/**498* Decode an UTF-8 character from a string.499* @param txt pointer to '\0' terminated string500* @param i start byte index in 'txt' where to start.501* After call it will point to the next UTF-8 char in 'txt'.502* NULL to use txt[0] as index503* @return the decoded Unicode character or 0 on invalid UTF-8 code504*/505static uint32_t lv_txt_utf8_next(const char * txt, uint32_t * i)506{507/* Unicode to UTF-8508* 00000000 00000000 00000000 0xxxxxxx -> 0xxxxxxx509* 00000000 00000000 00000yyy yyxxxxxx -> 110yyyyy 10xxxxxx510* 00000000 00000000 zzzzyyyy yyxxxxxx -> 1110zzzz 10yyyyyy 10xxxxxx511* 00000000 000wwwzz zzzzyyyy yyxxxxxx -> 11110www 10zzzzzz 10yyyyyy 10xxxxxx512* */513514uint32_t result = 0;515516/*Dummy 'i' pointer is required*/517uint32_t i_tmp = 0;518if(i == NULL) i = &i_tmp;519520/*Normal ASCII*/521if((txt[*i] & 0x80) == 0) {522result = txt[*i];523(*i)++;524}525/*Real UTF-8 decode*/526else {527/*2 bytes UTF-8 code*/528if((txt[*i] & 0xE0) == 0xC0) {529result = (uint32_t)(txt[*i] & 0x1F) << 6;530(*i)++;531if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/532result += (txt[*i] & 0x3F);533(*i)++;534}535/*3 bytes UTF-8 code*/536else if((txt[*i] & 0xF0) == 0xE0) {537result = (uint32_t)(txt[*i] & 0x0F) << 12;538(*i)++;539540if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/541result += (uint32_t)(txt[*i] & 0x3F) << 6;542(*i)++;543544if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/545result += (txt[*i] & 0x3F);546(*i)++;547}548/*4 bytes UTF-8 code*/549else if((txt[*i] & 0xF8) == 0xF0) {550result = (uint32_t)(txt[*i] & 0x07) << 18;551(*i)++;552553if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/554result += (uint32_t)(txt[*i] & 0x3F) << 12;555(*i)++;556557if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/558result += (uint32_t)(txt[*i] & 0x3F) << 6;559(*i)++;560561if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/562result += txt[*i] & 0x3F;563(*i)++;564} else {565(*i)++; /*Not UTF-8 char. Go the next.*/566}567}568return result;569}570571/**572* Get previous UTF-8 character form a string.573* @param txt pointer to '\0' terminated string574* @param i start byte index in 'txt' where to start. After the call it will point to the previous UTF-8 char in 'txt'.575* @return the decoded Unicode character or 0 on invalid UTF-8 code576*/577static uint32_t lv_txt_utf8_prev(const char * txt, uint32_t * i)578{579uint8_t c_size;580uint8_t cnt = 0;581582/*Try to find a !0 long UTF-8 char by stepping one character back*/583(*i)--;584do {585if(cnt >= 4) return 0; /*No UTF-8 char found before the initial*/586587c_size = lv_txt_encoded_size(&txt[*i]);588if(c_size == 0) {589if(*i != 0)(*i)--;590else return 0;591}592cnt++;593} while(c_size == 0);594595uint32_t i_tmp = *i;596uint32_t letter = lv_txt_encoded_next(txt, &i_tmp); /*Character found, get it*/597598return letter;599600}601602/**603* Convert a character index (in an UTF-8 text) to byte index.604* E.g. in "AÁRT" index of 'R' is 2th char but start at byte 3 because 'Á' is 2 bytes long605* @param txt a '\0' terminated UTF-8 string606* @param utf8_id character index607* @return byte index of the 'utf8_id'th letter608*/609static uint32_t lv_txt_utf8_get_byte_id(const char * txt, uint32_t utf8_id)610{611uint32_t i;612uint32_t byte_cnt = 0;613for(i = 0; i < utf8_id; i++) {614byte_cnt += lv_txt_encoded_size(&txt[byte_cnt]);615}616617return byte_cnt;618619}620621622/**623* Convert a byte index (in an UTF-8 text) to character index.624* E.g. in "AÁRT" index of 'R' is 2th char but start at byte 3 because 'Á' is 2 bytes long625* @param txt a '\0' terminated UTF-8 string626* @param byte_id byte index627* @return character index of the letter at 'byte_id'th position628*/629static uint32_t lv_txt_utf8_get_char_id(const char * txt, uint32_t byte_id)630{631uint32_t i = 0;632uint32_t char_cnt = 0;633634while(i < byte_id) {635lv_txt_encoded_next(txt, &i); /*'i' points to the next letter so use the prev. value*/636char_cnt++;637}638639return char_cnt;640}641642/**643* Get the number of characters (and NOT bytes) in a string. Decode it with UTF-8 if enabled.644* E.g.: "ÁBC" is 3 characters (but 4 bytes)645* @param txt a '\0' terminated char string646* @return number of characters647*/648static uint32_t lv_txt_utf8_get_length(const char * txt)649{650#if LV_TXT_UTF8 == 0651return strlen(txt);652#else653uint32_t len = 0;654uint32_t i = 0;655656while(txt[i] != '\0') {657lv_txt_encoded_next(txt, &i);658len++;659}660661return len;662#endif663}664665#else666/**667* Give the size of an UTF-8 coded character668* @param str pointer to a character in a string669* @return length of the UTF-8 character (1,2,3 or 4). O on invalid code670*/671static uint8_t lv_txt_ascii_size(const char * str)672{673return 1;674}675676677/**678* Convert an Unicode letter to UTF-8.679* @param letter_uni an Unicode letter680* @return UTF-8 coded character in Little Endian to be compatible with C chars (e.g. 'Á', 'Ű')681*/682static uint32_t lv_txt_unicode_to_ascii(uint32_t letter_uni)683{684if(letter_uni < 128) return letter_uni;685else return ' ';686}687688/**689* Convert wide characters to ASCII, however wide characters in ASCII range (e.g. 'A') are ASCII compatible by default.690* So this function does nothing just returns with `c`.691* @param c a character, e.g. 'A'692* @return same as `c`693*/694static uint32_t lv_txt_ascii_conv_wc(uint32_t c)695{696return c;697}698699/**700* Decode an UTF-8 character from a string.701* @param txt pointer to '\0' terminated string702* @param i start byte index in 'txt' where to start.703* After call it will point to the next UTF-8 char in 'txt'.704* NULL to use txt[0] as index705* @return the decoded Unicode character or 0 on invalid UTF-8 code706*/707static uint32_t lv_txt_ascii_next(const char * txt, uint32_t * i)708{709if(i == NULL) return txt[1]; /*Get the next char */710711uint8_t letter = txt[*i] ;712(*i)++;713return letter;714}715716/**717* Get previous UTF-8 character form a string.718* @param txt pointer to '\0' terminated string719* @param i start byte index in 'txt' where to start. After the call it will point to the previous UTF-8 char in 'txt'.720* @return the decoded Unicode character or 0 on invalid UTF-8 code721*/722static uint32_t lv_txt_ascii_prev(const char * txt, uint32_t * i)723{724if(i == NULL) return *(txt - 1); /*Get the prev. char */725726(*i)--;727uint8_t letter = txt[*i] ;728729return letter;730}731732/**733* Convert a character index (in an UTF-8 text) to byte index.734* E.g. in "AÁRT" index of 'R' is 2th char but start at byte 3 because 'Á' is 2 bytes long735* @param txt a '\0' terminated UTF-8 string736* @param utf8_id character index737* @return byte index of the 'utf8_id'th letter738*/739static uint32_t lv_txt_ascii_get_byte_id(const char * txt, uint32_t utf8_id)740{741return utf8_id; /*In Non encoded no difference*/742}743744745/**746* Convert a byte index (in an UTF-8 text) to character index.747* E.g. in "AÁRT" index of 'R' is 2th char but start at byte 3 because 'Á' is 2 bytes long748* @param txt a '\0' terminated UTF-8 string749* @param byte_id byte index750* @return character index of the letter at 'byte_id'th position751*/752static uint32_t lv_txt_ascii_get_char_id(const char * txt, uint32_t byte_id)753{754return byte_id; /*In Non encoded no difference*/755}756757/**758* Get the number of characters (and NOT bytes) in a string. Decode it with UTF-8 if enabled.759* E.g.: "ÁBC" is 3 characters (but 4 bytes)760* @param txt a '\0' terminated char string761* @return number of characters762*/763static uint32_t lv_txt_ascii_get_length(const char * txt)764{765return strlen(txt);766}767#endif768/**********************769* STATIC FUNCTIONS770**********************/771772/**773* Test if char is break char or not (a text can broken here or not)774* @param letter a letter775* @return false: 'letter' is not break char776*/777static bool is_break_char(uint32_t letter)778{779uint8_t i;780bool ret = false;781782/*Compare the letter to TXT_BREAK_CHARS*/783for(i = 0; LV_TXT_BREAK_CHARS[i] != '\0'; i++) {784if(letter == (uint32_t)LV_TXT_BREAK_CHARS[i]) {785ret = true; /*If match then it is break char*/786break;787}788}789790return ret;791}792793794795