2
0
Alex Szpakowski 12 жил өмнө
parent
commit
91e4feeca0

+ 1 - 1
src/common/StringMap.h

@@ -70,7 +70,7 @@ public:
 
 
 		for (unsigned i = 0; i < MAX; ++i)
 		for (unsigned i = 0; i < MAX; ++i)
 		{
 		{
-			unsigned str_i = (str_hash + i) % MAX; //this isn't used, is this intentional?
+			unsigned str_i = (str_hash + i) % MAX;
 
 
 			if (!records[str_i].set)
 			if (!records[str_i].set)
 				return false;
 				return false;

+ 15 - 3
src/modules/graphics/opengl/Font.cpp

@@ -258,7 +258,7 @@ float Font::getHeight() const
 	return static_cast<float>(height);
 	return static_cast<float>(height);
 }
 }
 
 
-void Font::print(const std::string &text, float x, float y, float letter_spacing, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
+void Font::print(const std::string &text, float x, float y, float extra_spacing, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
 {
 {
 	// Spacing counter and newline handling.
 	// Spacing counter and newline handling.
 	float dx = 0.0f;
 	float dx = 0.0f;
@@ -320,7 +320,11 @@ void Font::print(const std::string &text, float x, float y, float letter_spacing
 			}
 			}
 
 
 			// Advance the x position for the next glyph.
 			// Advance the x position for the next glyph.
-			dx += glyph->spacing + letter_spacing;
+			dx += glyph->spacing;
+
+			// Account for extra spacing given to space characters.
+			if (g == ' ' && extra_spacing != 0.0f)
+				dx = floorf(dx + extra_spacing);
 		}
 		}
 	}
 	}
 	catch (utf8::exception &e)
 	catch (utf8::exception &e)
@@ -403,7 +407,7 @@ int Font::getWidth(char character)
 	return g->spacing;
 	return g->spacing;
 }
 }
 
 
-std::vector<std::string> Font::getWrap(const std::string &text, float wrap, int *max_width)
+std::vector<std::string> Font::getWrap(const std::string &text, float wrap, int *max_width, std::vector<bool> *wrappedlines)
 {
 {
 	using namespace std;
 	using namespace std;
 	const float width_space = static_cast<float>(getWidth(' '));
 	const float width_space = static_cast<float>(getWidth(' '));
@@ -445,6 +449,10 @@ std::vector<std::string> Font::getWrap(const std::string &text, float wrap, int
 				realw -= (int) width;
 				realw -= (int) width;
 				if (realw > maxw)
 				if (realw > maxw)
 					maxw = realw;
 					maxw = realw;
+
+				// Indicate that this line was automatically wrapped.
+				if (wrappedlines)
+					wrappedlines->push_back(true);
 			}
 			}
 			string_builder << word << " ";
 			string_builder << word << " ";
 			width += width_space;
 			width += width_space;
@@ -455,6 +463,10 @@ std::vector<std::string> Font::getWrap(const std::string &text, float wrap, int
 			maxw = (int) width;
 			maxw = (int) width;
 		string tmp = string_builder.str();
 		string tmp = string_builder.str();
 		lines_to_draw.push_back(tmp.substr(0,tmp.size()-1));
 		lines_to_draw.push_back(tmp.substr(0,tmp.size()-1));
+
+		// Indicate that this line was not automatically wrapped.
+		if (wrappedlines)
+			wrappedlines->push_back(false);
 	}
 	}
 
 
 	if (max_width)
 	if (max_width)

+ 5 - 3
src/modules/graphics/opengl/Font.h

@@ -59,7 +59,7 @@ public:
 	 * @param text A string.
 	 * @param text A string.
 	 * @param x The x-coordinate.
 	 * @param x The x-coordinate.
 	 * @param y The y-coordinate.
 	 * @param y The y-coordinate.
-	 * @param letter_spacing Additional spacing between letters.
+	 * @param extra_spacing Additional spacing added to spaces (" ").
 	 * @param angle The amount of rotation.
 	 * @param angle The amount of rotation.
 	 * @param sx Scale along the x axis.
 	 * @param sx Scale along the x axis.
 	 * @param sy Scale along the y axis.
 	 * @param sy Scale along the y axis.
@@ -68,7 +68,7 @@ public:
 	 * @param kx Shear along the x axis.
 	 * @param kx Shear along the x axis.
 	 * @param ky Shear along the y axis.
 	 * @param ky Shear along the y axis.
 	 **/
 	 **/
