INFO: Calculating The Logical Height and Point Size of a Font

This article was previously published under Q74299
To create a font in the Microsoft Windows graphical environment given onlythe required point size, an application must calculate the logical heightof the font because the CreateFont() and CreateFontIndirect() functions use logical units to specify height.

To describe a font to the user, an application can calculate a font's pointsize, given its height. This article provides the formulas required toperform these calculations for the MM_TEXT mapping mode. You will have toderive a new equation to calculate the font size in another mapping mode.
To calculate the logical height, use the following formula:
                                Point Size * LOGPIXELSY   height = Internal Leading + -------------------------                                          72				
LOGPIXELSY is the number of pixels contained in a logical inch on thedevice. This value is obtained by calling the GetDeviceCaps() function with the LOGPIXELSY index. The value 72 is significant because one inch contains 72 points.

The problem with this calculation is that there is no method to determinethe internal leading for the font because it has not yet been created. Towork around this difficulty, use the following variation of the formula:
            -(Point Size * LOGPIXELSY)   height = --------------------------                         72				
This formula may also be written as follows:
   plf->lfHeight = -MulDiv (nPtSize, GetDeviceCaps (hdc, LOGPIXELSY), 72);				
When an application calls the CreateFont() or CreateFontIndirect() functions and specifies a negative value for the height parameter, the font mapper provides the closest match for the character height rather than the cell height. The difference between the cell height and the character height is the internal leading, as demonstrated by the following diagram:
   ----------  <------------------------------   |        |           |- Internal Leading  |   | |   |  |  <---------                    |   | |   |  |        |                       |- Cell Height   | |---|  |        |- Character Height     |   | |   |  |        |                       |   | |   |  |        |                       |   ----------  <------------------------------				
The following formula computes the point size of a font:
                (Height - Internal Leading) * 72   Point Size = --------------------------------                           LOGPIXELSY				
The Height and Internal Leading values are obtained from theTEXTMETRIC data structure. The LOGPIXELSY value is obtained from theGetDeviceCaps function as outlined above.

Round the calculated point size to the nearest integer. The WindowsMulDiv() function rounds its result and is an excellent choice toperform the previous calculation.
3.00 3.10 3.50 4.00 win16sdk TrueType

Article ID: 74299 - Last Review: 11/21/2006 15:47:12 - Revision: 3.2

Microsoft Windows Software Development Kit 3.1, Microsoft Win32 Application Programming Interface

  • kbinfo KB74299