Results 1 to 6 of 6

Thread: justified font rendering

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2006
    Posts
    8

    justified font rendering

    Hi,

    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?

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: justified font rendering

    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).

  3. #3
    Junior Member Newbie
    Join Date
    Aug 2006
    Posts
    8

    Re: justified font rendering

    Hi Bob,

    Turns out theres a clean solution.
    GetGlyphOutline().
    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.

    Cheers,
    metric

  4. #4
    Senior Member Regular Contributor
    Join Date
    Aug 2004
    Location
    Palo Alto, CA
    Posts
    111

    Re: justified font rendering

    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 ?

  5. #5
    Senior Member OpenGL Guru zed's Avatar
    Join Date
    Nov 2010
    Posts
    2,466

    Re: justified font rendering

    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

  6. #6
    Junior Member Newbie
    Join Date
    Aug 2006
    Posts
    8

    Re: justified font rendering

    Hi dimensionX,

    Yes, here is the code:
    Code :
        // The glyph widths for each character
        int glyphWidths[96];
        GLYPHMETRICS gm;
        // 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.

    Cheers,
    metric

Similar Threads

  1. Font Rendering
    By glfreak in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 08-19-2010, 01:18 PM
  2. Legay curiosities and font rendering
    By dukey in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 02-18-2010, 07:02 AM
  3. Quality and Speed on Font rendering
    By lsdi in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 11-20-2002, 04:32 PM
  4. Font rendering transparency
    By dodgyposse in forum OpenGL: Advanced Coding
    Replies: 7
    Last Post: 06-12-2002, 09:01 AM
  5. font rendering
    By Rabbia in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 05-31-2001, 05:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean