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