Fixed bug in sf::Text bounds calculation (was introduced in SFML 2.1)

This commit is contained in:
Laurent Gomila 2013-08-17 19:26:46 +02:00
parent c6767d0af1
commit 5f4257187e

View File

@ -179,10 +179,10 @@ Vector2f Text::findCharacterPos(std::size_t index) const
// Handle special characters // Handle special characters
switch (curChar) switch (curChar)
{ {
case L' ' : position.x += hspace; continue; case ' ' : position.x += hspace; continue;
case L'\t' : position.x += hspace * 4; continue; case '\t' : position.x += hspace * 4; continue;
case L'\n' : position.y += vspace; position.x = 0; continue; case '\n' : position.y += vspace; position.x = 0; continue;
case L'\v' : position.y += vspace * 4; continue; case '\v' : position.y += vspace * 4; continue;
} }
// For regular characters, add the advance offset of the glyph // For regular characters, add the advance offset of the glyph
@ -251,7 +251,7 @@ void Text::updateGeometry()
float y = static_cast<float>(m_characterSize); float y = static_cast<float>(m_characterSize);
// Create one quad for each character // Create one quad for each character
float minY = static_cast<float>(m_characterSize); float minX = m_characterSize, minY = m_characterSize, maxX = 0, maxY = 0;
Uint32 prevChar = 0; Uint32 prevChar = 0;
for (std::size_t i = 0; i < m_string.getSize(); ++i) for (std::size_t i = 0; i < m_string.getSize(); ++i)
{ {
@ -274,26 +274,26 @@ void Text::updateGeometry()
} }
// Handle special characters // Handle special characters
switch (curChar) if ((curChar == ' ') || (curChar == '\t') || (curChar == '\n') || (curChar == '\v'))
{ {
case L' ' : // Update the current bounds (min coordinates)
x += hspace; minX = std::min(minX, x);
continue; minY = std::min(minY, y);
case L'\t' : switch (curChar)
x += hspace * 4; {
continue; case ' ' : x += hspace; break;
case '\t' : x += hspace * 4; break;
case '\n' : y += vspace; x = 0; break;
case '\v' : y += vspace * 4; break;
}
case L'\n' : // Update the current bounds (max coordinates)
if (x > m_bounds.width) maxX = std::max(maxX, x);
m_bounds.width = x; maxY = std::max(maxY, y);
y += vspace;
x = 0;
continue;
case L'\v' : // Next glyph, no need to create a quad for whitespace
y += vspace * 4; continue;
continue;
} }
// Extract the current glyph's description // Extract the current glyph's description
@ -315,12 +315,14 @@ void Text::updateGeometry()
m_vertices.append(Vertex(Vector2f(x + right - italic * bottom, y + bottom), m_color, Vector2f(u2, v2))); m_vertices.append(Vertex(Vector2f(x + right - italic * bottom, y + bottom), m_color, Vector2f(u2, v2)));
m_vertices.append(Vertex(Vector2f(x + left - italic * bottom, y + bottom), m_color, Vector2f(u1, v2))); m_vertices.append(Vertex(Vector2f(x + left - italic * bottom, y + bottom), m_color, Vector2f(u1, v2)));
// Update the current bounds
minX = std::min(minX, x + left - italic * bottom);
maxX = std::max(maxX, x + right - italic * top);
minY = std::min(minY, y + top);
maxY = std::max(maxY, y + bottom);
// Advance to the next character // Advance to the next character
x += glyph.advance; x += glyph.advance;
// Update the minimum Y coordinate
if (y + top < minY)
minY = y + top;
} }
// If we're using the underlined style, add the last line // If we're using the underlined style, add the last line
@ -336,11 +338,10 @@ void Text::updateGeometry()
} }
// Update the bounding rectangle // Update the bounding rectangle
m_bounds.left = 0; m_bounds.left = minX;
m_bounds.top = minY; m_bounds.top = minY;
if (x > m_bounds.width) m_bounds.width = maxX - minX;
m_bounds.width = x; m_bounds.height = maxY - minY;
m_bounds.height = y - minY;
} }
} // namespace sf } // namespace sf