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.

text_ellipsis.cpp 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*********************
  2. * text_ellipsis.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 ENABLED(FTDI_EXTENDED)
  22. namespace FTDI {
  23. /**
  24. * Helper function for drawing text with ellipses. The str buffer may be modified and should have space for up to two extra characters.
  25. */
  26. static void _draw_text_with_ellipsis(CommandProcessor& cmd, int16_t x, int16_t y, int16_t w, int16_t h, char *str, uint16_t options, uint8_t font) {
  27. FontMetrics fm(font);
  28. const int16_t ellipsisWidth = fm.get_char_width('.') * 3;
  29. // Compute the total line length, as well as
  30. // the location in the string where it can
  31. // split and still allow the ellipsis to fit.
  32. int16_t lineWidth = 0;
  33. char *breakPoint = str;
  34. #ifdef TOUCH_UI_USE_UTF8
  35. char *tstr = str;
  36. while (*tstr) {
  37. breakPoint = tstr;
  38. const utf8_char_t c = get_utf8_char_and_inc(tstr);
  39. lineWidth += fm.get_char_width(c);
  40. if (lineWidth + ellipsisWidth < w)
  41. break;
  42. }
  43. #else
  44. for (char *c = str; *c; c++) {
  45. lineWidth += fm.get_char_width(*c);
  46. if (lineWidth + ellipsisWidth < w)
  47. breakPoint = c;
  48. }
  49. #endif
  50. if (lineWidth > w) {
  51. *breakPoint = '\0';
  52. strcpy_P(breakPoint,PSTR("..."));
  53. }
  54. cmd.apply_text_alignment(x, y, w, h, options);
  55. #if ENABLED(TOUCH_UI_USE_UTF8)
  56. if (has_utf8_chars(str)) {
  57. draw_utf8_text(cmd, x, y, str, font_size_t::from_romfont(font), options);
  58. } else
  59. #endif
  60. {
  61. cmd.CLCD::CommandFifo::text(x, y, font, options);
  62. cmd.CLCD::CommandFifo::str(str);
  63. }
  64. }
  65. /**
  66. * These functions draws text inside a bounding box, truncating the text and
  67. * adding ellipsis if the text does not fit.
  68. */
  69. void draw_text_with_ellipsis(CommandProcessor& cmd, int x, int y, int w, int h, const char *str, uint16_t options, uint8_t font) {
  70. char tmp[strlen(str) + 3];
  71. strcpy(tmp, str);
  72. _draw_text_with_ellipsis(cmd, x, y, w, h, tmp, options, font);
  73. }
  74. void draw_text_with_ellipsis(CommandProcessor& cmd, int x, int y, int w, int h, progmem_str pstr, uint16_t options, uint8_t font) {
  75. char tmp[strlen_P((const char*)pstr) + 3];
  76. strcpy_P(tmp, (const char*)pstr);
  77. _draw_text_with_ellipsis(cmd, x, y, w, h, tmp, options, font);
  78. }
  79. } // namespace FTDI
  80. #endif // FTDI_EXTENDED