BlackBerry Forums Support Community

BlackBerry Forums Support Community (http://www.blackberryforums.com/index.php)
-   Developer Forum (http://www.blackberryforums.com/forumdisplay.php?f=15)
-   -   Word Wrap using Graphics.drawText? (http://www.blackberryforums.com/showthread.php?t=163989)

Massif 12-08-2008 10:03 AM

Word Wrap using Graphics.drawText?
 
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?

Ivanov 12-08-2008 10:15 AM

use getFont().getAdvance(your_string_var) to find out the length of the string using the Font set for your field

Mark Rejhon 12-08-2008 12:38 PM

Quote:

Originally Posted by Massif (Post 1200951)
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...

Dan East 12-08-2008 03:01 PM

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.


All times are GMT -5. The time now is 03:46 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.