1234567891011121314151617181920212223242526272829303132333435363738394041 |
- #include "base/strings.hpp"
- #include "field.h"
- TextExtent GetTextExtent(ZKTextView* view, const std::string& str) {
- if (view == NULL) {
- return {0, 0};
- }
- int w;
- int h;
- view->getTextExtent(str.c_str(), w, h);
- return {w, h};
- }
- std::string EllipsisText(ZKTextView* view, const std::string& str) {
- if (view == NULL) {
- return "";
- }
- if (str.empty()) {
- return str;
- }
- const char* elipsis = "…";
- const int elipsis_width = GetTextExtent(view, elipsis).w;
- const int view_width = view->getPosition().mWidth;
- int w = GetTextExtent(view, str.c_str()).w;
- if (w <= (view->getPosition().mWidth)) {
- return str;
- }
- std::vector<std::string> words;
- base::split_word(str, &words);
- std::string cut_str;
- while (words.size() > 0) {
- words.erase(words.end() - 1);
- cut_str = base::join(words);
- w = GetTextExtent(view, cut_str).w;
- if ((w + elipsis_width) > view_width) {
- continue;
- }
- break;
- }
- return cut_str + elipsis;
- }
|