Saturday, October 9, 2010

The Joy of Fonts

Question of the day:
"Hey dmac... is using NSHeight([myFont boundingRectForFont]) the right height to use when working with a font?"
I responded that [myFont ascender] - [myFont descender] should give you the same value, and may be faster. It turns out that [myFont ascender] - [myFont descender]for Helvetica 16 gave a value of 16 which seemed logical, however the height of [myFont boundingRectForFont] came up with 25.6406. Whoa... that's insane.


I grabbed a copy of NSFontAttributeExplorer from Apple's example code, and modified it to be 16 pt instead of 12 pt (my version here). Giving it a run showed (click on image for bigger picture):



What is occurring is that the bounding rect for font does measure the difference between the highest ascender and the lowest descender for a font, and that those values don't necessarily agree with the "documented" ascender for a font. You can see this playing with TextEdit. 
Basically the glyphs have collided due to them extending beyond their documented "bounds". So what is the "right" value to use? TextEdit seems to think that [myFont ascender] - [myFont descender]is proper even though you may run into colliding glyphs.

No comments:

Post a Comment