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?