diff --git a/include/pd/lithium/font.hpp b/include/pd/lithium/font.hpp index efdbee8..4f7d34a 100755 --- a/include/pd/lithium/font.hpp +++ b/include/pd/lithium/font.hpp @@ -103,6 +103,8 @@ class PD_LITHIUM_API Font { PD::Li::Texture::Ref tex); std::string pWrapText(const std::string& txt, float scale, const PD::fvec2& max, PD::fvec2& dim); + std::string pShortText(const std::string& txt, float scale, + const PD::fvec2& max, PD::fvec2& dim); /** Data Section */ int PixelHeight; diff --git a/pd/lithium/source/font.cpp b/pd/lithium/source/font.cpp index fcba9e2..bb57c67 100755 --- a/pd/lithium/source/font.cpp +++ b/pd/lithium/source/font.cpp @@ -272,10 +272,10 @@ PD_LITHIUM_API void Font::CmdTextEx(std::vector &cmds, } for (auto &it : lines) { - /*if (flags & LITextFlags_Short) { + if (flags & LiTextFlags_Short) { fvec2 tmp_dim; - it = ShortText(it, box.x() - pos.x(), tmp_dim); - }*/ + it = pShortText(it, scale, box - pos, tmp_dim); + } auto wline = Strings::MakeWstring(it); auto cmd = Command::New(); auto Tex = GetCodepoint(wline[0]).Tex; @@ -344,5 +344,37 @@ PD_LITHIUM_API std::string Font::pWrapText(const std::string &txt, float scale, return ret; } +PD_LITHIUM_API std::string Font::pShortText(const std::string &txt, float scale, + const PD::fvec2 &max, + PD::fvec2 &dim) { + auto test = GetTextBounds(txt, scale); + if (test.x < max.x) { + return txt; + } + std::string ext; + std::string ph = "(...)"; // placeholder + std::string tmp = txt; + std::string ret; + auto maxlen = max.x; + size_t ext_ = tmp.find_last_of('.'); + if (ext_ != tmp.npos) { + ext = tmp.substr(ext_); + tmp = tmp.substr(0, ext_); + } + maxlen -= GetTextBounds(ext, scale).x; + maxlen -= GetTextBounds(ph, scale).x; + + for (auto &it : tmp) { + if (GetTextBounds(ret, scale).x > maxlen) { + ret += ph; + ret += ext; + dim = GetTextBounds(ret, scale); + return ret; + } + ret += it; + } + return ret; +} + } // namespace Li } // namespace PD \ No newline at end of file