Centering bitmap fonts

Started by
2 comments, last by MonkeyInBlack 18 years, 10 months ago
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();
Advertisement
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.
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.
Quote:Original post by Dave Hunt
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.


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

This topic is closed to new replies.

Advertisement