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

Author Topic: Text off-center, problems getting size  (Read 2737 times)

0 Members and 1 Guest are viewing this topic.

GarrickW

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Text off-center, problems getting size
« on: April 28, 2013, 08:33:21 pm »
I am trying to get images of arbitrary text, and am using this thread as a reference.  However, I am having some issues.  First, my implementation:

       
        sf::Font f;
        f.loadFromFile("courier.ttf");

        sf::Text text;
        text.setString("Test!");
        text.setFont(f);
        text.setCharacterSize(24);
        text.setColor(sf::Color::Black);

        sf::RenderTexture target;
        target.create(text.getLocalBounds().width,
                      text.getLocalBounds().height);
        target.clear(sf::Color::White);
        target.draw(text);
        target.display();

        sf::Image image = target.getTexture().copyToImage();
        image.saveToFile("text_test.png");

The problem is that if I just do this, what I get is an image wherein the top half is just white, and the bottom half of the image contains the top half of the text (with the bottom half of the text clipped out).

Adding
text.setPosition(0, -text.getLocalBounds().height / 2);

seems to mostly correct the issue... at 24 pts.  But if I change the character size to other values further from 24, such as 12 or 64, an upper or lower sliver of the text is again clipped out of view, respectively.

Am I misunderstanding something about how sf::Text objects are sized, positioned or displayed?  How can I neatly get an image that encompasses exactly all of the displayed text, and no more?

gostron

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Text off-center, problems getting size
« Reply #1 on: April 29, 2013, 10:43:39 am »
I think that you might want to try with getGlobalBounds(). I am not sure about this, but it might not hurt to try it ^^

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text off-center, problems getting size
« Reply #2 on: April 29, 2013, 10:55:15 am »
It seems like you didn't read the last message of your reference thread.

And also this:
https://github.com/SFML/SFML/issues/216
Laurent Gomila - SFML developer

GarrickW

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Re: Text off-center, problems getting size
« Reply #3 on: April 29, 2013, 06:55:23 pm »
If I try this (which is what I assume the last message meant):

target.create(text.getGlobalBounds().left + text.getGlobalBounds().width,
              text.getGlobalBounds().top + text.getGlobalBounds().height);

then the entire text does always appear, but a large blank space also always appears above the text, about half as high as the text itself.  If I move the text up by half its own height, again, going to smaller character sizes cuts out the top of the image, and going to larger sizes creates a slightly larger gap above the text than below.


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text off-center, problems getting size
« Reply #4 on: April 29, 2013, 08:58:33 pm »
The padding on top of the text is expected. The first line has the maximum height, it doesn't depend on the glyph's heights. This is so that the baseline stays at a constant distance from the top of the text entity.
Laurent Gomila - SFML developer

GarrickW

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Re: Text off-center, problems getting size
« Reply #5 on: April 29, 2013, 09:51:52 pm »
Ah, okay.  Good to know it's expected, at least, so I'm not doing anything wrong.  Thanks!