Fixed a text abbreviation issue.

This commit is contained in:
Leon Styhre 2022-09-24 16:45:18 +02:00
parent 789bd7f756
commit 0736192417

View file

@ -304,6 +304,7 @@ std::string Font::wrapText(std::string text, float maxLength, float maxHeight, f
float accumHeight {0.0f}; float accumHeight {0.0f};
const bool restrictHeight {maxHeight > 0.0f}; const bool restrictHeight {maxHeight > 0.0f};
bool skipLastLine {false}; bool skipLastLine {false};
float currLineLength {0.0f};
// While there's text or we still have text to render. // While there's text or we still have text to render.
while (text.length() > 0) { while (text.length() > 0) {
@ -330,11 +331,32 @@ std::string Font::wrapText(std::string text, float maxLength, float maxHeight, f
// If the word will fit on the line, add it to our line and continue. // If the word will fit on the line, add it to our line and continue.
if (textSize.x <= maxLength) { if (textSize.x <= maxLength) {
line = temp; line = temp;
currLineLength = textSize.x;
continue; continue;
} }
else { else {
// If the word is too long to fit within maxLength then abbreviate it. // If the word is too long to fit within maxLength then abbreviate it.
if (sizeText(word).x > maxLength) { float wordSize {sizeText(word).x};
if (restrictHeight && currLineLength != 0.0f && maxHeight < lineHeight &&
wordSize > maxLength - textSize.x) {
// Multi-word lines.
if (maxLength - currLineLength + dotsSize < wordSize) {
while (sizeText(line).x + dotsSize > maxLength)
line.pop_back();
}
else {
while (word != "" && wordSize + dotsSize > maxLength - currLineLength) {
word.pop_back();
wordSize = sizeText(word).x;
}
line = line + word;
}
line.append("...");
break;
}
if (wordSize > maxLength) {
if (line != "" && line.back() != '\n') { if (line != "" && line.back() != '\n') {
if (restrictHeight) { if (restrictHeight) {
@ -345,8 +367,7 @@ std::string Font::wrapText(std::string text, float maxLength, float maxHeight, f
line.append("\n"); line.append("\n");
} }
float lineLength {sizeText(word).x}; const float cutTarget {wordSize - maxLength + dotsSize};
float cutTarget {lineLength - maxLength + dotsSize};
float cutSize {0.0f}; float cutSize {0.0f};
while (word != "" && cutSize < cutTarget) { while (word != "" && cutSize < cutTarget) {