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.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #ifdef 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. for(char* c = str; *c; c++) {
  35. lineWidth += fm.get_char_width(*c);
  36. if(lineWidth + ellipsisWidth < w)
  37. breakPoint = c;
  38. }
  39. if(lineWidth > w) {
  40. *breakPoint = '\0';
  41. strcpy_P(breakPoint,PSTR("..."));
  42. }
  43. cmd.apply_text_alignment(x, y, w, h, options);
  44. #ifdef TOUCH_UI_USE_UTF8
  45. if (has_utf8_chars(str)) {
  46. draw_utf8_text(cmd, x, y, str, font_size_t::from_romfont(font), options);
  47. } else
  48. #endif
  49. {
  50. cmd.CLCD::CommandFifo::text(x, y, font, options);
  51. cmd.CLCD::CommandFifo::str(str);
  52. }
  53. }
  54. /**
  55. * These functions draws text inside a bounding box, truncating the text and
  56. * adding ellipsis if the text does not fit.
  57. */
  58. void draw_text_with_ellipsis(CommandProcessor& cmd, int x, int y, int w, int h, const char *str, uint16_t options, uint8_t font) {
  59. char tmp[strlen(str) + 3];
  60. strcpy(tmp, str);
  61. _draw_text_with_ellipsis(cmd, x, y, w, h, tmp, options, font);
  62. }
  63. void draw_text_with_ellipsis(CommandProcessor& cmd, int x, int y, int w, int h, progmem_str pstr, uint16_t options, uint8_t font) {
  64. char tmp[strlen_P((const char*)pstr) + 3];
  65. strcpy_P(tmp, (const char*)pstr);
  66. _draw_text_with_ellipsis(cmd, x, y, w, h, tmp, options, font);
  67. }
  68. } // namespace FTDI
  69. #endif // FTDI_EXTENDED