core text and CALayer: improve text quality

Discussion in 'Mac Programming' started by MrFusion, Jun 5, 2011.

  1. MrFusion macrumors 6502a

    Jun 8, 2005
    I recently found out about core text. So I am playing around with this API.

    I use core text to draw text in a CALayer subclass. This is similar to CATextLayer, but should (eventually) give more flexibility on how and where the text is drawn.

    Text is displayed but the quality is not very good. A common problem according to google.
    One requirement for decent looking text is not to have a transparent background. (This is already the case in my code).
    Another suggestion somewhere was to set CGContextSetAllowsAntialiasing, CGContextSetAllowsFontSubpixelQuantization, etc.
    I assumed these had to be set to YES, but this did not improve the text drawing.
    I then found a message that CGContextSetShouldSmoothFonts should be set to NO. This did improve the quality.
    There are (at least) 7 of these type of functions. With 2 possibilities (yes,no) that gives (at least) 2^7 possible combinations.
    A bit too much to try out.

    So, my question would be: In your experience, which functions give the best text render quality with core text and CALayer?

    -(void) drawInContext:(CGContextRef)context
    	//[super drawInContext:context]; //default implementation does nothing (see docs)
    	//get graphics context
    	NSGraphicsContext *graphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:context
    	[NSGraphicsContext saveGraphicsState];
    	[NSGraphicsContext setCurrentContext:graphicsContext];
    [B]	//How to improve text quality?
    //	CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    //	CGContextSetAllowsAntialiasing(context, YES);
    //	CGContextSetAllowsFontSmoothing(context, YES);
    	CGContextSetShouldSmoothFonts(context, [COLOR="Blue"]NO[/COLOR]);	//improves draw quality of text
    //	CGContextSetAllowsFontSubpixelPositioning(context, YES);
    //	CGContextSetShouldSubpixelPositionFonts(context, YES);
    //	CGContextSetAllowsFontSubpixelQuantization(context, YES);
    //	CGContextSetShouldSubpixelQuantizeFonts(context, YES);[/B]
    	//custom drawing
    	CTFrameDraw(_coreTextFrame, context);    
    	//restore graphics context
    	[NSGraphicsContext restoreGraphicsState];
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    I disagree with the 2^7 count.

    There are 4 SetShould* functions. There are also 4 SetAllows* functions. The actual state of a feature is the Boolean AND of two function args.

    For example, anti-aliasing is the AND of the two flags passed to CGContextSetAllowsAntialiasing() and CGContextSetShouldAntialias(). If the flag to either function is false/no, then no anti-aliasing should occur.

    The CGContext reference doc even says for CGContextSetAllowsAntialiasing():
    Quartz performs antialiasing for a graphics context if both the allowsAntialiasing parameter and the graphics state parameter shouldAntialias are true.

    You should be able to set all the SetAllows states to true/yes, then manipulate only the SetShould states directly, to see 16 different permutations. That's a 4x4 grid, if you displayed them all at once. If you don't display them all at once, then assign a checkbox for each SetShould state, and display the result by itself. This is just the kind of thing that's good to have an interactive program for.

Share This Page