My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

fontutils.cpp 5.6KB

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