Do fonts have anchorpoints?

I am working with trueTypeFont and the ofBox2D engine. I am trying to place letters inside boxes. But while I can place a letter like “A” at the center of a box without an issue, letters that have a bit below the line, like the Q, a small j, g, and many more are not placed in the center. Is there a way to grab the font from the top or bottom like you do with ofImage when you use the setAnchorPercent() function?

Otherwise interaction between letter-objects gets a bit funny-looking.
Thanks for any tips.

type is baseline aligned. Your best bet is to maybe do font.getStringBoundBox() for a specific character to get it’s proper dimensions (width and height). Then, you figure out how to center the individual character based on it’s true with and height.

1 Like

Thank you @zach. That worked perfectly. Here is a simplified version of the code I wrote in case anyone else needs it:

//run the box2D stuff here//
//and initialize the font stuff//

//then when it's time to draw the font in the center of the box//
float pX=box.get()->getPosition().x; //box is a box2DRect object
float pY=box.get()->getPosition().y;
float a=box.get()->getRotation();

ofRectangle tempRect = myFont.getStringBoundingBox("Q", pX, pY);
ofPoint centerDiff = tempRect.getCenter() - ofPoint(pX, pY);

ofTranslate(pX, pY);
myFont.drawString("Q", -centerDiff.x, -centerDiff.y);
1 Like