PDA

View Full Version : Calculating the size of a string in pixels to be rendered with wglUseFontBitmaps



Kilam Malik
03-02-2006, 05:29 AM
I use the wglUseFontBitmaps to render a font on the screen. Now I want to show something like tooltips in 3D where I render a yellow background rectangle behind the text. For this I need to know the size of the rectangle.

How can I get this? Reading the documentation of wglUseFontBitmaps it must have this information inside to add the translate to the displaylist. It uses glBitmap inside which has the width and the heigth of the glyph. But how can I get it?

I have read that using the wglUseFontBitmaps to create the font and using wglUseFontOutlines to determine the size is not good because of differences in rasterizing.


I know that it is possible by using OutlineFont and render into a texture on my own or using a 3rd party library. But this is slower at startup and I need a very fast start of my app.

Thanks,
Kilam.

03-07-2006, 04:27 PM
There is nothing in OpenGL for this. You'll have to dig into the Win32 API. Check out GetCharABCWidths, for example.

Kilam Malik
03-20-2006, 11:40 AM
Thanks for that hint. The GetCharABCWidths didnt make it excatly, but it lead me to the right functions ;-)

For those who are interested:

Init:

// Create font:
fontOffset = glGenLists (256);
SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
wglUseFontBitmaps (hdc, 0, 255, fontOffset);

// Retrieve font widths and save in array "fontWidths":
GetCharWidth32(hdc, 0, 255, fontWidths);
// Retrieve font heights:
TEXTMETRIC tm;
GetTextMetrics(hdc, &tm);
fontHeight = tm.tmHeight;Then when drawing, you calculate the lengths of the string in pixels by summing up the values from the array. Now can draw a colored box behind your text in exactly the size of the text.

Kilam.