Search in sources :

Example 1 with TextFlowRenderable

use of org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.TextFlowRenderable in project Terasology by MovingBlocks.

the class HTMLLikeParser method parseHTMLLike.

public static Collection<FlowRenderable> parseHTMLLike(String text) {
    if (text == null) {
        return null;
    }
    StringReader reader = new StringReader(text);
    int character;
    try {
        StringBuilder sb = new StringBuilder();
        Font font = null;
        Color color = null;
        String hyperlink = null;
        List<FlowRenderable> result = new LinkedList<>();
        while ((character = reader.read()) != -1) {
            char c = (char) character;
            if (c == '\n') {
                throw new IllegalArgumentException("Parsed text cannot contain line breaks.");
            } else if (c == '<') {
                char nextChar = (char) reader.read();
                if (nextChar == '/') {
                    char id = (char) reader.read();
                    if (id == 'f') {
                        if (sb.length() > 0) {
                            result.add(new TextFlowRenderable(sb.toString(), new DefaultTextRenderStyle(font, color), hyperlink));
                            sb.setLength(0);
                        }
                        font = null;
                    } else if (id == 'c') {
                        if (sb.length() > 0) {
                            result.add(new TextFlowRenderable(sb.toString(), new DefaultTextRenderStyle(font, color), hyperlink));
                            sb.setLength(0);
                        }
                        color = null;
                    } else if (id == 'h') {
                        if (sb.length() > 0) {
                            result.add(new TextFlowRenderable(sb.toString(), new DefaultTextRenderStyle(font, color), hyperlink));
                            sb.setLength(0);
                        }
                        hyperlink = null;
                    } else {
                        throw new IllegalArgumentException("Unable to parse text - " + text);
                    }
                    reader.read();
                } else if (nextChar == 'f') {
                    if (sb.length() > 0) {
                        result.add(new TextFlowRenderable(sb.toString(), new DefaultTextRenderStyle(font, color), hyperlink));
                        sb.setLength(0);
                    }
                    reader.read();
                    font = Assets.getFont(readUntilCharacter(reader, '>')).get();
                } else if (nextChar == 'c') {
                    if (sb.length() > 0) {
                        result.add(new TextFlowRenderable(sb.toString(), new DefaultTextRenderStyle(font, color), hyperlink));
                        sb.setLength(0);
                    }
                    reader.read();
                    color = new Color(Integer.parseInt(readUntilCharacter(reader, '>'), 16));
                } else if (nextChar == 'h') {
                    if (sb.length() > 0) {
                        result.add(new TextFlowRenderable(sb.toString(), new DefaultTextRenderStyle(font, color), hyperlink));
                        sb.setLength(0);
                    }
                    reader.read();
                    hyperlink = readUntilCharacter(reader, '>');
                } else if (nextChar == 'l') {
                    readUntilCharacter(reader, '>');
                    sb.append('\n');
                }
            } else if (c == '&') {
                String escape = readUntilCharacter(reader, ';');
                if (escape.equals("gt")) {
                    sb.append('>');
                } else if (escape.equals("lt")) {
                    sb.append('<');
                } else if (escape.equals("amp")) {
                    sb.append('&');
                } else {
                    throw new IllegalArgumentException("Unknown escape sequence - " + escape);
                }
            } else {
                sb.append(c);
            }
        }
        if (sb.length() > 0) {
            result.add(new TextFlowRenderable(sb.toString(), new DefaultTextRenderStyle(font, color), hyperlink));
        }
        return result;
    } catch (IOException exp) {
    // Ignore - can't happen
    }
    return null;
}
Also used : Color(org.terasology.nui.Color) IOException(java.io.IOException) Font(org.terasology.nui.asset.font.Font) LinkedList(java.util.LinkedList) StringReader(java.io.StringReader) FlowRenderable(org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.FlowRenderable) TextFlowRenderable(org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.TextFlowRenderable) TextFlowRenderable(org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.TextFlowRenderable)

Example 2 with TextFlowRenderable

use of org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.TextFlowRenderable in project Terasology by MovingBlocks.

the class ParagraphBuilder method text.

@Override
public void text(String text) {
    StaticTextRenderStyle renderStyle = new StaticTextRenderStyle(htmlFontResolver.getFont(fontName, bold), color);
    paragraphData.append(new TextFlowRenderable(text, renderStyle, hyperlink));
}
Also used : TextFlowRenderable(org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.TextFlowRenderable)

Aggregations

TextFlowRenderable (org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.TextFlowRenderable)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 LinkedList (java.util.LinkedList)1 FlowRenderable (org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.FlowRenderable)1 Color (org.terasology.nui.Color)1 Font (org.terasology.nui.asset.font.Font)1