Commits

Alex Szpakowski  committed 66aa2f8

Fixed justify AlignMode

  • Participants
  • Parent commits 67559c8

Comments (0)

Files changed (4)

File src/common/StringMap.h

 
 		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)
 				return false;

File src/modules/graphics/opengl/Font.cpp

 	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.
 	float dx = 0.0f;
 			}
 
 			// 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)
 	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;
 	const float width_space = static_cast<float>(getWidth(' '));
 				realw -= (int) width;
 				if (realw > maxw)
 					maxw = realw;
+
+				// Indicate that this line was automatically wrapped.
+				if (wrappedlines)
+					wrappedlines->push_back(true);
 			}
 			string_builder << word << " ";
 			width += width_space;
 			maxw = (int) width;
 		string tmp = string_builder.str();
 		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)

File src/modules/graphics/opengl/Font.h

 	 * @param text A string.
 	 * @param x The x-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 sx Scale along the x axis.
 	 * @param sy Scale along the y axis.
 	 * @param kx Shear along the x 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.
 	 * @param text The input text
 	 * @param wrap The number of pixels to wrap at
 	 * @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.
 	 **/
-	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,

File src/modules/graphics/opengl/Graphics.cpp

 	using std::string;
 	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();
 
 	{
 		// now for the actual printing
 		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)
 		{
 			float width = static_cast<float>(currentFont->getWidth(*line_iter));
 				currentFont->print(*line_iter, ceil(x + (wrap - width) / 2), ceil(y), 0.0f);
 				break;
 			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
-					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;
 			case ALIGN_LEFT:
 			default:
 				break;
 			}
 			y += currentFont->getHeight() * currentFont->getLineHeight();
+			i++;
 		}
 	}
 	catch (love::Exception &)