lv_txt.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @file lv_text.h
  3. *
  4. */
  5. #ifndef LV_TXT_H
  6. #define LV_TXT_H
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. /*********************
  11. * INCLUDES
  12. *********************/
  13. #include <stdbool.h>
  14. #include "lv_area.h"
  15. #include "lv_font.h"
  16. #include "lv_area.h"
  17. /*********************
  18. * DEFINES
  19. *********************/
  20. #define LV_TXT_COLOR_CMD "#"
  21. /**********************
  22. * TYPEDEFS
  23. **********************/
  24. typedef enum
  25. {
  26. LV_TXT_FLAG_NONE = 0x00,
  27. LV_TXT_FLAG_RECOLOR = 0x01, /*Enable parsing of recolor command*/
  28. LV_TXT_FLAG_EXPAND = 0x02, /*Ignore width (Used by the library)*/
  29. LV_TXT_FLAG_NO_BREAK = 0x04, /*Ignore line breaks (Used by the library)*/
  30. LV_TXT_FLAG_CENTER = 0x08, /*Align the text to the middle*/
  31. }lv_txt_flag_t;
  32. typedef enum
  33. {
  34. LV_TXT_CMD_STATE_WAIT, /*Waiting for command*/
  35. LV_TXT_CMD_STATE_PAR, /*Processing the parameter*/
  36. LV_TXT_CMD_STATE_IN, /*Processing the command*/
  37. }lv_txt_cmd_state_t;
  38. /**********************
  39. * GLOBAL PROTOTYPES
  40. **********************/
  41. /**
  42. * Get size of a text
  43. * @param size_res pointer to a 'point_t' variable to store the result
  44. * @param text pointer to a text
  45. * @param font pinter to font of the text
  46. * @param letter_space letter space of the text
  47. * @param line_space line space of the text
  48. * @param flags settings for the text from 'txt_flag_t' enum
  49. * @param max_width max with of the text (break the lines to fit this size) Set CORD_MAX to avoid line breaks
  50. */
  51. void lv_txt_get_size(lv_point_t * size_res, const char * text, const lv_font_t * font,
  52. lv_coord_t letter_space, lv_coord_t line_space, lv_coord_t max_width, lv_txt_flag_t flag);
  53. /**
  54. * Get the next line of text. Check line length and break chars too.
  55. * @param txt a '\0' terminated string
  56. * @param font_p pointer to a font
  57. * @param letter_space letter space
  58. * @param max_width max with of the text (break the lines to fit this size) Set CORD_MAX to avoid line breaks
  59. * @param flags settings for the text from 'txt_flag_t' enum
  60. * @return the index of the first char of the new line
  61. */
  62. uint16_t lv_txt_get_next_line(const char * txt, const lv_font_t * font_p,
  63. lv_coord_t letter_space, lv_coord_t max_l, lv_txt_flag_t flag);
  64. /**
  65. * Give the length of a text with a given font
  66. * @param txt a '\0' terminate string
  67. * @param char_num number of characters in 'txt'
  68. * @param font_p pointer to a font
  69. * @param letter_space letter space
  70. * @param flags settings for the text from 'txt_flag_t' enum
  71. * @return length of a char_num long text
  72. */
  73. lv_coord_t lv_txt_get_width(const char * txt, uint16_t char_num,
  74. const lv_font_t * font_p, lv_coord_t letter_space, lv_txt_flag_t flag);
  75. /**
  76. * Check next character in a string and decide if te character is part of the command or not
  77. * @param state pointer to a txt_cmd_state_t variable which stores the current state of command processing
  78. * @param c the current character
  79. * @return true: the character is part of a command and should not be written,
  80. * false: the character should be written
  81. */
  82. bool lv_txt_is_cmd(lv_txt_cmd_state_t * state, uint32_t c);
  83. /**
  84. * Insert a string into an other
  85. * @param txt_buf the original text (must be big enough for the result text)
  86. * @param pos position to insert (0: before the original text, 1: after the first char etc.)
  87. * @param ins_txt text to insert
  88. */
  89. void lv_txt_ins(char * txt_buf, uint32_t pos, const char * ins_txt);
  90. /**
  91. * Delete a part of a string
  92. * @param txt string to modify
  93. * @param pos position where to start the deleting (0: before the first char, 1: after the first char etc.)
  94. * @param len number of characters to delete
  95. */
  96. void lv_txt_cut(char * txt, uint32_t pos, uint32_t len);
  97. /**
  98. * Give the size of an UTF-8 coded character
  99. * @param c A character where the UTF-8 character starts
  100. * @return length of the UTF-8 character (1,2,3 or 4). O on invalid code
  101. */
  102. uint8_t lv_txt_utf8_size(uint8_t c);
  103. /**
  104. * Convert an Unicode letter to UTF-8.
  105. * @param letter_uni an Unicode letter
  106. * @return UTF-8 coded character in Little Endian to be compatible with C chars (e.g. 'Á', 'Ű')
  107. */
  108. uint32_t lv_txt_unicode_to_utf8(uint32_t letter_uni);
  109. /**
  110. * Decode an UTF-8 character from a string.
  111. * @param txt pointer to '\0' terminated string
  112. * @param i start index in 'txt' where to start.
  113. * After the call it will point to the next UTF-8 char in 'txt'.
  114. * NULL to use txt[0] as index
  115. * @return the decoded Unicode character or 0 on invalid UTF-8 code
  116. */
  117. uint32_t lv_txt_utf8_next(const char * txt, uint32_t * i);
  118. /**
  119. * Get previous UTF-8 character form a string.
  120. * @param txt pointer to '\0' terminated string
  121. * @param i_start index in 'txt' where to start. After the call it will point to the next UTF-8 char in 'txt'.
  122. * @return the decoded Unicode character or 0 on invalid UTF-8 code
  123. */
  124. uint32_t lv_txt_utf8_prev(const char * txt, uint32_t * i_start);
  125. /**
  126. * Convert a letter index (in an UTF-8 text) to byte index.
  127. * E.g. in "AÁRT" index of 'R' is 2 but start at byte 3 because 'Á' is 2 bytes long
  128. * @param txt a '\0' terminated UTF-8 string
  129. * @param utf8_id letter index
  130. * @return byte index of the 'utf8_id'th letter
  131. */
  132. uint32_t txt_utf8_get_byte_id(const char * txt, uint32_t utf8_id);
  133. /**
  134. * Convert a byte index (in an UTF-8 text) to character index.
  135. * E.g. in "AÁRT" index of 'R' is 2 but start at byte 3 because 'Á' is 2 bytes long
  136. * @param txt a '\0' terminated UTF-8 string
  137. * @param byte_id byte index
  138. * @return character index of the letter at 'byte_id'th position
  139. */
  140. uint32_t lv_txt_utf8_get_char_id(const char * txt, uint32_t byte_id);
  141. /**
  142. * Get the number of characters (and NOT bytes) in a string. Decode it with UTF-8 if enabled.
  143. * E.g.: "ÁBC" is 3 characters (but 4 bytes)
  144. * @param txt a '\0' terminated char string
  145. * @return number of characters
  146. */
  147. uint32_t lv_txt_get_length(const char * txt);
  148. /**********************
  149. * MACROS
  150. **********************/
  151. #ifdef __cplusplus
  152. } /* extern "C" */
  153. #endif
  154. #endif /*USE_TXT*/