views:

337

answers:

1

Hi

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

Thanks in advance

+1  A: 

When the mapping mode is mm_Text (which it usually is), and when the lfHeight field is positive, it already gives the height in points. When it's negative, the units are pixels. MSDN for LogFont gives you the formula to convert between them:

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

There are 72 points per inch. GetDeviceCaps tells you the number of pixels per inch on a given device. Invert the formula to get pixels from points:

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

The important thing to realize here is that you need a device context. Font sizes don't exist independently of the media they appear on. The pixel height of a font on the screen will be different from the pixel height of a font on a printer. Use the Handle property of whatever canvas you're planning on drawing to.

Rob Kennedy
> "when the lfHeight field is positive" - Then the font mapper provides a match for the cell height, which is character height + internal leading. To get the point size, one have to subtract internal leading from (cell) height and proceed as above.
Sertac Akyuz
I think my first comment is somehow vague and somehow misleading. Vague wouldn't be if said; "lfHeight" is always in pixels, the only difference is when positive it's the full cell height, and when negative its absolute value is character height. Misleading wouldn't be if said; since a LOGFONT structure has no "internal leading" information there's no way to calculate the point size with a positive lfHeight. One have to select the font to a DC then use GetTextMetrics, and then character height is tmHeight-tmInternalLeading.
Sertac Akyuz