BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 12-08-2008, 09:03 AM   #1 (permalink)
Knows Where the Search Button Is
 
Join Date: May 2008
Model: 8330
PIN: N/A
Carrier: Telus
Posts: 26
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Word Wrap using Graphics.drawText?

Please Login to Remove!

I'm trying to implement word wrap functionality using Graphics.drawText. Is there any way to measure the width (in pixels) of a string using a particular font? Or will I have to measure each character's width and calculate it that way?
Offline  
Old 12-08-2008, 09:15 AM   #2 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

use getFont().getAdvance(your_string_var) to find out the length of the string using the Font set for your field
__________________
Blessed is the end user who expects nothing, for he/she will not be disappointed. (Franklin's Rule)
Offline  
Old 12-08-2008, 11:38 AM   #3 (permalink)
Retired BBF Moderator
 
Mark Rejhon's Avatar
 
Join Date: Aug 2004
Location: Ottawa, Ontario, Canada
Model: Bold
Carrier: Rogers
Posts: 4,870
Post Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Massif View Post
I'm trying to implement word wrap functionality using Graphics.drawText. Is there any way to measure the width (in pixels) of a string using a particular font? Or will I have to measure each character's width and calculate it that way?
The last poster is correct. The getFont().getAdvance() is the way to determine whether your string needs to wordwrap. Usually by adding 1 character at a time to a line buffer, until it no longer fits. Then you split the string away from the rest of the string, up to the last space (for complete word). Then repeat this task with the remaining text (after the split point), as many times as needed, to do subsequent lines of text...

In the case of wrapping huge numbers of lines (i.e. split a single string into hundreds of lines of text), that can get pretty slow. Large blocks of text (especially tens of thousands of characters) can be a performance bottleneck when doing wordwrapping. Some known optimization techiques: Yes, measure each character's width and put it into a lookup table based on unicode value. Best done on the fly (first time a specific character is encountered). Total them up until it all fits, this is less expensive performance-wise than making a call to getFont().getAdvance() every single character. Another optimization technique when displaying huge amounts of text, is to use an optimized algorithm (i.e. begin searching for the wrap point beginning approximately close to an estimated wordwrapping point, then going backwards/forwards based on whether that amount of text wordwraps or not. If needed, one could even use a binary search algorithm.) for the wordwrap point in the string, this does raise programming complexity but speeds up wordwrapping for hundreds of lines, on a huge string.

Make sure to include handling for words that are longer than the wordwrap line -- often should simply be trunctated and put to the next line. If the info is non-critical (i.e. popup tooltip and complete text otherwise accessible by a different detailed screen, etc), you can just put an ellipsis at the end of the line...
__________________
Thanks,
Mark Rejhon
Author of XMPP extension XEP-0301:
www.xmpp.org/extensions/xep-0301.html - specification
www.realjabber.org - open source

Last edited by Mark Rejhon : 12-08-2008 at 11:40 AM.
Offline  
Old 12-08-2008, 02:01 PM   #4 (permalink)
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  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.