Re: Cocoa text via Quartz CGLayer
Re: Cocoa text via Quartz CGLayer
- Subject: Re: Cocoa text via Quartz CGLayer
- From: Sander Stoks <email@hidden>
- Date: Sat, 6 Feb 2010 11:26:28 +0100
Thanks for looking at this David,
The "selection" isn't really there in my application; I added this later to the screen shots to show more clearly what was going on. For those reading along who don't want to download the screen shot, here's what's happening:
I print the string "abcabcabcabcabc" 25 times, each time with an extra period appended, like so:
abcabcabcabcabc
abcabcabcabcabc.
abcabcabcabcabc..
abcabcabcabcabc...
etc.
The effect I'm seeing is that the "abcabcabcabcabc" part of the string doesn't have the same width when I draw the text via an intermediate CGLayer. The "abcabcabcabcabc" in the last line is a few pixels "tighter" than in the first line (and it's a gradual effect).
I can understand that text rendering is (subtly) different depending on the context I'm drawing to, so I could understand that the "direct" version has different (sub-pixel) anti-aliasing and even (as a consequence) different spacing. (By the way, given your explanation I would expect the spacing differences in the "direct" version and not in the CGLayer version.) However, the point is that the spacing is inconsistent _within_ the same context. It sounds nit-picky, but in my "real" app the text is editable, and it is very noticeable that while typing, the characters to the left of the cursor "jitter around".
I tried setting CGContextShouldAntialias and CGContextShouldSmoothFonts to NO (in all permutations) and the effect is still there.
In case it matters, I am seeing the exact same behavior on an Intel MBP with 10.6 and on a PPC iBook G4 with 10.5.
Thanks,
Sander
On Feb 5, 2010, at 9:06 PM, David Duncan wrote:
> On Feb 5, 2010, at 7:46 AM, Sander Stoks wrote:
>
>> I want to draw rotated text, and I found out that the regular Cocoa Text drawing in that case looks surprisingly bad. I posted about this before, and got the suggestion of drawing to an offscreen bitmap instead, and rotating that. That works, but when I'm editing the text I see the text which is already there "jitter" on screen. I pruned this down to a minimal project which shows the problem. This can be downloaded at http://www.stoks.nl/TextTest.zip - it only contains a single NSView subclass which has selectable code paths (via an #if 0) for either drawing directly using NSLayoutManager's drawGlyphsForGlyphRange. This looks fine (as long as it's not rotated). The other path uses a CGLayer, sets the current NSGraphicsContext so it points to this layer, use drawGlyphsForGlyphRange again, and finally CGContextDrawLayerAtPoint to display the resulting text.
>>
>> As you can see in http://www.stoks.nl/TextCompared.png, the text drawn via the intermediate CGLayer "shrinks" its intra-glyph-spacing as the string gets longer.
>
>
> Its hard to tell because your text is selected in your screen shot, but I would guess that this has to do with subpixel antialiasing. By default your text will be drawn without a background. When you draw directly into the NSView however, there is an established background, that of the window, and so the text can be blended into it with subpixel antialiasing. When you draw to the CGLayer however, there is no such background, so the text is drawn without.
>
> This typically manifests as the text not appearing as if it has the right weight (appears thin or not bold enough), although it is harder to tell in your case due to the selection area. If you see colored highlights when you zoom in on the image in one but not the other, then it is the issue I'm describing.
> --
> David Duncan
> Apple DTS Animation and Printing
>
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden