Using SFML 2.1, I can visualise the global bounding box of a text object by doing:
// "text" is an sf::Text object, "rec" is an sf::RectangleShape
sf::FloatRect textBounds = text.getGlobalBounds();
rec.setPosition(textBounds.left, textBounds.top);
sf::Vector2f recSize(textBounds.width, textBounds.height);
rec.setSize(recSize);
Setting the font to arial, font size to 50, setting the rectangle to red and rendering with text "abc" gives:
which seems correct (though the box is one pixel too high and wide which is not a massive problem).
When using capital letters, the bounding box adapts fine:
though the C seems one pixel too low oddly - again not a big deal.
A problem appears when using a "descender" letter - ie. a letter which hangs below the bottom of the text line like g, p or q:
it can't seem to handle the bounding box correctly. If I change the size so that:
sf::Vector2f recSize(textBounds.width, text.getCharacterSize());
then it is handled better:
But I can't use this method all the time because if the text
doesn't have descenders, the bounding box is too big:
So, what's going on here? Am I using the wrong method to get an accurate bounding box for text? Or am I forced to use some ugly hack to check the text string for descenders and change the rectangle size accordingly? If that is the case it seems like SFML should handle that internally.