From 340ee8442d4bcc55b65ab4acdb243be9f73d8241 Mon Sep 17 00:00:00 2001 From: vrld Date: Thu, 22 Mar 2012 13:11:35 +0100 Subject: [PATCH] Fix issue #397: printf does not center string correctly. --- src/modules/graphics/opengl/Font.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/modules/graphics/opengl/Font.cpp b/src/modules/graphics/opengl/Font.cpp index 9fc6186ba..aa900d08b 100644 --- a/src/modules/graphics/opengl/Font.cpp +++ b/src/modules/graphics/opengl/Font.cpp @@ -261,6 +261,7 @@ namespace opengl //split text at newlines istringstream iss( text ); string line; + ostringstream string_builder; while (getline(iss, line, '\n')) { // split line into words @@ -272,19 +273,22 @@ namespace opengl // put words back together until a wrap occurs float width = 0.0f; float oldwidth = 0.0f; - ostringstream string_builder; - vector::const_iterator word_iter; - for (word_iter = words.begin(); word_iter != words.end(); ++word_iter) + string_builder.str(""); + vector::const_iterator word_iter, wend = words.end(); + for (word_iter = words.begin(); word_iter != wend; ++word_iter) { - string word( *word_iter ); + const string& word = *word_iter; width += getWidth( word ); // on wordwrap, push line to line buffer and clear string builder if (width >= wrap && oldwidth > 0) { int realw = (int) width; - lines_to_draw.push_back( string_builder.str() ); - string_builder.str( "" ); + + // remove trailing space + string tmp = string_builder.str(); + lines_to_draw.push_back( tmp.substr(0,tmp.size()-1) ); + string_builder.str(""); width = static_cast(getWidth( word )); realw -= (int) width; if (realw > maxw) @@ -297,7 +301,8 @@ namespace opengl // push last line if (width > maxw) maxw = (int) width; - lines_to_draw.push_back( string_builder.str() ); + string tmp = string_builder.str(); + lines_to_draw.push_back( tmp.substr(0,tmp.size()-1) ); } if (max_width)