NSBezierPath to NSImage with subpixel rendering

  • Hello,

    I am trying to stroke a bezier path into an off-screen buffer.

    in the -initWithFrame: method of my custom view, i created an NSImage
    instance

    image = [[NSImage alloc] initWithSize:frameRect.size];

    i created an NSBezierPath and added stuff:

    path = [[NSBezierPath alloc] init];
    [path *drawStuff*]; // drawing here

    then, still inside -initWithFrame: i stroke the path into the image:

    [image lockFocus];
    [[NScolor whiteColor] set];
    [NSBezierPath fillRect:[frameRect]];

    [[NScolor blackColor] set];
    [path stroke];
    [image unlockFocus];

    so by now, as part of my custom view initialization the NSImage instance
    is supposed to contain my stroked path.

    afterwards, in -drawRect: i just do:

    [image drawInRect: rect
              fromRect: rect
            operation: NSCompositeCopy
              fraction: 1.0];

    by doing that, i don't benefit from subpixel precision :(
    the problem is that it seems that subpixel rendering wasn't applied when
    rendering into the NSImage, although i painted a white background (by
    filling a white rect).

    however, if i move the path stroking code into drawRect:

    - (void)drawRect:(NSRect)rect
    {
      [image lockFocus];
      [[NScolor whiteColor] set];
      [NSBezierPath fillRect:[frameRect]];

      [path stroke];
      [image unlockFocus];

      [image drawInRect: rect
                fromRect: rect
              operation: NSCompositeCopy
                fraction: 1.0];
    }

    then i get subpixel precision.

    so, what's the proper way to achieve offscreen antialising + subpixel
    precision in an NSImage that has a background ??? (
    http://michelf.com/weblog/2006/subpixel-antialiasing-achilles-heel/
    seems to explain why it's impossible to do it with transparent backgrounds)

    i guess this is related to graphic contexts but i'm just learning cocoa,
    please help :)

    regards,
    g.
  • Hi Gregory,

    On 2008-01-09, at 22:10, Gregory 'guardian' Pakosz wrote:

    > so, what's the proper way to achieve offscreen antialising +
    > subpixel precision in an NSImage that has a background

    Is antialiasing turned on in your graphics context?
    [[NSGraphicsContext currentContext] setShouldAntialias:YES]

    <http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Cla
    sses/NSGraphicsContext_Class/Reference/Reference.html#//apple_ref/occ/instm
    /NSGraphicsContext/setShouldAntialias:
    >


    This only seems to relate to LCD antialiasing of text, which is not
    what you're wanting.

    Jonathon Mah
    <me...>
  • >
    > Is antialiasing turned on in your graphics context?
    > [[NSGraphicsContext currentContext] setShouldAntialias:YES]
    >
    adding this didn't help unfortunately :( although it thought it was
    enabled by default.

    > http://michelf.com/weblog/2006/subpixel-antialiasing-achilles-heel/
    >
    > This only seems to relate to LCD antialiasing of text, which is not what
    > you're wanting.
    >
    well what you seem to call "LCD antialising" is subpixel rendering, and
    it makes sense for anything drawn to the screen.

    in the meantime, by using

    NSBitmapImageRep *rep = [[self
    bitmapImageRepForCachingDisplayInRect:[self bounds]] retain];

    NSImage *image = [[NSImage alloc] initWithSize:[self bounds]];
    [image addRepresentation:rep];

    [self cacheDisplayInRect:[self bounds] toBitmapImageRep:rep];

    i was able to get back an NSImage with subpixel precision. now i am
    curious about whether or not it's possible to achieve the same result by
    directly rendering into the NSImage instance.

    cheers,
    g.

    >
    >
    > Jonathon Mah
    > <me...>
    >
    >
    >
  • Le 9 janv. 08 à 13:01, Jonathon Mah a écrit :

    > Hi Gregory,
    >
    > On 2008-01-09, at 22:10, Gregory 'guardian' Pakosz wrote:
    >
    >> so, what's the proper way to achieve offscreen antialising +
    >> subpixel precision in an NSImage that has a background
    >
    > Is antialiasing turned on in your graphics context?
    > [[NSGraphicsContext currentContext] setShouldAntialias:YES]
    >
    > <http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Cla
    sses/NSGraphicsContext_Class/Reference/Reference.html#//apple_ref/occ/instm
    /NSGraphicsContext/setShouldAntialias:
    > >
    >
    >> http://michelf.com/weblog/2006/subpixel-antialiasing-achilles-heel/
    >
    > This only seems to relate to LCD antialiasing of text, which is not
    > what you're wanting.

    To get a good image scaling quality, you have to change your graphic
    context settings.

    [image lockFocus];
    [[NSGraphicsContext currentContext]
    setImageInterpolation:NSImageInterpolationHigh];
    ....
  • Gregory,

    On 2008-01-09, at 23:04, Gregory 'guardian' Pakosz wrote:

    > well what you seem to call "LCD antialising" is subpixel rendering,
    > and it makes sense for anything drawn to the screen.

    My apologies, I thought you meant subpixel precision (which would be
    rendered as anti-aliasing with whole pixels). As far as I'm aware,
    Quartz does not draw paths with subpixel rendering, only text (if it
    is enabled).

    Personally, I can't stand the color fringes on text (on my MacBook),
    so I have text set to normal anti-aliasing.

    Jonathon Mah
    <me...>
  • Jonathon Mah wrote:
    > Personally, I can't stand the color fringes on text (on my MacBook),
    > so I have text set to normal anti-aliasing.
    I used to strongly share this opinion, but I've found that as display
    DPIs get higher and higher, it becomes less of an issue. I used a 22"
    Cinema Display for years and years, and with its low DPI, the color
    fringes really did stand out, but on a MacBook Pro 15" screen, you can't
    really see them at all.

    However, there are still long-standing bugs in the LCD antialiasing mode
    which cause light text on dark backgrounds to become extremely "bold;"
    they look radically different from CRT antialiasing mode. (In terms of
    font weight, Windows looks much more like Apple's CRT mode, with or
    without ClearType.) You can see this most easily on web sites which use
    dark backgrounds, such as www.blizzard.com. The radar's been filed for a
    few OS generations now… I guess it wasn't considered a major problem.
  • > However, there are still long-standing bugs in the LCD antialiasing mode
    > which cause light text on dark backgrounds to become extremely "bold;"
    > they look radically different from CRT antialiasing mode. (In terms of
    > font weight, Windows looks much more like Apple's CRT mode, with or
    > without ClearType.) You can see this most easily on web sites which use
    > dark backgrounds, such as www.blizzard.com. The radar's been filed for a
    > few OS generations now… I guess it wasn't considered a major problem.

    Not fixed != not considered to be a problem.

    We did attempt to address this in some restricted cases for Leopard.
    The LCD text rendering algorithm still produces less than ideal output
    when rendering text onto a dark background, but when AppKit has enough
    information to (reasonably cheaply) know that that's what's happening,
    it uses some private tweaks to advise CG to use a slightly different
    algorithm.

    In particular, when text is drawn via a cell, and the cell's
    interiorBackgroundStyle is NSBackgroundStyleDark, the LCD rendered
    text will not look quite so bold.

    Eyeball, say, text in selected rows in Mail.

    Ken Ferry
    Cocoa Frameworks

    On Jan 9, 2008 9:28 AM, John Stiles <JStiles...> wrote:
    > Jonathon Mah wrote:
    >> Personally, I can't stand the color fringes on text (on my MacBook),
    >> so I have text set to normal anti-aliasing.
    > I used to strongly share this opinion, but I've found that as display
    > DPIs get higher and higher, it becomes less of an issue. I used a 22"
    > Cinema Display for years and years, and with its low DPI, the color
    > fringes really did stand out, but on a MacBook Pro 15" screen, you can't
    > really see them at all.
    >
    > However, there are still long-standing bugs in the LCD antialiasing mode
    > which cause light text on dark backgrounds to become extremely "bold;"
    > they look radically different from CRT antialiasing mode. (In terms of
    > font weight, Windows looks much more like Apple's CRT mode, with or
    > without ClearType.) You can see this most easily on web sites which use
    > dark backgrounds, such as www.blizzard.com. The radar's been filed for a
    > few OS generations now… I guess it wasn't considered a major problem.
    >
  • On Jan 9, 2008, at 5:23 PM, Ken Ferry wrote:

    >> However, there are still long-standing bugs in the LCD antialiasing
    >> mode
    >> which cause light text on dark backgrounds to become extremely
    >> "bold;"
    >> they look radically different from CRT antialiasing mode. (In terms
    >> of
    >> font weight, Windows looks much more like Apple's CRT mode, with or
    >> without ClearType.) You can see this most easily on web sites which
    >> use
    >> dark backgrounds, such as www.blizzard.com. The radar's been filed
    >> for a
    >> few OS generations now… I guess it wasn't considered a major problem.
    >
    > Not fixed != not considered to be a problem.
    >
    > We did attempt to address this in some restricted cases for Leopard.
    > The LCD text rendering algorithm still produces less than ideal output
    > when rendering text onto a dark background, but when AppKit has enough
    > information to (reasonably cheaply) know that that's what's happening,
    > it uses some private tweaks to advise CG to use a slightly different
    > algorithm.
    >
    > In particular, when text is drawn via a cell, and the cell's
    > interiorBackgroundStyle is NSBackgroundStyleDark, the LCD rendered
    > text will not look quite so bold.

    Thanks for pointing those APIs out.  While my custom UI framework is
    currently baselined to 10.4, I will definitely add these APIs on my to-
    do list when I next re-baseline.

    I have quite a bit of custom cells that will render text onto
    different colored backgrounds. To me, the current text looks great,
    but it will be interesting to check out any visual differences with
    those hints in place.

    ___________________________________________________________
    Ricky A. Sharp        mailto:<rsharp...>
    Instant Interactive(tm)  http://www.instantinteractive.com
  • On 9 Jan 2008, at 23:23, Ken Ferry wrote:

    >> However, there are still long-standing bugs in the LCD antialiasing
    >> mode
    >> which cause light text on dark backgrounds to become extremely
    >> "bold;"
    >> they look radically different from CRT antialiasing mode.
    >
    > Not fixed != not considered to be a problem.
    >
    > We did attempt to address this in some restricted cases for Leopard.

    All of this will presumably go away eventually as the display
    resolution of output devices increases.  iPhone doesn't seem to bother
    with it, for instance, and at 160dpi most people seem to agree that
    the text on iPhone's display is very readable.

    I don't think I'd waste too much time on trying to make the system do
    sub-pixel text rendering in situations where it presently doesn't.
    And it certainly doesn't make sense trying to extend this
    functionality to non-text-rendering, not with high-DPI displays just
    around the corner.

    Kind regards,

    Alastair.

    --
    http://alastairs-place.net
previous month january 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