use of org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.FlowRenderable 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.engine.rendering.nui.widgets.browser.data.basic.flow.FlowRenderable in project Terasology by MovingBlocks.
the class FlowParagraphRenderable method renderContents.
@Override
public void renderContents(Canvas canvas, Vector2i startPos, ContainerRenderSpace containerRenderSpace, int leftIndent, int rightIndent, ParagraphRenderStyle defaultStyle, HorizontalAlign horizontalAlign, HyperlinkRegister hyperlinkRegister) {
int y = startPos.y;
for (LaidFlowLine<FlowRenderable> line : updateCacheIfNeeded(defaultStyle, startPos.y, containerRenderSpace)) {
int x = 0;
int insetXAdvance = containerRenderSpace.getAdvanceForVerticalPosition(y);
int availableWidth = containerRenderSpace.getWidthForVerticalPosition(y);
if (horizontalAlign == HorizontalAlign.LEFT || horizontalAlign == HorizontalAlign.CENTER) {
availableWidth -= leftIndent;
}
if (horizontalAlign == HorizontalAlign.RIGHT || horizontalAlign == HorizontalAlign.CENTER) {
availableWidth -= rightIndent;
}
int lineHeight = line.getHeight();
int lineWidth = line.getWidth();
int alignOffset = horizontalAlign.getOffset(lineWidth, availableWidth);
for (FlowRenderable flowRenderable : line.getFlowRenderables()) {
int elementWidth = flowRenderable.getWidth(defaultStyle);
Rectanglei renderableRegion = RectUtility.createFromMinAndSize(insetXAdvance + leftIndent + alignOffset + startPos.x + x, y, elementWidth, lineHeight);
String hyperlink = flowRenderable.getAction();
if (hyperlink != null) {
hyperlinkRegister.registerHyperlink(renderableRegion, hyperlink);
}
flowRenderable.render(canvas, renderableRegion, defaultStyle);
x += elementWidth;
}
y += lineHeight;
}
}
Aggregations