PDA

View Full Version : Font width!



ProfEich
03-30-2003, 07:42 AM
Hi!
How can I get the Fontwidth in pixel of a font I created.

Here's the code where I create the font:




UINT CreateOpenGLFont(LPSTR strFontName, int height)
{
UINT fontListID = 0;
HFONT hFont;

fontListID = glGenLists(MAX_CHARS);

hFont = CreateFont( height,
0, // The WIDTH (If we leave this zero it will pick the best width depending on the height)
0,
0,
FW_BOLD,
FALSE,
FALSE,
FALSE,
ANSI_CHARSET,
OUT_TT_PRECIS,
CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY,
FF_DONTCARE|DEFAULT_PITCH,
strFontName);

hOldFont = (HFONT)SelectObject(g_window->hDC, hFont);

wglUseFontBitmaps(g_window->hDC, 0, MAX_CHARS - 1, fontListID);

return fontListID;
}


How can I get the font width?

mikael_aronsson
03-30-2003, 07:57 AM
Hi !

That is a bit tricky, the rasterposition is advanced when you draw a character so you can use that to calculate the width, but this requires a glGet call, not the best solution.

Another solution is to use freetype instead, this also gives you platform independence, if you want that, there are some very nice libraris that use freetype to draw text using OpenGL with all kinds of neat features.

Mikael

ProfEich
03-30-2003, 08:22 AM
Hi!
I use "Arial" because the other fonts takes to much space.

Maybe I can store the width of every char in an array. But I don't know how to use glGet

shinpaughp
03-30-2003, 10:07 AM
Look it up on http://msdn.microsoft.com
Mentions something about GLYPHMETRICS, so you might be able to use GetGlyphOutlines or GetTextMetrics. Maybe.

Mihail121
03-30-2003, 11:55 AM
Ahum,guys!It seems that you've forgotten one the most used Win32 functions.Yep this is "GetCharWidth".Take a look at the MSDN for it and check what does it do