Business as usual: Flipped context causing grief
Business as usual: Flipped context causing grief
- Subject: Business as usual: Flipped context causing grief
- From: Graham Cox <email@hidden>
- Date: Tue, 13 Dec 2011 10:04:12 +1100
I'm drawing objects into a bitmap rep and a context created from it.
The y axis extends downwards (i.e. it's flipped).
Normal objects render correctly.
Text is inverted.
This problem keeps rearing its ugly head. I'm still not sure I really understand it.
When I set up the context from the bitmap, I create a transform that flips the context. Paths and so forth appear the right way up. I'm rendering text using the built-in text rendering engine (NSLayoutManager) and it fails to notice that the context is flipped, so it appears to add its own flipping transform and so text is inverted. I need to let the context I create from the bitmap know that it is flipped, but there is no -setFlipped: method on NSGraphicsContext, and the method that creates a context from a bitmap doesn't let me pass the flippedness, as the method for creating a context from a graphicsPort does.
How do I inform the context that it is flipped, so that text is rendered correctly?
--Graham
My code:
- (NSBitmapImageRep*) bitmapOfSelectedObjectsWithResolution:(NSInteger) dpi
{
NSBitmapImageRep* rep;
NSRect sb = [self selectionBounds];
if( dpi == 0 )
dpi = 72;
NSInteger pixelsWide, pixelsHigh;
pixelsWide = ceil(NSWidth( sb ) * dpi / 72.0 );
pixelsHigh = ceil(NSHeight( sb ) * dpi / 72.0 );
rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:pixelsWide
pixelsHigh:pixelsHigh
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:0
bitsPerPixel:0];
if( rep == nil )
[NSException raise:kDrawkitErrorDomain format:@"bitmap could not be created for selection export"];
NSGraphicsContext* bmContext = [NSGraphicsContext graphicsContextWithBitmapImageRep:rep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:bmContext];
// transform the origin to where the selected objects are, and scale to allow for dpi and flippedness
NSSize scale;
CGFloat yOrigin = -NSMinY( sb );
scale.width = (CGFloat)pixelsWide / NSWidth( sb );
scale.height = (CGFloat)pixelsHigh / NSHeight( sb );
if([[self drawing] isFlipped]) // generally true
{
yOrigin = -NSMaxY( sb );
scale.height *= -1.0;
}
NSAffineTransform* transform = [NSAffineTransform transform];
[transform scaleXBy:scale.width yBy:scale.height];
[transform translateXBy:-NSMinX( sb ) yBy:yOrigin];
[transform concat];
// seems I need to let the context know it is flipped at this point for true rendering of text -- but how????
[self drawSelectedObjects];
[NSGraphicsContext restoreGraphicsState];
return [rep autorelease];
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden