Facebook
From xd, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 202
  1. package xguard;
  2.  
  3. import java.awt.Font;
  4. import java.awt.FontFormatException;
  5. import java.awt.GraphicsEnvironment;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11.  
  12. import org.lwjgl.opengl.GL11;
  13. import org.newdawn.slick.UnicodeFont;
  14. import org.newdawn.slick.font.effects.ColorEffect;
  15.  
  16. import net.minecraft.client.renderer.GlStateManager;
  17. import net.minecraft.util.StringUtils;
  18.  
  19.  
  20.  
  21. public class UnicodeFontRenderer {
  22.        
  23.         public static UnicodeFontRenderer getFontOnPC(String name, int size) {
  24.                 return getFontOnPC(name, size, Font.PLAIN);
  25.         }
  26.        
  27.         public static UnicodeFontRenderer getFontOnPC(String name, int size, int fontType) {
  28.                 return getFontOnPC(name, size, fontType, 0);
  29.         }
  30.        
  31.         public static UnicodeFontRenderer getFontOnPC(String name, int size, int fontType, float kerning) {
  32.                 return getFontOnPC(name, size, fontType, kerning, 3.0f);
  33.         }
  34.        
  35.         public static UnicodeFontRenderer getFontOnPC(String name, int size, int fontType, float kerning, float antiAliasingFactor) {
  36.                 return new UnicodeFontRenderer(new Font(name, fontType, size), kerning, antiAliasingFactor);
  37.         }
  38.        
  39.         public static UnicodeFontRenderer getFontFromAssets(String name, int size) {
  40.                 return getFontOnPC(name, size, Font.PLAIN);
  41.         }
  42.        
  43.         public static UnicodeFontRenderer getFontFromAssets(String name, int size, int fontType) {
  44.                 return getFontOnPC(name, fontType, size, 0);
  45.         }
  46.        
  47.         public static UnicodeFontRenderer getFontFromAssets(String name, int size, float kerning, int fontType) {
  48.                 return getFontFromAssets(name, size, fontType, kerning, 3.0f);
  49.         }
  50.        
  51.         public static UnicodeFontRenderer getFontFromAssets(String name, int size, int fontType,  float kerning, float antiAliasingFactor) {
  52.                 return new UnicodeFontRenderer(name, fontType, size, kerning, antiAliasingFactor);
  53.         }
  54.        
  55.        
  56.         /*-------------------------------------------------------------------------------------------------------------------*/
  57.        
  58.         public final int FONT_HEIGHT = 9;
  59.     private final int[] colorCodes = new int[32];
  60.     private final float kerning;
  61.     private final Map<String, Float> cachedStringWidth = new HashMap<>();
  62.     private float antiAliasingFactor;
  63.     private UnicodeFont unicodeFont;
  64.  
  65.     private UnicodeFontRenderer(String fontName, int fontType, float fontSize, float kerning, float antiAliasingFactor) {
  66.         this.antiAliasingFactor = antiAliasingFactor;
  67.         try {
  68.             this.unicodeFont = new UnicodeFont(getFontByName(fontName).deriveFont(fontSize * this.antiAliasingFactor));
  69.         } catch (FontFormatException | IOException e) {
  70.             e.printStackTrace();
  71.         }
  72.         this.kerning = kerning;
  73.  
  74.         this.unicodeFont.addAsciiGlyphs();
  75.         this.unicodeFont.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
  76.  
  77.         try {
  78.             this.unicodeFont.loadGlyphs();
  79.         } catch (Exception e) {
  80.             e.printStackTrace();
  81.         }
  82.  
  83.         for (int i = 0; i < 32; i++) {
  84.             int shadow = (i >> 3 & 1) * 85;
  85.             int red = (i >> 2 & 1) * 170 + shadow;
  86.             int green = (i >> 1 & 1) * 170 + shadow;
  87.             int blue = (i & 1) * 170 + shadow;
  88.  
  89.             if (i == 6) {
  90.                 red += 85;
  91.             }
  92.  
  93.             if (i >= 16) {
  94.                 red /= 4;
  95.                 green /= 4;
  96.                 blue /= 4;
  97.             }
  98.  
  99.             this.colorCodes[i] = (red & 255) << 16 | (green & 255) << 8 | blue & 255;
  100.         }
  101.     }
  102.  
  103.     private UnicodeFontRenderer(Font font, float kerning, float antiAliasingFactor) {
  104.         this.antiAliasingFactor = antiAliasingFactor;
  105.         this.unicodeFont = new UnicodeFont(new Font(font.getName(), font.getStyle(), (int) (font.getSize() * antiAliasingFactor)));
  106.         this.kerning = kerning;
  107.  
  108.         this.unicodeFont.addAsciiGlyphs();
  109.         this.unicodeFont.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
  110.  
  111.         try {
  112.             this.unicodeFont.loadGlyphs();
  113.         } catch (Exception e) {
  114.             e.printStackTrace();
  115.         }
  116.  
  117.         for (int i = 0; i < 32; i++) {
  118.             int shadow = (i >> 3 & 1) * 85;
  119.             int red = (i >> 2 & 1) * 170 + shadow;
  120.             int green = (i >> 1 & 1) * 170 + shadow;
  121.             int blue = (i & 1) * 170 + shadow;
  122.  
  123.             if (i == 6) {
  124.                 red += 85;
  125.             }
  126.  
  127.             if (i >= 16) {
  128.                 red /= 4;
  129.                 green /= 4;
  130.                 blue /= 4;
  131.             }
  132.  
  133.             this.colorCodes[i] = (red & 255) << 16 | (green & 255) << 8 | blue & 255;
  134.         }
  135.     }
  136.  
  137.     private Font getFontByName(String name) throws IOException, FontFormatException {
  138.         return getFontFromInput("/assets/minecraft/clientname/fonts/" + name + ".ttf");
  139.     }
  140.  
  141.     private Font getFontFromInput(String path) throws IOException, FontFormatException {
  142.         return Font.createFont(Font.TRUETYPE_FONT, UnicodeFontRenderer.class.getResourceAsStream(path));
  143.     }
  144.  
  145.     public void drawStringScaled(String text, int givenX, int givenY, int color, double givenScale) {
  146.  
  147.         GL11.glPushMatrix();
  148.         GL11.glTranslated(givenX, givenY, 0);
  149.         GL11.glScaled(givenScale, givenScale, givenScale);
  150.         drawString(text, 0, 0, color);
  151.         GL11.glPopMatrix();
  152.  
  153.     }
  154.  
  155.     public int drawString(String text, float x, float y, int color) {
  156.         if (text == null)
  157.             return 0;
  158.  
  159.         x *= 2.0F;
  160.         y *= 2.0F;
  161.  
  162.         float originalX = x;
  163.  
  164.         GL11.glPushMatrix();
  165.         GlStateManager.scale(1 / antiAliasingFactor, 1 / antiAliasingFactor, 1 / antiAliasingFactor);
  166.         GL11.glScaled(0.5F, 0.5F, 0.5F);
  167.         x *= antiAliasingFactor;
  168.         y *= antiAliasingFactor;
  169.         float red = (float) (color >> 16 & 255) / 255.0F;
  170.         float green = (float) (color >> 8 & 255) / 255.0F;
  171.         float blue = (float) (color & 255) / 255.0F;
  172.         float alpha = (float) (color >> 24 & 255) / 255.0F;
  173.         GlStateManager.color(red, green, blue, alpha);
  174.  
  175.         boolean blend = GL11.glIsEnabled(GL11.GL_BLEND);
  176.         boolean lighting = GL11.glIsEnabled(GL11.GL_LIGHTING);
  177.         boolean texture = GL11.glIsEnabled(GL11.GL_TEXTURE_2D);
  178.         if (!blend)
  179.             GL11.glEnable(GL11.GL_BLEND);
  180.         if (lighting)
  181.             GL11.glDisable(GL11.GL_LIGHTING);
  182.         if (texture)
  183.             GL11.glDisable(GL11.GL_TEXTURE_2D);
  184.  
  185.         int currentColor = color;
  186.         char[] characters = text.toCharArray();
  187.  
  188.         int index = 0;
  189.         for (char c : characters) {
  190.             if (c == '\r') {
  191.                 x = originalX;
  192.             }
  193.             if (c == '\n') {
  194.                 y += getHeight(Character.toString(c)) * 2.0F;
  195.             }
  196.             if (c != '\247' && (index == 0 || index == characters.length - 1 || characters[index - 1] != '\247')) {
  197.                 //Line causing error
  198.                 unicodeFont.drawString(x, y, Character.toString(c), new org.newdawn.slick.Color(currentColor));
  199.                 x += (getWidth(Character.toString(c)) * 2.0F * antiAliasingFactor);
  200.             } else if (c == ' ') {
  201.                 x += unicodeFont.getSpaceWidth();
  202.             } else if (c == '\247' && index != characters.length - 1) {
  203.                 int codeIndex = "0123456789abcdefg".indexOf(text.charAt(index + 1));
  204.                 if (codeIndex < 0) continue;
  205.  
  206.                 currentColor = this.colorCodes[codeIndex];
  207.             }
  208.  
  209.             index++;
  210.         }
  211.  
  212.         GL11.glScaled(2.0F, 2.0F, 2.0F);
  213.         if (texture)
  214.             GL11.glEnable(GL11.GL_TEXTURE_2D);
  215.         if (lighting)
  216.             GL11.glEnable(GL11.GL_LIGHTING);
  217.         if (!blend)
  218.             GL11.glDisable(GL11.GL_BLEND);
  219.         GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
  220.         GL11.glPopMatrix();
  221.         return (int) x / 2;
  222.     }
  223.  
  224.     public int drawStringWithShadow(String text, float x, float y, int color) {
  225.         drawString(StringUtils.stripControlCodes(text), x + 0.5F, y + 0.5F, 0x000000);
  226.         return drawString(text, x, y, color);
  227.     }
  228.  
  229.     public void drawCenteredString(String text, float x, float y, int color) {
  230.         drawString(text, x - (int) getWidth(text) / 2, y, color);
  231.     }
  232.  
  233.     public void drawCenteredTextScaled(String text, int givenX, int givenY, int color, double givenScale) {
  234.  
  235.         GL11.glPushMatrix();
  236.         GL11.glTranslated(givenX, givenY, 0);
  237.         GL11.glScaled(givenScale, givenScale, givenScale);
  238.         drawCenteredString(text, 0, 0, color);
  239.         GL11.glPopMatrix();
  240.  
  241.     }
  242.  
  243.     public void drawCenteredStringWithShadow(String text, float x, float y, int color) {
  244.         drawCenteredString(StringUtils.stripControlCodes(text), x + 0.5F, y + 0.5F, color);
  245.         drawCenteredString(text, x, y, color);
  246.     }
  247.  
  248.     public float getWidth(String s) {
  249.         if (cachedStringWidth.size() > 1000)
  250.             cachedStringWidth.clear();
  251.         return cachedStringWidth.computeIfAbsent(s, e -> {
  252.             float width = 0.0F;
  253.             String str = StringUtils.stripControlCodes(s);
  254.             for (char c : str.toCharArray()) {
  255.                 width += unicodeFont.getWidth(Character.toString(c)) + this.kerning;
  256.             }
  257.  
  258.             return width / 2.0F / antiAliasingFactor;
  259.         });
  260.  
  261.     }
  262.  
  263.     public int getStringWidth(String text) {
  264.         if (text == null) {
  265.             return 0;
  266.         } else {
  267.             int i = 0;
  268.             boolean flag = false;
  269.  
  270.             for (int j = 0; j < text.length(); ++j) {
  271.                 char c0 = text.charAt(j);
  272.                 float k = this.getWidth(String.valueOf(c0));
  273.  
  274.                 if (k < 0 && j < text.length() - 1) {
  275.                     ++j;
  276.                     c0 = text.charAt(j);
  277.  
  278.                     if (c0 != 'l' && c0 != 'L') {
  279.                         if (c0 == 'r' || c0 == 'R') {
  280.                             flag = false;
  281.                         }
  282.                     } else {
  283.                         flag = true;
  284.                     }
  285.  
  286.                     k = 0;
  287.                 }
  288.  
  289.                 i += k;
  290.  
  291.                 if (flag && k > 0) {
  292.                     ++i;
  293.                 }
  294.             }
  295.  
  296.             return i;
  297.         }
  298.     }
  299.  
  300.     public float getCharWidth(char c) {
  301.         return unicodeFont.getWidth(String.valueOf(c));
  302.     }
  303.  
  304.     public float getHeight(String s) {
  305.         return unicodeFont.getHeight(s) / 2.0F;
  306.     }
  307.  
  308.     public UnicodeFont getFont() {
  309.         return this.unicodeFont;
  310.     }
  311.  
  312.     public String trimStringToWidth(String par1Str, int par2) {
  313.         StringBuilder var4 = new StringBuilder();
  314.         float var5 = 0.0F;
  315.         int var6 = 0;
  316.         int var7 = 1;
  317.         boolean var8 = false;
  318.         boolean var9 = false;
  319.  
  320.         for (int var10 = var6; var10 >= 0 && var10 < par1Str.length() && var5 < (float) par2; var10 += var7) {
  321.             char var11 = par1Str.charAt(var10);
  322.             float var12 = this.getCharWidth(var11);
  323.  
  324.             if (var8) {
  325.                 var8 = false;
  326.  
  327.                 if (var11 != 108 && var11 != 76) {
  328.                     if (var11 == 114 || var11 == 82) {
  329.                         var9 = false;
  330.                     }
  331.                 } else {
  332.                     var9 = true;
  333.                 }
  334.             } else if (var12 < 0.0F) {
  335.                 var8 = true;
  336.             } else {
  337.                 var5 += var12;
  338.  
  339.                 if (var9) {
  340.                     ++var5;
  341.                 }
  342.             }
  343.  
  344.             if (var5 > (float) par2) {
  345.                 break;
  346.             } else {
  347.                 var4.append(var11);
  348.             }
  349.         }
  350.  
  351.         return var4.toString();
  352.     }
  353.  
  354.     public void drawSplitString(ArrayList<String> lines, int x, int y, int color) {
  355.         drawString(
  356.             String.join("\n\r", lines),
  357.             x,
  358.             y,
  359.             color
  360.         );
  361.     }
  362.  
  363.     public List<String> splitString(String text, int wrapWidth) {
  364.         List<String> lines = new ArrayList<>();
  365.  
  366.         String[] splitText = text.split(" ");
  367.         StringBuilder currentString = new StringBuilder();
  368.  
  369.         for (String word : splitText) {
  370.             String potential = currentString + " " + word;
  371.  
  372.             if (getWidth(potential) >= wrapWidth) {
  373.                 lines.add(currentString.toString());
  374.                 currentString = new StringBuilder();
  375.             }
  376.             currentString.append(word).append(" ");
  377.         }
  378.         lines.add(currentString.toString());
  379.         return lines;
  380.     }
  381.        
  382.