Re: Forcing text layout
Re: Forcing text layout
- Subject: Re: Forcing text layout
- From: Keith Blount <email@hidden>
- Date: Fri, 12 Mar 2010 15:58:50 -0800 (PST)
Hi Martin,
Many thanks for your reply (as always :) ). And d'oh, yes, the glyph/character thing was an oversight - I'm usually very careful about this as I am aware they can be very different, so thanks for picking me up on that. Also thanks for the pointer to -ensureLayoutForCharacterRange: - that works perfectly. So my new method looks like this:
- (void)forceInitialLayout
{
NSInteger charIndex = (20000 > [[self textStorage] length] ? [[self textStorage] length] : 20000);
if (charIndex > 0)
{
if(floor(NSAppKitVersionNumber) >= NSAppKitVersionNumber10_5)
{
[layoutManagerensureLayoutForCharacterRange:NSMakeRange(0, charIndex)];
}
else
{
charIndex -= 1;
[layoutManagerlocationForGlyphAtIndex:[layoutManagerglyphIndexForCharacterAtIndex:charIndex]];
}
}
}
I think your explanation of the problem is most likely spot on, too - the text container of the glyph is different after layout has finished. I do have noncontiguous layout enabled, but the above seems to work nicely either way.
Thanks again!
All the best,
Keith
----- Original Message ----
From: Martin Wierschin <email@hidden>
To: Keith Blount <email@hidden>
Cc: Cocoa Developers <email@hidden>
Sent: Fri, March 12, 2010 10:52:07 PM
Subject: Re: Forcing text layout
Hello Keith!
> - (void)forceInitialLayout
> {
> NSInteger charIndex = (50000 > [[self textStorage] length] ? [[self textStorage] length] : 50000);
> if (charIndex > 0)
> {
> charIndex -= 1;
> [layoutManagerlocationForGlyphAtIndex:charIndex];
You shouldn't play loose which character/glyph indexes. I'm sure you know, but the mapping between chars/glyphs is arbitrary and your method could theoretically trigger an out-of-bounds exception. In practice it probably won't because I believe the Cocoa typesetter always produces more glyphs than characters (eg: inserting null glyphs as padding).
That point aside, I've also experienced issues with when forcing layout to a specific character index. Notably that the the mapping from characters to glyphs doesn't seem stable until layout is complete. I've seen -[NSLayoutManager numberOfGlyphs] change before/after layout! Perhaps the TextEdit code you quoted fails for that reason, eg: the mapping from the final character index (the image attachment) to its corresponding glyph changes after layout finishes.
My solution was to conditionally use -[NSLayoutManager ensureLayoutForCharacterRange:] when running on Leopard or later. That punts the problem entirely to Apple to solve. Another thing to check: do you have discontinuous layout enabled? I never did, but perhaps that's a factor for you.
~Martin
_______________________________________________
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