/** * Split a long line into several lines. * * @param origLine the original line * @return a string with new-line characters at the line-split locations. */ protected String splitLine(String origLine) { StringBuilder newLines = new StringBuilder(); // Determine the line's current indent. Any indent added below must be added to it. String currentIndent = ""; for (int i = 0; i < origLine.length(); i++) { if (origLine.charAt(i) == '\u00a0') currentIndent += HARD_SPACE; else break; } // Add the words of the line to a line builder until adding a word would exceed the max allowed // length. StringBuilder line = new StringBuilder(currentIndent); String[] words = Util.splitWords(origLine, "[\u00a0 ]"); // either hard or soft space for (String word : words) { if (line.length() + 1 + word.length() + currentIndent.length() > this.maxLineLength) { if (newLines.length() == 0) currentIndent += INDENT; // indent continuation lines newLines.append(line.toString()); line = new StringBuilder("\n").append(currentIndent); } // Add a space in front of the word if it's not the first word. if (!line.toString().endsWith(HARD_SPACE)) line.append(HARD_SPACE); line.append(word); } // Add the final words to the split lines. if (line.length() > 1) newLines.append(line.toString()); return newLines.toString(); }