Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad kerning when using -[NSString drawInRect: withAttributes:]

When using -[NSString drawInRect:withAttributes:], mainly when using the built-in Helvetica Neue but also with other fonts, the kerning is really bad. However, when the same string is rendered in Text Edit (which uses NSTextView) the kerning is great. Consider these two strings:

Bad kerning

Good kerning

For small point sizes, the first case (rendered in Cocoa) is nearly illegible, while the second case (from Text Edit) is much better. I don't think that kerning information is being used at all here. If I apply the NSKernAttribute with a value of 0.0 (which according to the documentation turns off kerning) I get the same result, the one at the top.

I also tried using Core Text with CTLineDraw, but still got the bad kerning. So my question is, is there anything special I need to do to enable kerning in the Cocoa text system? If not, how does NSTextView apply kerning?

like image 568
Aderstedt Avatar asked Nov 12 '22 23:11

Aderstedt


1 Answers

The top example is 'tighter' and the bottom one is 'looser' see -[NSTextView tightenKerning], -[NSTextView loosenKerning]. Here Apple is confusingly referring to what everyone else calls 'Tracking' as 'Kerning' ie. a constant amount of additional space (positive or negative) uniformly applied between each glyph, after the 'character advance' specified in the font, to make text appear, well, tighter or looser.

The default 'tightness' or 'looseness' is a property of the rendering engine, not the font, and could easily be different between different OSX versions, apps or UI elements.

In a font, the kerning table is for adjustments to the character advance needed by specific pairs of glyphs, eg, in my hypothetical font: 'AV' is listed as a kerning pair - the 'A' should have a smaller advance when the character following it is a 'V'.

The OSX fonts Helvetica and HelveticaNeue (and many of the other fonts) have no kerning information at all, so you are right that no kerning information is being used in your examples.

If the font has no Kerning info, your question "How do i enable kerning in the Cocoa text system?" doesn't make sense or doesn't mean what you think it means (?).

like image 84
hooleyhoop Avatar answered Dec 09 '22 21:12

hooleyhoop