use of org.terasology.nui.asset.font.Font in project Terasology by MovingBlocks.
the class TextFlowRenderable method splitAt.
@Override
public FlowRenderable.SplitResult<TextFlowRenderable> splitAt(TextRenderStyle defaultRenderStyle, int width) {
Font font = getTextRenderStyle(defaultRenderStyle).getFont(hyperlink != null);
if (!text.contains("\n")) {
int wholeTextWidth = font.getWidth(text);
if (wholeTextWidth <= width) {
return new SplitResult<>(this, null);
}
}
int spaceWidth = font.getWidth(' ');
boolean first = true;
int usedSpace = 0;
StringBuilder before = new StringBuilder();
StringBuilder after = new StringBuilder();
boolean appendingToBefore = true;
String[] lines = text.split("\n");
String[] words = lines[0].split(" ");
for (String word : words) {
if (appendingToBefore) {
if (!first) {
usedSpace += spaceWidth;
before.append(" ");
}
usedSpace += font.getWidth(word);
if (usedSpace > width) {
if (before.length() == 0) {
return new SplitResult<>(null, this);
} else {
appendingToBefore = false;
after.append(word);
}
} else {
before.append(word);
}
first = false;
} else {
after.append(" ");
after.append(word);
}
}
if (lines[0].endsWith(" ")) {
after.append(" ");
}
after = trimLeft(after);
boolean firstLine = true;
for (int i = 1; i < lines.length; i++) {
if (!firstLine || after.length() > 0) {
after.append("\n");
}
after.append(lines[i]);
firstLine = false;
}
String beforeText = trimRight(before).toString();
String afterText = after.toString();
if (afterText.isEmpty()) {
return new SplitResult<>(new TextFlowRenderable(beforeText, textRenderStyle, hyperlink), null);
} else {
return new SplitResult<>(new TextFlowRenderable(beforeText, textRenderStyle, hyperlink), new TextFlowRenderable(afterText, textRenderStyle, hyperlink));
}
}
Aggregations