Search in sources :

Example 1 with Font

use of org.terasology.nui.asset.font.Font 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 Font

use of org.terasology.nui.asset.font.Font in project Terasology by MovingBlocks.

the class TextFlowRenderable method getMinWidth.

@Override
public int getMinWidth(TextRenderStyle defaultRenderStyle) {
    Font font = getTextRenderStyle(defaultRenderStyle).getFont(hyperlink != null);
    int minWidth = 0;
    String[] words = text.split("[ \\n]");
    for (String word : words) {
        int width = font.getWidth(word);
        minWidth = Math.max(minWidth, width);
    }
    return minWidth;
}
Also used : Font(org.terasology.nui.asset.font.Font)

Example 3 with Font

use of org.terasology.nui.asset.font.Font in project Terasology by MovingBlocks.

the class TextFlowRenderable method render.

@Override
public void render(Canvas canvas, Rectanglei bounds, TextRenderStyle defaultRenderStyle) {
    TextRenderStyle safeRenderStyle = getTextRenderStyle(defaultRenderStyle);
    Font font = safeRenderStyle.getFont(hyperlink != null);
    int lineHeight = font.getLineHeight();
    Rectanglei bottomBounds = new Rectanglei(bounds.minX, bounds.maxY - lineHeight).setSize(bounds.getSizeX(), lineHeight);
    canvas.drawTextRaw(text, font, safeRenderStyle.getColor(hyperlink != null), bottomBounds);
}
Also used : TextRenderStyle(org.terasology.engine.rendering.nui.widgets.browser.ui.style.TextRenderStyle) FallbackTextRenderStyle(org.terasology.engine.rendering.nui.widgets.browser.ui.style.FallbackTextRenderStyle) Rectanglei(org.terasology.joml.geom.Rectanglei) Font(org.terasology.nui.asset.font.Font)

Example 4 with Font

use of org.terasology.nui.asset.font.Font in project Terasology by MovingBlocks.

the class LwjglCanvasRenderer method drawText.

