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