Browse Source

Fix issue #397: printf does not center string correctly.

vrld 13 years ago
parent
commit
340ee8442d
1 changed files with 12 additions and 7 deletions
  1. 12 7
      src/modules/graphics/opengl/Font.cpp

+ 12 - 7
src/modules/graphics/opengl/Font.cpp

@@ -261,6 +261,7 @@ namespace opengl
 		//split text at newlines
 		//split text at newlines
 		istringstream iss( text );
 		istringstream iss( text );
 		string line;
 		string line;
+		ostringstream string_builder;
 		while (getline(iss, line, '\n'))
 		while (getline(iss, line, '\n'))
 		{
 		{
 			// split line into words
 			// split line into words
@@ -272,19 +273,22 @@ namespace opengl
 			// put words back together until a wrap occurs
 			// put words back together until a wrap occurs
 			float width = 0.0f;
 			float width = 0.0f;
 			float oldwidth = 0.0f;
 			float oldwidth = 0.0f;
-			ostringstream string_builder;
-			vector<string>::const_iterator word_iter;
-			for (word_iter = words.begin(); word_iter != words.end(); ++word_iter)
+			string_builder.str("");
+			vector<string>::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 );
 				width += getWidth( word );
 
 
 				// on wordwrap, push line to line buffer and clear string builder
 				// on wordwrap, push line to line buffer and clear string builder
 				if (width >= wrap && oldwidth > 0)
 				if (width >= wrap && oldwidth > 0)
 				{
 				{
 					int realw = (int) width;
 					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<float>(getWidth( word ));
 					width = static_cast<float>(getWidth( word ));
 					realw -= (int) width;
 					realw -= (int) width;
 					if (realw > maxw)
 					if (realw > maxw)
@@ -297,7 +301,8 @@ namespace opengl
 			// push last line
 			// push last line
 			if (width > maxw)
 			if (width > maxw)
 				maxw = (int) width;
 				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)
 		if (max_width)