My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fontutils.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * @file fontutils.cpp
  24. * @brief help functions for font and char
  25. * @author Yunhui Fu (yhfudev@gmail.com)
  26. * @version 1.0
  27. * @date 2016-08-19
  28. * @copyright GPL/BSD
  29. */
  30. #include "../inc/MarlinConfig.h"
  31. #if HAS_WIRED_LCD
  32. #include "marlinui.h"
  33. #include "../MarlinCore.h"
  34. #endif
  35. #include "fontutils.h"
  36. uint8_t read_byte_ram(const uint8_t *str) { return *str; }
  37. uint8_t read_byte_rom(const uint8_t *str) { return pgm_read_byte(str); }
  38. /**
  39. * @brief Using binary search to find the position by data_pin
  40. *
  41. * @param userdata : User's data
  42. * @param num_data : the item number of the sorted data
  43. * @param cb_comp : the callback function to compare the user's data and pin
  44. * @param data_pin : The reference data to be found
  45. * @param ret_idx : the position of the required data; If failed, then it is the failed position, which is the insert position if possible.
  46. *
  47. * @return 0 on found, <0 on failed(fail position is saved by ret_idx)
  48. *
  49. * Using binary search to find the position by data_pin. The user's data should be sorted.
  50. */
  51. int pf_bsearch_r(void *userdata, size_t num_data, pf_bsearch_cb_comp_t cb_comp, void *data_pinpoint, size_t *ret_idx) {
  52. int retcomp;
  53. if (num_data < 1) {
  54. *ret_idx = 0;
  55. return -1;
  56. }
  57. size_t i = 0, ileft = 1, iright = num_data;
  58. bool flg_found = false;
  59. for (; ileft <= iright;) {
  60. i = (ileft + iright) / 2 - 1;
  61. /* cb_comp should return the *userdata[i] - *data_pinpoint */
  62. retcomp = cb_comp (userdata, i, data_pinpoint);
  63. if (retcomp > 0)
  64. iright = i;
  65. else if (retcomp < 0)
  66. ileft = i + 2;
  67. else {
  68. /* found ! */
  69. flg_found = true;
  70. break;
  71. }
  72. }
  73. if (flg_found) {
  74. *ret_idx = i;
  75. return 0;
  76. }
  77. if (iright <= i)
  78. *ret_idx = i;
  79. else if (ileft >= i + 2)
  80. *ret_idx = i + 1;
  81. return -1;
  82. }
  83. /* Returns true if passed byte is first byte of UTF-8 char sequence */
  84. static inline bool utf8_is_start_byte_of_char(const uint8_t b) {
  85. return 0x80 != (b & 0xC0);
  86. }
  87. /* This function gets the character at the pstart position, interpreting UTF8 multibyte sequences
  88. and returns the pointer to the next character */
  89. const uint8_t* get_utf8_value_cb(const uint8_t *pstart, read_byte_cb_t cb_read_byte, lchar_t &pval) {
  90. uint32_t val = 0;
  91. const uint8_t *p = pstart;
  92. #define NEXT_6_BITS() do{ val <<= 6; p++; valcur = cb_read_byte(p); val |= (valcur & 0x3F); }while(0)
  93. uint8_t valcur = cb_read_byte(p);
  94. if (0 == (0x80 & valcur)) {
  95. val = valcur;
  96. p++;
  97. }
  98. else if (0xC0 == (0xE0 & valcur)) {
  99. val = valcur & 0x1F;
  100. NEXT_6_BITS();
  101. p++;
  102. }
  103. #if MAX_UTF8_CHAR_SIZE >= 3
  104. else if (0xE0 == (0xF0 & valcur)) {
  105. val = valcur & 0x0F;
  106. NEXT_6_BITS();
  107. NEXT_6_BITS();
  108. p++;
  109. }
  110. #endif
  111. #if MAX_UTF8_CHAR_SIZE >= 4
  112. else if (0xF0 == (0xF8 & valcur)) {
  113. val = valcur & 0x07;
  114. NEXT_6_BITS();
  115. NEXT_6_BITS();
  116. NEXT_6_BITS();
  117. p++;
  118. }
  119. #endif
  120. #if MAX_UTF8_CHAR_SIZE >= 5
  121. else if (0xF8 == (0xFC & valcur)) {
  122. val = valcur & 0x03;
  123. NEXT_6_BITS();
  124. NEXT_6_BITS();
  125. NEXT_6_BITS();
  126. NEXT_6_BITS();
  127. p++;
  128. }
  129. #endif
  130. #if MAX_UTF8_CHAR_SIZE >= 6
  131. else if (0xFC == (0xFE & valcur)) {
  132. val = valcur & 0x01;
  133. NEXT_6_BITS();
  134. NEXT_6_BITS();
  135. NEXT_6_BITS();
  136. NEXT_6_BITS();
  137. NEXT_6_BITS();
  138. p++;
  139. }
  140. #endif
  141. else if (!utf8_is_start_byte_of_char(valcur))
  142. for (; !utf8_is_start_byte_of_char(valcur); ) { p++; valcur = cb_read_byte(p); }
  143. else
  144. for (; 0xFC < (0xFE & valcur); ) { p++; valcur = cb_read_byte(p); }
  145. pval = val;
  146. return p;
  147. }
  148. static inline uint8_t utf8_strlen_cb(const char *pstart, read_byte_cb_t cb_read_byte) {
  149. uint8_t cnt = 0;
  150. uint8_t *p = (uint8_t *)pstart;
  151. if (p) for (;;) {
  152. const uint8_t b = cb_read_byte(p);
  153. if (!b) break;
  154. if (utf8_is_start_byte_of_char(b)) cnt++;
  155. p++;
  156. }
  157. return cnt;
  158. }
  159. uint8_t utf8_strlen(const char *pstart) {
  160. return utf8_strlen_cb(pstart, read_byte_ram);
  161. }
  162. uint8_t utf8_strlen_P(PGM_P pstart) {
  163. return utf8_strlen_cb(pstart, read_byte_rom);
  164. }
  165. 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) {
  166. uint8_t *p = (uint8_t *)pstart;
  167. uint8_t char_idx = 0;
  168. uint8_t byte_idx = 0;
  169. for (;;) {
  170. const uint8_t b = cb_read_byte(p + byte_idx);
  171. if (!b) return byte_idx; // Termination byte of string
  172. if (utf8_is_start_byte_of_char(b)) {
  173. char_idx++;
  174. if (char_idx == charnum + 1) return byte_idx;
  175. }
  176. byte_idx++;
  177. }
  178. }
  179. uint8_t utf8_byte_pos_by_char_num(const char *pstart, const uint8_t charnum) {
  180. return utf8_byte_pos_by_char_num_cb(pstart, read_byte_ram, charnum);
  181. }
  182. uint8_t utf8_byte_pos_by_char_num_P(PGM_P pstart, const uint8_t charnum) {
  183. return utf8_byte_pos_by_char_num_cb(pstart, read_byte_rom, charnum);
  184. }