CATextLayer and subpixel anti-aliasing

  • Hi all,

    I've noticed that when using layer backing on a NSTextField the font
    becomes fuzzy - it seems that it switches from subpixel anti-aliasing
    to normal. And indeed this seems to be the case. After reading
    CATextLayer's documentation (assuming that NSTextField uses it to draw
    its text), it becomes apparent that subpixel anti-aliasing cannot be
    used with CATextLayer. Here's the exact wording:
    "Note: CATextLayer disabled sub-pixel antialiasing when rendering
    text. Text can only be drawn using sub-pixel antialiasing when it is
    composited into an existing opaque background at the same time that
    it's rasterized. There is no way to draw subpixel-anialiased text by
    itself, whether into an image or a layer, separately in advance of
    having the background pixels to weave the text pixels into. Setting
    the opacityproperty of the layer to YES does not change the rendering
    mode."

    After reading the above note it looks to me that if the CATextLayer
    has an opaque background, it should be able to use sub-pixel anti-
    aliasing *if* we provide the background pixels in advance. The
    question is, is it possible to do this? I tried setting the
    backgroundColor property of a NSTextField's layer and it still
    produced normal anti-aliased text as opposed to subpixel anti-aliased.
    Has anyone else bumped into this issue?

    I've also taken a sample screencast of the degradation of the quality
    of the text when using layer backing on a NSTextField - http://www.doc.ic.ac.uk/~md207/ca_bugs/text_render/CA_text_render.mov
    .

    Kind regards,
    Milen
  • On Mar 28, 2008, at 9:19 AM, Milen Dzhumerov wrote:
    > Hi all,
    >
    > I've noticed that when using layer backing on a NSTextField the font
    > becomes fuzzy - it seems that it switches from subpixel anti-
    > aliasing to normal. And indeed this seems to be the case. After
    > reading CATextLayer's documentation (assuming that NSTextField uses
    > it to draw its text), it becomes apparent that subpixel anti-
    > aliasing cannot be used with CATextLayer. Here's the exact wording:
    > "Note: CATextLayer disabled sub-pixel antialiasing when rendering
    > text. Text can only be drawn using sub-pixel antialiasing when it is
    > composited into an existing opaque background at the same time that
    > it's rasterized. There is no way to draw subpixel-anialiased text by
    > itself, whether into an image or a layer, separately in advance of
    > having the background pixels to weave the text pixels into. Setting
    > the opacityproperty of the layer to YES does not change the
    > rendering mode."
    >
    > After reading the above note it looks to me that if the CATextLayer
    > has an opaque background, it should be able to use sub-pixel anti-
    > aliasing *if* we provide the background pixels in advance. The
    > question is, is it possible to do this? I tried setting the
    > backgroundColor property of a NSTextField's layer and it still
    > produced normal anti-aliased text as opposed to subpixel anti-
    > aliased. Has anyone else bumped into this issue?

    Yes, it's possible, but there are two separate cases here:

    Case 1: If you're using a layer-backed NSTextField, then CATextLayer
    is not actually involved.  Setting the NSTextField's backgroundColor
    to an opaque color is on the right track, but you also need to do set
    drawsBackground to YES for it to take effect.  Then you should see
    subpixel AA in the rendered results.  (This can even be done in IB
    using the NSTextField inspector.)

    Case 2: If you're using a CATextLayer directly, you'll need to
    subclass CATextLayer and do something like the following in your
    drawing code:

    - (void)drawInContext:(CGContextRef)ctx
    {
    CGContextSetRGBFillColor (ctx, r, g, b, a);
    CGContextFillRect (ctx, [self bounds]);
    CGContextSetShouldSmoothFonts (ctx, true);
    [super drawInContext:ctx];
    }

    > I've also taken a sample screencast of the degradation of the
    > quality of the text when using layer backing on a NSTextField - http://www.doc.ic.ac.uk/~md207/ca_bugs/text_render/CA_text_render.mov
    > .
    >
    > Kind regards,
    > Milen

    --
    Troy Stephens
    Cocoa Frameworks
    Apple, Inc.
previous month march 2008 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today