Last active
August 29, 2017 15:44
-
-
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Link to the blog post introducing this method: http://www.sparkfiregames.org/blog/displaying-subscript-and-superscript-text-with-libgdx.