My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

fontutils.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**
  2. * @file fontutils.cpp
  3. * @brief help functions for font and char
  4. * @author Yunhui Fu (yhfudev@gmail.com)
  5. * @version 1.0
  6. * @date 2016-08-19
  7. * @copyright GPL/BSD
  8. */
  9. #include "../inc/MarlinConfig.h"
  10. #define MAX_UTF8_CHAR_SIZE 4
  11. #if HAS_WIRED_LCD
  12. #include "marlinui.h"
  13. #include "../MarlinCore.h"
  14. #endif
  15. #include "fontutils.h"
  16. uint8_t read_byte_ram(uint8_t * str) {
  17. return *str;
  18. }
  19. uint8_t read_byte_rom(uint8_t * str) {
  20. return pgm_read_byte(str);
  21. }
  22. /**
  23. * @brief Using binary search to find the position by data_pin
  24. *
  25. * @param userdata : User's data
  26. * @param num_data : the item number of the sorted data
  27. * @param cb_comp : the callback function to compare the user's data and pin
  28. * @param data_pin : The reference data to be found
  29. * @param ret_idx : the position of the required data; If failed, then it is the failed position, which is the insert position if possible.
  30. *
  31. * @return 0 on found, <0 on failed(fail position is saved by ret_idx)
  32. *
  33. * Using binary search to find the position by data_pin. The user's data should be sorted.
  34. */
  35. int pf_bsearch_r(void *userdata, size_t num_data, pf_bsearch_cb_comp_t cb_comp, void *data_pinpoint, size_t *ret_idx) {
  36. int retcomp;
  37. if (num_data < 1) {
  38. *ret_idx = 0;
  39. return -1;
  40. }
  41. size_t i = 0, ileft = 1, iright = num_data;
  42. bool flg_found = false;
  43. for (; ileft <= iright;) {
  44. i = (ileft + iright) / 2 - 1;
  45. /* cb_comp should return the *userdata[i] - *data_pinpoint */
  46. retcomp = cb_comp (userdata, i, data_pinpoint);
  47. if (retcomp > 0)
  48. iright = i;
  49. else if (retcomp < 0)
  50. ileft = i + 2;
  51. else {
  52. /* found ! */
  53. flg_found = true;
  54. break;
  55. }
  56. }
  57. if (flg_found) {
  58. *ret_idx = i;
  59. return 0;
  60. }
  61. if (iright <= i)
  62. *ret_idx = i;
  63. else if (ileft >= i + 2)
  64. *ret_idx = i + 1;
  65. return -1;
  66. }
  67. /* Returns true if passed byte is first byte of UTF-8 char sequence */
  68. static inline bool utf8_is_start_byte_of_char(const uint8_t b) {
  69. return 0x80 != (b & 0xC0);
  70. }
  71. /* This function gets the character at the pstart position, interpreting UTF8 multibyte sequences
  72. and returns the pointer to the next character */
  73. uint8_t* get_utf8_value_cb(uint8_t *pstart, read_byte_cb_t cb_read_byte, wchar_t *pval) {
  74. uint32_t val = 0;
  75. uint8_t *p = pstart;
  76. #define NEXT_6_BITS() do{ val <<= 6; p++; valcur = cb_read_byte(p); val |= (valcur & 0x3F); }while(0)
  77. uint8_t valcur = cb_read_byte(p);
  78. if (0 == (0x80 & valcur)) {
  79. val = valcur;
  80. p++;
  81. }
  82. else if (0xC0 == (0xE0 & valcur)) {
  83. val = valcur & 0x1F;
  84. NEXT_6_BITS();
  85. p++;
  86. }
  87. #if MAX_UTF8_CHAR_SIZE >= 3
  88. else if (0xE0 == (0xF0 & valcur)) {
  89. val = valcur & 0x0F;
  90. NEXT_6_BITS();
  91. NEXT_6_BITS();
  92. p++;
  93. }
  94. #endif
  95. #if MAX_UTF8_CHAR_SIZE >= 4
  96. else if (0xF0 == (0xF8 & valcur)) {
  97. val = valcur & 0x07;
  98. NEXT_6_BITS();
  99. NEXT_6_BITS();
  100. NEXT_6_BITS();
  101. p++;
  102. }
  103. #endif
  104. #if MAX_UTF8_CHAR_SIZE >= 5
  105. else if (0xF8 == (0xFC & valcur)) {
  106. val = valcur & 0x03;
  107. NEXT_6_BITS();
  108. NEXT_6_BITS();
  109. NEXT_6_BITS();
  110. NEXT_6_BITS();
  111. p++;
  112. }
  113. #endif
  114. #if MAX_UTF8_CHAR_SIZE >= 6
  115. else if (0xFC == (0xFE & valcur)) {
  116. val = valcur & 0x01;
  117. NEXT_6_BITS();
  118. NEXT_6_BITS();
  119. NEXT_6_BITS();
  120. NEXT_6_BITS();
  121. NEXT_6_BITS();
  122. p++;
  123. }
  124. #endif
  125. else if (!utf8_is_start_byte_of_char(valcur))
  126. for (; !utf8_is_start_byte_of_char(valcur); ) { p++; valcur = cb_read_byte(p); }
  127. else
  128. for (; 0xFC < (0xFE & valcur); ) { p++; valcur = cb_read_byte(p); }
  129. if (pval) *pval = val;
  130. return p;
  131. }
  132. static inline uint8_t utf8_strlen_cb(const char *pstart, read_byte_cb_t cb_read_byte) {
  133. uint8_t cnt = 0;
  134. uint8_t *p = (uint8_t *)pstart;
  135. for (;;) {
  136. const uint8_t b = cb_read_byte(p);
  137. if (!b) break;
  138. if (utf8_is_start_byte_of_char(b)) cnt++;
  139. p++;
  140. }
  141. return cnt;
  142. }
  143. uint8_t utf8_strlen(const char *pstart) {
  144. return utf8_strlen_cb(pstart, read_byte_ram);
  145. }
  146. uint8_t utf8_strlen_P(PGM_P pstart) {
  147. return utf8_strlen_cb(pstart, read_byte_rom);
  148. }
  149. static inline uint8_t utf8_byte_pos_by_char_num_cb(const char *pstart, read_byte_cb_t cb_read_byte, const uint8_t charnum) {
  150. uint8_t *p = (uint8_t *)pstart;
  151. uint8_t char_idx = 0;
  152. uint8_t byte_idx = 0;
  153. for (;;) {
  154. const uint8_t b = cb_read_byte(p + byte_idx);
  155. if (!b) return byte_idx; // Termination byte of string
  156. if (utf8_is_start_byte_of_char(b)) {
  157. char_idx++;
  158. if (char_idx == charnum + 1) return byte_idx;
  159. }
  160. byte_idx++;
  161. }
  162. }
  163. uint8_t utf8_byte_pos_by_char_num(const char *pstart, const uint8_t charnum) {
  164. return utf8_byte_pos_by_char_num_cb(pstart, read_byte_ram, charnum);
  165. }
  166. uint8_t utf8_byte_pos_by_char_num_P(PGM_P pstart, const uint8_t charnum) {
  167. return utf8_byte_pos_by_char_num_cb(pstart, read_byte_rom, charnum);
  168. }