1 /**
2  * @file lv_text.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_txt.h"
10 #include "lv_math.h"
11 
12 /*********************
13  *      DEFINES
14  *********************/
15 #define NO_BREAK_FOUND UINT32_MAX
16 
17 /**********************
18  *      TYPEDEFS
19  **********************/
20 
21 /**********************
22  *  STATIC PROTOTYPES
23  **********************/
24 static inline bool is_break_char(uint32_t letter);
25 
26 #if LV_TXT_ENC == LV_TXT_ENC_UTF8
27 static uint8_t lv_txt_utf8_size(const char * str);
28 static uint32_t lv_txt_unicode_to_utf8(uint32_t letter_uni);
29 static uint32_t lv_txt_utf8_conv_wc(uint32_t c);
30 static uint32_t lv_txt_utf8_next(const char * txt, uint32_t * i);
31 static uint32_t lv_txt_utf8_prev(const char * txt, uint32_t * i_start);
32 static uint32_t lv_txt_utf8_get_byte_id(const char * txt, uint32_t utf8_id);
33 static uint32_t lv_txt_utf8_get_char_id(const char * txt, uint32_t byte_id);
34 static uint32_t lv_txt_utf8_get_length(const char * txt);
35 #elif LV_TXT_ENC == LV_TXT_ENC_ASCII
36 static uint8_t lv_txt_iso8859_1_size(const char * str);
37 static uint32_t lv_txt_unicode_to_iso8859_1(uint32_t letter_uni);
38 static uint32_t lv_txt_iso8859_1_conv_wc(uint32_t c);
39 static uint32_t lv_txt_iso8859_1_next(const char * txt, uint32_t * i);
40 static uint32_t lv_txt_iso8859_1_prev(const char * txt, uint32_t * i_start);
41 static uint32_t lv_txt_iso8859_1_get_byte_id(const char * txt, uint32_t utf8_id);
42 static uint32_t lv_txt_iso8859_1_get_char_id(const char * txt, uint32_t byte_id);
43 static uint32_t lv_txt_iso8859_1_get_length(const char * txt);
44 #endif
45 /**********************
46  *  STATIC VARIABLES
47  **********************/
48 
49 /**********************
50  *  GLOBAL VARIABLES
51  **********************/
52 #if LV_TXT_ENC == LV_TXT_ENC_UTF8
53 uint8_t (*lv_txt_encoded_size)(const char *)                   = lv_txt_utf8_size;
54 uint32_t (*lv_txt_unicode_to_encoded)(uint32_t)                = lv_txt_unicode_to_utf8;
55 uint32_t (*lv_txt_encoded_conv_wc)(uint32_t)                   = lv_txt_utf8_conv_wc;
56 uint32_t (*lv_txt_encoded_next)(const char *, uint32_t *)      = lv_txt_utf8_next;
57 uint32_t (*lv_txt_encoded_prev)(const char *, uint32_t *)      = lv_txt_utf8_prev;
58 uint32_t (*lv_txt_encoded_get_byte_id)(const char *, uint32_t) = lv_txt_utf8_get_byte_id;
59 uint32_t (*lv_encoded_get_char_id)(const char *, uint32_t)     = lv_txt_utf8_get_char_id;
60 uint32_t (*lv_txt_get_encoded_length)(const char *)            = lv_txt_utf8_get_length;
61 #elif LV_TXT_ENC == LV_TXT_ENC_ASCII
62 uint8_t (*lv_txt_encoded_size)(const char *)                   = lv_txt_iso8859_1_size;
63 uint32_t (*lv_txt_unicode_to_encoded)(uint32_t)                = lv_txt_unicode_to_iso8859_1;
64 uint32_t (*lv_txt_encoded_conv_wc)(uint32_t)                   = lv_txt_iso8859_1_conv_wc;
65 uint32_t (*lv_txt_encoded_next)(const char *, uint32_t *)      = lv_txt_iso8859_1_next;
66 uint32_t (*lv_txt_encoded_prev)(const char *, uint32_t *)      = lv_txt_iso8859_1_prev;
67 uint32_t (*lv_txt_encoded_get_byte_id)(const char *, uint32_t) = lv_txt_iso8859_1_get_byte_id;
68 uint32_t (*lv_encoded_get_char_id)(const char *, uint32_t)     = lv_txt_iso8859_1_get_char_id;
69 uint32_t (*lv_txt_get_encoded_length)(const char *)            = lv_txt_iso8859_1_get_length;
70 
71 #endif
72 
73 /**********************
74  *      MACROS
75  **********************/
76 
77 /**********************
78  *   GLOBAL FUNCTIONS
79  **********************/
80 
81 /**
82  * Get size of a text
83  * @param size_res pointer to a 'point_t' variable to store the result
84  * @param text pointer to a text
85  * @param font pinter to font of the text
86  * @param letter_space letter space of the text
87  * @param txt.line_space line space of the text
88  * @param flags settings for the text from 'txt_flag_t' enum
89  * @param max_width max with of the text (break the lines to fit this size) Set CORD_MAX to avoid
90  * line breaks
91  */
lv_txt_get_size(lv_point_t * size_res,const char * text,const lv_font_t * font,lv_coord_t letter_space,lv_coord_t line_space,lv_coord_t max_width,lv_txt_flag_t flag)92 void lv_txt_get_size(lv_point_t * size_res, const char * text, const lv_font_t * font, lv_coord_t letter_space,
93                      lv_coord_t line_space, lv_coord_t max_width, lv_txt_flag_t flag)
94 {
95     size_res->x = 0;
96     size_res->y = 0;
97 
98     if(text == NULL) return;
99     if(font == NULL) return;
100 
101     if(flag & LV_TXT_FLAG_EXPAND) max_width = LV_COORD_MAX;
102 
103     uint32_t line_start     = 0;
104     uint32_t new_line_start = 0;
105     lv_coord_t act_line_length;
106     uint8_t letter_height = lv_font_get_line_height(font);
107 
108     /*Calc. the height and longest line*/
109     while(text[line_start] != '\0') {
110         new_line_start += lv_txt_get_next_line(&text[line_start], font, letter_space, max_width, flag);
111         size_res->y += letter_height;
112         size_res->y += line_space;
113 
114         /*Calculate the the longest line*/
115         act_line_length = lv_txt_get_width(&text[line_start], new_line_start - line_start, font, letter_space, flag);
116 
117         size_res->x = LV_MATH_MAX(act_line_length, size_res->x);
118         line_start  = new_line_start;
119     }
120 
121     /*Ma ke the text one line taller if the last character is '\n' or '\r'*/
122     if((line_start != 0) && (text[line_start - 1] == '\n' || text[line_start - 1] == '\r')) {
123         size_res->y += letter_height + line_space;
124     }
125 
126     /*Correction with the last line space or set the height manually if the text is empty*/
127     if(size_res->y == 0)
128         size_res->y = letter_height;
129     else
130         size_res->y -= line_space;
131 }
132 
133 /**
134  * Get the next line of text. Check line length and break chars too.
135  * @param txt a '\0' terminated string
136  * @param font pointer to a font
137  * @param letter_space letter space
138  * @param max_width max with of the text (break the lines to fit this size) Set CORD_MAX to avoid line breaks
139  * @param flags settings for the text from 'txt_flag_type' enum
140  * @return the index of the first char of the new line (in byte index not letter index. With UTF-8 they are different)
141  */
lv_txt_get_next_line(const char * txt,const lv_font_t * font,lv_coord_t letter_space,lv_coord_t max_width,lv_txt_flag_t flag)142 uint16_t lv_txt_get_next_line(const char * txt, const lv_font_t * font, lv_coord_t letter_space, lv_coord_t max_width,
143                               lv_txt_flag_t flag)
144 {
145     if(txt == NULL) return 0;
146     if(font == NULL) return 0;
147 
148     if(flag & LV_TXT_FLAG_EXPAND) max_width = LV_COORD_MAX;
149 
150     uint32_t i                   = 0;
151     uint32_t i_next              = 0;
152     lv_coord_t cur_w             = 0;
153     uint32_t last_break          = NO_BREAK_FOUND;
154     lv_txt_cmd_state_t cmd_state = LV_TXT_CMD_STATE_WAIT;
155     uint32_t letter_w;
156     uint32_t letter      = 0;
157     uint32_t letter_next = 0;
158 
159     letter_next = lv_txt_encoded_next(txt, &i_next);
160 
161     while(txt[i] != '\0') {
162         letter      = letter_next;
163         i           = i_next;
164         letter_next = lv_txt_encoded_next(txt, &i_next);
165 
166         /*Handle the recolor command*/
167         if((flag & LV_TXT_FLAG_RECOLOR) != 0) {
168             if(lv_txt_is_cmd(&cmd_state, letter) != false) {
169                 continue; /*Skip the letter is it is part of a command*/
170             }
171         }
172 
173         /*Check for new line chars*/
174         if(letter == '\n' || letter == '\r') {
175             /*Return with the first letter of the next line*/
176             if(letter == '\r' && letter_next == '\n')
177                 return i_next;
178             else
179                 return i;
180         } else { /*Check the actual length*/
181             letter_w = lv_font_get_glyph_width(font, letter, letter_next);
182             cur_w += letter_w;
183 
184             /*If the txt is too long then finish, this is the line end*/
185             if(cur_w > max_width) {
186                 /*If a break character was already found break there*/
187                 if(last_break != NO_BREAK_FOUND) {
188                     i = last_break;
189                 } else {
190                     /* Now this character is out of the area so it will be first character of the next line*/
191                     /* But 'i' already points to the next character (because of lv_txt_utf8_next) step beck one*/
192                     lv_txt_encoded_prev(txt, &i);
193                 }
194 
195                 /* Do not let to return without doing nothing.
196                  * Find at least one character (Avoid infinite loop )*/
197                 if(i == 0) lv_txt_encoded_next(txt, &i);
198 
199                 return i;
200             }
201             /*If this char still can fit to this line then check if
202              * txt can be broken here later */
203             else if(is_break_char(letter)) {
204                 last_break = i; /*Save the first char index after break*/
205             }
206         }
207 
208         if(letter_w > 0) {
209             cur_w += letter_space;
210         }
211     }
212 
213     return i;
214 }
215 
216 /**
217  * Give the length of a text with a given font
218  * @param txt a '\0' terminate string
219  * @param length length of 'txt' in byte count and not characters (Á is 1 character but 2 bytes in
220  * UTF-8)
221  * @param font pointer to a font
222  * @param letter_space letter space
223  * @param flags settings for the text from 'txt_flag_t' enum
224  * @return length of a char_num long text
225  */
lv_txt_get_width(const char * txt,uint16_t length,const lv_font_t * font,lv_coord_t letter_space,lv_txt_flag_t flag)226 lv_coord_t lv_txt_get_width(const char * txt, uint16_t length, const lv_font_t * font, lv_coord_t letter_space,
227                             lv_txt_flag_t flag)
228 {
229     if(txt == NULL) return 0;
230     if(font == NULL) return 0;
231 
232     uint32_t i                   = 0;
233     lv_coord_t width             = 0;
234     lv_txt_cmd_state_t cmd_state = LV_TXT_CMD_STATE_WAIT;
235     uint32_t letter;
236     uint32_t letter_next;
237 
238     if(length != 0) {
239         while(i < length) {
240             letter      = lv_txt_encoded_next(txt, &i);
241             letter_next = lv_txt_encoded_next(&txt[i], NULL);
242             if((flag & LV_TXT_FLAG_RECOLOR) != 0) {
243                 if(lv_txt_is_cmd(&cmd_state, letter) != false) {
244                     continue;
245                 }
246             }
247 
248             lv_coord_t char_width = lv_font_get_glyph_width(font, letter, letter_next);
249             if(char_width > 0) {
250                 width += char_width;
251                 width += letter_space;
252             }
253         }
254 
255         if(width > 0) {
256             width -= letter_space; /*Trim the last letter space. Important if the text is center
257                                       aligned */
258         }
259     }
260 
261     return width;
262 }
263 
264 /**
265  * Check next character in a string and decide if the character is part of the command or not
266  * @param state pointer to a txt_cmd_state_t variable which stores the current state of command
267  * processing (Initied. to TXT_CMD_STATE_WAIT )
268  * @param c the current character
269  * @return true: the character is part of a command and should not be written,
270  *         false: the character should be written
271  */
lv_txt_is_cmd(lv_txt_cmd_state_t * state,uint32_t c)272 bool lv_txt_is_cmd(lv_txt_cmd_state_t * state, uint32_t c)
273 {
274     bool ret = false;
275 
276     if(c == (uint32_t)LV_TXT_COLOR_CMD[0]) {
277         if(*state == LV_TXT_CMD_STATE_WAIT) { /*Start char*/
278             *state = LV_TXT_CMD_STATE_PAR;
279             ret    = true;
280         }
281         /*Other start char in parameter is escaped cmd. char */
282         else if(*state == LV_TXT_CMD_STATE_PAR) {
283             *state = LV_TXT_CMD_STATE_WAIT;
284         }
285         /*Command end */
286         else if(*state == LV_TXT_CMD_STATE_IN) {
287             *state = LV_TXT_CMD_STATE_WAIT;
288             ret    = true;
289         }
290     }
291 
292     /*Skip the color parameter and wait the space after it*/
293     if(*state == LV_TXT_CMD_STATE_PAR) {
294         if(c == ' ') {
295             *state = LV_TXT_CMD_STATE_IN; /*After the parameter the text is in the command*/
296         }
297         ret = true;
298     }
299 
300     return ret;
301 }
302 
303 /**
304  * Insert a string into an other
305  * @param txt_buf the original text (must be big enough for the result text)
306  * @param pos position to insert. Expressed in character index and not byte index (Different in
307  * UTF-8) 0: before the original text, 1: after the first char etc.
308  * @param ins_txt text to insert
309  */
lv_txt_ins(char * txt_buf,uint32_t pos,const char * ins_txt)310 void lv_txt_ins(char * txt_buf, uint32_t pos, const char * ins_txt)
311 {
312     uint32_t old_len = strlen(txt_buf);
313     uint32_t ins_len = strlen(ins_txt);
314     uint32_t new_len = ins_len + old_len;
315     pos              = lv_txt_encoded_get_byte_id(txt_buf, pos); /*Convert to byte index instead of letter index*/
316 
317     /*Copy the second part into the end to make place to text to insert*/
318     uint32_t i;
319     for(i = new_len; i >= pos + ins_len; i--) {
320         txt_buf[i] = txt_buf[i - ins_len];
321     }
322 
323     /* Copy the text into the new space*/
324     memcpy(txt_buf + pos, ins_txt, ins_len);
325 }
326 
327 /**
328  * Delete a part of a string
329  * @param txt string to modify
330  * @param pos position where to start the deleting (0: before the first char, 1: after the first
331  * char etc.)
332  * @param len number of characters to delete
333  */
lv_txt_cut(char * txt,uint32_t pos,uint32_t len)334 void lv_txt_cut(char * txt, uint32_t pos, uint32_t len)
335 {
336 
337     uint32_t old_len = strlen(txt);
338 
339     pos = lv_txt_encoded_get_byte_id(txt, pos); /*Convert to byte index instead of letter index*/
340     len = lv_txt_encoded_get_byte_id(&txt[pos], len);
341 
342     /*Copy the second part into the end to make place to text to insert*/
343     uint32_t i;
344     for(i = pos; i <= old_len - len; i++) {
345         txt[i] = txt[i + len];
346     }
347 }
348 
349 #if LV_TXT_ENC == LV_TXT_ENC_UTF8
350 /*******************************
351  *   UTF-8 ENCODER/DECOER
352  ******************************/
353 
354 /**
355  * Give the size of an UTF-8 coded character
356  * @param str pointer to a character in a string
357  * @return length of the UTF-8 character (1,2,3 or 4). O on invalid code
358  */
lv_txt_utf8_size(const char * str)359 static uint8_t lv_txt_utf8_size(const char * str)
360 {
361     if((str[0] & 0x80) == 0)
362         return 1;
363     else if((str[0] & 0xE0) == 0xC0)
364         return 2;
365     else if((str[0] & 0xF0) == 0xE0)
366         return 3;
367     else if((str[0] & 0xF8) == 0xF0)
368         return 4;
369     return 1; /*If the char was invalid step tell it's 1 byte long*/
370 }
371 
372 /**
373  * Convert an Unicode letter to UTF-8.
374  * @param letter_uni an Unicode letter
375  * @return UTF-8 coded character in Little Endian to be compatible with C chars (e.g. 'Á', 'Ű')
376  */
lv_txt_unicode_to_utf8(uint32_t letter_uni)377 static uint32_t lv_txt_unicode_to_utf8(uint32_t letter_uni)
378 {
379     if(letter_uni < 128) return letter_uni;
380     uint8_t bytes[4];
381 
382     if(letter_uni < 0x0800) {
383         bytes[0] = ((letter_uni >> 6) & 0x1F) | 0xC0;
384         bytes[1] = ((letter_uni >> 0) & 0x3F) | 0x80;
385         bytes[2] = 0;
386         bytes[3] = 0;
387     } else if(letter_uni < 0x010000) {
388         bytes[0] = ((letter_uni >> 12) & 0x0F) | 0xE0;
389         bytes[1] = ((letter_uni >> 6) & 0x3F) | 0x80;
390         bytes[2] = ((letter_uni >> 0) & 0x3F) | 0x80;
391         bytes[3] = 0;
392     } else if(letter_uni < 0x110000) {
393         bytes[0] = ((letter_uni >> 18) & 0x07) | 0xF0;
394         bytes[1] = ((letter_uni >> 12) & 0x3F) | 0x80;
395         bytes[2] = ((letter_uni >> 6) & 0x3F) | 0x80;
396         bytes[3] = ((letter_uni >> 0) & 0x3F) | 0x80;
397     }
398 
399     uint32_t * res_p = (uint32_t *)bytes;
400     return *res_p;
401 }
402 
403 /**
404  * Convert a wide character, e.g. 'Á' little endian to be UTF-8 compatible
405  * @param c a wide character or a  Little endian number
406  * @return `c` in big endian
407  */
lv_txt_utf8_conv_wc(uint32_t c)408 static uint32_t lv_txt_utf8_conv_wc(uint32_t c)
409 {
410     /*Swap the bytes (UTF-8 is big endian, but the MCUs are little endian)*/
411     if((c & 0x80) != 0) {
412         uint32_t swapped;
413         uint8_t c8[4];
414         memcpy(c8, &c, 4);
415         swapped = (c8[0] << 24) + (c8[1] << 16) + (c8[2] << 8) + (c8[3]);
416         uint8_t i;
417         for(i = 0; i < 4; i++) {
418             if((swapped & 0xFF) == 0)
419                 swapped = (swapped >> 8); /*Ignore leading zeros (they were in the end originally)*/
420         }
421         c = swapped;
422     }
423 
424     return c;
425 }
426 
427 /**
428  * Decode an UTF-8 character from a string.
429  * @param txt pointer to '\0' terminated string
430  * @param i start byte index in 'txt' where to start.
431  *          After call it will point to the next UTF-8 char in 'txt'.
432  *          NULL to use txt[0] as index
433  * @return the decoded Unicode character or 0 on invalid UTF-8 code
434  */
lv_txt_utf8_next(const char * txt,uint32_t * i)435 static uint32_t lv_txt_utf8_next(const char * txt, uint32_t * i)
436 {
437     /* Unicode to UTF-8
438      * 00000000 00000000 00000000 0xxxxxxx -> 0xxxxxxx
439      * 00000000 00000000 00000yyy yyxxxxxx -> 110yyyyy 10xxxxxx
440      * 00000000 00000000 zzzzyyyy yyxxxxxx -> 1110zzzz 10yyyyyy 10xxxxxx
441      * 00000000 000wwwzz zzzzyyyy yyxxxxxx -> 11110www 10zzzzzz 10yyyyyy 10xxxxxx
442      * */
443 
444     uint32_t result = 0;
445 
446     /*Dummy 'i' pointer is required*/
447     uint32_t i_tmp = 0;
448     if(i == NULL) i = &i_tmp;
449 
450     /*Normal ASCII*/
451     if((txt[*i] & 0x80) == 0) {
452         result = txt[*i];
453         (*i)++;
454     }
455     /*Real UTF-8 decode*/
456     else {
457         /*2 bytes UTF-8 code*/
458         if((txt[*i] & 0xE0) == 0xC0) {
459             result = (uint32_t)(txt[*i] & 0x1F) << 6;
460             (*i)++;
461             if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/
462             result += (txt[*i] & 0x3F);
463             (*i)++;
464         }
465         /*3 bytes UTF-8 code*/
466         else if((txt[*i] & 0xF0) == 0xE0) {
467             result = (uint32_t)(txt[*i] & 0x0F) << 12;
468             (*i)++;
469 
470             if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/
471             result += (uint32_t)(txt[*i] & 0x3F) << 6;
472             (*i)++;
473 
474             if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/
475             result += (txt[*i] & 0x3F);
476             (*i)++;
477         }
478         /*4 bytes UTF-8 code*/
479         else if((txt[*i] & 0xF8) == 0xF0) {
480             result = (uint32_t)(txt[*i] & 0x07) << 18;
481             (*i)++;
482 
483             if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/
484             result += (uint32_t)(txt[*i] & 0x3F) << 12;
485             (*i)++;
486 
487             if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/
488             result += (uint32_t)(txt[*i] & 0x3F) << 6;
489             (*i)++;
490 
491             if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/
492             result += txt[*i] & 0x3F;
493             (*i)++;
494         } else {
495             (*i)++; /*Not UTF-8 char. Go the next.*/
496         }
497     }
498     return result;
499 }
500 
501 /**
502  * Get previous UTF-8 character form a string.
503  * @param txt pointer to '\0' terminated string
504  * @param i start byte index in 'txt' where to start. After the call it will point to the previous
505  * UTF-8 char in 'txt'.
506  * @return the decoded Unicode character or 0 on invalid UTF-8 code
507  */
lv_txt_utf8_prev(const char * txt,uint32_t * i)508 static uint32_t lv_txt_utf8_prev(const char * txt, uint32_t * i)
509 {
510     uint8_t c_size;
511     uint8_t cnt = 0;
512 
513     /*Try to find a !0 long UTF-8 char by stepping one character back*/
514     (*i)--;
515     do {
516         if(cnt >= 4) return 0; /*No UTF-8 char found before the initial*/
517 
518         c_size = lv_txt_encoded_size(&txt[*i]);
519         if(c_size == 0) {
520             if(*i != 0)
521                 (*i)--;
522             else
523                 return 0;
524         }
525         cnt++;
526     } while(c_size == 0);
527 
528     uint32_t i_tmp  = *i;
529     uint32_t letter = lv_txt_encoded_next(txt, &i_tmp); /*Character found, get it*/
530 
531     return letter;
532 }
533 
534 /**
535  * Convert a character index (in an UTF-8 text) to byte index.
536  * E.g. in "AÁRT" index of 'R' is 2th char but start at byte 3 because 'Á' is 2 bytes long
537  * @param txt a '\0' terminated UTF-8 string
538  * @param utf8_id character index
539  * @return byte index of the 'utf8_id'th letter
540  */
lv_txt_utf8_get_byte_id(const char * txt,uint32_t utf8_id)541 static uint32_t lv_txt_utf8_get_byte_id(const char * txt, uint32_t utf8_id)
542 {
543     uint32_t i;
544     uint32_t byte_cnt = 0;
545     for(i = 0; i < utf8_id; i++) {
546         byte_cnt += lv_txt_encoded_size(&txt[byte_cnt]);
547     }
548 
549     return byte_cnt;
550 }
551 
552 /**
553  * Convert a byte index (in an UTF-8 text) to character index.
554  * E.g. in "AÁRT" index of 'R' is 2th char but start at byte 3 because 'Á' is 2 bytes long
555  * @param txt a '\0' terminated UTF-8 string
556  * @param byte_id byte index
557  * @return character index of the letter at 'byte_id'th position
558  */
lv_txt_utf8_get_char_id(const char * txt,uint32_t byte_id)559 static uint32_t lv_txt_utf8_get_char_id(const char * txt, uint32_t byte_id)
560 {
561     uint32_t i        = 0;
562     uint32_t char_cnt = 0;
563 
564     while(i < byte_id) {
565         lv_txt_encoded_next(txt, &i); /*'i' points to the next letter so use the prev. value*/
566         char_cnt++;
567     }
568 
569     return char_cnt;
570 }
571 
572 /**
573  * Get the number of characters (and NOT bytes) in a string. Decode it with UTF-8 if enabled.
574  * E.g.: "ÁBC" is 3 characters (but 4 bytes)
575  * @param txt a '\0' terminated char string
576  * @return number of characters
577  */
lv_txt_utf8_get_length(const char * txt)578 static uint32_t lv_txt_utf8_get_length(const char * txt)
579 {
580     uint32_t len = 0;
581     uint32_t i   = 0;
582 
583     while(txt[i] != '\0') {
584         lv_txt_encoded_next(txt, &i);
585         len++;
586     }
587 
588     return len;
589 }
590 
591 #elif LV_TXT_ENC == LV_TXT_ENC_ASCII
592 /*******************************
593  *  ASCII ENCODER/DECOER
594  ******************************/
595 
596 /**
597  * Give the size of an ISO8859-1 coded character
598  * @param str pointer to a character in a string
599  * @return length of the UTF-8 character (1,2,3 or 4). O on invalid code
600  */
lv_txt_iso8859_1_size(const char * str)601 static uint8_t lv_txt_iso8859_1_size(const char * str)
602 {
603     (void)str; /*Unused*/
604     return 1;
605 }
606 
607 /**
608  * Convert an Unicode letter to ISO8859-1.
609  * @param letter_uni an Unicode letter
610  * @return ISO8859-1 coded character in Little Endian to be compatible with C chars (e.g. 'Á', 'Ű')
611  */
lv_txt_unicode_to_iso8859_1(uint32_t letter_uni)612 static uint32_t lv_txt_unicode_to_iso8859_1(uint32_t letter_uni)
613 {
614     if(letter_uni < 128)
615         return letter_uni;
616     else
617         return ' ';
618 }
619 
620 /**
621  * Convert wide characters to ASCII, however wide characters in ASCII range (e.g. 'A') are ASCII compatible by default.
622  * So this function does nothing just returns with `c`.
623  * @param c a character, e.g. 'A'
624  * @return same as `c`
625  */
lv_txt_iso8859_1_conv_wc(uint32_t c)626 static uint32_t lv_txt_iso8859_1_conv_wc(uint32_t c)
627 {
628     return c;
629 }
630 
631 /**
632  * Decode an ISO8859-1 character from a string.
633  * @param txt pointer to '\0' terminated string
634  * @param i start byte index in 'txt' where to start.
635  *          After call it will point to the next UTF-8 char in 'txt'.
636  *          NULL to use txt[0] as index
637  * @return the decoded Unicode character or 0 on invalid UTF-8 code
638  */
lv_txt_iso8859_1_next(const char * txt,uint32_t * i)639 static uint32_t lv_txt_iso8859_1_next(const char * txt, uint32_t * i)
640 {
641     if(i == NULL) return txt[1]; /*Get the next char */
642 
643     uint8_t letter = txt[*i];
644     (*i)++;
645     return letter;
646 }
647 
648 /**
649  * Get previous ISO8859-1 character form a string.
650  * @param txt pointer to '\0' terminated string
651  * @param i start byte index in 'txt' where to start. After the call it will point to the previous UTF-8 char in 'txt'.
652  * @return the decoded Unicode character or 0 on invalid UTF-8 code
653  */
lv_txt_iso8859_1_prev(const char * txt,uint32_t * i)654 static uint32_t lv_txt_iso8859_1_prev(const char * txt, uint32_t * i)
655 {
656     if(i == NULL) return *(txt - 1); /*Get the prev. char */
657 
658     (*i)--;
659     uint8_t letter = txt[*i];
660 
661     return letter;
662 }
663 
664 /**
665  * Convert a character index (in an ISO8859-1 text) to byte index.
666  * E.g. in "AÁRT" index of 'R' is 2th char but start at byte 3 because 'Á' is 2 bytes long
667  * @param txt a '\0' terminated UTF-8 string
668  * @param utf8_id character index
669  * @return byte index of the 'utf8_id'th letter
670  */
lv_txt_iso8859_1_get_byte_id(const char * txt,uint32_t utf8_id)671 static uint32_t lv_txt_iso8859_1_get_byte_id(const char * txt, uint32_t utf8_id)
672 {
673     (void)txt;      /*Unused*/
674     return utf8_id; /*In Non encoded no difference*/
675 }
676 
677 /**
678  * Convert a byte index (in an ISO8859-1 text) to character index.
679  * E.g. in "AÁRT" index of 'R' is 2th char but start at byte 3 because 'Á' is 2 bytes long
680  * @param txt a '\0' terminated UTF-8 string
681  * @param byte_id byte index
682  * @return character index of the letter at 'byte_id'th position
683  */
lv_txt_iso8859_1_get_char_id(const char * txt,uint32_t byte_id)684 static uint32_t lv_txt_iso8859_1_get_char_id(const char * txt, uint32_t byte_id)
685 {
686     (void)txt;      /*Unused*/
687     return byte_id; /*In Non encoded no difference*/
688 }
689 
690 /**
691  * Get the number of characters (and NOT bytes) in a string. Decode it with UTF-8 if enabled.
692  * E.g.: "ÁBC" is 3 characters (but 4 bytes)
693  * @param txt a '\0' terminated char string
694  * @return number of characters
695  */
lv_txt_iso8859_1_get_length(const char * txt)696 static uint32_t lv_txt_iso8859_1_get_length(const char * txt)
697 {
698     return strlen(txt);
699 }
700 #else
701 
702 #error "Invalid character encoding. See `LV_TXT_ENC` in `lv_conf.h`"
703 
704 #endif
705 
706 /**********************
707  *   STATIC FUNCTIONS
708  **********************/
709 
710 /**
711  * Test if char is break char or not (a text can broken here or not)
712  * @param letter a letter
713  * @return false: 'letter' is not break char
714  */
is_break_char(uint32_t letter)715 static inline bool is_break_char(uint32_t letter)
716 {
717     uint8_t i;
718     bool ret = false;
719 
720     /*Compare the letter to TXT_BREAK_CHARS*/
721     for(i = 0; LV_TXT_BREAK_CHARS[i] != '\0'; i++) {
722         if(letter == (uint32_t)LV_TXT_BREAK_CHARS[i]) {
723             ret = true; /*If match then it is break char*/
724             break;
725         }
726     }
727 
728     return ret;
729 }
730