View Full Version : justified font rendering

08-15-2007, 01:04 PM

Spent the better part of the day trying to think of a way to do justified text rendering.
The obvious solution is via something like Nehe #13 using a fixed width font, and then left/right justify via the '-' format flag as neceassry. Unfortunatley this is not desirable for my application. Without knowing the pixel width of the bitmaps creating by wglUseFontBitmaps, I dont see how this is easily done. Any ideas?

08-15-2007, 01:29 PM
Without knowing the dimensions of your glyphs, you cannot do non-trivial formating without ugly hacks. If you need non-trivial formatting, consider using another font rendering library, or search for methods to get the actual size of the glyphs produced (if possible).

08-15-2007, 03:29 PM
Hi Bob,

Turns out theres a clean solution.
Simply maintain an array of of glyph widths
indexed by the ascii character value so
you can look up the total width of a string
at run time. Using the width its very easy
to justify text against a fixed position.


08-15-2007, 04:53 PM
I tried the GetGlyphOutline() to get the dimenions of the Glyph bitmap texture in pixels but the GetGlyphOutline() function call always failed.

Metric where you able to solve the problem with the above function ?

08-15-2007, 11:48 PM
what i do is with a 16x8 bitmap containing the font characters (like most games use)
is use glGetImage(...) + then physically measure how far from the left + right each character is in its block
gives perfect results

08-16-2007, 02:08 AM
Hi dimensionX,

Yes, here is the code:

// The glyph widths for each character
int glyphWidths[96];
// initialize an identity matrix
MAT2 T = { 0,1,0,1,0,1,0,1 };
// loop through all the ascii glyphs we are using
// and extract the raster width.
for(UINT i = 0; i < 96; i++) {
DWORD ret = GetGlyphOutline(m_hDC,i+32,GGO_METRICS,&amp;gm,0,NULL, &amp;T);
if(ret != GDI_ERROR) {
glyphWidths[i] = gm.gmCellIncX;
}This function kept failing for me as well, until I realized I was not initializing the MAT2 matrix correctly. Here I have set it to an identity matrix, because we dont want to apply any transformations to the glyph. Check out the MSDN docs for more info.
Whats nice about keeping the widths in an array like this is that its very easy to compute the total width of a character string. For each character, get its equivalent integer value, subtract 32, and just use that as an index into the array.