Browse Source

Fix width for getWrap and catch utf8 decoding errors in getWrap and getSize (issues #230 and #244)

Bart van Strien 14 years ago
parent
commit
6e630e8987
2 changed files with 37 additions and 13 deletions
  1. 18 9
      src/modules/graphics/opengl/Font.cpp
  2. 19 4
      src/modules/graphics/opengl/wrap_Font.cpp

+ 18 - 9
src/modules/graphics/opengl/Font.cpp

@@ -199,13 +199,20 @@ namespace opengl
 		
 		
 		Glyph * g;
 		Glyph * g;
 
 
-		utf8::iterator<std::string::const_iterator> i (line.begin(), line.begin(), line.end());
-		utf8::iterator<std::string::const_iterator> end (line.end(), line.begin(), line.end());
-		while (i != end) {
-			int c = *i++;
-			g = glyphs[c];
-			if (!g) g = addGlyph(c);
-			temp += static_cast<int>(g->spacing * mSpacing);
+		try
+		{
+			utf8::iterator<std::string::const_iterator> i (line.begin(), line.begin(), line.end());
+			utf8::iterator<std::string::const_iterator> end (line.end(), line.begin(), line.end());
+			while (i != end) {
+				int c = *i++;
+				g = glyphs[c];
+				if (!g) g = addGlyph(c);
+				temp += static_cast<int>(g->spacing * mSpacing);
+			}
+		}
+		catch (utf8::invalid_utf8 e)
+		{
+			throw love::Exception(e.what());
 		}
 		}
 
 
 		return temp;
 		return temp;
@@ -251,11 +258,13 @@ namespace opengl
 
 
 				// 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) {
-					if (width > maxw)
-						maxw = width;
+					int realw = width;
 					lines_to_draw.push_back( string_builder.str() );
 					lines_to_draw.push_back( string_builder.str() );
 					string_builder.str( "" );
 					string_builder.str( "" );
 					width = static_cast<float>(getWidth( word ));
 					width = static_cast<float>(getWidth( word ));
+					realw -= width;
+					if (realw > maxw)
+						maxw = realw;
 				}
 				}
 				string_builder << word << " ";
 				string_builder << word << " ";
 				width += width_space;
 				width += width_space;

+ 19 - 4
src/modules/graphics/opengl/wrap_Font.cpp

@@ -43,7 +43,14 @@ namespace opengl
 	{
 	{
 		Font * t = luax_checkfont(L, 1);
 		Font * t = luax_checkfont(L, 1);
 		const char * str = luaL_checkstring(L, 2);
 		const char * str = luaL_checkstring(L, 2);
-		lua_pushinteger(L, t->getWidth(str));
+		try
+		{
+			lua_pushinteger(L, t->getWidth(str));
+		}
+		catch (love::Exception & e)
+		{
+			return luaL_error(L, e.what());
+		}
 		return 1;
 		return 1;
 	}
 	}
 
 
@@ -52,10 +59,18 @@ namespace opengl
 		Font * t = luax_checkfont(L, 1);
 		Font * t = luax_checkfont(L, 1);
 		const char * str = luaL_checkstring(L, 2);
 		const char * str = luaL_checkstring(L, 2);
 		float wrap = (float) luaL_checknumber(L, 3);
 		float wrap = (float) luaL_checknumber(L, 3);
-		int max_width = 0;
-		std::vector<std::string> lines = t->getWrap(str, wrap, &max_width);
+		int max_width = 0, numlines = 0;
+		try
+		{
+			std::vector<std::string> lines = t->getWrap(str, wrap, &max_width);
+			numlines = lines.size();
+		}
+		catch (love::Exception & e)
+		{
+			return luaL_error(L, e.what());
+		}
 		lua_pushinteger(L, max_width);
 		lua_pushinteger(L, max_width);
-		lua_pushinteger(L, lines.size());
+		lua_pushinteger(L, numlines);
 		return 2;
 		return 2;
 	}
 	}