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;
}
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;
}
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);
}
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();
});
}
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);
}
Aggregations