Re: Unwanted clip of rotated text
Re: Unwanted clip of rotated text
- Subject: Re: Unwanted clip of rotated text
- From: Erez Anzel <email@hidden>
- Date: Mon, 23 Feb 2004 22:03:07 -0500
The workaround works! My solution is below, for anyone who wants (to
use or to improve; I'm not proud).
Thanks, Allan, for hinting that I should stop banging my head against
the wall. If you had problems with undesired clipping as well, then I
should look elsewhere. You pointed me towards ATSU, which smelled of
Carbon. So instead I looked to the CircleView example from Apple
(AppKit), and "simplified" it. It sure is "expensive" code just to
rotate text 90 degrees; maybe one day Cocoa will have something
simpler, waiting for the taking.
My original code is in my initial posting, from earlier today. That
didn't work. I have just submitted a bug report to Apple (Problem ID
#3568051).
Here is the method which I wrote for my NSRulerView subclass. It ain't
pretty, but it works. Maybe it will help reduce the bruising in someone
else's head. I left in most of the comments from the CircleView
example.
Bye...Erez
*********************************************
*********************************************
- (void)drawRotatedString:(NSString *)inNSString
atNSPoint:(NSPoint)inNSPoint
{
NSTextStorage *textStorage = [[NSTextStorage alloc]
initWithString:inNSString];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer *textContainer = [[NSTextContainer alloc] init];
[layoutManager addTextContainer:textContainer];
[textContainer release]; // The layoutManager will retain the
textContainer
[textStorage addLayoutManager:layoutManager];
[layoutManager release]; // The textStorage will retain the
layoutManager
// Screen fonts are not suitable for scaled or rotated drawing.
// Views that use NSLayoutManager directly for text drawing should
// set this parameter appropriately.
[layoutManager setUsesScreenFonts:NO];
unsigned glyphIndex;
NSRange glyphRange;
NSRect usedRect;
// Note that usedRectForTextContainer: does not force layout, so it
must
// be called after glyphRangeForTextContainer:, which does force
layout.
glyphRange = [layoutManager
glyphRangeForTextContainer:textContainer];
usedRect = [layoutManager usedRectForTextContainer:textContainer];
NSGraphicsContext *context = nil;
NSRect lineFragmentRect;
NSPoint viewLocation;
NSPoint layoutLocation;
NSAffineTransform *transform = nil;
for (glyphIndex = glyphRange.location; glyphIndex <
NSMaxRange(glyphRange); glyphIndex++) {
context = [NSGraphicsContext currentContext];
lineFragmentRect = [layoutManager
lineFragmentRectForGlyphAtIndex:glyphIndex effectiveRange:NULL];
layoutLocation = [layoutManager locationForGlyphAtIndex:glyphIndex];
transform = [NSAffineTransform transform];
// Here layoutLocation is the location (in container coordinates)
where the glyph was laid out.
layoutLocation.x += lineFragmentRect.origin.x;
layoutLocation.y += lineFragmentRect.origin.y;
viewLocation.x = inNSPoint.x;
viewLocation.y = inNSPoint.y + layoutLocation.x;
// We use a different affine transform for each glyph, to position
and rotate it
// based on its calculated position around the circle.
[transform translateXBy:viewLocation.x yBy:viewLocation.y];
[transform rotateByDegrees:90.0];
// We save and restore the graphics state so that the transform
applies only to this glyph.
[context saveGraphicsState];
[transform concat];
// drawGlyphsForGlyphRange: draws the glyph at its laid-out location
in container coordinates.
// Since we are using the transform to place the glyph, we subtract
the laid-out location here.
[layoutManager drawGlyphsForGlyphRange:NSMakeRange(glyphIndex, 1)
atPoint:NSMakePoint(-layoutLocation.x, -layoutLocation.y)];
[context restoreGraphicsState];
} // for (glyphIndex...)
}
*********************************************
*********************************************
On 23-Feb-04, at 1:27 PM, Allan Odgaard wrote:
>
On 23. Feb 2004, at 18:58, Erez Anzel wrote:
>
>
> I want to draw rotated strings into a vertical ruler. But the text
>
> appears to get clipped by the unrotated clipping rect.
>
>
>
> I'd be grateful for any solutions, workarounds or hints, such as how
>
> to
>
> rotate the clipping rect (which I'll refer to as clipRect). This must
>
> run on Mac OS X 10.2 minimum. [...]
>
>
Try to draw something else than text, I am almost certain that this
>
will be drawn with the correct clip rect -- the problem is in
>
NSString's drawText, at least I have previously had problems related
>
with this in relation to clip rects and transformations.
>
>
My workaround was not to use NSString's text drawing functions but
>
instead use ATSU. This is a hell of a lot more code, but it works...
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.