Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML Text bounding box & descenders  (Read 2449 times)

0 Members and 1 Guest are viewing this topic.

jukka

  • Newbie
  • *
  • Posts: 4
    • View Profile
SFML Text bounding box & descenders
« on: November 06, 2015, 06:38:46 pm »
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.
« Last Edit: November 06, 2015, 06:48:17 pm by jukka »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: SFML Text bounding box & descenders
« Reply #1 on: November 06, 2015, 08:41:11 pm »
The bounding box is supposed to be ok, this looks like a bug. But first, please check with the latest version, SFML 2.1 is quite old.
Laurent Gomila - SFML developer

jukka

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: SFML Text bounding box & descenders
« Reply #2 on: November 06, 2015, 09:11:16 pm »
Huh, just upgraded to 2.3.2 and it seems to have been fixed. My bad, thanks!

(thread can be deleted if needed)