1 /**
2  * @file lv_text.h
3  *
4  */
5 
6 #ifndef LV_TXT_H
7 #define LV_TXT_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #include "../lv_conf_internal.h"
17 
18 #include <stdbool.h>
19 #include "lv_area.h"
20 #include "lv_area.h"
21 #include "../lv_font/lv_font.h"
22 
23 /*********************
24  *      DEFINES
25  *********************/
26 #ifndef LV_TXT_COLOR_CMD
27 #define LV_TXT_COLOR_CMD "#"
28 #endif
29 
30 #define LV_TXT_ENC_UTF8 1
31 #define LV_TXT_ENC_ASCII 2
32 
33 /**********************
34  *      TYPEDEFS
35  **********************/
36 /**
37  * Options for text rendering.
38  */
39 enum {
40     LV_TXT_FLAG_NONE    = 0x00,
41     LV_TXT_FLAG_RECOLOR = 0x01, /**< Enable parsing of recolor command*/
42     LV_TXT_FLAG_EXPAND  = 0x02, /**< Ignore max-width to avoid automatic word wrapping*/
43     LV_TXT_FLAG_CENTER  = 0x04, /**< Align the text to the middle*/
44     LV_TXT_FLAG_RIGHT   = 0x08, /**< Align the text to the right*/
45     LV_TXT_FLAG_FIT     = 0x10, /**< Max-width is already equal to the longest line. (Used to skip some calculation)*/
46 };
47 typedef uint8_t lv_txt_flag_t;
48 
49 /**
50  * State machine for text renderer. */
51 enum {
52     LV_TXT_CMD_STATE_WAIT, /**< Waiting for command*/
53     LV_TXT_CMD_STATE_PAR,  /**< Processing the parameter*/
54     LV_TXT_CMD_STATE_IN,   /**< Processing the command*/
55 };
56 typedef uint8_t lv_txt_cmd_state_t;
57 
58 /**********************
59  * GLOBAL PROTOTYPES
60  **********************/
61 
62 /**
63  * Get size of a text
64  * @param size_res pointer to a 'point_t' variable to store the result
65  * @param text pointer to a text
66  * @param font pointer to font of the text
67  * @param letter_space letter space of the text
68  * @param line_space line space of the text
69  * @param flags settings for the text from 'txt_flag_t' enum
70  * @param max_width max with of the text (break the lines to fit this size) Set CORD_MAX to avoid
71  * line breaks
72  */
73 void _lv_txt_get_size(lv_point_t * size_res, const char * text, const lv_font_t * font, lv_coord_t letter_space,
74                       lv_coord_t line_space, lv_coord_t max_width, lv_txt_flag_t flag);
75 
76 /**
77  * Get the next line of text. Check line length and break chars too.
78  * @param txt a '\0' terminated string
79  * @param font pointer to a font
80  * @param letter_space letter space
81  * @param max_width max with of the text (break the lines to fit this size) Set CORD_MAX to avoid
82  * line breaks
83  * @param flags settings for the text from 'txt_flag_type' enum
84  * @return the index of the first char of the new line (in byte index not letter index. With UTF-8
85  * they are different)
86  */
87 uint32_t _lv_txt_get_next_line(const char * txt, const lv_font_t * font, lv_coord_t letter_space, lv_coord_t max_width,
88                                lv_txt_flag_t flag);
89 
90 /**
91  * Give the length of a text with a given font
92  * @param txt a '\0' terminate string
93  * @param length length of 'txt' in byte count and not characters (Á is 1 character but 2 bytes in
94  * UTF-8)
95  * @param font pointer to a font
96  * @param letter_space letter space
97  * @param flags settings for the text from 'txt_flag_t' enum
98  * @return length of a char_num long text
99  */
100 lv_coord_t _lv_txt_get_width(const char * txt, uint32_t length, const lv_font_t * font, lv_coord_t letter_space,
101                              lv_txt_flag_t flag);
102 
103 /**
104  * Check next character in a string and decide if the character is part of the command or not
105  * @param state pointer to a txt_cmd_state_t variable which stores the current state of command
106  * processing
107  * @param c the current character
108  * @return true: the character is part of a command and should not be written,
109  *         false: the character should be written
110  */
111 bool _lv_txt_is_cmd(lv_txt_cmd_state_t * state, uint32_t c);
112 
113 /**
114  * Insert a string into an other
115  * @param txt_buf the original text (must be big enough for the result text)
116  * @param pos position to insert (0: before the original text, 1: after the first char etc.)
117  * @param ins_txt text to insert
118  */
119 void _lv_txt_ins(char * txt_buf, uint32_t pos, const char * ins_txt);
120 
121 /**
122  * Delete a part of a string
123  * @param txt string to modify
124  * @param pos position where to start the deleting (0: before the first char, 1: after the first
125  * char etc.)
126  * @param len number of characters to delete
127  */
128 void _lv_txt_cut(char * txt, uint32_t pos, uint32_t len);
129 
130 /***************************************************************
131  *  GLOBAL FUNCTION POINTERS FOR CAHRACTER ENCODING INTERFACE
132  ***************************************************************/
133 
134 /**
135  * Give the size of an encoded character
136  * @param str pointer to a character in a string
137  * @return length of the encoded character (1,2,3 ...). O in invalid
138  */
139 extern uint8_t (*_lv_txt_encoded_size)(const char *);
140 
141 /**
142  * Convert an Unicode letter to encoded
143  * @param letter_uni an Unicode letter
144  * @return Encoded character in Little Endian to be compatible with C chars (e.g. 'Á', 'Ü')
145  */
146 extern uint32_t (*_lv_txt_unicode_to_encoded)(uint32_t);
147 
148 /**
149  * Convert a wide character, e.g. 'Á' little endian to be compatible with the encoded format.
150  * @param c a wide character
151  * @return `c` in the encoded format
152  */
153 extern uint32_t (*_lv_txt_encoded_conv_wc)(uint32_t c);
154 
155 /**
156  * Decode the next encoded character from a string.
157  * @param txt pointer to '\0' terminated string
158  * @param i start index in 'txt' where to start.
159  *                After the call it will point to the next encoded char in 'txt'.
160  *                NULL to use txt[0] as index
161  * @return the decoded Unicode character or 0 on invalid data code
162  */
163 extern uint32_t (*_lv_txt_encoded_next)(const char *, uint32_t *);
164 
165 /**
166  * Get the previous encoded character form a string.
167  * @param txt pointer to '\0' terminated string
168  * @param i_start index in 'txt' where to start. After the call it will point to the previous
169  * encoded char in 'txt'.
170  * @return the decoded Unicode character or 0 on invalid data
171  */
172 extern uint32_t (*_lv_txt_encoded_prev)(const char *, uint32_t *);
173 
174 /**
175  * Convert a letter index (in an the encoded text) to byte index.
176  * E.g. in UTF-8 "AÁRT" index of 'R' is 2 but start at byte 3 because 'Á' is 2 bytes long
177  * @param txt a '\0' terminated UTF-8 string
178  * @param enc_id letter index
179  * @return byte index of the 'enc_id'th letter
180  */
181 extern uint32_t (*_lv_txt_encoded_get_byte_id)(const char *, uint32_t);
182 
183 /**
184  * Convert a byte index (in an encoded text) to character index.
185  * E.g. in UTF-8 "AÁRT" index of 'R' is 2 but start at byte 3 because 'Á' is 2 bytes long
186  * @param txt a '\0' terminated UTF-8 string
187  * @param byte_id byte index
188  * @return character index of the letter at 'byte_id'th position
189  */
190 extern uint32_t (*_lv_txt_encoded_get_char_id)(const char *, uint32_t);
191 
192 /**
193  * Get the number of characters (and NOT bytes) in a string.
194  * E.g. in UTF-8 "ÁBC" is 3 characters (but 4 bytes)
195  * @param txt a '\0' terminated char string
196  * @return number of characters
197  */
198 extern uint32_t (*_lv_txt_get_encoded_length)(const char *);
199 
200 /**********************
201  *      MACROS
202  **********************/
203 
204 #ifdef __cplusplus
205 } /* extern "C" */
206 #endif
207 
208 #endif /*USE_TXT*/
209