Win32 Font Metrics

new topic     » topic index » view thread      » older message » newer message

Has anyone got Win32 code that returns the width of font characters?

I've written a routine (included at the end of this note), but I'm not sure
what the problem with it is... The values returned from the function are
suspicious, and I suspect that I in addition I have to scale the values by
converting logical units to physical units.

In the Win95 book, Petzold leaves this out. He "cheats" by using the average
character size. I need the character metrics so I can calculate the length
of a string, and be able to display justified text.

Even a pointer to some C or VB code that does this would be a great help.

Thanks.

-- David Cuny

-- begin code --

global constant xGetCharABCWidths =
   linkFunc(gdi32,  "GetCharABCWidthsA",
      {C_POINTER, C_LONG, C_LONG, C_POINTER}, C_POINTER )

-- Type ABC
global constant
    abcA                    = allot( Integer ),
    abcB                    = allot( Integer ),
    abcC                    = allot( Integer ),
    SIZEOF_ABC              = allotted_size()


global function getFontCharSize( integer id )

    sequence widths
    atom hdc, result, abc, ptr

    -- get the device context
    hdc = getDC( id )

    -- set the font
    putFontIntoHDC( hdc, id )

    -- array of chars 1-255
    abc = allocate_struct( SIZEOF_TEXTMETRIC * 255 )

    -- fill with zeros
    widths = repeat( 0, 255 )

    -- get ABC structures for chars 1 through 255
    if not c_func( xGetCharABCWidths, { hdc, 1, 255, abc } ) then
        warnErr( "GetCharABCWidths in getFontCharSize failed." )
    else

        -- starting char to retrieve
        ptr = abc

        -- retrieve values
        for i = 1 to 255 do

            -- width is sum of spacing
            widths[i] = ( fetch( ptr, abcA ) +
                          fetch( ptr, abcB ) +
                          fetch( ptr, abcC ) ) * xScale

            -- advance to next char
            ptr += SIZEOF_ABC

        end for

    end if


    -- cleanup: replace the font
    replaceObject( hdc, DefaultFontID, ForProgram )

    -- cleanup: release the device context
    releaseDC( id )

    -- cleanup: free the structure
    free( abc )

    return widths

end function

-- end code --

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu