I am trying to get images of arbitrary text, and am using this thread (http://en.sfml-dev.org/forums/index.php?topic=10558.msg72838) 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?
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.