Array of Letters - Can't draw them with even spacing

Hi all,

I’ve been working on this code for a while, but can’t seem to figure out why what I have isn’t working perfectly. Essentially I’m trying to have the alphabet A-Z drawn with equal padding between each letter. Behind each letter I’m looking to make a red rectangle (which will eventually be a button). This sounds simple, but the font I’m using isn’t a fixed width. I’m using OF 007, xCode4.0, and am on a MacBookPro 10.7.2. Below is my code in my testApp.cpp:

void testApp::setup(){
gothamUltraKeys.loadFont(“fonts/gothamUltra.ttf”, 60);
buttons = new string[26];
buttons[0] = “A”;
buttons[1] = “B”;
buttons[2] = “C”;
buttons[3] = “D”;
buttons[4] = “E”;
buttons[5] = “F”;
buttons[6] = “G”;
buttons[7] = “H”;
buttons[8] = “I”;
buttons[9] = “J”;
buttons[10] = “K”;
buttons[11] = “L”;
buttons[12] = “M”;
buttons[13] = “N”;
buttons[14] = “O”;
buttons[15] = “P”;
buttons[16] = “Q”;
buttons[17] = “R”;
buttons[18] = “S”;
buttons[19] = “T”;
buttons[20] = “U”;
buttons[21] = “V”;
buttons[22] = “W”;
buttons[23] = “X”;
buttons[24] = “Y”;
buttons[25] = “Z”;

bounds = new ofRectangle[26];
}

void testApp::draw(){
ofPushMatrix();
ofPushStyle();
ofSetRectMode(OF_RECTMODE_CENTER);
ofTranslate(10, 500, 0);

int fontX = 0;
int padding = 5;

for (int i=0; i<26; i++) {
bounds[i] = gothamUltraKeys.getStringBoundingBox(buttons[i], 0, 0);
fontX+= bounds[i].width+padding;

ofSetColor(255, 0, 0);
ofRect(fontX, bounds[i].y+bounds[i].height/2, bounds[i].width, bounds[i].height);

ofSetColor(255);
gothamUltraKeys.drawString(buttons[i], fontX-bounds[i].width/2, bounds[i].height/2+bounds[i].y/2);
}
ofPopStyle();
ofPopMatrix();
}

Any help would be really appreciated!

if you use ofTrueTypeFont you can get each strings width with stringWidth(…)

Just fixed the problem. Since I had rectmode centered I had to update the end of my draw loop to this:
fontX+= bounds[i].width/2;

ofSetColor(255, 0, 0);
ofRect(fontX, bounds[i].y+bounds[i].height/2, bounds[i].width, bounds[i].height);

fontX += bounds[i].width/2 + padding;