Re: Faster way to draw strings?
Re: Faster way to draw strings?
- Subject: Re: Faster way to draw strings?
- From: Douglas Davidson <email@hidden>
- Date: Wed, 23 Jan 2002 09:57:50 -0800
On Wednesday, January 23, 2002, at 05:03 AM, Stephan R. Cleaves wrote:
I've been having difficulties with redraw speed in my application
and have narrowed the problem down to NSGraphics drawAttributedString
method. The problem is most noticeable during resizing of the window
which contains my custom view. What I have done is turn off the drawing
of the strings during resize and i can achieve acceptable redraw
speeds, then when the live resize ends I turn back on the strings. This
looks a bit odd, but currently its the only solution I have. I have
over 100 unique strings being drawn in the view, and I do make sure
only to draw the requested region, but often that is the entire view
area during live resize. Is there a faster way to draw strings? Other
than using the font attribute, the over head off the
NSAttributedStrings is completely unnecessary for my application. I
Actually use NSMutableAttributedStrings and pre-cache these, though
doing so is not noticeably faster than just using a single
NSMutableAttributedString in drawRect and changing its string reference
string value.
There is a faster way to draw strings; as a matter of fact, we gave an
example of this at last year's WWDC. The problem with the string
drawing routines is that they generate glyphs for the string and lay
them out, then throw that information away after the string has been
drawn. All of that work has to be redone when the string is drawn
again. You can see significant improvements by preserving this
information instead in your own NSLayoutManager. For ~100 strings you
may wish to use a single NSLayoutManager rather than a separate
NSLayoutManager per string; you can do this by concatenating the
individual strings into the NSTextStorage, separated by hard line
breaks, and maintaining an index of the subranges they occupy. The
exact procedure used would depend a bit on what you know about your
strings--for example, whether they might contain line breaks within
themselves, whether they are to be allowed to wrap, etc. The CircleView
example demonstrates basic NSLayoutManager-based drawing, although in
that case the drawing is done glyph by glyph rather than by ranges of
glyphs, as you would probably wish to do it. If you need help with
this, you can probably get it on this list.
Douglas Davidson