02-15-2010 02:10 PM
I have a doubt whether Font.getAdvance(String) uses Font.getAdvance(char) internally for calculations? Coz say i was writing a code to wrap a text and i thought if i wrap text based on words it would be faster so i went with Font.getAdvance(String) but still am not sure whether this function itself uses Font.getAdvance(char) internally for calculation. Plz help me on this.
Thanks a lot,
02-15-2010 02:34 PM
Also is there no native method that can give me the number of characters of a string with a given font that will fit in a given width i know i can achieve this by writing my own logic but it would be nice to know if there is any native method for this plz let me know.
Thanks a lot,
02-15-2010 03:07 PM
My guess is that Font.getAdvance(String) does not rely on Font.getAdvance(char). The String version needs to deal with kerning, combining characters, ligatures, bidirectional text analysis, and possibly other stuff, that the char version can safely ignore.
As to your second question, there is no built-in method that I know of to fit characters to a fixed width. The Graphics class docs make reference to a TextMetrics class, but that class is apparently not exposed (and who knows if it does what you want?) I think that your best bet is, as you suspected, to write your own logic.
02-16-2010 12:00 AM
02-16-2010 12:31 AM
For analyzing a fixed piece of text, my guess is that Font.getAdvance(String) will be faster than repeated calls to Font.getAdvance(char) for each character in the String.
I'm not sure what you mean by "heavy operations". My guess is that both are pretty efficient. I'd be very surprised if the choice of method would make any noticeable difference in the performance of your app. (This depends on the application, of course, so I might be off base here. But I doubt it.) Just pick the one that fits best with the rest of your application logic.
If you are indeed experiencing performance problems, I suggest profiling your code to confirm the source. If you are simply anticipating performance issues, my advice is to not bother. (This advice is based on the old programmer's maxim: "I'd rather optimize debugged code than debug optimized code.")