getStringBoundingBox and space characters


getStringBoundingBox and consequently stringWidth return funny values if there are spaces at the beginning or end of the text string being evaluated.

This seems to be a known bug (or limitation) as there is already a note about it in the ofTrueTypeFont code. From what I can gather it isn’t possible to evaluate the width of the " " character like the other characters.

Any ideas regarding how I might be able to get around this for now? Thanks!


hey, thanks for pointing out that bug.

a quick solution – maybe replace all the spaces ’ ’ with ‘p’, which is what we translate by when drawing the text.

  • z

Just to clarify, the funny values I mentioned above appear to be more precisely the following: when a string with a trailing " " is evaluated that space is ignored by getStringBoundingBox. When a string that begins with " " is rendered it appears shifted one full character to the left.

I went ahead and tried replacing the spaces with ‘p’'s in order to use the getStringBoundingBox function but that still seemed off. I tried rendering two texts side by side, one with spaces between each letter and one with ‘p’ replacing every space. It looks like the space added by the draw function is actually smaller to that of a rendered ‘p’.

Looking at the code in ofTrueTypeFont.cpp inside the drawString function I noticed that the " " character is modified by the width of p while other characters are normally modified by their setWidth. So setWidth of ‘p’ appears to be larger than the width of a ‘p’. If I change references to width with setWidth for the " " character in the drawString function my texts now align correctly and every space is the same size a a rendered p. Aesthetically these spaces appear a little too large, which may be the reason the other value was being used?

I tried doing the same with getStringBoundingBox but this did not change the error of the trailing " " not being added by getStringBoundingBox. At least now I can substitute those " " for “p” in order to get getStringBoundingBox.width and it will align with the rendered spaces.


Here’s my solution to this problem:

int TextInput::getTextWidth(string text){  
	int result = 0;  
		// we gotta do some funky stuff if it starts or ends with a space... stringWidth doesn't calculate properly otherwise  
		string tempChecker = text;  
		bool spaceEnder = false;  
		bool spaceStarter = false;  
		if(tempChecker != "" && == ' '){  
			tempChecker = '.' + tempChecker;  
			spaceStarter = true;  
		if(tempChecker != "" && == ' '){  
			tempChecker += '.';  
			spaceEnder = true;  
		result = this->font.stringWidth(tempChecker);  
			result -= (this->font.stringWidth(".") - 1);  
			result -= (this->font.stringWidth(".") - 1);  
		result = text.length() * 8.2;  
	return result;  

ofTrueTypeFont - spaceWidth and characterSpacing settings

In v0.10.1 spaces at the end of a string are ignored by getStringBoundingBox() and spaces at the beginning are not.