I am drawing an NSAttributedString into a bitmap image and uploading to OpenGL
as a texture. The strings origin is animated to mimic a scroll view, so speed
is of the essence. Considering my need for animation I have migrated to
NSLayoutManager / NSTextContainer and NSTextStorage, as opposed simply drawing
with NSAttributedString drawWithRect: options, which has been a *huge*
performance gain, but now I have some layout inconsistencies.
I have noticed that I have lost paragraph justification in the process of my
migration/optimization with NSLayoutManager. I am drawing using
drawGlyphsForGlyphRange: atPoint. Is there another mechanism that I need to
trigger/calculate to ensure justification is correct? I had assumed this was
part of the layout process that drawGlyphsForGlyphRange states it handles,
which is apparently incorrect.
My drawing code is as follows, with only the relevant bitmap and string drawing
code left in:
[NSGraphicsContext saveGraphicsState];
// Must supply flipped context for NSLayoutManagers drawing.
NSGraphicsContext* flipped = [NSGraphicsContext
graphicsContextWithBitmapImageRep:bitmapImage];
flipped = [NSGraphicsContext
graphicsContextWithGraphicsPort:[flipped graphicsPort] flipped:YES];
[NSGraphicsContext setCurrentContext:flipped];
NSAffineTransform *transform = [NSAffineTransform transform];
[transform translateXBy:0 yBy:h];
[transform scaleXBy:1.0 yBy:-1.0];
[transform concat];
// This is slower. Layout Manager is the way to go.
// This nets us 80% CPU for our test text - however
justification is correct.
//[self.drawString drawWithRect:rect
options:NSStringDrawingUsesLineFragmentOrigin];
// This might be optimizable more? This gets us ~ 40% CPU for
our test text
//NSRange glyphRange = [self.layoutManager
glyphRangeForTextContainer:self.textContainer];
// Only give us the glyphs we need for our rect. ~15 - 30% CPU
for our test text
NSRange glyphRange = [self.layoutManager
glyphRangeForBoundingRect:rect inTextContainer:self.textContainer];
// Possibly even better?
//NSRange glyphRange = [self.layoutManager
glyphRangeForBoundingRectWithoutAdditionalLayout:rect
inTextContainer:self.textContainer];
[self.layoutManager drawBackgroundForGlyphRange:glyphRange
atPoint:drawPoint];
[self.layoutManager drawGlyphsForGlyphRange: glyphRange
atPoint:drawPoint];
[NSGraphicsContext restoreGraphicsState];
Thank you for any information.
_______________________________________________
Cocoa-dev mailing list ([email protected])
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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]