Code: Select all
public static TextBlock createTextBlock(final String text, final Font font,
final Paint paint, final float maxWidth, final int maxLines,
final TextMeasurer measurer) {
final TextBlock result = new TextBlock();
final BreakIterator iterator = BreakIterator.getLineInstance();
iterator.setText(text);
int current = 0;
int lines = 0;
final int length = text.length();
while (current < length && lines < maxLines) {
final int next = nextLineBreak(text, current, maxWidth, iterator, measurer);
if (next == BreakIterator.DONE) {
result.addLine(text.substring(current), font, paint);
return result;
}
//MODIFIED CODE BEGIN
if (next==0) {
//fix for OOME when width of text is less, that real place for text
result.addLine("", font, paint);
break;
}
//MODIFIED CODE END
result.addLine(text.substring(current, next), font, paint);
lines++;
current = next;
while (current < text.length()&& text.charAt(current) == '\n') {
current++;
}
}
if (current < length) {
final TextLine lastLine = result.getLastLine();
final TextFragment lastFragment = lastLine.getLastTextFragment();
final String oldStr = lastFragment.getText();
String newStr = "...";
if (oldStr.length() > 3) {
newStr = oldStr.substring(0, oldStr.length() - 3) + "...";
}
lastLine.removeFragment(lastFragment);
final TextFragment newFragment = new TextFragment(newStr,
lastFragment.getFont(), lastFragment.getPaint());
lastLine.addFragment(newFragment);
}
return result;
}