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.
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!
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).
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.
//--- 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;
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:
_______________________________________________
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