Mailing Lists: Apple Mailing Lists

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

ATSUI text rendering



Hi,

I'm having a problem writing text to a Quartz graphics context in a view using ATSUI: there is no text displayed, regardless of the kind of graphics context I use (I tried it with a PDFContext or the View itself). I added a blue square just to test if at least anything is drawn, but this is displayed correctly in both cases - just no text.
Here is my code; is there any obvious error I just don't see? If anybody has a hint, I'd really appreciate that...


- (void)drawRect:(NSRect *)rect
// the drawRect method of my custom view
{
float frh, frw;
NSRect fr = [self frame];
frh = fr.size.height;
frw = fr.size.width;
CGRect cgfr = CGRectMake(0, 0, frw, frh);
CGContextRef gcr = [[NSGraphicsContext currentContext] graphicsPort];
CGContextBeginPage(gcr, &cgfr);


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;
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));
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;
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);
}



_______________________________________________ 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


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.