ofTrueTypeFont – drawing text with a fixed width

i’ve just written a little extension of ofTrueTypeFont, that might be useful to some of you out there.

there are now overloaded versions of drawString() and stringHeight(), each of them taking an additional float value that specifies the with of a textblock as an argument.

drawString(string s, float x, float y, float w) will then automatically break your text to fit into the specified width. if a word is longer than an entire line, it will simply not break that line at all.

equally, stringHeight(string s, float w) will calculate the final width of the formatted text.

drawing blocks of text is notoriously slow in oF at the moment and the pre-formatting doesn’t really help. so i’d suggest to only use this when drawing text into an ofxFBOTexture for increased performance.

// setting up the fbo  
	ofxFBOTexture tex;  
	tex.allocate(500, myofTTF.stringHeight(theTextString, 500), false);  
	myofTTF.drawString(theTextString, 0, 0, 500);  
// drawing the texture  

the whole thing is pretty hacked right now, but it works just fine for me. if you have any suggestions on how to make it work more efficient, that would be great.

note that the code attached comes without any warranty whatsoever.


oops, just realized there’s still some debug-code in the attached file.
just delete line 972 in ofTrueTypeFont.cpp to get rid of the constant status-messages!