I'm not sure whether this is something I'm doing wrong or a bug in the library, but I'm getting boxes around some of the letters when I render text. Example:
I'm not entirely sure what code to post, since I have no idea why this is happening. I created a small program that just draws text to the screen and then waits to be closed, but wasn't able to reproduce this.
If it's relevant, I'm on a Mac using clang and libc++, for which I had to compile SFML myself. I haven't checked yet whether this occurs on Windows as well (that would require a reboot and rewriting a few functions, so it's unlikely I'll check soon).
Here's the function I'm using to draw text (the "Left" in the name refers to alignment), and its subroutines:
FloatRect drawTextLeft(short left, short top, string str, int size, Color clr, int style) {
Text text = makeText(str, size, clr, style);
FloatRect bounds = text.getLocalBounds();
drawText(left, top, text);
adjustForWhitespace(str, &bounds.left, &bounds.width, text.getFont(), size, style);
return bounds;
}
void adjustForWhitespace(string str, float* left, float* width, const Font& font, int size, int style) {
// Hack to force the text drawing functions to account for whitespace
// Only accounts for actual space, unfortunately; tabs not handled
int space = ({
Text withSpace("| |", font, size);
Text withoutSpace("||", font, size);
withSpace.setStyle(style);
withoutSpace.setStyle(style);
withSpace.getLocalBounds().width - withoutSpace.getLocalBounds().width;
});
size_t pre = str.find_first_not_of(' ');
if(pre == string::npos) pre = 0;
size_t post = str.size() - str.find_last_not_of(' ') - 1;
if(left) *left -= pre * space;
if(width) *width += (pre + post) * space;
}
Text makeText(string str, int size, Color clr, int style) {
Text text(str);
text.setCharacterSize(size);
text.setColor(clr);
text.setStyle(style);
return text;
}
void drawText(int left, int top, Text& text) {
text.setPosition(left, top);
app.draw(text);
}
When I commented out the call to adjustForWhitespace, there were less artifacts, but there were still some — less boxes, and sometimes just fragments of boxes. Also, I notice that it tends to happen to certain letters. Maybe that's a hint towards the cause of this?