Skip to content

Instantly share code, notes, and snippets.

@guoguo12
Last active August 29, 2017 15:44
Show Gist options
  • Select an option

  • Save guoguo12/6138017 to your computer and use it in GitHub Desktop.

Select an option

Save guoguo12/6138017 to your computer and use it in GitHub Desktop.
This is one way to display subscript and superscript text using Libgdx. The markup used assumes that, in the raw string, subscript text is surrounded by underscores and superscript text is surrounded by carets. "bigFontName" and "smallFontName" correspond to fonts in the skin argument. See this blog post for more details: http://www.sparkfiregam…
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
public class TextRenderer {
private static final float PADDING = 50; // Vertical padding for subscript and superscript blocks
public static Table renderString(Skin skin, String string, Color color) {
Table table = new Table(skin);
StringBuilder input = new StringBuilder(string);
StringBuilder currentBlock = new StringBuilder();
RenderMode currentMode = RenderMode.NORMAL;
while (input.length() >= 1) {
char currentChar = input.charAt(0);
input.deleteCharAt(0);
boolean endOfBlock = false;
RenderMode newMode = null;
switch(currentMode) {
case NORMAL:
endOfBlock = true;
if (currentChar == '_') {
newMode = RenderMode.SUBSCRIPT;
} else if (currentChar == '^') {
newMode = RenderMode.SUPERSCRIPT;
} else {
currentBlock.append(currentChar);
endOfBlock = false;
}
break;
case SUBSCRIPT:
if (currentChar == '_') {
newMode = RenderMode.NORMAL;
endOfBlock = true;
} else {
currentBlock.append(currentChar);
}
break;
case SUPERSCRIPT:
if (currentChar == '^') {
newMode = RenderMode.NORMAL;
endOfBlock = true;
} else {
currentBlock.append(currentChar);
}
break;
}
if ((endOfBlock || input.length() == 0) && currentBlock.length() > 0) {
switch (currentMode) {
case NORMAL:
String text = currentBlock.toString();
table.add(text, "bigFontName", color);
break;
case SUBSCRIPT:
table.add(currentBlock.toString(), "smallFontName", color).padTop(PADDING);
break;
case SUPERSCRIPT:
table.add(currentBlock.toString(), "smallFontName", color).padBottom(PADDING);
break;
}
currentBlock = new StringBuilder();
}
if (newMode != null) {
currentMode = newMode;
}
}
return table;
}
public enum RenderMode {
NORMAL, SUBSCRIPT, SUPERSCRIPT;
}
}
@guoguo12

guoguo12 commented Aug 2, 2013

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment