# Centering bitmap fonts

This topic is 4875 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been looking for a way to display bitmap fonts centered but it seems that i'm stuck. The problem i'm having with the fonts is that the different characters composing the font have variable widths. Therefor I can't find out the width of the entire string - and can't center it. I'm sure i must be missing some way to get the width of the different characters in the font. If anyone could help me out here that would make me really [smile]. PS : the code i'm using to build and display the font is pretty standard, but i thought i'd post it anyway.
// Building the font
base = glGenLists(256);
HFONT	font = CreateFont(height,
0,
0,
0,
FW_BOLD,
FALSE,
FALSE,
FALSE,
ANSI_CHARSET,
OUT_TT_PRECIS,
CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY,
FF_DONTCARE|DEFAULT_PITCH,
fontfile);

SelectObject(hDC, font);

wglUseFontBitmaps(hDC, 32, 96, base);

// Displaying text
char		text[256];
va_list		ap;

va_start(ap, string);
vsprintf(text, originalstring, ap);
va_end(ap);

glPushAttrib(GL_LIST_BIT);
glListBase(base-32);

glTranslated(x,y,0);
glRasterPos2f(0.0f,0.0f);
glCallLists(strlen(text),GL_UNSIGNED_BYTE,text);
glTranslated(-x,-y,0);

glPopAttrib();


##### Share on other sites
In standard Win32 you can find it out by having TEXTMETRICS defined and aquired when you print out the text or align it. You can probably find something similar with Win32 functions related to font, because I'm pretty much sure they have specified for width/height/ave for each size of the font.

Good luck.

##### Share on other sites
Have a look at GetTextExtentPoint32. It will give you the width and height of a string based on the font currently selected into the DC.

##### Share on other sites
Quote:
 Original post by Dave HuntHave a look at GetTextExtentPoint32. It will give you the width and height of a string based on the font currently selected into the DC.

That was exactly what i was looking for. A big 'thank you' to you both [smile] .

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 12
• 10
• 13
• 104
• 11
• ### Forum Statistics

• Total Topics
632979
• Total Posts
3009683
• ### Who's Online (See full list)

There are no registered users currently online

×