BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 12-08-2008, 10:03 AM   #1
Massif
Knows Where the Search Button Is
 
Join Date: May 2008
Model: 8330
PIN: N/A
Carrier: Telus
Posts: 26
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, 10:15 AM   #2
Ivanov
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
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, 12:38 PM   #3
Mark Rejhon
Retired BBF Moderator
 
Mark Rejhon's Avatar
 
Join Date: Aug 2004
Location: Ottawa, Ontario, Canada
Model: Bold
Carrier: Rogers
Posts: 4,870
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 12:40 PM..
Offline  
Old 12-08-2008, 03:01 PM   #4
Dan East
Thumbs Must Hurt
 
Join Date: Apr 2008
Model: 8130
PIN: N/A
Carrier: US Cellular
Posts: 82
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


ENDOXPERT GASTROENTROLOGY MEMORY BASKET BRAND NEW picture

ENDOXPERT GASTROENTROLOGY MEMORY BASKET BRAND NEW

$118.36



2 - RAM DRAM Tray-Container Box For Server PC Memory DIMM Modules - Fits 100 NEW picture

2 - RAM DRAM Tray-Container Box For Server PC Memory DIMM Modules - Fits 100 NEW

$21.90



2 Computer Memory Packaging Tray Case for Desktop PC DDR4 Modules - Fits 100 New picture

2 Computer Memory Packaging Tray Case for Desktop PC DDR4 Modules - Fits 100 New

$21.50



Desktop Memory Case Tray Case for PC DDR DRAM RAM DIMM Modules - 2 fits 100 New picture

Desktop Memory Case Tray Case for PC DDR DRAM RAM DIMM Modules - 2 fits 100 New

$20.50



Okuma E4809-045-091-E OPUS 5000II Board CMOS Memory E4809-045-167-C TCC-A 112-C picture

Okuma E4809-045-091-E OPUS 5000II Board CMOS Memory E4809-045-167-C TCC-A 112-C

$225.00



We R Makers The Cinch Book Binding Machine Round hole V 2 White Aqua 71050-9 picture

We R Makers The Cinch Book Binding Machine Round hole V 2 White Aqua 71050-9

$79.99







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