Ugly fonts with NSGlyph

Discussion in 'Mac Programming' started by spt, Oct 28, 2010.

  1. spt macrumors member

    Oct 27, 2009
    As one of my first Cocoa programs, I've made an application to draw maps in a View. To add some labels in the drawing (made using the NSBezierPath class), I used NSGlyph to draw them, using the code below. It works, but the rendered characters look ugly, they look like a bad photocopy, and have inconsistent sizes (even with the same character, some seem thinner then others).

    I would very much appreciate some tips to get this right...

    NSFont *font = [NSFont fontWithName:@"Times New Roman" size: (int)(0.007 * fontsize_items * bounds.size.width)];
    textPoint.x = 0.05 * bounds.size.width + 0.3 * bounds.size.width;
    textPoint.y = legend_itemPoint.y - 0.18 * bound.size.height;
    for (l = 0; l < strlen(legend_text); l++)
    	NSGlyph text = [font _defaultGlyphForChar: legend_text[l]];
    	[text_path moveToPoint:textPoint];
    	[text_path appendBezierPathWithGlyph:text inFont:font];
    	[ltext_path setLineWidth:(0.0001 * fontsize_items * bounds.size.width)];
    	[[NSColor blackColor] set];
    	[text_path fill];
    	textPoint.x += (0.0035 * fontsize_items * bounds.size.width);
  2. kpua macrumors 6502

    Jul 25, 2006
    Why aren't you just using -[NSString drawInRect:withAttributes:]?
  3. Thomas Harte macrumors 6502

    Nov 30, 2005
    An NSGlyph is just an unsigned int internally, hence the existence of NSGlyphInfo if you actually want to know anything about one and the way that you have to specify a glyph and font pair to everywhere that takes a glyph.

    Apple's official solution, beyond the NSString AppKit additions that supply drawInRect:withAttributes: and the other similar ones, is to use an NSLayoutManager. You're using an unofficial API manually to perform glyph-by-glyph string plotting, which itself tends to lead to poor typography (because it discards pair kerning, doesn't allow ligatures, etc) but shouldn't give a photocopied appearance.

    Can you post a screenshot? Is it possible you have anti-aliasing switched off? Check out NSGraphicsContext for various bits of graphics context that affect how an NSBezierPath renders.
  4. spt thread starter macrumors member

    Oct 27, 2009
    I now am :). I didn't know it was so easy to do.

    As this wasn't in the book I was using to learn Cocoa, I referenced the "Cocoa drawing guide". It did does explain drawAtPoint, but doesn't tell that you can use it in combination with NSString. It does however, under the title "Adding text", include an entire section on using NSGlyph, starting with "Because NSBezierPath only supports path-based content, you cannot add text characters directly to a path; instead, you must add glyphs."

    So it says I MUST use glyphs :).

Share This Page