field.cpp 983 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "base/strings.hpp"
  2. #include "field.h"
  3. TextExtent GetTextExtent(ZKTextView* view, const std::string& str) {
  4. if (view == NULL) {
  5. return {0, 0};
  6. }
  7. int w;
  8. int h;
  9. view->getTextExtent(str.c_str(), w, h);
  10. return {w, h};
  11. }
  12. std::string EllipsisText(ZKTextView* view, const std::string& str) {
  13. if (view == NULL) {
  14. return "";
  15. }
  16. if (str.empty()) {
  17. return str;
  18. }
  19. const char* elipsis = "…";
  20. const int elipsis_width = GetTextExtent(view, elipsis).w;
  21. const int view_width = view->getPosition().mWidth;
  22. int w = GetTextExtent(view, str.c_str()).w;
  23. if (w <= (view->getPosition().mWidth)) {
  24. return str;
  25. }
  26. std::vector<std::string> words;
  27. base::split_word(str, &words);
  28. std::string cut_str;
  29. while (words.size() > 0) {
  30. words.erase(words.end() - 1);
  31. cut_str = base::join(words);
  32. w = GetTextExtent(view, cut_str).w;
  33. if ((w + elipsis_width) > view_width) {
  34. continue;
  35. }
  36. break;
  37. }
  38. return cut_str + elipsis;
  39. }