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,

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.


// 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.


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.


if (pdfImgRep) [pdfImgRep drawInRect:CGtoNSRect(&cgfr)];
// (the rect is not correct to make the code shorter, but that doesn't matter for testing purposes)
[self drawText:@"Test string" toContext:gcr]; // try to draw the string; only the square is drawn!


    CGContextEndPage(gcr);
    CGContextFlush(gcr);
}

- (void)drawText:(NSString *)tmpStr toContext:(CGContextRef)gcr
{
    ATSUAttributeTag tags[3];
    ByteCount sizes[3];
    ATSUAttributeValuePtr values[3];

    CGContextSetRGBFillColor(gcr, .2, .3, 0, 1);
    CGContextSetRGBStrokeColor(gcr, 0, 0, 1, 1);

    //--- create the style:
    ATSUStyle myTextStyle;
    checkStatus(ATSUCreateStyle(&myTextStyle));

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).


ATSURGBAlphaColor blue = {0,0,1,.5};
tags[0] = kATSUSizeTag;
tags[1] = kATSURGBAlphaColorTag;
tags[2] = kATSUFontTag;
sizes[0] = sizeof(Fixed);
sizes[1] = sizeof(ATSURGBAlphaColor);
sizes[2] = sizeof(ATSUFontID);
values[0] = &atsuSize;
values[1] = &blue;
values[2] = &font;
checkStatus(ATSUSetAttributes(myTextStyle, 3, tags, sizes, values));


//--- create the ATSUTextLayout:
ATSUTextLayout myTextLayout;
checkStatus(ATSUCreateTextLayout(&myTextLayout));
tags[0] = kATSUCGContextTag;
sizes[0] = sizeof(CGContextRef);
values[0] = &gcr;
checkStatus(ATSUSetLayoutControls (myTextLayout, 1, tags, sizes, values));

Most ATSUI commands require that you set the text which you want to draw before you call them, because they may need to inspect the text in order to make a decision what to do next. One such example is the ATSUSetTransientFontMatching() function. Therefor, you should move the code which sets the text pointer location of the text layout object up here:


//--- add the text to the ATSUTextLayout:
UniCharCount length = [tmpStr length];
UniCharArrayPtr theUnicodeText = (UniCharArrayPtr) NewPtr(length*sizeof(UniChar));
[tmpStr getCharacters:theUnicodeText];
checkStatus(ATSUSetTextPointerLocation(myTextLayout, theUnicodeText,
kATSUFromTextBeginning, kATSUToTextEnd, [tmpStr length]));


After that you can continue with the text layout configuration code.

    checkStatus(ATSUSetTransientFontMatching(myTextLayout, true));

//--- add the style information to the ATSUTextLayout:
checkStatus(ATSUSetRunStyle(myTextLayout, myTextStyle, kATSUFromTextBeginning, kATSUToTextEnd));


//--- add the text to the ATSUTextLayout:
UniCharCount length = [tmpStr length];
UniCharArrayPtr theUnicodeText = (UniCharArrayPtr) NewPtr(length*sizeof(UniChar));
[tmpStr getCharacters:theUnicodeText];
checkStatus(ATSUSetTextPointerLocation(myTextLayout, theUnicodeText,
kATSUFromTextBeginning, kATSUToTextEnd, [tmpStr length]));


//--- now draw the text: (twice at different coordinates just to make sure)
Fixed x = 100;
Fixed y = x;

Another opportunity to put IntToFixed() to work.

checkStatus(ATSUDrawText (myTextLayout, kATSUFromTextBeginning, kATSUToTextEnd, (Fixed)-20, (Fixed)-20));
checkStatus(ATSUDrawText (myTextLayout, kATSUFromTextBeginning, kATSUToTextEnd, x, y));


CGContextStrokeRect(gcr, CGRectMake(100.,100.,200.,200.)); // just to test if ANYTHING is drawn

    CGContextFlush(gcr);
    ATSUDisposeStyle(myTextStyle);
    ATSUDisposeTextLayout(myTextLayout);
    DisposePtr((Ptr)theUnicodeText);
}

void checkStatus(OSStatus status)
{    // log if an error occurred
    if (status) NSLog(@"%i", status);
}

NSRect CGtoNSRect(CGRect *r)
{ // convert an CGRect to an NSRect:
return NSMakeRect(r->origin.x, r->origin.y, r->size.width, r->size.height);
}

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:


- (void)drawText:(NSString *)tmpStr
{
   NSMutableDictionary * atts = [NSMutableDictionary dictionary];

[atts setObject: [NSFont fontWithName: @"Times Roman" size: 72] forKey: NSFontAttributeName];
[atts setObject: [NSColor blueColor] forKey: NSForegroundColorAttributeName];


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


Regards,

Dietmar Planitzer

_______________________________________________
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>)



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.