Convert LogFont height to font size in points

I have a LOGFONT structure. Now all I would like to do is get the associated font size in points from the LOGFONT height.

+4
source share
2 answers

When the display mode is mm_Text (which is usually there), and when the lfHeight field lfHeight positive, it already gives the height in points. When it is negative, units are pixels. MSDN for LogFont provides a formula for converting between:

 lfHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72); 

There are 72 dpi. GetDeviceCaps indicates the number of pixels per inch on this device. Invert the formula to get pixels from points:

 PointSize := MulDiv(-lfHeight, 72, GetDeviceCaps(hDC, LogPixelsY); 

It is important to understand that you need a device context. Font size does not exist no matter what medium they appear on. The font pixel height on the screen will be different from the font pixel height on the printer. Use the Handle property of any canvas that you plan to draw.

+11
source

I find this a bit confusing. Here are a few things I learned .;)

  • Examine the two low-order bits of lfPitchAndFamily to determine the type of font.
  • For fixed-pitch fonts, use GetTextMetrics and the TEXTMETRIC structure.
  • For fonts with variable pitch (true-type, etc.), use GetOutlineTextMetrics and the OUTLINETEXTMETRIC structure. Make sure the structure is aligned correctly. In addition, the structure has a variable size. Call the function once to get the size, select the place, then call the function again to fill the structure.
  • From there, you can find the correct information about ascents, descents and other sizes.

Keep in mind that these are recommended values ​​and not all display procedures will use them correctly. For example, I am in the process of finding out the correct method for determining the required height of the static element of a dialog box for a given line of text. It seems that Microsoft has not completed its own documentation .;) Not to ensure that the documentation is fully understandable or complete, for starters.

+1
source

Source: https://habr.com/ru/post/1311432/


All Articles