My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

unicode.cpp 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /***************
  2. * unicode.cpp *
  3. ***************/
  4. /****************************************************************************
  5. * Written By Marcio Teixeira 2019 - Aleph Objects, Inc. *
  6. * *
  7. * This program is free software: you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation, either version 3 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * To view a copy of the GNU General Public License, go to the following *
  18. * location: <https://www.gnu.org/licenses/>. *
  19. ****************************************************************************/
  20. #include "../ftdi_extended.h"
  21. #if BOTH(FTDI_EXTENDED, TOUCH_UI_USE_UTF8)
  22. using namespace FTDI;
  23. /**
  24. * Return true if a string has UTF8 characters
  25. *
  26. * Parameters:
  27. *
  28. * c - Pointer to a string.
  29. *
  30. * Returns: True if the strings has UTF8 characters
  31. */
  32. bool FTDI::has_utf8_chars(const char *str) {
  33. for (;;) {
  34. const char c = *str++;
  35. if (!c) break;
  36. if ((c & 0xC0) == 0x80) return true;
  37. }
  38. return false;
  39. }
  40. bool FTDI::has_utf8_chars(progmem_str _str) {
  41. const char *str = (const char *) _str;
  42. for (;;) {
  43. const char c = pgm_read_byte(str++);
  44. if (!c) break;
  45. if ((c & 0xC0) == 0x80) return true;
  46. }
  47. return false;
  48. }
  49. /**
  50. * Return a character in a UTF8 string and increment the
  51. * pointer to the next character
  52. *
  53. * Parameters:
  54. *
  55. * c - Pointer to a UTF8 encoded string.
  56. *
  57. * Returns: The packed bytes of a UTF8 encoding of a single
  58. * character (this is not the unicode codepoint)
  59. */
  60. utf8_char_t FTDI::get_utf8_char_and_inc(const char *&c) {
  61. utf8_char_t val = *(uint8_t*)c++;
  62. while ((*c & 0xC0) == 0x80)
  63. val = (val << 8) | *(uint8_t*)c++;
  64. return val;
  65. }
  66. /**
  67. * Helper function to draw and/or measure a UTF8 string
  68. *
  69. * Parameters:
  70. *
  71. * cmd - If non-NULL the symbol is drawn to the screen.
  72. * If NULL, only increment position for text measurement.
  73. *
  74. * x, y - The location at which to draw the string.
  75. *
  76. * str - The UTF8 string to draw or measure.
  77. *
  78. * fs - A scaling object used to specify the font size.
  79. */
  80. static uint16_t render_utf8_text(CommandProcessor* cmd, int x, int y, const char *str, font_size_t fs) {
  81. const int start_x = x;
  82. while (*str) {
  83. const utf8_char_t c = get_utf8_char_and_inc(str);
  84. #ifdef TOUCH_UI_UTF8_WESTERN_CHARSET
  85. WesternCharSet::render_glyph(cmd, x, y, fs, c) ||
  86. #endif
  87. StandardCharSet::render_glyph(cmd, x, y, fs, c);
  88. }
  89. return x - start_x;
  90. }
  91. /**
  92. * Load the font bitmap data into RAMG. Called once at program start.
  93. *
  94. * Parameters:
  95. *
  96. * addr - Address in RAMG where the font data is written
  97. */
  98. void FTDI::load_utf8_data(uint16_t addr) {
  99. #ifdef TOUCH_UI_UTF8_WESTERN_CHARSET
  100. WesternCharSet::load_data(addr);
  101. #endif
  102. StandardCharSet::load_data(addr);
  103. }
  104. /**
  105. * Populate the bitmap handles for the custom fonts into the display list.
  106. * Called once at the start of each display list.
  107. *
  108. * Parameters:
  109. *
  110. * cmd - Object used for writing to the FTDI chip command queue.
  111. */
  112. void FTDI::load_utf8_bitmaps(CommandProcessor &cmd) {
  113. #ifdef TOUCH_UI_UTF8_WESTERN_CHARSET
  114. WesternCharSet::load_bitmaps(cmd);
  115. #endif
  116. StandardCharSet::load_bitmaps(cmd);
  117. }
  118. /**
  119. * Measure a UTF8 text character
  120. *
  121. * Parameters:
  122. *
  123. * c - The unicode code point to measure.
  124. *
  125. * fs - A scaling object used to specify the font size.
  126. *
  127. * Returns: A width in pixels
  128. */
  129. uint16_t FTDI::get_utf8_char_width(utf8_char_t c, font_size_t fs) {
  130. int x = 0, y = 0;
  131. #ifdef TOUCH_UI_UTF8_WESTERN_CHARSET
  132. WesternCharSet::render_glyph(nullptr, x, y, fs, c) ||
  133. #endif
  134. StandardCharSet::render_glyph(nullptr, x, y, fs, c);
  135. return x;
  136. }
  137. /**
  138. * Measure a UTF8 text string
  139. *
  140. * Parameters:
  141. *
  142. * str - The UTF8 string to measure.
  143. *
  144. * fs - A scaling object used to specify the font size.
  145. *
  146. * Returns: A width in pixels
  147. */
  148. uint16_t FTDI::get_utf8_text_width(const char *str, font_size_t fs) {
  149. return render_utf8_text(nullptr, 0, 0, str, fs);
  150. }
  151. uint16_t FTDI::get_utf8_text_width(progmem_str pstr, font_size_t fs) {
  152. char str[strlen_P((const char*)pstr) + 1];
  153. strcpy_P(str, (const char*)pstr);
  154. return get_utf8_text_width(str, fs);
  155. }
  156. /**
  157. * Draw a UTF8 text string
  158. *
  159. * Parameters:
  160. *
  161. * cmd - Object used for writing to the FTDI chip command queue.
  162. *
  163. * x, y - The location at which to draw the string.
  164. *
  165. * str - The UTF8 string to draw.
  166. *
  167. * fs - A scaling object used to specify the font size.
  168. *
  169. * options - Text alignment options (i.e. OPT_CENTERX, OPT_CENTERY, OPT_CENTER or OPT_RIGHTX)
  170. *
  171. */
  172. void FTDI::draw_utf8_text(CommandProcessor& cmd, int x, int y, const char *str, font_size_t fs, uint16_t options) {
  173. cmd.cmd(SAVE_CONTEXT());
  174. cmd.cmd(BITMAP_TRANSFORM_A(fs.get_coefficient()));
  175. cmd.cmd(BITMAP_TRANSFORM_E(fs.get_coefficient()));
  176. cmd.cmd(BEGIN(BITMAPS));
  177. // Apply alignment options
  178. if (options & OPT_CENTERX)
  179. x -= get_utf8_text_width(str, fs) / 2;
  180. else if (options & OPT_RIGHTX)
  181. x -= get_utf8_text_width(str, fs);
  182. if (options & OPT_CENTERY)
  183. y -= fs.get_height()/2;
  184. // Render the text
  185. render_utf8_text(&cmd, x, y, str, fs);
  186. cmd.cmd(RESTORE_CONTEXT());
  187. }
  188. void FTDI::draw_utf8_text(CommandProcessor& cmd, int x, int y, progmem_str pstr, font_size_t fs, uint16_t options) {
  189. char str[strlen_P((const char*)pstr) + 1];
  190. strcpy_P(str, (const char*)pstr);
  191. draw_utf8_text(cmd, x, y, (const char*) str, fs, options);
  192. }
  193. #endif // FTDI_EXTENDED && TOUCH_UI_USE_UTF8