@Override
public void drawText(String text, Font font, HorizontalAlign hAlign, VerticalAlign vAlign, Rectanglei absoluteRegionRectangle, Colorc color, Colorc shadowColor, float alpha, boolean underlined) {
    Rectanglei absoluteRegion = new Rectanglei(absoluteRegionRectangle);
    TextCacheKey key = new TextCacheKey(text, font, absoluteRegion.getSizeX(), hAlign, color, shadowColor, underlined);
    usedText.add(key);
    Map<Material, Mesh> fontMesh = cachedText.get(key);
    List<String> lines = TextLineBuilder.getLines(font, text, absoluteRegion.getSizeX());
    if (fontMesh != null) {
        for (Mesh mesh : fontMesh.values()) {
            if (mesh.isDisposed()) {
                fontMesh = null;
                break;
            }
        }
    }
    if (fontMesh == null) {
        fontMesh = fontMeshBuilder.createTextMesh((org.terasology.engine.rendering.assets.font.Font) font, lines, absoluteRegion.getSizeX(), hAlign, color, shadowColor, underlined);
        cachedText.put(key, fontMesh);
    }
    Vector2i offset = new Vector2i(absoluteRegion.minX, absoluteRegion.minY);
    offset.y += vAlign.getOffset(lines.size() * font.getLineHeight(), absoluteRegion.lengthY());
    fontMesh.entrySet().stream().filter(entry -> entry.getKey().isRenderable()).forEach(entry -> {
        entry.getKey().bindTextures();
        entry.getKey().setMatrix4("projectionMatrix", projMatrix);
        entry.getKey().setMatrix4("modelViewMatrix", modelMatrixStack);
        entry.getKey().setFloat4(CROPPING_BOUNDARIES_PARAM, requestedCropRegion.minX, requestedCropRegion.maxX, requestedCropRegion.minY, requestedCropRegion.maxY);
        entry.getKey().setFloat2("offset", offset.x, offset.y);
        entry.getKey().setFloat("alpha", alpha);
        entry.getValue().render();
    });
}
Also used : Quaternionf(org.joml.Quaternionf) GL11.glDisable(org.lwjgl.opengl.GL11.glDisable) GL11.glViewport(org.lwjgl.opengl.GL11.glViewport) Vector2ic(org.joml.Vector2ic) DisplayDevice(org.terasology.engine.core.subsystem.DisplayDevice) UITextureRegion(org.terasology.nui.UITextureRegion) Map(java.util.Map) Vector3f(org.joml.Vector3f) GL11(org.lwjgl.opengl.GL11) TeraMath(org.terasology.math.TeraMath) VerticalAlign(org.terasology.nui.VerticalAlign) Quaternionfc(org.joml.Quaternionfc) FrameBufferObject(org.terasology.engine.rendering.opengl.FrameBufferObject) GL_SRC_ALPHA(org.lwjgl.opengl.GL11.GL_SRC_ALPHA) RenderingConfig(org.terasology.engine.config.RenderingConfig) GL_DEPTH_TEST(org.lwjgl.opengl.GL11.GL_DEPTH_TEST) TextLineBuilder(org.terasology.nui.TextLineBuilder) Set(java.util.Set) ScaleMode(org.terasology.nui.ScaleMode) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) MeshBuilder(org.terasology.engine.rendering.assets.mesh.MeshBuilder) List(java.util.List) Context(org.terasology.engine.context.Context) TextureRegion(org.terasology.engine.rendering.assets.texture.TextureRegion) Vector2i(org.joml.Vector2i) Border(org.terasology.nui.Border) PropertyChangeListener(java.beans.PropertyChangeListener) Vector2f(org.joml.Vector2f) Matrix3f(org.joml.Matrix3f) LwjglFrameBufferObject(org.terasology.engine.rendering.opengl.LwjglFrameBufferObject) Matrix4fStack(org.joml.Matrix4fStack) Rectanglef(org.terasology.joml.geom.Rectanglef) Rectanglei(org.terasology.joml.geom.Rectanglei) Colorc(org.terasology.nui.Colorc) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) AABBfc(org.terasology.joml.geom.AABBfc) GL11.glClear(org.lwjgl.opengl.GL11.glClear) Font(org.terasology.nui.asset.font.Font) Matrix4f(org.joml.Matrix4f) FontMeshBuilder(org.terasology.engine.rendering.assets.font.FontMeshBuilder) PropertyChangeEvent(java.beans.PropertyChangeEvent) AssetManager(org.terasology.gestalt.assets.management.AssetManager) GL11.glEnable(org.lwjgl.opengl.GL11.glEnable) Assets(org.terasology.engine.utilities.Assets) Iterator(java.util.Iterator) Maps(com.google.common.collect.Maps) GL11.glBlendFunc(org.lwjgl.opengl.GL11.glBlendFunc) GL_ONE_MINUS_SRC_ALPHA(org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA) Vector3fc(org.joml.Vector3fc) Config(org.terasology.engine.config.Config) Material(org.terasology.engine.rendering.assets.material.Material) GL_BLEND(org.lwjgl.opengl.GL11.GL_BLEND) Mesh(org.terasology.engine.rendering.assets.mesh.Mesh) HorizontalAlign(org.terasology.nui.HorizontalAlign) Mesh(org.terasology.engine.rendering.assets.mesh.Mesh) Material(org.terasology.engine.rendering.assets.material.Material) Rectanglei(org.terasology.joml.geom.Rectanglei) Font(org.terasology.nui.asset.font.Font) Vector2i(org.joml.Vector2i)

Example 5 with Font

use of org.terasology.nui.asset.font.Font in project Terasology by MovingBlocks.

the class UIInputBind method getPreferredContentSize.

@Override
public Vector2i getPreferredContentSize(Canvas canvas, Vector2i areaHint) {
    Font font = canvas.getCurrentStyle().getFont();
    String text = "";
    if (capturingInput) {
        text = "???";
    } else if (newInput != null) {
        text = newInput.getDisplayName();
    }
    List<String> lines = TextLineBuilder.getLines(font, text, areaHint.x());
    return font.getSize(lines);
}
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