21

I have a Cocoa OS X application that creates PDFs for printing. I'm having a problem that when I use small font sizes, the kerning seems all wrong. Here is a screen shot. This is an enlargement from a PDF output by my app, using Helvetica at 6 points. Horrible Kerning Output

As you can see, the kerning is horrible, with some characters touching and others too far apart.

The code I am using basically looks like this (this is a simplified example that reproduces the problem for me):

NSString* dateStr = @"Printed 04/03/2012";
NSFont* detailsFont = [NSFont fontWithName:@"Helvetica" size:6];
NSMutableAttributedString* printedDate = [[NSMutableAttributedString alloc] initWithString:dateStr];
[printedDate addAttribute:NSFontAttributeName value:detailsFont range:NSMakeRange(0, [dateStr length])];
NSRect printedDateRect = NSMakeRect(0, 0, theWidth, 10);
[printedDate drawInRect:printedDateRect];

This isn't unique to Helvetica, it happens in all the fonts I've tried, though it is more pronounced in some than others. If I use a larger size, like 10, it looks fine. 6 point Helvetica from other apps such as Text Edit looks fine. What do I need to do to render 6 point text properly?

Edited to add: I just noticed that this seems much worse with TrueType fonts than with PostScript fonts. Sadly, I don't have PostScript versions of many of the fonts I want to use, so avoiding TrueType isn't really an option.

I'm running OS X 10.7.3 and XCode 4.2.1.

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
Wouldchux
  • 284
  • 1
  • 8
  • 3
    Man, that is some serious [keming](http://www.ironicsans.com/2008/02/idea_a_new_typography_term.html). – blahdiblah Apr 03 '12 at 23:13
  • I tested your code on my computer. I was unable to get anything that bad, but I did notice it was affected by the value I used for `theWidth`, so you might want to try varying that. Also, for simple drawing like this, you might want to use [NSString's `drawInRect:withAttributes:`](http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSString_AppKitAdditions/Reference/Reference.html#//apple_ref/occ/instm/NSString/drawInRect:withAttributes:) instead to make your code simpler. – ughoavgfhw Apr 03 '12 at 23:46
  • Thanks. Making theWidth too small definitely causes problems, but in my code it's set to the full width of the paper. I can set it to a huge number and it still exhibits the same problem. – Wouldchux Apr 04 '12 at 00:05
  • Is this version of Helvetica the Mac OS X-supplied version, or one from another source? – Rob Keniger Apr 04 '12 at 01:11
  • It's the OS X-supplied version. /System/Library/Fonts/Helvetica.dfont. "© 1990-2006 Apple Computer Inc. © 1981 Linotype AG © 1990-91 Type Solutions Inc". However, it's not just Helvetica, it seems to be any Postscript font. – Wouldchux Apr 04 '12 at 01:29

2 Answers2

1

I think the printedDateRect's size is incorrect. To check this, try using - (void)drawAtPoint:(NSPoint)point.

Just a tip...

MacAndor
  • 205
  • 2
  • 6
0

If you are getting this inside of a cell you may want to try variations of the font size and return extra lines inside the cell. Try this approach.

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont boldSystemFontOfSize:11];
    cell.textLabel.numberOfLines = 13;
Chip Russell
  • 65
  • 10