Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ATSUI text rendering



Hi,

On 15. Mai 2005, at 23:51, Dietmar Planitzer wrote:
there are a couple of issues with your code which together are probably the reason why it doesn't work as expected.

On May 15, 2005, at 9:17 PM, Bernhard Barkow wrote:

- (void)drawRect:(NSRect *)rect


The AppKit passes the dirty rect by value - not by reference. Thus, this should be changed to -(void)drawRect: (NSRect)rect.

Wow, I didn't notice that...

// the drawRect method of my custom view
{
    float frh, frw;
    NSRect fr = [self frame];


The -frame method returns the view's bounding rectangle in terms of the parent view's coordinate system. That's why most often what you want here is the -bounds method which returns the view's bounding rectangle in terms of its own coordinate system. The important difference is that the origin of -frame is the origin of the view relative to the parent view, while the origin of -bounds is de facto always (0, 0), except when scrolling comes into play.

Ok, this has no effect in my special case, because I do a transformation anyway, but it's good to keep in mind...


frh = fr.size.height;
frw = fr.size.width;
CGRect cgfr = CGRectMake(0, 0, frw, frh);
CGContextRef gcr = [[NSGraphicsContext currentContext] graphicsPort];
CGContextBeginPage(gcr, &cgfr);



Normally, calling CGContextBeginPage, CGContextEndPage and CGContextFlush from inside a -drawRect: method is not necessary, as this stuff is already handled by the AppKit. You just need to implement the code which does the actual drawing. AppKit automatically makes sure that the graphic commands are only flushed if really necessary, thus potentially improving drawing performance.



[...]

ATSUFontID font;
checkStatus(ATSUFindFontFromName("Times Roman", 11, kFontFullName, kFontNoPlatform, kFontNoScript, kFontNoLanguage, &font));
Fixed atsuSize = (Fixed)72;



The fixed data type is special in the sense that the upper bits hold the integer part of the fractional number while the lower bits hold the fractional part. Thats why trying to assign an integer straight to a fixed won't work as expected. Use the IntToFixed function / macro instead: Fixed atsuSize = IntToFixed(72).

Ah, I think that was the main culprit! These are my first experiments with Quartz, I guess I have to aquire a lot more background knowledge. I changed everything to Long2Fix(), and now it works perfectly.


Now, playing devils advocate for a second: a much simpler way to draw Unicode text in Cocoa is to take advantage of the string drawing methods which the AppKit adds to the NSString class via a category. Thus, all of the above code could be replaced with something like this:

[...]
   [tmpStr drawAtPoint: NSMakePoint(20, 20) withAttributes: atts];
}


Of course you are right, this is a much more elegant approach; it's just a matter of performance: I have to draw about 20-30 strings in similar style, so the ATSUI-approach seems to be more efficient (although the user won't probably notice the difference on a modern machine...).


Thanks a lot!

Bernhard Barkow

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Quartz-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/quartz-dev/email@hidden

This email sent to email@hidden
References: 
 >ATSUI text rendering (From: Bernhard Barkow <email@hidden>)
 >Re: ATSUI text rendering (From: Dietmar Planitzer <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.