Text Metrics in the .Net Framework, Part II - ' Vertical Correction' (
Page 2 of 2 )
The Vertical Correction
I thought that deriving the vertical correction factor would be easy—just move the display coordinate upward by an amount equal to Font.Size. I was being too optimistic. This technique moved the text up too much, as shown in Figure 3; the text floats above the desired baseline rather than sitting right on it.
ADVERTISEMENT
Figure 3. Moving the text up by an amount equal to Font.Size did not give the desired results.
Looking at Figure 3, I noticed that the amount by which the text was too high seemed to be half of the descender size. You can calculate the descender height in display units as follows (as before, ff is the FontFamily object and f is the Font object):
Then you could calculate the Y correction factor like this:
yCorrection = f.Size DescenderHeight / 2
Initially this seemed to work just fine, giving perfect vertical alignment with Times and Arial font families. But, yet again, when I tested it with more obscure font families, such as Book Antiqua and Rockwell, it failed.
I won't bore you with the details of my further experimentation, but the bottom line as far as I can tell is that the vertical correction for precise text placement must also be ad hoc. The font size and descender parameters are a good place to start, and for some fonts they are all you need. For other fonts, however, a correction factor must be applied, and the only way to determine this factor is through trial and error.
Conclusions
The .NET Framework provides powerful tools for displaying text, but it falls down when it comes to the really precise text placement that is required in some applications, such as Computer Aided Design (CAD). Because of the limitation of the metrics available for Windows fonts, solutions must be based on trial and error. By following the approaches presented here, you should be able to obtain the text placement accuracy that you need.