Search in sources :

Example 6 with Font

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));
    }
}
Also used : Font(org.terasology.nui.asset.font.Font)

Aggregations

Font (org.terasology.nui.asset.font.Font)6 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Matrix3f (org.joml.Matrix3f)1 Matrix4f (org.joml.Matrix4f)1 Matrix4fStack (org.joml.Matrix4fStack)1 Quaternionf (org.joml.Quaternionf)1 Quaternionfc (org.joml.Quaternionfc)1 Vector2f (org.joml.Vector2f)1 Vector2i (org.joml.Vector2i)1