From d2928459a0d4f0ba2cbc9237c63235b5851a9dfc Mon Sep 17 00:00:00 2001 From: Titus Tscharntke Date: Wed, 3 Apr 2013 16:16:39 +0000 Subject: [PATCH] better line wrapping respecting \n too now --- source/shared_lib/sources/graphics/font.cpp | 22 +++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/source/shared_lib/sources/graphics/font.cpp b/source/shared_lib/sources/graphics/font.cpp index 7e4902a3..4f066b25 100644 --- a/source/shared_lib/sources/graphics/font.cpp +++ b/source/shared_lib/sources/graphics/font.cpp @@ -184,7 +184,7 @@ float FontMetrics::getHeight(const string &str) const { string FontMetrics::wordWrapText(string text, int maxWidth) { // Strip newlines from source - //replaceAll(text, "\n", " "); + replaceAll(text, "\n", " \n "); // Get all words (space separated text) vector words; @@ -195,13 +195,19 @@ string FontMetrics::wordWrapText(string text, int maxWidth) { for(unsigned int i = 0; i < words.size(); ++i) { string word = words[i]; - float wordWidth = this->getTextWidth(word); - if (lineWidth + wordWidth > maxWidth) { - wrappedText += "\n"; - lineWidth = 0; - } - lineWidth += wordWidth; - wrappedText += word + " "; + if(word=="\n"){ + wrappedText += word; + lineWidth = 0; + } + else { + float wordWidth = this->getTextWidth(word); + if (lineWidth + wordWidth > maxWidth) { + wrappedText += "\n"; + lineWidth = 0; + } + lineWidth += this->getTextWidth(word+" "); + wrappedText += word + " "; + } } return wrappedText;