# How to force a line break before the last word of a line reach the edge?

I'm printing a sliding text by adding each letter after a short period of time. My problem is that when the text multilines, the last word of a line starts on the current line and jumps to the next when it grows too big to fit, making for an ugly effect.

So I guess I need to determine the word where the line would break and replace the previous space with line break to force the break before the word reach the edge. I'm failing to see how to do that. In addition, if there is any way to make the libgdx BitmapFont calculate that I would love to learn.

• BitmapFonts have a getBounds method: (libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/graphics/g2d/… You could use it to check when a line goes over the wrap width. – Anko Mar 6 '13 at 16:47
• @Anko yep, and I can use that position find the next white space. The problem is, I need the previous white space, and from there calc again for the second line until the text is over. Any way I try visualize that seems too complex and get me cross eyed =P. Text has been draw before, therefore someone is bound to know how to do this. – petervaz Mar 6 '13 at 17:11

Basically, the algorithm is as follows:

1. Set maxBreak to the wrap length.
2. If string is shorter than max break, return string.
3. Set nextBreak to maxBreak and newString to ""and lastBreak to 0
4. Check if char at nextBreak is a break char (space, new line, etc.)
5. If char is not a break char, subtract one from nextBreak and repeat step 4
6. If char none of the chars are break chars, you must split the string, so do so at wrap length, so set nextBreak to wrap length
7. Add substring from lastBreak -> nextBreak and a new line char to newString
8. Set lastBreak = nextBreak and add wrap length to nextBreak
9. Go back to step 4 until nextBreak is larger than the original string length.

In fewer words. You want to start at the maximum wrap length and work backwards until you find a break point. When you find it, insert a newline character. Repeat that until you've reached then end of your original string.

Now, in code:

public static String wrapString(String string, int charWrap) {
int lastBreak = 0;
int nextBreak = charWrap;
if (string.length() > charWrap) {
String setString = "";
do {
while (string.charAt(nextBreak) != ' ' && nextBreak > lastBreak) {
nextBreak--;
}
if (nextBreak == lastBreak) {
nextBreak = lastBreak + charWrap;
}
setString += string.substring(lastBreak, nextBreak).trim() + "\n";
lastBreak = nextBreak;
nextBreak += charWrap;

} while (nextBreak < string.length());
setString += string.substring(lastBreak).trim();
return setString;
} else {
return string;
}
}


Where string is the string you want to break and charWrap is the maximum number of characters allowed per line.

To find out what charWrap should be, you'll likely want to find the average width in pixels of your characters, then see how many you can fit in the space you have available.

• That worked beautyfully. I'm getting the value of charWrap by calling font.computeVisibleGlyphs(text, 0, text.length(), WIDTH), where WIDTH is the screen available size (changes with resize), so I don't need to guess the amount chars, at least for the first line, still working on it =). You code worked perfectly as is, and was a nice extra mile after showing the algorithm. Thanks. – petervaz Mar 6 '13 at 19:12
• Thanks. I wrote the code for my game, so it had been tested before :) I dislike code only responses, so I wanted to ensure that it was sufficiently explained. Glad it worked for you. – MichaelHouse Mar 6 '13 at 19:42