lv_txt.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /**
  2. * @file lv_text.c
  3. *
  4. */
  5. /*********************
  6. * INCLUDES
  7. *********************/
  8. #include "lv_txt.h"
  9. #include "../../lv_conf.h"
  10. #include "lv_math.h"
  11. /*********************
  12. * DEFINES
  13. *********************/
  14. #define NO_BREAK_FOUND UINT32_MAX
  15. /**********************
  16. * TYPEDEFS
  17. **********************/
  18. /**********************
  19. * STATIC PROTOTYPES
  20. **********************/
  21. static bool is_break_char(uint32_t letter);
  22. /**********************
  23. * STATIC VARIABLES
  24. **********************/
  25. /**********************
  26. * MACROS
  27. **********************/
  28. /**********************
  29. * GLOBAL FUNCTIONS
  30. **********************/
  31. /**
  32. * Get size of a text
  33. * @param size_res pointer to a 'point_t' variable to store the result
  34. * @param text pointer to a text
  35. * @param font pinter to font of the text
  36. * @param letter_space letter space of the text
  37. * @param txt.line_space line space of the text
  38. * @param flags settings for the text from 'txt_flag_t' enum
  39. * @param max_width max with of the text (break the lines to fit this size) Set CORD_MAX to avoid line breaks
  40. */
  41. void lv_txt_get_size(lv_point_t * size_res, const char * text, const lv_font_t * font,
  42. lv_coord_t letter_space, lv_coord_t line_space, lv_coord_t max_width, lv_txt_flag_t flag)
  43. {
  44. size_res->x = 0;
  45. size_res->y = 0;
  46. if(text == NULL) return;
  47. if(font == NULL) return;
  48. if(flag & LV_TXT_FLAG_EXPAND) max_width = LV_COORD_MAX;
  49. uint32_t line_start = 0;
  50. uint32_t new_line_start = 0;
  51. lv_coord_t act_line_length;
  52. uint8_t letter_height = lv_font_get_height(font);
  53. /*Calc. the height and longest line*/
  54. while (text[line_start] != '\0') {
  55. new_line_start += lv_txt_get_next_line(&text[line_start], font, letter_space, max_width, flag);
  56. size_res->y += letter_height ;
  57. size_res->y += line_space;
  58. /*Calculate the the longest line*/
  59. act_line_length = lv_txt_get_width(&text[line_start], new_line_start - line_start,
  60. font, letter_space, flag);
  61. size_res->x = LV_MATH_MAX(act_line_length, size_res->x);
  62. line_start = new_line_start;
  63. }
  64. if(line_start != 0 && (text[line_start - 1] == '\n' || text[line_start - 1] == '\r')) {
  65. size_res->y += letter_height + line_space;
  66. }
  67. /*Correction with the last line space or set the height manually if the text is empty*/
  68. if(size_res->y == 0) size_res->y = letter_height;
  69. else size_res->y -= line_space;
  70. }
  71. /**
  72. * Get the next line of text. Check line length and break chars too.
  73. * @param txt a '\0' terminated string
  74. * @param font pointer to a font
  75. * @param letter_space letter space
  76. * @param max_width max with of the text (break the lines to fit this size) Set CORD_MAX to avoid line breaks
  77. * @param flags settings for the text from 'txt_flag_type' enum
  78. * @return the index of the first char of the new line (in byte index not letter index. With UTF-8 they are different)
  79. */
  80. uint16_t lv_txt_get_next_line(const char * txt, const lv_font_t * font,
  81. lv_coord_t letter_space, lv_coord_t max_width, lv_txt_flag_t flag)
  82. {
  83. if(txt == NULL) return 0;
  84. if(font == NULL) return 0;
  85. if(flag & LV_TXT_FLAG_EXPAND) max_width = LV_COORD_MAX;
  86. uint32_t i = 0;
  87. lv_coord_t cur_w = 0;
  88. uint32_t last_break = NO_BREAK_FOUND;
  89. lv_txt_cmd_state_t cmd_state = LV_TXT_CMD_STATE_WAIT;
  90. uint32_t letter = 0;
  91. while(txt[i] != '\0') {
  92. letter = lv_txt_utf8_next(txt, &i);
  93. /*Handle the recolor command*/
  94. if((flag & LV_TXT_FLAG_RECOLOR) != 0) {
  95. if(lv_txt_is_cmd(&cmd_state, letter) != false) {
  96. continue; /*Skip the letter is it is part of a command*/
  97. }
  98. }
  99. /*Check for new line chars*/
  100. if((flag & LV_TXT_FLAG_NO_BREAK) == 0 && (letter == '\n' || letter == '\r')) {
  101. /*Handle \r\n as well*/
  102. uint32_t i_tmp = i;
  103. uint32_t letter_next = lv_txt_utf8_next(txt, &i_tmp);
  104. if(letter == '\r' && letter_next == '\n') i = i_tmp;
  105. return i; /*Return with the first letter of the next line*/
  106. } else { /*Check the actual length*/
  107. cur_w += lv_font_get_width(font, letter);
  108. /*If the txt is too long then finish, this is the line end*/
  109. if(cur_w > max_width) {
  110. /*If this a break char then break here.*/
  111. if(is_break_char(letter)) {
  112. /* Now 'i' points to the next char because of txt_utf8_next()
  113. * But we need the first char of the next line so keep it.
  114. * Hence do nothing here*/
  115. }
  116. /*If already a break character is found, then break there*/
  117. if(last_break != NO_BREAK_FOUND ) {
  118. i = last_break;
  119. } else {
  120. /* Now this character is out of the area so it will be first character of the next line*/
  121. /* But 'i' already points to the next character (because of lv_txt_utf8_next) step beck one*/
  122. lv_txt_utf8_prev(txt, &i);
  123. }
  124. /* Do not let to return without doing nothing.
  125. * Find at least one character (Avoid infinite loop )*/
  126. if(i == 0) lv_txt_utf8_next(txt, &i);
  127. return i;
  128. }
  129. /*If this char still can fit to this line then check if
  130. * txt can be broken here later */
  131. else if(is_break_char(letter)) {
  132. last_break = i; /*Save the first char index after break*/
  133. }
  134. }
  135. cur_w += letter_space;
  136. }
  137. return i;
  138. }
  139. /**
  140. * Give the length of a text with a given font
  141. * @param txt a '\0' terminate string
  142. * @param length length of 'txt' in bytes
  143. * @param font pointer to a font
  144. * @param letter_space letter space
  145. * @param flags settings for the text from 'txt_flag_t' enum
  146. * @return length of a char_num long text
  147. */
  148. lv_coord_t lv_txt_get_width(const char * txt, uint16_t length,
  149. const lv_font_t * font, lv_coord_t letter_space, lv_txt_flag_t flag)
  150. {
  151. if(txt == NULL) return 0;
  152. if(font == NULL) return 0;
  153. uint32_t i = 0;
  154. lv_coord_t width = 0;
  155. lv_txt_cmd_state_t cmd_state = LV_TXT_CMD_STATE_WAIT;
  156. uint32_t letter;
  157. if(length != 0) {
  158. while(i < length) {
  159. letter = lv_txt_utf8_next(txt, &i);
  160. if((flag & LV_TXT_FLAG_RECOLOR) != 0) {
  161. if(lv_txt_is_cmd(&cmd_state, letter) != false) {
  162. continue;
  163. }
  164. }
  165. width += lv_font_get_width(font, letter);
  166. width += letter_space;
  167. }
  168. /*Trim closing spaces. Important when the text is aligned to the middle */
  169. for(i = length - 1; i > 0; i--) {
  170. if(txt[i] == ' ') {
  171. width -= lv_font_get_width(font, txt[i]);
  172. width -= letter_space;
  173. } else {
  174. break;
  175. }
  176. }
  177. }
  178. return width;
  179. }
  180. /**
  181. * Check next character in a string and decide if the character is part of the command or not
  182. * @param state pointer to a txt_cmd_state_t variable which stores the current state of command processing
  183. * (Initied. to TXT_CMD_STATE_WAIT )
  184. * @param c the current character
  185. * @return true: the character is part of a command and should not be written,
  186. * false: the character should be written
  187. */
  188. bool lv_txt_is_cmd(lv_txt_cmd_state_t * state, uint32_t c)
  189. {
  190. bool ret = false;
  191. if(c == (uint32_t)LV_TXT_COLOR_CMD[0]) {
  192. if(*state == LV_TXT_CMD_STATE_WAIT) { /*Start char*/
  193. *state = LV_TXT_CMD_STATE_PAR;
  194. ret = true;
  195. } else if(*state == LV_TXT_CMD_STATE_PAR) { /*Other start char in parameter is escaped cmd. char */
  196. *state = LV_TXT_CMD_STATE_WAIT;
  197. }else if(*state == LV_TXT_CMD_STATE_IN) { /*Command end */
  198. *state = LV_TXT_CMD_STATE_WAIT;
  199. ret = true;
  200. }
  201. }
  202. /*Skip the color parameter and wait the space after it*/
  203. if(*state == LV_TXT_CMD_STATE_PAR) {
  204. if(c == ' ') {
  205. *state = LV_TXT_CMD_STATE_IN; /*After the parameter the text is in the command*/
  206. }
  207. ret = true;
  208. }
  209. return ret;
  210. }
  211. /**
  212. * Insert a string into an other
  213. * @param txt_buf the original text (must be big enough for the result text)
  214. * @param pos position to insert. Expressed in character index and not byte index (Different in UTF-8)
  215. * 0: before the original text, 1: after the first char etc.
  216. * @param ins_txt text to insert
  217. */
  218. void lv_txt_ins(char * txt_buf, uint32_t pos, const char * ins_txt)
  219. {
  220. uint32_t old_len = strlen(txt_buf);
  221. uint32_t ins_len = strlen(ins_txt);
  222. uint32_t new_len = ins_len + old_len;
  223. #if LV_TXT_UTF8 != 0
  224. pos = txt_utf8_get_byte_id(txt_buf, pos); /*Convert to byte index instead of letter index*/
  225. #endif
  226. /*Copy the second part into the end to make place to text to insert*/
  227. uint32_t i;
  228. for(i = new_len; i >= pos + ins_len; i--){
  229. txt_buf[i] = txt_buf[i - ins_len];
  230. }
  231. /* Copy the text into the new space*/
  232. memcpy(txt_buf + pos, ins_txt, ins_len);
  233. }
  234. /**
  235. * Delete a part of a string
  236. * @param txt string to modify
  237. * @param pos position where to start the deleting (0: before the first char, 1: after the first char etc.)
  238. * @param len number of characters to delete
  239. */
  240. void lv_txt_cut(char * txt, uint32_t pos, uint32_t len)
  241. {
  242. uint32_t old_len = strlen(txt);
  243. #if LV_TXT_UTF8 != 0
  244. pos = txt_utf8_get_byte_id(txt, pos); /*Convert to byte index instead of letter index*/
  245. len = txt_utf8_get_byte_id(&txt[pos], len);
  246. #endif
  247. /*Copy the second part into the end to make place to text to insert*/
  248. uint32_t i;
  249. for(i = pos; i <= old_len - len; i++){
  250. txt[i] = txt[i+len];
  251. }
  252. }
  253. /**
  254. * Give the size of an UTF-8 coded character
  255. * @param c A character where the UTF-8 character starts
  256. * @return length of the UTF-8 character (1,2,3 or 4). O on invalid code
  257. */
  258. uint8_t lv_txt_utf8_size(uint8_t c)
  259. {
  260. if((c & 0x80) == 0) return 1;
  261. else if((c & 0xE0) == 0xC0) return 2;
  262. else if((c & 0xF0) == 0xE0) return 3;
  263. else if((c & 0xF8) == 0xF0) return 4;
  264. return 0;
  265. }
  266. /**
  267. * Convert an Unicode letter to UTF-8.
  268. * @param letter_uni an Unicode letter
  269. * @return UTF-8 coded character in Little Endian to be compatible with C chars (e.g. 'Á', 'Ű')
  270. */
  271. uint32_t lv_txt_unicode_to_utf8(uint32_t letter_uni)
  272. {
  273. if(letter_uni < 128) return letter_uni;
  274. uint8_t bytes[4];
  275. if (letter_uni < 0x0800) {
  276. bytes[0] = ((letter_uni>>6) & 0x1F) | 0xC0;
  277. bytes[1] = ((letter_uni>>0) & 0x3F) | 0x80;
  278. bytes[2] = 0;
  279. bytes[3] = 0;
  280. }
  281. else if (letter_uni < 0x010000) {
  282. bytes[0] = ((letter_uni>>12) & 0x0F) | 0xE0;
  283. bytes[1] = ((letter_uni>>6) & 0x3F) | 0x80;
  284. bytes[2] = ((letter_uni>>0) & 0x3F) | 0x80;
  285. bytes[3] = 0;
  286. }
  287. else if (letter_uni < 0x110000) {
  288. bytes[0] = ((letter_uni>>18) & 0x07) | 0xF0;
  289. bytes[1] = ((letter_uni>>12) & 0x3F) | 0x80;
  290. bytes[2] = ((letter_uni>>6) & 0x3F) | 0x80;
  291. bytes[3] = ((letter_uni>>0) & 0x3F) | 0x80;
  292. }
  293. uint32_t *res_p = (uint32_t *)bytes;
  294. return *res_p;
  295. }
  296. /**
  297. * Decode an UTF-8 character from a string.
  298. * @param txt pointer to '\0' terminated string
  299. * @param i start byte index in 'txt' where to start.
  300. * After call it will point to the next UTF-8 char in 'txt'.
  301. * NULL to use txt[0] as index
  302. * @return the decoded Unicode character or 0 on invalid UTF-8 code
  303. */
  304. uint32_t lv_txt_utf8_next(const char * txt, uint32_t * i)
  305. {
  306. #if LV_TXT_UTF8 == 0
  307. if(i == NULL) return txt[1]; /*Get the next char */
  308. uint8_t letter = txt[*i] ;
  309. (*i)++;
  310. return letter;
  311. #else
  312. /* Unicode to UTF-8
  313. * 00000000 00000000 00000000 0xxxxxxx -> 0xxxxxxx
  314. * 00000000 00000000 00000yyy yyxxxxxx -> 110yyyyy 10xxxxxx
  315. * 00000000 00000000 zzzzyyyy yyxxxxxx -> 1110zzzz 10yyyyyy 10xxxxxx
  316. * 00000000 000wwwzz zzzzyyyy yyxxxxxx -> 11110www 10zzzzzz 10yyyyyy 10xxxxxx
  317. * */
  318. uint32_t result = 0;
  319. /*Dummy 'i' pointer is required*/
  320. uint32_t i_tmp = 0;
  321. if(i == NULL) i = &i_tmp;
  322. /*Normal ASCII*/
  323. if((txt[*i] & 0x80) == 0) {
  324. result = txt[*i];
  325. (*i)++;
  326. }
  327. /*Real UTF-8 decode*/
  328. else {
  329. /*2 bytes UTF-8 code*/
  330. if((txt[*i] & 0xE0) == 0xC0) {
  331. result = (uint32_t)(txt[*i] & 0x1F) << 6;
  332. (*i)++;
  333. if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/
  334. result += (txt[*i] & 0x3F);
  335. (*i)++;
  336. }
  337. /*3 bytes UTF-8 code*/
  338. else if((txt[*i] & 0xF0) == 0xE0) {
  339. result = (uint32_t)(txt[*i] & 0x0F) << 12;
  340. (*i)++;
  341. if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/
  342. result += (uint32_t)(txt[*i] & 0x3F) << 6;
  343. (*i)++;
  344. if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/
  345. result += (txt[*i] & 0x3F);
  346. (*i)++;
  347. }
  348. /*4 bytes UTF-8 code*/
  349. else if((txt[*i] & 0xF8) == 0xF0) {
  350. result = (uint32_t)(txt[*i] & 0x07) << 18;
  351. (*i)++;
  352. if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/
  353. result += (uint32_t)(txt[*i] & 0x3F) << 12;
  354. (*i)++;
  355. if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/
  356. result += (uint32_t)(txt[*i] & 0x3F) << 6;
  357. (*i)++;
  358. if((txt[*i] & 0xC0) != 0x80) return 0; /*Invalid UTF-8 code*/
  359. result += txt[*i] & 0x3F;
  360. (*i)++;
  361. } else {
  362. (*i)++; /*Not UTF-8 char. Go the next.*/
  363. }
  364. }
  365. return result;
  366. #endif
  367. }
  368. /**
  369. * Get previous UTF-8 character form a string.
  370. * @param txt pointer to '\0' terminated string
  371. * @param i start byte index in 'txt' where to start. After the call it will point to the previous UTF-8 char in 'txt'.
  372. * @return the decoded Unicode character or 0 on invalid UTF-8 code
  373. */
  374. uint32_t lv_txt_utf8_prev(const char * txt, uint32_t * i)
  375. {
  376. #if LV_TXT_UTF8 == 0
  377. if(i == NULL) return *(txt- 1); /*Get the prev. char */
  378. (*i)--;
  379. uint8_t letter = txt[*i] ;
  380. return letter;
  381. #else
  382. uint8_t c_size;
  383. uint8_t cnt = 0;
  384. /*Try to find a !0 long UTF-8 char by stepping one character back*/
  385. (*i)--;
  386. do {
  387. if(cnt >= 4) return 0; /*No UTF-8 char found before the initial*/
  388. c_size = lv_txt_utf8_size(txt[*i]);
  389. if(c_size == 0) {
  390. if(*i != 0) (*i)--;
  391. else return 0;
  392. }
  393. cnt++;
  394. } while(c_size == 0);
  395. uint32_t i_tmp = *i;
  396. uint32_t letter = lv_txt_utf8_next(txt, &i_tmp); /*Character found, get it*/
  397. return letter;
  398. #endif
  399. }
  400. /**
  401. * Convert a character index (in an UTF-8 text) to byte index.
  402. * E.g. in "AÁRT" index of 'R' is 2th char but start at byte 3 because 'Á' is 2 bytes long
  403. * @param txt a '\0' terminated UTF-8 string
  404. * @param utf8_id character index
  405. * @return byte index of the 'utf8_id'th letter
  406. */
  407. uint32_t txt_utf8_get_byte_id(const char * txt, uint32_t utf8_id)
  408. {
  409. #if LV_TXT_UTF8 == 0
  410. return utf8_id; /*In Non UTF-8 no difference*/
  411. #else
  412. uint32_t i;
  413. uint32_t byte_cnt = 0;
  414. for(i = 0; i < utf8_id; i++) {
  415. byte_cnt += lv_txt_utf8_size(txt[byte_cnt]);
  416. }
  417. return byte_cnt;
  418. #endif
  419. }
  420. /**
  421. * Convert a byte index (in an UTF-8 text) to character index.
  422. * E.g. in "AÁRT" index of 'R' is 2th char but start at byte 3 because 'Á' is 2 bytes long
  423. * @param txt a '\0' terminated UTF-8 string
  424. * @param byte_id byte index
  425. * @return character index of the letter at 'byte_id'th position
  426. */
  427. uint32_t lv_txt_utf8_get_char_id(const char * txt, uint32_t byte_id)
  428. {
  429. #if LV_TXT_UTF8 == 0
  430. return byte_id; /*In Non UTF-8 no difference*/
  431. #else
  432. uint32_t i = 0;
  433. uint32_t char_cnt = 0;
  434. while(i < byte_id) {
  435. lv_txt_utf8_next(txt, &i); /*'i' points to the next letter so use the prev. value*/
  436. char_cnt++;
  437. }
  438. return char_cnt;
  439. #endif
  440. }
  441. /**
  442. * Get the number of characters (and NOT bytes) in a string. Decode it with UTF-8 if enabled.
  443. * E.g.: "ÁBC" is 3 characters (but 4 bytes)
  444. * @param txt a '\0' terminated char string
  445. * @return number of characters
  446. */
  447. uint32_t lv_txt_get_length(const char * txt)
  448. {
  449. #if LV_TXT_UTF8 == 0
  450. return strlen(txt);
  451. #else
  452. uint32_t len = 0;
  453. uint32_t i = 0;
  454. while(txt[i] != '\0') {
  455. lv_txt_utf8_next(txt, &i);
  456. len++;
  457. }
  458. return len;
  459. #endif
  460. }
  461. /**********************
  462. * STATIC FUNCTIONS
  463. **********************/
  464. /**
  465. * Test if char is break char or not (a text can broken here or not)
  466. * @param letter a letter
  467. * @return false: 'letter' is not break char
  468. */
  469. static bool is_break_char(uint32_t letter)
  470. {
  471. uint8_t i;
  472. bool ret = false;
  473. /*Compare the letter to TXT_BREAK_CHARS*/
  474. for(i = 0; LV_TXT_BREAK_CHARS[i] != '\0'; i++) {
  475. if(letter == (uint32_t)LV_TXT_BREAK_CHARS[i]) {
  476. ret = true; /*If match then it is break char*/
  477. break;
  478. }
  479. }
  480. return ret;
  481. }