1 /**
2  * @file lv_text_private.h
3  *
4  */
5 
6 #ifndef LV_TEXT_PRIVATE_H
7 #define LV_TEXT_PRIVATE_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 
17 #include "lv_text.h"
18 
19 /*********************
20  *      DEFINES
21  *********************/
22 
23 /**********************
24  *      TYPEDEFS
25  **********************/
26 
27 /**********************
28  * GLOBAL PROTOTYPES
29  **********************/
30 
31 /**
32  * Get the next line of text. Check line length and break chars too.
33  * @param txt a '\0' terminated string
34  * @param len length of 'txt' in bytes
35  * @param font pointer to a font
36  * @param letter_space letter space
37  * @param max_width max width of the text (break the lines to fit this size). Set COORD_MAX to avoid
38  * line breaks
39  * @param used_width When used_width != NULL, save the width of this line if
40  * flag == LV_TEXT_FLAG_NONE, otherwise save -1.
41  * @param flag settings for the text from 'txt_flag_type' enum
42  * @return the index of the first char of the new line (in byte index not letter index. With UTF-8
43  * they are different)
44  */
45 uint32_t lv_text_get_next_line(const char * txt, uint32_t len, const lv_font_t * font, int32_t letter_space,
46                                int32_t max_width, int32_t * used_width, lv_text_flag_t flag);
47 
48 /**
49  * Insert a string into another
50  * @param txt_buf the original text (must be big enough for the result text and NULL terminated)
51  * @param pos position to insert (0: before the original text, 1: after the first char etc.)
52  * @param ins_txt text to insert, must be '\0' terminated
53  */
54 void lv_text_ins(char * txt_buf, uint32_t pos, const char * ins_txt);
55 
56 /**
57  * Delete a part of a string
58  * @param txt string to modify, must be '\0' terminated and should point to a heap or stack frame, not read-only memory.
59  * @param pos position where to start the deleting (0: before the first char, 1: after the first
60  * char etc.)
61  * @param len number of characters to delete
62  */
63 void lv_text_cut(char * txt, uint32_t pos, uint32_t len);
64 
65 /**
66  * return a new formatted text. Memory will be allocated to store the text.
67  * @param fmt `printf`-like format
68  * @param ap items to print
69 
70  * @return pointer to the allocated text string.
71  */
72 char * lv_text_set_text_vfmt(const char * fmt, va_list ap) LV_FORMAT_ATTRIBUTE(1, 0);
73 
74 /**
75  * Decode two encoded character from a string.
76  * @param txt pointer to '\0' terminated string
77  * @param letter the first decoded Unicode character or 0 on invalid data code
78  * @param letter_next the second decoded Unicode character or 0 on invalid data code
79  * @param ofs start index in 'txt' where to start.
80  *                After the call it will point to the next encoded char in 'txt'.
81  *                NULL to use txt[0] as index
82  */
83 void lv_text_encoded_letter_next_2(const char * txt, uint32_t * letter, uint32_t * letter_next, uint32_t * ofs);
84 
85 /**
86  * Test if char is break char or not (a text can broken here or not)
87  * @param letter a letter
88  * @return false: 'letter' is not break char
89  */
lv_text_is_break_char(uint32_t letter)90 static inline bool lv_text_is_break_char(uint32_t letter)
91 {
92     uint8_t i;
93     bool ret = false;
94 
95     /*Compare the letter to TXT_BREAK_CHARS*/
96     for(i = 0; LV_TXT_BREAK_CHARS[i] != '\0'; i++) {
97         if(letter == (uint32_t)LV_TXT_BREAK_CHARS[i]) {
98             ret = true; /*If match then it is break char*/
99             break;
100         }
101     }
102 
103     return ret;
104 }
105 
106 /**
107  * Test if char is break char or not (a text can broken here or not)
108  * @param letter a letter
109  * @return false: 'letter' is not break char
110  */
lv_text_is_a_word(uint32_t letter)111 static inline bool lv_text_is_a_word(uint32_t letter)
112 {
113     /*Cheap check on invalid letter*/
114     if(letter == 0) return false;
115 
116     /*CJK Unified Ideographs*/
117     if(letter >= 0x4E00 && letter <= 0x9FFF) {
118         return true;
119     }
120 
121     /*Fullwidth ASCII variants*/
122     if(letter >= 0xFF01 && letter <= 0xFF5E) {
123         return true;
124     }
125 
126     /*CJK symbols and punctuation*/
127     if(letter >= 0x3000 && letter <= 0x303F) {
128         return true;
129     }
130 
131     /*CJK Radicals Supplement*/
132     if(letter >= 0x2E80 && letter <= 0x2EFF) {
133         return true;
134     }
135 
136     /*CJK Strokes*/
137     if(letter >= 0x31C0 && letter <= 0x31EF) {
138         return true;
139     }
140 
141     /*Hiragana and Katakana*/
142     if(letter >= 0x3040 && letter <= 0x30FF) {
143         return true;
144     }
145 
146     /*Chinese Vertical Forms*/
147     if(letter >= 0xFE10 && letter <= 0xFE1F) {
148         return true;
149     }
150 
151     /*CJK Compatibility Forms*/
152     if(letter >= 0xFE30 && letter <= 0xFE4F) {
153         return true;
154     }
155 
156     return false;
157 }
158 
159 /**
160  * Test if character can be treated as marker, and don't need to be rendered.
161  * Note, this is not a full list. Add your findings to the list.
162  *
163  * @param letter a letter
164  * @return true if so
165  */
lv_text_is_marker(uint32_t letter)166 static inline bool lv_text_is_marker(uint32_t letter)
167 {
168     if(letter < 0x20) return true;
169 
170     /*U+061C ARABIC LETTER MARK, see https://www.compart.com/en/unicode/block/U+0600*/
171     if(letter == 0x061C) return true;
172 
173     /*U+115F HANGUL CHOSEONG FILLER, See https://www.compart.com/en/unicode/block/U+1100*/
174     if(letter == 0x115F) return true;
175     /*U+1160 HANGUL JUNGSEONG FILLER*/
176     if(letter == 0x1160) return true;
177 
178     /*See https://www.compart.com/en/unicode/block/U+1800*/
179     if(letter >= 0x180B && letter <= 0x180E) return true;
180 
181     /*See https://www.compart.com/en/unicode/block/U+2000*/
182     if(letter >= 0x200B && letter <= 0x200F) return true;
183     if(letter >= 0x2028 && letter <= 0x202F) return true;
184     if(letter >= 0x205F && letter <= 0x206F) return true;
185 
186     /*U+FEFF ZERO WIDTH NO-BREAK SPACE, see https://www.compart.com/en/unicode/block/U+FE70*/
187     if(letter == 0xFEFF) return true;
188 
189     if(letter == 0xF8FF) return true; /*LV_SYMBOL_DUMMY*/
190 
191     return false;
192 }
193 
194 /***************************************************************
195  *  GLOBAL FUNCTION POINTERS FOR CHARACTER ENCODING INTERFACE
196  ***************************************************************/
197 
198 /**
199  * Give the size of an encoded character
200  * @param txt  pointer to a character in a string
201  * @return length of the encoded character (1,2,3 ...). O in invalid
202  */
203 extern uint8_t (*const lv_text_encoded_size)(const char * txt);
204 
205 /**
206  * Convert a Unicode letter to encoded
207  * @param letter_uni a Unicode letter
208  * @return Encoded character in Little Endian to be compatible with C chars (e.g. 'Á', 'Ü')
209  */
210 extern uint32_t (*const lv_text_unicode_to_encoded)(uint32_t letter_uni);
211 
212 /**
213  * Convert a wide character, e.g. 'Á' little endian to be compatible with the encoded format.
214  * @param c a wide character
215  * @return `c` in the encoded format
216  */
217 extern uint32_t (*const lv_text_encoded_conv_wc)(uint32_t c);
218 
219 /**
220  * Decode the next encoded character from a string.
221  * @param txt      pointer to '\0' terminated string
222  * @param i_start  start index in 'txt' where to start.
223  *                 After the call it will point to the next encoded char in 'txt'.
224  *                 NULL to use txt[0] as index
225  * @return the decoded Unicode character or 0 on invalid data code
226  */
227 extern uint32_t (*const lv_text_encoded_next)(const char * txt, uint32_t * i_start);
228 
229 /**
230  * Get the previous encoded character form a string.
231  *
232  * @param  txt      pointer to '\0' terminated string
233  * @param  i_start  index in 'txt' where to start. After the call it will point to the previous
234  *                    encoded char in 'txt'.
235  *
236  * @return the decoded Unicode character or 0 on invalid data
237  */
238 extern uint32_t (*const lv_text_encoded_prev)(const char * txt, uint32_t * i_start);
239 
240 /**
241  * Convert a letter index (in the encoded text) to byte index.
242  * E.g. in UTF-8 "AÁRT" index of 'R' is 2 but start at byte 3 because 'Á' is 2 bytes long
243  * @param txt a '\0' terminated UTF-8 string
244  * @param utf8_id character index
245  * @return byte index of the 'enc_id'th letter
246  */
247 extern uint32_t (*const lv_text_encoded_get_byte_id)(const char * txt, uint32_t utf8_id);
248 
249 /**
250  * Convert a byte index (in an encoded text) to character index.
251  * E.g. in UTF-8 "AÁRT" index of 'R' is 2 but start at byte 3 because 'Á' is 2 bytes long
252  * @param txt a '\0' terminated UTF-8 string
253  * @param byte_id byte index
254  * @return character index of the letter at 'byte_id'th position
255  */
256 extern uint32_t (*const lv_text_encoded_get_char_id)(const char * txt, uint32_t byte_id);
257 
258 /**
259  * Get the number of characters (and NOT bytes) in a string.
260  * E.g. in UTF-8 "ÁBC" is 3 characters (but 4 bytes)
261  * @param txt a '\0' terminated char string
262  * @return number of characters
263  */
264 extern uint32_t (*const lv_text_get_encoded_length)(const char * txt);
265 
266 /**********************
267  *      MACROS
268  **********************/
269 
270 #ifdef __cplusplus
271 } /*extern "C"*/
272 #endif
273 
274 #endif /*LV_TEXT_PRIVATE_H*/
275