-	void print(const std::string &text, float x, float y, float letter_spacing = 0.0f, float angle = 0.0f, float sx = 1.0f, float sy = 1.0f, float ox = 0.0f, float oy = 0.0f, float kx = 0.0f, float ky = 0.0f);
+	void print(const std::string &text, float x, float y, float extra_spacing = 0.0f, float angle = 0.0f, float sx = 1.0f, float sy = 1.0f, float ox = 0.0f, float oy = 0.0f, float kx = 0.0f, float ky = 0.0f);
 
 
 	/**
 	/**
 	 * Returns the height of the font.
 	 * Returns the height of the font.
@@ -96,9 +96,11 @@ public:
 	 * @param text The input text
 	 * @param text The input text
 	 * @param wrap The number of pixels to wrap at
 	 * @param wrap The number of pixels to wrap at
 	 * @param max_width Optional output of the maximum width
 	 * @param max_width Optional output of the maximum width
+	 * @param wrapped_lines Optional output indicating which lines were
+	 *        auto-wrapped. Indices correspond to indices of the returned value.
 	 * Returns a vector with the lines.
 	 * Returns a vector with the lines.
 	 **/
 	 **/
-	std::vector<std::string> getWrap(const std::string &text, float wrap, int *max_width = 0);
+	std::vector<std::string> getWrap(const std::string &text, float wrap, int *max_width = 0, std::vector<bool> *wrapped_lines = 0);
 
 
 	/**
 	/**
 	 * Sets the line height (which should be a number to multiply the font size by,
 	 * Sets the line height (which should be a number to multiply the font size by,

+ 14 - 6
src/modules/graphics/opengl/Graphics.cpp

@@ -791,7 +791,10 @@ void Graphics::printf(const std::string &str, float x, float y, float wrap, Alig
 	using std::string;
 	using std::string;
 	using std::vector;
 	using std::vector;
 
 
-	vector<string> lines_to_draw = currentFont->getWrap(str, wrap);
+	// wrappedlines indicates which lines were automatically wrapped. It's
+	// guaranteed to have the same number of elements as lines_to_draw.
+	vector<bool> wrappedlines;
+	vector<string> lines_to_draw = currentFont->getWrap(str, wrap, 0, &wrappedlines);
 
 
 	glPushMatrix();
 	glPushMatrix();
 
 
@@ -805,7 +808,10 @@ void Graphics::printf(const std::string &str, float x, float y, float wrap, Alig
 	{
 	{
 		// now for the actual printing
 		// now for the actual printing
 		vector<string>::const_iterator line_iter, line_end = lines_to_draw.end();
 		vector<string>::const_iterator line_iter, line_end = lines_to_draw.end();
-		float letter_spacing = 0.0f;
+		float extra_spacing = 0.0f;
+		int num_spaces = 0;
+		int i = 0;
+
 		for (line_iter = lines_to_draw.begin(); line_iter != line_end; ++line_iter)
 		for (line_iter = lines_to_draw.begin(); line_iter != line_end; ++line_iter)
 		{
 		{
 			float width = static_cast<float>(currentFont->getWidth(*line_iter));
 			float width = static_cast<float>(currentFont->getWidth(*line_iter));
@@ -818,11 +824,12 @@ void Graphics::printf(const std::string &str, float x, float y, float wrap, Alig
 				currentFont->print(*line_iter, ceil(x + (wrap - width) / 2), ceil(y), 0.0f);
 				currentFont->print(*line_iter, ceil(x + (wrap - width) / 2), ceil(y), 0.0f);
 				break;
 				break;
 			case ALIGN_JUSTIFY:
 			case ALIGN_JUSTIFY:
-				if (line_iter->length() > 1)
-					letter_spacing = (wrap - width) / float(line_iter->length() - 1);
+				num_spaces = std::count(line_iter->begin(), line_iter->end(), ' ');
+				if (wrappedlines[i] && num_spaces >= 1)
+					extra_spacing = (wrap - width) / float(num_spaces);
 				else
 				else
-					letter_spacing = 0.0f;
-				currentFont->print(*line_iter, ceil(x), ceil(y), letter_spacing);
+					extra_spacing = 0.0f;
+				currentFont->print(*line_iter, ceil(x), ceil(y), extra_spacing);
 				break;
 				break;
 			case ALIGN_LEFT:
 			case ALIGN_LEFT:
 			default:
 			default:
@@ -830,6 +837,7 @@ void Graphics::printf(const std::string &str, float x, float y, float wrap, Alig
 				break;
 				break;
 			}
 			}
 			y += currentFont->getHeight() * currentFont->getLineHeight();
 			y += currentFont->getHeight() * currentFont->getLineHeight();
+			i++;
 		}
 		}
 	}
 	}
 	catch (love::Exception &)
 	catch (love::Exception &)