How to get size of [ ofDrawBitmapString(...) ]?


One day. I need know bound size text by ofDrawBitmapString(…).
It can move text to correct center position on screen or something , if we founded text size.
Anybody answer please.



I was figure out myself.
(Width : 8pt , Height : 11pt ) each character.

And any comment is still able.

This is x3 expand Image.

It is real size.

did you try stringWidth() yet?

    string myString_0 = "Please stand out of all camera views for 20 seconds.";
    string myString_1 = "A reference image will be taken."; //\n"+ofToString(ofGetElapsedTimef(),1);
    int strWidth_0 = helvetica.stringWidth(myString_0);
    int strHeight_0 = helvetica.stringHeight(myString_0);
    int strWidth_1 = helvetica.stringWidth(myString_1);
    int strHeight_1 = helvetica.stringHeight(myString_1);
    int strWidth = MAX(strWidth_0,strWidth_1);
    int strHeight = MAX(strHeight_0,strHeight_1);
    strHeight = strHeight * 2;
    ofDrawRectangle(ofGetWidth()/2 - strWidth/2 - 10, ofGetHeight()/2 - strHeight - 10, strWidth + 20, strHeight + 20);
    helvetica.drawString(myString_0, ofGetWidth()/2 - strWidth_0/2, ofGetHeight()/2 - strHeight/2 - 5);
    helvetica.drawString(myString_1, ofGetWidth()/2 - strWidth_1/2, ofGetHeight()/2 - strHeight/2 - 5 + strHeight_1 + 2);!show_stringWidth

oh thanks.
But my problem isn’t ofTrueTypeFont.

My problem about just ofDrawBitmapString() in Draw().


additional >
ofDrawBitmapString is not using some font.

@stephanschulz that is for truetype fonts, what @jjongun is asking is for bitmap string.
@jjongun you’re right use the following function to get the boundingbox for a bitmap string.

ofRectangle getBitmapStringBoundingBox(string text){
vector<string> lines = ofSplitString(text, "\n");
    int maxLineLength = 0;
    for(int i = 0; i < (int)lines.size(); i++) {
        // tabs are not rendered
        const string & line(lines[i]);
        int currentLineLength = 0;
        for(int j = 0; j < (int)line.size(); j++) {
            if (line[j] == '\t') {
                currentLineLength += 8 - (currentLineLength % 8);
            } else {
        maxLineLength = MAX(maxLineLength, currentLineLength);
    int padding = 4;
    int fontSize = 8;
    float leading = 1.7;
    int height = lines.size() * fontSize * leading - 1;
    int width = maxLineLength * fontSize;
return ofRectangle(0,0,width, height);

then if you want to get the width you might type

getBitmapStringBoundingBox("Hello World!").getWidth()

hope it helps


good! It works clearly.

using code

string comment = "thanks roymacdonald!\nIt is exactly what I need.\nyo!\n-openframeworks- ";
	ofRectangle rect = getBitmapStringBoundingBox(comment);
	rect.x = 100;
	rect.y = 100;
	ofRect(rect.x, rect.y, rect.width, rect.height);
	ofDrawBitmapString(comment, rect.x, rect.y + 11); // '11' is a static character size of height.
1 Like

nice trick to learn. thanks @roymacdonald

i was just suggesting a work around. i know it was not exactly what you where looking for @jjongun

No prob guys. glad to help.
I’ve submitted an issue at github for addressing this correctly.


hi roy,

do i need to include a specific library to use getBitmapStringBoundingBox()?
i get a “use of undeclared identifier ‘getBitmapStringBoundingBox’” error:

ofRectangle infoBox = getBitmapStringBoundingBox(info);


That getBitmapStringBoundingBox(...)function was making reference to a snippet of code that I posted before in this thread. Although that is no longer needed as it is included in OF.

ofBitmapFont f;
ofRectangle r = f.getBoundingBox("some string");

You can keep an instance of ofBitmapFont somewhere or simply instantiate it as in the code above. This class resources are static so it shouldn’t be much overhead instantiated when needed.

thanks, roy.


Hi all

I tried to do this tonight (learning oF) and am getting an error message (see below).

    string comment = "thanks roymacdonald!\nIt is exactly what I need.\nyo!\n-openframeworks- ";
    ofBitmapFont f;
    ofRectangle rect = f.getBoundingBox(comment);
    rect.x = 100;
    rect.y = 100;
    ofRectangle(rect.x, rect.y, rect.width, rect.height);

f.getBoundingBox(comment) – “Too few arguments to function call, expected at least 3, have 1”

Thank you

hi @GucciCode
you need to pass the string and two numbers, which are the position where the text will be. If you dont mind about this and you just want the width and or height of the string just pass 0,0, so in your code above it should say in the 3d line

 ofRectangle rect = f.getBoundingBox(comment, 0, 0);