Browse Source

Close issue #408: printf-justify alignment.

Add letter_spacing argument to Font::print(). Might be worth to expose to Lua
at some point. Justify alignment may result in mushy/blurred glyphs.
vrld 13 years ago
parent
commit
6d02057864

+ 1 - 0
src/modules/graphics/Graphics.cpp

@@ -112,6 +112,7 @@ StringMap<Graphics::AlignMode, Graphics::ALIGN_MAX_ENUM>::Entry Graphics::alignM
 	{ "left", Graphics::ALIGN_LEFT },
 	{ "right", Graphics::ALIGN_RIGHT },
 	{ "center", Graphics::ALIGN_CENTER },
+	{ "justify", Graphics::ALIGN_JUSTIFY },
 };
 
 StringMap<Graphics::AlignMode, Graphics::ALIGN_MAX_ENUM> Graphics::alignModes(Graphics::alignModeEntries, sizeof(Graphics::alignModeEntries));

+ 1 - 0
src/modules/graphics/Graphics.h

@@ -46,6 +46,7 @@ public:
 		ALIGN_LEFT = 1,
 		ALIGN_CENTER,
 		ALIGN_RIGHT,
+		ALIGN_JUSTIFY,
 		ALIGN_MAX_ENUM
 	};
 

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

@@ -177,7 +177,7 @@ float Font::getHeight() const
 	return static_cast<float>(height);
 }
 
-void Font::print(std::string text, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
+void Font::print(std::string text, float x, float y, float letter_spacing, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
 {
 	float dx = 0.0f; // spacing counter for newline handling
 	glPushMatrix();
@@ -207,8 +207,8 @@ void Font::print(std::string text, float x, float y, float angle, float sx, floa
 			bindTexture(glyph->texture);
 			glCallList(glyph->list);
 			glPopMatrix();
-			glTranslatef(static_cast<GLfloat>(glyph->spacing), 0, 0);
-			dx += glyph->spacing;
+			glTranslatef(static_cast<GLfloat>(glyph->spacing + letter_spacing), 0, 0);
+			dx += glyph->spacing + letter_spacing;
 		}
 	}
 	catch(utf8::exception &e)

+ 2 - 1
src/modules/graphics/opengl/Font.h

@@ -60,6 +60,7 @@ public:
 	 * @param text A string.
 	 * @param x The x-coordinate.
 	 * @param y The y-coordinate.
+	 * @param letter_spacing Additional spacing between letters.
 	 * @param angle The amount of rotation.
 	 * @param sx Scale along the x axis.
 	 * @param sy Scale along the y axis.
@@ -68,7 +69,7 @@ public:
 	 * @param kx Shear along the x axis.
 	 * @param ky Shear along the y axis.
 	 **/
-	void print(std::string text, float x, float y, 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(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);
 
 	/**
 	 * Prints the character at the designated position.

+ 9 - 1
src/modules/graphics/opengl/Graphics.cpp

@@ -686,7 +686,7 @@ void Graphics::print(const char *str, float x, float y , float angle, float sx,
 	if (currentFont != 0)
 	{
 		std::string text(str);
-		currentFont->print(text, x, y, angle, sx, sy, ox, oy, kx, ky);
+		currentFont->print(text, x, y, 0.0, angle, sx, sy, ox, oy, kx, ky);
 	}
 }
 
@@ -701,6 +701,7 @@ void Graphics::printf(const char *str, float x, float y, float wrap, AlignMode a
 
 	// now for the actual printing
 	vector<string>::const_iterator line_iter, line_end = lines_to_draw.end();
+	float letter_spacing = 0.0f;
 	for (line_iter = lines_to_draw.begin(); line_iter != line_end; ++line_iter)
 	{
 		float width = static_cast<float>(currentFont->getWidth(*line_iter));
@@ -712,6 +713,13 @@ void Graphics::printf(const char *str, float x, float y, float wrap, AlignMode a
 		case ALIGN_CENTER:
 			currentFont->print(*line_iter, ceil(x + (wrap - width) / 2), ceil(y));
 			break;
+		case ALIGN_JUSTIFY:
+			if (line_iter->length() > 1 && (line_iter+1) != line_end)
+				letter_spacing = (wrap - width) / float(line_iter->length() - 1);
+			else
+				letter_spacing = 0.0f;
+			currentFont->print(*line_iter, ceil(x), ceil(y), letter_spacing);
+			break;
 		case ALIGN_LEFT:
 		default:
 			currentFont->print(*line_iter, ceil(x), ceil(y));