by Michael S. Kaplan, published on 2008/02/28 10:01 -05:00, original URI: http://blogs.msdn.com/b/michkap/archive/2008/02/28/7934894.aspx
Please read the disclaimer!
It is probably a good thing that there are people who feel comfortable turning to me to get information from time to time.
And it is probably better for my own peace of mind that even while mired in conflict (such as is hinted at here!) that they happen to be doing it very recently, since that helps keep up both my resolve to help people and my morale while helping. :-)
Like the other day when that developer who managed to impress me with her insight that led to finding some interesting bugs that I mentioned previously.
She called the other day to ask me about a problem she was having when it came to the results she was getting back from GDI and GDI+ when trying to retrieve the size of text.
Obviously with two different technologies one could expect some differences, but when the font settings and the text are identical, you really wouldn't expect the differences to be too massive, right? I mean shouldn't it boil down to two different techniques that ought to come to nearly the same answer?
She was almost at that "guess it's by design" stage that developers get to when there doesn't seem to be a good explanation for behavior that they might have to live with, but before completely giving up she thought's she'd ask if I had any ideas about what might be behind the differences, which in one of her test cases was leading to two whole characters worth of difference when comparing the two sizes.
I immediately knew what the cause was because it was on my list of things to blog about.
But her call then accidentally saved me from embarrassing myself by actually writing that blog, since it is already explained in something I wrote back in September 2006, in Font sizes vary more than one might expect. So far I think I have only made that mistake once and I was quite happy to avoid doing it again. :-)
Many of the underlying issues are covered in the MS Knowledge Base in 307208 (Why text appears different when drawn with GDIPlus versus GDI), though the KB article's principal concern is with the unusual drawing results in GDI+ in the many edge cases that lead to narrower or wider glyphs. The basics of how to scale them together are almost out of scope in this rather impressive article!
Anyway, back to that Font sizes vary more than one might expect post, it explains the only real workaround one can use for managed applications:
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....
The workaround reminds me more than a little bit of the Private fonts: for members only blog from November 2005, with the goal of providing sample that would allow manged code to consistently work with private fonts, or that Rhymes with Amharic series from April of last year that Scott Hanselman inspired me to write about font embedding in managed code.
Given the mixed nature of WinForms, this workaround has a nice holistic feel about it, just as these other two solutions do.
Suggests that it might make an interesting future blog to write up a sample that does the sizing! Sound interesting to anyone?
And then, like Scott suggested with the font embedding sample and I have suggested to others previously about the private fonts sample, this also might actually make for an interesting core feature to consider in WinForms!
Imagine it -- a setting that would automatically adjust the font size at runtime to make sure that all controls (the purely managed ones, the managed wrappers around unmanaged ones, and the managed ones that work through TextRenderer) have a single size that will return consistent results. The only downside to such a feature is that it could easily lead to clipping based on the font size increasing a bit in that quest for the ideal GDI pixel boundary size, but that seems like a small price to pay for controls that have the same font size setting having identically sized text, right? :-)
This blog brought to you by ভ (U+09ad, aka BENGALI LETTER BHA)
Doug on 28 Feb 2008 10:31 AM:
The rendering size of text is indeed tricky.
According to Mr. Hill, the priority for font rendering is readability (which is a good thing).
One cosequence is that the size of output is different for each device. Finding out the exact size that the text will render when sent to the printer has not been easy to find from my web searches. The result is that my screen preview often does not match the printout.
Michael S. Kaplan on 28 Feb 2008 10:38 AM:
The issue with the difference between the screen and the printer is one I mention here in an entirely different context (TextRenderer support) but with similar principles....
go to newer or older post, or back to index or month or day