From 172182a4e8ef11f8361ec166f2c9a016d39600de Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Tue, 9 Mar 2021 17:19:26 +0100 Subject: [PATCH] Fixed an issue where long words would sometimes render partly outside the designated text area. --- es-core/src/resources/Font.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/es-core/src/resources/Font.cpp b/es-core/src/resources/Font.cpp index e4a63b60a..108ae975f 100644 --- a/es-core/src/resources/Font.cpp +++ b/es-core/src/resources/Font.cpp @@ -447,10 +447,12 @@ std::string Font::wrapText(std::string text, float xLen) std::string out; std::string line; std::string word; + std::string abbreviatedWord; std::string temp; size_t space; Vector2f textSize; + float dotsSize = sizeText("...").x(); // While there's text or we still have text to render. while (text.length() > 0) { @@ -472,7 +474,20 @@ std::string Font::wrapText(std::string text, float xLen) } else { // The next word won't fit, so break here. - out += line + '\n'; + + // If the word is too long to fit within xLen, then abbreviate it. + if (xLen > 0 && sizeText(word).x() > xLen) { + float length = xLen - dotsSize; + if (length < 0) + length = 0; + abbreviatedWord = getTextMaxWidth(word, length); + abbreviatedWord += "..."; + word = abbreviatedWord; + out += line; + } + else { + out += line + '\n'; + } line = word; } }