From 6d020578645ad00568a7a92688dbed4822a0c2d8 Mon Sep 17 00:00:00 2001 From: vrld Date: Sun, 8 Jul 2012 13:37:09 +0200 Subject: [PATCH] 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. --- src/modules/graphics/Graphics.cpp | 1 + src/modules/graphics/Graphics.h | 1 + src/modules/graphics/opengl/Font.cpp | 6 +++--- src/modules/graphics/opengl/Font.h | 3 ++- src/modules/graphics/opengl/Graphics.cpp | 10 +++++++++- 5 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/modules/graphics/Graphics.cpp b/src/modules/graphics/Graphics.cpp index c55bb5d6b..5f4205150 100644 --- a/src/modules/graphics/Graphics.cpp +++ b/src/modules/graphics/Graphics.cpp @@ -112,6 +112,7 @@ StringMap::Entry Graphics::alignM { "left", Graphics::ALIGN_LEFT }, { "right", Graphics::ALIGN_RIGHT }, { "center", Graphics::ALIGN_CENTER }, + { "justify", Graphics::ALIGN_JUSTIFY }, }; StringMap Graphics::alignModes(Graphics::alignModeEntries, sizeof(Graphics::alignModeEntries)); diff --git a/src/modules/graphics/Graphics.h b/src/modules/graphics/Graphics.h index 1325d94ce..13d2cc660 100644 --- a/src/modules/graphics/Graphics.h +++ b/src/modules/graphics/Graphics.h @@ -46,6 +46,7 @@ public: ALIGN_LEFT = 1, ALIGN_CENTER, ALIGN_RIGHT, + ALIGN_JUSTIFY, ALIGN_MAX_ENUM }; diff --git a/src/modules/graphics/opengl/Font.cpp b/src/modules/graphics/opengl/Font.cpp index 60c21e8fa..40911ccf3 100644 --- a/src/modules/graphics/opengl/Font.cpp +++ b/src/modules/graphics/opengl/Font.cpp @@ -177,7 +177,7 @@ float Font::getHeight() const return static_cast(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(glyph->spacing), 0, 0); - dx += glyph->spacing; + glTranslatef(static_cast(glyph->spacing + letter_spacing), 0, 0); + dx += glyph->spacing + letter_spacing; } } catch(utf8::exception &e) diff --git a/src/modules/graphics/opengl/Font.h b/src/modules/graphics/opengl/Font.h index d771cd084..0dde331b6 100644 --- a/src/modules/graphics/opengl/Font.h +++ b/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. diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index 1233c9986..0399e407b 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/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::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(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));