if (getPixelWidth(candidate) <= width) {
// check for line breaks within the provided text
// unfortunately, the BreakIterators are too dumb to tell *why* they consider the
// location a break, so the check needs to be implemented here
final CharacterIterator cit = iter.getText();
if (isHardLineBreak(cit)) {
lines.add(candidate);
previous = iter.current();
best = null;
} else {
best = candidate;
}
} else {
if (best == null) {
// could not break the line - the word's simply too long. Use more force to
// to fit it to the width
best = splitAggressively(candidate, width);
// splitAggressively returns an iterator with its own indexing,
// so instead of using it directly we need to adjust the old one
previous += best.getEndIndex() - best.getBeginIndex();
} else {
previous = best.getEndIndex();
// Trim the trailing white space
char endChar = best.last();
int endIndex = previous;
while (Character.isWhitespace(endChar) && endChar != CharacterIterator.DONE) {
endIndex = best.getIndex();
endChar = best.previous();
}
best = text.getIterator(null, best.getBeginIndex(), endIndex);
}
lines.add(best);
// a special check for a hard line break just after the word
// that got moved to the next line
final CharacterIterator cit = iter.getText();
if (isHardLineBreak(cit)) {
lines.add(text.getIterator(null, previous, iter.current()));
previous = iter.current();
}