Browse Source

Text: fixed alignment.

The original code assumed that the text bounds lower left corner is
always at origin. It is now not the case with multi-line text. All tests
now pass again.
pull/34/head
Vladimír Vondruš 13 years ago
parent
commit
4fea3efbd3
  1. 10
      src/Text/Renderer.cpp
  2. 6
      src/Text/Test/RendererGLTest.cpp

10
src/Text/Renderer.cpp

@ -123,12 +123,11 @@ std::tuple<std::vector<Vertex>, Rectangle> renderVerticesInternal(AbstractFont&
/** @todo What about top-down text? */
/* Horizontally align the rendered line */
const Float renderedWidth = lineRectangle.width();
Float alignmentOffsetX = 0.0f;
if((UnsignedByte(alignment) & Implementation::AlignmentHorizontal) == Implementation::AlignmentCenter)
alignmentOffsetX = -renderedWidth*0.5f;
alignmentOffsetX = -lineRectangle.left()-lineRectangle.width()*0.5f;
else if((UnsignedByte(alignment) & Implementation::AlignmentHorizontal) == Implementation::AlignmentRight)
alignmentOffsetX = -renderedWidth;
alignmentOffsetX = -lineRectangle.right();
/* Integer alignment */
if(UnsignedByte(alignment) & Implementation::AlignmentIntegral)
@ -152,12 +151,11 @@ std::tuple<std::vector<Vertex>, Rectangle> renderVerticesInternal(AbstractFont&
pos != std::string::npos);
/* Vertically align the rendered text */
const Float renderedHeight = rectangle.height();
Float alignmentOffsetY = 0.0f;
if((UnsignedByte(alignment) & Implementation::AlignmentVertical) == Implementation::AlignmentMiddle)
alignmentOffsetY = -renderedHeight*0.5f;
alignmentOffsetY = -rectangle.bottom()-rectangle.height()*0.5f;
else if((UnsignedByte(alignment) & Implementation::AlignmentVertical) == Implementation::AlignmentTop)
alignmentOffsetY = -renderedHeight;
alignmentOffsetY = -rectangle.top();
/* Integer alignment */
if(UnsignedByte(alignment) & Implementation::AlignmentIntegral)

6
src/Text/Test/RendererGLTest.cpp

@ -94,8 +94,8 @@ void RendererGLTest::renderData() {
CORRADE_COMPARE(textureCoordinates.size(), 12);
CORRADE_COMPARE(indices.size(), 18);
/* Alignment offset */
const Vector2 offset{-5.0f, -1.0f};
/* Alignment offset. Y would be -0.25f if it wasn't integral */
const Vector2 offset{-5.0f, 0.0f};
/* Bounds */
CORRADE_COMPARE(bounds, Rectangle({0.0f, -0.5f}, {5.0f, 1.0f}).translated(offset));
@ -173,7 +173,7 @@ void RendererGLTest::renderMesh() {
MAGNUM_VERIFY_NO_ERROR();
/* Alignment offset */
const Vector2 offset{-2.5f, -1.5f};
const Vector2 offset{-2.5f, -1.0f};
/* Bounds */
CORRADE_COMPARE(bounds, Rectangle({0.0f, -0.5f}, {5.0f, 1.0f}).translated(offset));

Loading…
Cancel
Save