View Single Post
Old 12-08-2008, 03:01 PM   #4 (permalink)
Dan East
Thumbs Must Hurt
 
Join Date: Apr 2008
Model: 8130
PIN: N/A
Carrier: US Cellular
Posts: 82
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've done this type of coding before for a general purpose GUI framework for PC and Windows Mobile. The implementation can get complex quickly. In my case I support rich-text, based on a very primitive html-like syntax (allowing font attributes, word breaks, images, etc). As far as performance, I only perform text layout once, during which I generate a set of draw commands. Essentially each call to a Graphics drawing routine is cached as a command, with all associated parameters you would normally pass. The only time a new layout needs to occur is if your text is changed or the control is resized. You do not need to do a new layout while scrolling - simply offset your drawing origin based on the scroll position.

As Mark suggested, estimation can vastly increase performance. For estimation use the average character width. I don't see an api to determine that exactly, so instead you can use getAdvance and pass it a string like "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ0123456789" and throw in whatever other symbols (don't forget the space character) you expect to encounter, then divide the resulting width by the number of letters in your string.
Offline   Reply With Quote