Font sizes vary more than one might expect

by Michael S. Kaplan, published on 2006/09/21 21:35 -04:00, original URI: http://blogs.msdn.com/b/michkap/archive/2006/09/21/765438.aspx


Regular reader Mike Dimmick commented in response to a post I did yesterday:

The Windows CE team decided on Tahoma 9pt as their system font way back in the mists of antiquity, so I'm used to it.

And if you want the 'normal' Windows 2000/Office 2000 appearance, you actually want 8.25pt, because 8pt gives you a different pixel count.

The real situation is slightly more complicated (though still quite interesting) and it came up in another question that was asked the other day:

GDI part of the question:

 I found the following explanation on a newsgroup:

You cannot set the size of a font to any value you want. This is because Windows does all its graphic work in pixels and it therefore likes to set the font size to a value that will result in an integer pixel value. So, if you are running at the standard Windows Small Fonts Setting (15 Twips per pixel) then Windows will set display font sizes to the nearest value that results in an exact multiple of 15 Twips (or an integer value of pixels).

For example, if you set a Text Box font to Arial size 10 (which is effectively asking for a font size of 200 Twips, since there are always 20 Twips per Point) then you are effectively asking Windows to set the font size to 200/15 pixels (since there are 15 Twips per Pixel at the standard Windows setting as mentioned above). That means you are asking Windows for a font size of (10 * 20) / 15 =  13.3333 pixels. Windows doesn't like this, and so it will actually set the font size to 9.75 Points, resulting in a font size of (9.75 * 20) / 15 = 13 pixels. Ask for a size of 12, however, and you will get what you ask for, because (12 * 20) / 15 = 16 and is an integer.

Is this correct?

 Windows Forms part:

It appears that if I assign a font of ‘Arial, 10pt” to the TabControl, it treats it as if the size is 9.75.  When it calculates the tab width, and when it draws the text.  The problem is I have taken over drawing the text, but it is still calculating the tab width.  When I use a font of “Arial, 10pt” with the TextRenderer it truly uses a 10pt size.  This means on some tabs I overflow the tab.

The truth is essentially much more sordid than Mike was thinking, and at least as sordid as this other situation where the scaling, which differs between

So determining how best to scale is going to always be dependent on both the control type and the rendering technology, and thus any measuring that is done with them has to take that into account....

One workaround that can be used is to ALWAYS request font sizes that will be on the right pixel boundary for GDI -- thus the size that is passed will be the same as the rounded size would be.

Note that this means you have to set the font size at runtime, since the twips and pixel settings can be different depending on runtime settings....

 

This post brought to you by (U+a1dc, YI SYLLABLE GI)


no comments

referenced by

2011/06/10 What do fonts and keyboards have in common? Not "crap bag" compatibility!

2008/02/28 Font size scaling -- GDI vs. GDI+

go to newer or older post, or back to index or month or day