Posted by
| Nick Gammon
Australia (23,158 posts) Bio
Forum Administrator |
Message
| Er, let's see. The calculation in the code to convert what you pass into the font height is this:
int lfHeight = -MulDiv(Size ? Size : 10.0, dc.GetDeviceCaps(LOGPIXELSY), 72);
Now:
#define LOGPIXELSY 90 /* Logical pixels/inch in Y */
So, substituting in this line:
/print (GetDeviceCaps (90)) --> 96
Assuming the size is not zero, and replacing we get:
Height = -MulDiv(Size, 96, 72);
The help for MulDiv says:
MulDiv
The MulDiv function multiplies two 32-bit values and then divides the 64-bit result by a third 32-bit value. The return value is rounded up or down to the nearest integer.
Thus, it is really:
Solving for size given the height (and ignoring the negative sign, see the knowledge base article below for why we do this) gives us:
ie.
See: http://support.microsoft.com/kb/74299
Now using the example here: http://www.gammon.com.au/mushclient/mw_text.htm
We see the following figures:
WindowFont (win, "f", "Trebuchet MS", 28, true, false, false, false) -- define font
width = WindowTextWidth (win, "f", "Lazy dog yawns") -- width of text (270)
height = WindowFontInfo (win, "f", 1) -- height of the font (46)
ascent = WindowFontInfo (win, "f", 2) -- ascent (amount above the baseline) (36)
descent = WindowFontInfo (win, "f", 3) -- descent (amount below the baseline) (10)
leading = WindowFontInfo (win, "f", 4) -- leading (space above the highest letter) (9)
According to the Microsoft knowledge base article, the character height, which is what we want, is the font height minus the internal leading. So we need to take WindowFontInfo (win, "f", 1) and subtract WindowFontInfo (win, "f", 4) .
In this particular case it is:
Now multiply 37 by 0.75 and we get:
Round that up and we get back the original 28 which was the font size. Phew!
I'm not sure if the logical pixels per inch would vary from system to system, so you should probably recalculate that, ie.
Size = round ( Height * 72 / GetDeviceCaps (90) )
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|