Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Miniwindows ➜ Getting the size of a window's font

Getting the size of a window's font

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Twisol   USA  (2,257 posts)  Bio
Date Sun 22 Nov 2009 07:36 AM (UTC)
Message
I can't seem to find a way to get the size of an already-loaded font. I don't mean height or width or weight, I want the size that was initially passed to WindowFont() as the fourth parameter. I thought the weight from WindowFontInfo() might be it, but it gave me 400 from a 10pt Lucida Console. Is this possible?

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Bast   (78 posts)  Bio
Date Reply #1 on Sun 22 Nov 2009 02:21 PM (UTC)
Message
Here is how I get it

self.id = windowid
v = font_id

local size = tonumber(WindowFontInfo(self.id, v, 2)) - tonumber(WindowFontInfo(self.id, v, 3))

Bast

Bast

Scripts: http://github.com/endavis
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #2 on Sun 22 Nov 2009 08:12 PM (UTC)

Amended on Sun 22 Nov 2009 08:53 PM (UTC) by Nick Gammon

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:


 Height = -Size * 96 / 72


Solving for size given the height (and ignoring the negative sign, see the knowledge base article below for why we do this) gives us:


Size = Height * 72 / 96 


ie.


Size = Height * 0.75


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:


46 - 9 = 37


Now multiply 37 by 0.75 and we get:


37 * 0.75 = 27.75


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

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


14,850 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.