Re: NSLayoutManager and avoiding widows and orphans
Re: NSLayoutManager and avoiding widows and orphans
- Subject: Re: NSLayoutManager and avoiding widows and orphans
- From: Ross Carter <email@hidden>
- Date: Fri, 9 Nov 2007 11:32:59 -0500
I got widow-orphan protection working by subclassing the
NSATSTypesetter and overriding beginParagraph,
willSetLineFragmentRect:forGlyphRange:usedRect:baselineOffset:, and
endParagraph. It was kind of complicated, and I don't have the code
here, and frankly I don't remember how thoroughly I debugged it before
setting the project aside.
Basically, instead of modifying the text container height, I used
those methods to test for a widow/orphan condition and if true set a
variable specifying the first glyph that should be thrown to the next
container. In endParagraph, if that variable is set, I forced relayout
and on the next pass through
willSetLineFragmentRect:forGlyphRange:usedRect:baselineOffset: I
increased the the height of lineRect and usedRect so the typesetter on
its own would throw the line to the next container.
It was complicated because I had to turn off textView drawing to
eliminate flicker during relayout, but you might be able to figure out
a better implementation of this idea.
Ross
On Nov 7, 2007, at 6:06 PM, Keith Blount wrote:
Hi Martin,
Thanks for your help (again). Unfortunately, no, this frame
adjustment isn't triggered from the delegate methods - I tried to
shift everything out of there for the exact reasons you describe.
The only thing I do in the delegate method is make note of whether I
*need* to adjust the frame. Then I wait for all layout to finish,
and only once that's done do I adjust the frame. Then I trigger all
layout again, at the end of which I check to see if another
adjustment is needed. This goes on until the delegate method doesn't
return any more adjustments to be made. But I'm careful not to
trigger any extra layout from within the delegate method. The hang
happens when the frame change forces the delegate method to add
another page. For some reason, this seems to cause everything to go
crazy, but I'm not sure why...
Right now I may just have to scrap widow and orphans support from
printing in my app altogether. The layout manager just seems too
delicate, triggering hangs and crashes that are incredibly difficult
to track down.
Thanks again and all the best,
Keith
----- Original Message ----
From: Martin Wierschin <email@hidden>
To: Keith Blount <email@hidden>
Cc: email@hidden
Sent: Wednesday, November 7, 2007 1:24:04 AM
Subject: Re: NSLayoutManager and avoiding widows and orphans
Hi Keith,
So, next I tried waiting until all layout is complete and then
enumerating through the text containers, checking for a widow or
orphan at the end of each one, adjusting the frame and then forcing
layout. But again, if a page gets added because of this, we hit
problems - bounds and run storage exceptions, and problems caused by
adding a text container whilst enumerating through the text
containers.
Do you trigger this frame adjusting from the NSLayoutManager
delegate methods? In my experience it is not safe to modify text
container geometry/size from this callback. Try this and see if it
fixes your crash: after you detect that layout has completed
schedule your fixing code via
"performSelector:withObject:afterDelay:".
~Martin
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
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
_______________________________________________
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