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

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