FontRenderer .java

net.minecraft.client.gui.FontRenderer (class): members: listFormattedStringToWidth, drawStringWithShadow, unicodePageLocations, getFormatFromString, red.

net.minecraft.client.guiclass997 linesclient34.2 KB

Implements

API Surface (17)

void onResourceManagerReload(IResourceManager resourceManager)
int drawStringWithShadow(String text, float x, float y, int color)
int drawString(String text, int x, int y, int color)
int drawString(String text, float x, float y, int color, boolean dropShadow)
int getStringWidth(String text)
int getCharWidth(char character)
String trimStringToWidth(String text, int width)
String trimStringToWidth(String text, int width, boolean reverse)
void drawSplitString(String str, int x, int y, int wrapWidth, int textColor)
int splitStringWidth(String str, int maxLength)
void setUnicodeFlag(boolean unicodeFlagIn)
boolean getUnicodeFlag()
void setBidiFlag(boolean bidiFlagIn)
List<String> listFormattedStringToWidth(String str, int wrapWidth)
String getFormatFromString(String text)
boolean getBidiFlag()
int getColorCode(char character)

Fields & Constants (22)

unicodePageLocations
charWidth
FONT_HEIGHT
fontRandom
glyphWidth
colorCode
locationFontTexture
renderEngine
posX
posY
unicodeFlag
bidiFlag
red
blue
green
alpha
textColor
randomStyle
boldStyle
italicStyle
underlineStyle
strikethroughStyle

String literals (3)

"font/glyph_sizes.bin"
"0123456789abcdefklmnor"
"0123456789abcdef"

Full Source

FontRenderer.java997 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.client.gui;
2
3
import com.ibm.icu.text.ArabicShaping;
4
import com.ibm.icu.text.ArabicShapingException;
5
import com.ibm.icu.text.Bidi;
6
import java.awt.image.BufferedImage;
7
import java.io.IOException;
8
import java.io.InputStream;
9
import java.util.Arrays;
10
import java.util.List;
11
import java.util.Locale;
12
import java.util.Random;
13
import net.minecraft.client.Minecraft;
14
import net.minecraft.client.renderer.GlStateManager;
15
import net.minecraft.client.renderer.Tessellator;
16
import net.minecraft.client.renderer.WorldRenderer;
17
import net.minecraft.client.renderer.texture.TextureManager;
18
import net.minecraft.client.renderer.texture.TextureUtil;
19
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
20
import net.minecraft.client.resources.IResourceManager;
21
import net.minecraft.client.resources.IResourceManagerReloadListener;
22
import net.minecraft.client.settings.GameSettings;
23
import net.minecraft.util.ResourceLocation;
24
import org.apache.commons.io.IOUtils;
25
import org.lwjgl.opengl.GL11;
26
27
public class FontRenderer implements IResourceManagerReloadListener
28
{
29
    private static final ResourceLocation[] unicodePageLocations = new ResourceLocation[256];
30
31
    /** Array of width of all the characters in default.png */
32
    private int[] charWidth = new int[256];
33
34
    /** the height in pixels of default text */
35
    public int FONT_HEIGHT = 9;
36
    public Random fontRandom = new Random();
37
38
    /**
39
     * Array of the start/end column (in upper/lower nibble) for every glyph in the /font directory.
40
     */
41
    private byte[] glyphWidth = new byte[65536];
42
43
    /**
44
     * Array of RGB triplets defining the 16 standard chat colors followed by 16 darker version of the same colors for
45
     * drop shadows.
46
     */
47
    private int[] colorCode = new int[32];
48
    private final ResourceLocation locationFontTexture;
49
50
    /** The RenderEngine used to load and setup glyph textures. */
51
    private final TextureManager renderEngine;
52
53
    /** Current X coordinate at which to draw the next character. */
54
    private float posX;
55
56
    /** Current Y coordinate at which to draw the next character. */
57
    private float posY;
58
59
    /**
60
     * If true, strings should be rendered with Unicode fonts instead of the default.png font
61
     */
62
    private boolean unicodeFlag;
63
64
    /**
65
     * If true, the Unicode Bidirectional Algorithm should be run before rendering any string.
66
     */
67
    private boolean bidiFlag;
68
69
    /** Used to specify new red value for the current color. */
70
    private float red;
71
72
    /** Used to specify new blue value for the current color. */
73
    private float blue;
74
75
    /** Used to specify new green value for the current color. */
76
    private float green;
77
78
    /** Used to speify new alpha value for the current color. */
79
    private float alpha;
80
81
    /** Text color of the currently rendering string. */
82
    private int textColor;
83
84
    /** Set if the "k" style (random) is active in currently rendering string */
85
    private boolean randomStyle;
86
87
    /** Set if the "l" style (bold) is active in currently rendering string */
88
    private boolean boldStyle;
89
90
    /** Set if the "o" style (italic) is active in currently rendering string */
91
    private boolean italicStyle;
92
93
    /**
94
     * Set if the "n" style (underlined) is active in currently rendering string
95
     */
96
    private boolean underlineStyle;
97
98
    /**
99
     * Set if the "m" style (strikethrough) is active in currently rendering string
100
     */
101
    private boolean strikethroughStyle;
102
103
    public FontRenderer(GameSettings gameSettingsIn, ResourceLocation location, TextureManager textureManagerIn, boolean unicode)
104
    {
105
        this.locationFontTexture = location;
106
        this.renderEngine = textureManagerIn;
107
        this.unicodeFlag = unicode;
108
        textureManagerIn.bindTexture(this.locationFontTexture);
109
110
        for (int i = 0; i < 32; ++i)
111
        {
112
            int j = (i >> 3 & 1) * 85;
113
            int k = (i >> 2 & 1) * 170 + j;
114
            int l = (i >> 1 & 1) * 170 + j;
115
            int i1 = (i >> 0 & 1) * 170 + j;
116
117
            if (i == 6)
118
            {
119
                k += 85;
120
            }
121
122
            if (gameSettingsIn.anaglyph)
123
            {
124
                int j1 = (k * 30 + l * 59 + i1 * 11) / 100;
125
                int k1 = (k * 30 + l * 70) / 100;
126
                int l1 = (k * 30 + i1 * 70) / 100;
127
                k = j1;
128
                l = k1;
129
                i1 = l1;
130
            }
131
132
            if (i >= 16)
133
            {
134
                k /= 4;
135
                l /= 4;
136
                i1 /= 4;
137
            }
138
139
            this.colorCode[i] = (k & 255) << 16 | (l & 255) << 8 | i1 & 255;
140
        }
141
142
        this.readGlyphSizes();
143
    }
144
145
    public void onResourceManagerReload(IResourceManager resourceManager)
146
    {
147
        this.readFontTexture();
148
    }
149
150
    private void readFontTexture()
151
    {
152
        BufferedImage bufferedimage;
153
154
        try
155
        {
156
            bufferedimage = TextureUtil.readBufferedImage(Minecraft.getMinecraft().getResourceManager().getResource(this.locationFontTexture).getInputStream());
157
        }
158
        catch (IOException ioexception)
159
        {
160
            throw new RuntimeException(ioexception);
161
        }
162
163
        int i = bufferedimage.getWidth();
164
        int j = bufferedimage.getHeight();
165
        int[] aint = new int[i * j];
166
        bufferedimage.getRGB(0, 0, i, j, aint, 0, i);
167
        int k = j / 16;
168
        int l = i / 16;
169
        int i1 = 1;
170
        float f = 8.0F / (float)l;
171
172
        for (int j1 = 0; j1 < 256; ++j1)
173
        {
174
            int k1 = j1 % 16;
175
            int l1 = j1 / 16;
176
177
            if (j1 == 32)
178
            {
179
                this.charWidth[j1] = 3 + i1;
180
            }
181
182
            int i2;
183
184
            for (i2 = l - 1; i2 >= 0; --i2)
185
            {
186
                int j2 = k1 * l + i2;
187
                boolean flag = true;
188
189
                for (int k2 = 0; k2 < k && flag; ++k2)
190
                {
191
                    int l2 = (l1 * l + k2) * i;
192
193
                    if ((aint[j2 + l2] >> 24 & 255) != 0)
194
                    {
195
                        flag = false;
196
                    }
197
                }
198
199
                if (!flag)
200
                {
201
                    break;
202
                }
203
            }
204
205
            ++i2;
206
            this.charWidth[j1] = (int)(0.5D + (double)((float)i2 * f)) + i1;
207
        }
208
    }
209
210
    private void readGlyphSizes()
211
    {
212
        InputStream inputstream = null;
213
214
        try
215
        {
216
            inputstream = Minecraft.getMinecraft().getResourceManager().getResource(new ResourceLocation("font/glyph_sizes.bin")).getInputStream();
217
            inputstream.read(this.glyphWidth);
218
        }
219
        catch (IOException ioexception)
220
        {
221
            throw new RuntimeException(ioexception);
222
        }
223
        finally
224
        {
225
            IOUtils.closeQuietly(inputstream);
226
        }
227
    }
228
229
    /**
230
     * Render the given char
231
     */
232
    private float renderChar(char ch, boolean italic)
233
    {
234
        if (ch == 32)
235
        {
236
            return 4.0F;
237
        }
238
        else
239
        {
240
            int i = "\u00c0\u00c1\u00c2\u00c8\u00ca\u00cb\u00cd\u00d3\u00d4\u00d5\u00da\u00df\u00e3\u00f5\u011f\u0130\u0131\u0152\u0153\u015e\u015f\u0174\u0175\u017e\u0207\u0000\u0000\u0000\u0000\u0000\u0000\u0000 !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u0000\u00c7\u00fc\u00e9\u00e2\u00e4\u00e0\u00e5\u00e7\u00ea\u00eb\u00e8\u00ef\u00ee\u00ec\u00c4\u00c5\u00c9\u00e6\u00c6\u00f4\u00f6\u00f2\u00fb\u00f9\u00ff\u00d6\u00dc\u00f8\u00a3\u00d8\u00d7\u0192\u00e1\u00ed\u00f3\u00fa\u00f1\u00d1\u00aa\u00ba\u00bf\u00ae\u00ac\u00bd\u00bc\u00a1\u00ab\u00bb\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556\u2555\u2563\u2551\u2557\u255d\u255c\u255b\u2510\u2514\u2534\u252c\u251c\u2500\u253c\u255e\u255f\u255a\u2554\u2569\u2566\u2560\u2550\u256c\u2567\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256b\u256a\u2518\u250c\u2588\u2584\u258c\u2590\u2580\u03b1\u03b2\u0393\u03c0\u03a3\u03c3\u03bc\u03c4\u03a6\u0398\u03a9\u03b4\u221e\u2205\u2208\u2229\u2261\u00b1\u2265\u2264\u2320\u2321\u00f7\u2248\u00b0\u2219\u00b7\u221a\u207f\u00b2\u25a0\u0000".indexOf(ch);
241
            return i != -1 && !this.unicodeFlag ? this.renderDefaultChar(i, italic) : this.renderUnicodeChar(ch, italic);
242
        }
243
    }
244
245
    /**
246
     * Render a single character with the default.png font at current (posX,posY) location...
247
     */
248
    private float renderDefaultChar(int ch, boolean italic)
249
    {
250
        int i = ch % 16 * 8;
251
        int j = ch / 16 * 8;
252
        int k = italic ? 1 : 0;
253
        this.renderEngine.bindTexture(this.locationFontTexture);
254
        int l = this.charWidth[ch];
255
        float f = (float)l - 0.01F;
256
        GL11.glBegin(GL11.GL_TRIANGLE_STRIP);
257
        GL11.glTexCoord2f((float)i / 128.0F, (float)j / 128.0F);
258
        GL11.glVertex3f(this.posX + (float)k, this.posY, 0.0F);
259
        GL11.glTexCoord2f((float)i / 128.0F, ((float)j + 7.99F) / 128.0F);
260
        GL11.glVertex3f(this.posX - (float)k, this.posY + 7.99F, 0.0F);
261
        GL11.glTexCoord2f(((float)i + f - 1.0F) / 128.0F, (float)j / 128.0F);
262
        GL11.glVertex3f(this.posX + f - 1.0F + (float)k, this.posY, 0.0F);
263
        GL11.glTexCoord2f(((float)i + f - 1.0F) / 128.0F, ((float)j + 7.99F) / 128.0F);
264
        GL11.glVertex3f(this.posX + f - 1.0F - (float)k, this.posY + 7.99F, 0.0F);
265
        GL11.glEnd();
266
        return (float)l;
267
    }
268
269
    private ResourceLocation getUnicodePageLocation(int page)
270
    {
271
        if (unicodePageLocations[page] == null)
272
        {
273
            unicodePageLocations[page] = new ResourceLocation(String.format("textures/font/unicode_page_%02x.png", new Object[] {Integer.valueOf(page)}));
274
        }
275
276
        return unicodePageLocations[page];
277
    }
278
279
    /**
280
     * Load one of the /font/glyph_XX.png into a new GL texture and store the texture ID in glyphTextureName array.
281
     */
282
    private void loadGlyphTexture(int page)
283
    {
284
        this.renderEngine.bindTexture(this.getUnicodePageLocation(page));
285
    }
286
287
    /**
288
     * Render a single Unicode character at current (posX,posY) location using one of the /font/glyph_XX.png files...
289
     */
290
    private float renderUnicodeChar(char ch, boolean italic)
291
    {
292
        if (this.glyphWidth[ch] == 0)
293
        {
294
            return 0.0F;
295
        }
296
        else
297
        {
298
            int i = ch / 256;
299
            this.loadGlyphTexture(i);
300
            int j = this.glyphWidth[ch] >>> 4;
301
            int k = this.glyphWidth[ch] & 15;
302
            float f = (float)j;
303
            float f1 = (float)(k + 1);
304
            float f2 = (float)(ch % 16 * 16) + f;
305
            float f3 = (float)((ch & 255) / 16 * 16);
306
            float f4 = f1 - f - 0.02F;
307
            float f5 = italic ? 1.0F : 0.0F;
308
            GL11.glBegin(GL11.GL_TRIANGLE_STRIP);
309
            GL11.glTexCoord2f(f2 / 256.0F, f3 / 256.0F);
310
            GL11.glVertex3f(this.posX + f5, this.posY, 0.0F);
311
            GL11.glTexCoord2f(f2 / 256.0F, (f3 + 15.98F) / 256.0F);
312
            GL11.glVertex3f(this.posX - f5, this.posY + 7.99F, 0.0F);
313
            GL11.glTexCoord2f((f2 + f4) / 256.0F, f3 / 256.0F);
314
            GL11.glVertex3f(this.posX + f4 / 2.0F + f5, this.posY, 0.0F);
315
            GL11.glTexCoord2f((f2 + f4) / 256.0F, (f3 + 15.98F) / 256.0F);
316
            GL11.glVertex3f(this.posX + f4 / 2.0F - f5, this.posY + 7.99F, 0.0F);
317
            GL11.glEnd();
318
            return (f1 - f) / 2.0F + 1.0F;
319
        }
320
    }
321
322
    /**
323
     * Draws the specified string with a shadow.
324
     */
325
    public int drawStringWithShadow(String text, float x, float y, int color)
326
    {
327
        return this.drawString(text, x, y, color, true);
328
    }
329
330
    /**
331
     * Draws the specified string.
332
     */
333
    public int drawString(String text, int x, int y, int color)
334
    {
335
        return this.drawString(text, (float)x, (float)y, color, false);
336
    }
337
338
    /**
339
     * Draws the specified string.
340
     */
341
    public int drawString(String text, float x, float y, int color, boolean dropShadow)
342
    {
343
        GlStateManager.enableAlpha();
344
        this.resetStyles();
345
        int i;
346
347
        if (dropShadow)
348
        {
349
            i = this.renderString(text, x + 1.0F, y + 1.0F, color, true);
350
            i = Math.max(i, this.renderString(text, x, y, color, false));
351
        }
352
        else
353
        {
354
            i = this.renderString(text, x, y, color, false);
355
        }
356
357
        return i;
358
    }
359
360
    /**
361
     * Apply Unicode Bidirectional Algorithm to string and return a new possibly reordered string for visual rendering.
362
     */
363
    private String bidiReorder(String text)
364
    {
365
        try
366
        {
367
            Bidi bidi = new Bidi((new ArabicShaping(8)).shape(text), 127);
368
            bidi.setReorderingMode(0);
369
            return bidi.writeReordered(2);
370
        }
371
        catch (ArabicShapingException var3)
372
        {
373
            return text;
374
        }
375
    }
376
377
    /**
378
     * Reset all style flag fields in the class to false; called at the start of string rendering
379
     */
380
    private void resetStyles()
381
    {
382
        this.randomStyle = false;
383
        this.boldStyle = false;
384
        this.italicStyle = false;
385
        this.underlineStyle = false;
386
        this.strikethroughStyle = false;
387
    }
388
389
    /**
390
     * Render a single line string at the current (posX,posY) and update posX
391
     */
392
    private void renderStringAtPos(String text, boolean shadow)
393
    {
394
        for (int i = 0; i < text.length(); ++i)
395
        {
396
            char c0 = text.charAt(i);
397
398
            if (c0 == 167 && i + 1 < text.length())
399
            {
400
                int i1 = "0123456789abcdefklmnor".indexOf(text.toLowerCase(Locale.ENGLISH).charAt(i + 1));
401
402
                if (i1 < 16)
403
                {
404
                    this.randomStyle = false;
405
                    this.boldStyle = false;
406
                    this.strikethroughStyle = false;
407
                    this.underlineStyle = false;
408
                    this.italicStyle = false;
409
410
                    if (i1 < 0 || i1 > 15)
411
                    {
412
                        i1 = 15;
413
                    }
414
415
                    if (shadow)
416
                    {
417
                        i1 += 16;
418
                    }
419
420
                    int j1 = this.colorCode[i1];
421
                    this.textColor = j1;
422
                    GlStateManager.color((float)(j1 >> 16) / 255.0F, (float)(j1 >> 8 & 255) / 255.0F, (float)(j1 & 255) / 255.0F, this.alpha);
423
                }
424
                else if (i1 == 16)
425
                {
426
                    this.randomStyle = true;
427
                }
428
                else if (i1 == 17)
429
                {
430
                    this.boldStyle = true;
431
                }
432
                else if (i1 == 18)
433
                {
434
                    this.strikethroughStyle = true;
435
                }
436
                else if (i1 == 19)
437
                {
438
                    this.underlineStyle = true;
439
                }
440
                else if (i1 == 20)
441
                {
442
                    this.italicStyle = true;
443
                }
444
                else if (i1 == 21)
445
                {
446
                    this.randomStyle = false;
447
                    this.boldStyle = false;
448
                    this.strikethroughStyle = false;
449
                    this.underlineStyle = false;
450
                    this.italicStyle = false;
451
                    GlStateManager.color(this.red, this.blue, this.green, this.alpha);
452
                }
453
454
                ++i;
455
            }
456
            else
457
            {
458
                int j = "\u00c0\u00c1\u00c2\u00c8\u00ca\u00cb\u00cd\u00d3\u00d4\u00d5\u00da\u00df\u00e3\u00f5\u011f\u0130\u0131\u0152\u0153\u015e\u015f\u0174\u0175\u017e\u0207\u0000\u0000\u0000\u0000\u0000\u0000\u0000 !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u0000\u00c7\u00fc\u00e9\u00e2\u00e4\u00e0\u00e5\u00e7\u00ea\u00eb\u00e8\u00ef\u00ee\u00ec\u00c4\u00c5\u00c9\u00e6\u00c6\u00f4\u00f6\u00f2\u00fb\u00f9\u00ff\u00d6\u00dc\u00f8\u00a3\u00d8\u00d7\u0192\u00e1\u00ed\u00f3\u00fa\u00f1\u00d1\u00aa\u00ba\u00bf\u00ae\u00ac\u00bd\u00bc\u00a1\u00ab\u00bb\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556\u2555\u2563\u2551\u2557\u255d\u255c\u255b\u2510\u2514\u2534\u252c\u251c\u2500\u253c\u255e\u255f\u255a\u2554\u2569\u2566\u2560\u2550\u256c\u2567\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256b\u256a\u2518\u250c\u2588\u2584\u258c\u2590\u2580\u03b1\u03b2\u0393\u03c0\u03a3\u03c3\u03bc\u03c4\u03a6\u0398\u03a9\u03b4\u221e\u2205\u2208\u2229\u2261\u00b1\u2265\u2264\u2320\u2321\u00f7\u2248\u00b0\u2219\u00b7\u221a\u207f\u00b2\u25a0\u0000".indexOf(c0);
459
460
                if (this.randomStyle && j != -1)
461
                {
462
                    int k = this.getCharWidth(c0);
463
                    char c1;
464
465
                    while (true)
466
                    {
467
                        j = this.fontRandom.nextInt("\u00c0\u00c1\u00c2\u00c8\u00ca\u00cb\u00cd\u00d3\u00d4\u00d5\u00da\u00df\u00e3\u00f5\u011f\u0130\u0131\u0152\u0153\u015e\u015f\u0174\u0175\u017e\u0207\u0000\u0000\u0000\u0000\u0000\u0000\u0000 !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u0000\u00c7\u00fc\u00e9\u00e2\u00e4\u00e0\u00e5\u00e7\u00ea\u00eb\u00e8\u00ef\u00ee\u00ec\u00c4\u00c5\u00c9\u00e6\u00c6\u00f4\u00f6\u00f2\u00fb\u00f9\u00ff\u00d6\u00dc\u00f8\u00a3\u00d8\u00d7\u0192\u00e1\u00ed\u00f3\u00fa\u00f1\u00d1\u00aa\u00ba\u00bf\u00ae\u00ac\u00bd\u00bc\u00a1\u00ab\u00bb\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556\u2555\u2563\u2551\u2557\u255d\u255c\u255b\u2510\u2514\u2534\u252c\u251c\u2500\u253c\u255e\u255f\u255a\u2554\u2569\u2566\u2560\u2550\u256c\u2567\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256b\u256a\u2518\u250c\u2588\u2584\u258c\u2590\u2580\u03b1\u03b2\u0393\u03c0\u03a3\u03c3\u03bc\u03c4\u03a6\u0398\u03a9\u03b4\u221e\u2205\u2208\u2229\u2261\u00b1\u2265\u2264\u2320\u2321\u00f7\u2248\u00b0\u2219\u00b7\u221a\u207f\u00b2\u25a0\u0000".length());
468
                        c1 = "\u00c0\u00c1\u00c2\u00c8\u00ca\u00cb\u00cd\u00d3\u00d4\u00d5\u00da\u00df\u00e3\u00f5\u011f\u0130\u0131\u0152\u0153\u015e\u015f\u0174\u0175\u017e\u0207\u0000\u0000\u0000\u0000\u0000\u0000\u0000 !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u0000\u00c7\u00fc\u00e9\u00e2\u00e4\u00e0\u00e5\u00e7\u00ea\u00eb\u00e8\u00ef\u00ee\u00ec\u00c4\u00c5\u00c9\u00e6\u00c6\u00f4\u00f6\u00f2\u00fb\u00f9\u00ff\u00d6\u00dc\u00f8\u00a3\u00d8\u00d7\u0192\u00e1\u00ed\u00f3\u00fa\u00f1\u00d1\u00aa\u00ba\u00bf\u00ae\u00ac\u00bd\u00bc\u00a1\u00ab\u00bb\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556\u2555\u2563\u2551\u2557\u255d\u255c\u255b\u2510\u2514\u2534\u252c\u251c\u2500\u253c\u255e\u255f\u255a\u2554\u2569\u2566\u2560\u2550\u256c\u2567\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256b\u256a\u2518\u250c\u2588\u2584\u258c\u2590\u2580\u03b1\u03b2\u0393\u03c0\u03a3\u03c3\u03bc\u03c4\u03a6\u0398\u03a9\u03b4\u221e\u2205\u2208\u2229\u2261\u00b1\u2265\u2264\u2320\u2321\u00f7\u2248\u00b0\u2219\u00b7\u221a\u207f\u00b2\u25a0\u0000".charAt(j);
469
470
                        if (k == this.getCharWidth(c1))
471
                        {
472
                            break;
473
                        }
474
                    }
475
476
                    c0 = c1;
477
                }
478
479
                float f1 = this.unicodeFlag ? 0.5F : 1.0F;
480
                boolean flag = (c0 == 0 || j == -1 || this.unicodeFlag) && shadow;
481
482
                if (flag)
483
                {
484
                    this.posX -= f1;
485
                    this.posY -= f1;
486
                }
487
488
                float f = this.renderChar(c0, this.italicStyle);
489
490
                if (flag)
491
                {
492
                    this.posX += f1;
493
                    this.posY += f1;
494
                }
495
496
                if (this.boldStyle)
497
                {
498
                    this.posX += f1;
499
500
                    if (flag)
501
                    {
502
                        this.posX -= f1;
503
                        this.posY -= f1;
504
                    }
505
506
                    this.renderChar(c0, this.italicStyle);
507
                    this.posX -= f1;
508
509
                    if (flag)
510
                    {
511
                        this.posX += f1;
512
                        this.posY += f1;
513
                    }
514
515
                    ++f;
516
                }
517
518
                if (this.strikethroughStyle)
519
                {
520
                    Tessellator tessellator = Tessellator.getInstance();
521
                    WorldRenderer worldrenderer = tessellator.getWorldRenderer();
522
                    GlStateManager.disableTexture2D();
523
                    worldrenderer.begin(7, DefaultVertexFormats.POSITION);
524
                    worldrenderer.pos((double)this.posX, (double)(this.posY + (float)(this.FONT_HEIGHT / 2)), 0.0D).endVertex();
525
                    worldrenderer.pos((double)(this.posX + f), (double)(this.posY + (float)(this.FONT_HEIGHT / 2)), 0.0D).endVertex();
526
                    worldrenderer.pos((double)(this.posX + f), (double)(this.posY + (float)(this.FONT_HEIGHT / 2) - 1.0F), 0.0D).endVertex();
527
                    worldrenderer.pos((double)this.posX, (double)(this.posY + (float)(this.FONT_HEIGHT / 2) - 1.0F), 0.0D).endVertex();
528
                    tessellator.draw();
529
                    GlStateManager.enableTexture2D();
530
                }
531
532
                if (this.underlineStyle)
533
                {
534
                    Tessellator tessellator1 = Tessellator.getInstance();
535
                    WorldRenderer worldrenderer1 = tessellator1.getWorldRenderer();
536
                    GlStateManager.disableTexture2D();
537
                    worldrenderer1.begin(7, DefaultVertexFormats.POSITION);
538
                    int l = this.underlineStyle ? -1 : 0;
539
                    worldrenderer1.pos((double)(this.posX + (float)l), (double)(this.posY + (float)this.FONT_HEIGHT), 0.0D).endVertex();
540
                    worldrenderer1.pos((double)(this.posX + f), (double)(this.posY + (float)this.FONT_HEIGHT), 0.0D).endVertex();
541
                    worldrenderer1.pos((double)(this.posX + f), (double)(this.posY + (float)this.FONT_HEIGHT - 1.0F), 0.0D).endVertex();
542
                    worldrenderer1.pos((double)(this.posX + (float)l), (double)(this.posY + (float)this.FONT_HEIGHT - 1.0F), 0.0D).endVertex();
543
                    tessellator1.draw();
544
                    GlStateManager.enableTexture2D();
545
                }
546
547
                this.posX += (float)((int)f);
548
            }
549
        }
550
    }
551
552
    /**
553
     * Render string either left or right aligned depending on bidiFlag
554
     */
555
    private int renderStringAligned(String text, int x, int y, int width, int color, boolean dropShadow)
556
    {
557
        if (this.bidiFlag)
558
        {
559
            int i = this.getStringWidth(this.bidiReorder(text));
560
            x = x + width - i;
561
        }
562
563
        return this.renderString(text, (float)x, (float)y, color, dropShadow);
564
    }
565
566
    /**
567
     * Render single line string by setting GL color, current (posX,posY), and calling renderStringAtPos()
568
     */
569
    private int renderString(String text, float x, float y, int color, boolean dropShadow)
570
    {
571
        if (text == null)
572
        {
573
            return 0;
574
        }
575
        else
576
        {
577
            if (this.bidiFlag)
578
            {
579
                text = this.bidiReorder(text);
580
            }
581
582
            if ((color & -67108864) == 0)
583
            {
584
                color |= -16777216;
585
            }
586
587
            if (dropShadow)
588
            {
589
                color = (color & 16579836) >> 2 | color & -16777216;
590
            }
591
592
            this.red = (float)(color >> 16 & 255) / 255.0F;
593
            this.blue = (float)(color >> 8 & 255) / 255.0F;
594
            this.green = (float)(color & 255) / 255.0F;
595
            this.alpha = (float)(color >> 24 & 255) / 255.0F;
596
            GlStateManager.color(this.red, this.blue, this.green, this.alpha);
597
            this.posX = x;
598
            this.posY = y;
599
            this.renderStringAtPos(text, dropShadow);
600
            return (int)this.posX;
601
        }
602
    }
603
604
    /**
605
     * Returns the width of this string. Equivalent of FontMetrics.stringWidth(String s).
606
     */
607
    public int getStringWidth(String text)
608
    {
609
        if (text == null)
610
        {
611
            return 0;
612
        }
613
        else
614
        {
615
            int i = 0;
616
            boolean flag = false;
617
618
            for (int j = 0; j < text.length(); ++j)
619
            {
620
                char c0 = text.charAt(j);
621
                int k = this.getCharWidth(c0);
622
623
                if (k < 0 && j < text.length() - 1)
624
                {
625
                    ++j;
626
                    c0 = text.charAt(j);
627
628
                    if (c0 != 108 && c0 != 76)
629
                    {
630
                        if (c0 == 114 || c0 == 82)
631
                        {
632
                            flag = false;
633
                        }
634
                    }
635
                    else
636
                    {
637
                        flag = true;
638
                    }
639
640
                    k = 0;
641
                }
642
643
                i += k;
644
645
                if (flag && k > 0)
646
                {
647
                    ++i;
648
                }
649
            }
650
651
            return i;
652
        }
653
    }
654
655
    /**
656
     * Returns the width of this character as rendered.
657
     */
658
    public int getCharWidth(char character)
659
    {
660
        if (character == 167)
661
        {
662
            return -1;
663
        }
664
        else if (character == 32)
665
        {
666
            return 4;
667
        }
668
        else
669
        {
670
            int i = "\u00c0\u00c1\u00c2\u00c8\u00ca\u00cb\u00cd\u00d3\u00d4\u00d5\u00da\u00df\u00e3\u00f5\u011f\u0130\u0131\u0152\u0153\u015e\u015f\u0174\u0175\u017e\u0207\u0000\u0000\u0000\u0000\u0000\u0000\u0000 !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u0000\u00c7\u00fc\u00e9\u00e2\u00e4\u00e0\u00e5\u00e7\u00ea\u00eb\u00e8\u00ef\u00ee\u00ec\u00c4\u00c5\u00c9\u00e6\u00c6\u00f4\u00f6\u00f2\u00fb\u00f9\u00ff\u00d6\u00dc\u00f8\u00a3\u00d8\u00d7\u0192\u00e1\u00ed\u00f3\u00fa\u00f1\u00d1\u00aa\u00ba\u00bf\u00ae\u00ac\u00bd\u00bc\u00a1\u00ab\u00bb\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556\u2555\u2563\u2551\u2557\u255d\u255c\u255b\u2510\u2514\u2534\u252c\u251c\u2500\u253c\u255e\u255f\u255a\u2554\u2569\u2566\u2560\u2550\u256c\u2567\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256b\u256a\u2518\u250c\u2588\u2584\u258c\u2590\u2580\u03b1\u03b2\u0393\u03c0\u03a3\u03c3\u03bc\u03c4\u03a6\u0398\u03a9\u03b4\u221e\u2205\u2208\u2229\u2261\u00b1\u2265\u2264\u2320\u2321\u00f7\u2248\u00b0\u2219\u00b7\u221a\u207f\u00b2\u25a0\u0000".indexOf(character);
671
672
            if (character > 0 && i != -1 && !this.unicodeFlag)
673
            {
674
                return this.charWidth[i];
675
            }
676
            else if (this.glyphWidth[character] != 0)
677
            {
678
                int j = this.glyphWidth[character] >>> 4;
679
                int k = this.glyphWidth[character] & 15;
680
681
                if (k > 7)
682
                {
683
                    k = 15;
684
                    j = 0;
685
                }
686
687
                ++k;
688
                return (k - j) / 2 + 1;
689
            }
690
            else
691
            {
692
                return 0;
693
            }
694
        }
695
    }
696
697
    /**
698
     * Trims a string to fit a specified Width.
699
     */
700
    public String trimStringToWidth(String text, int width)
701
    {
702
        return this.trimStringToWidth(text, width, false);
703
    }
704
705
    /**
706
     * Trims a string to a specified width, and will reverse it if par3 is set.
707
     */
708
    public String trimStringToWidth(String text, int width, boolean reverse)
709
    {
710
        StringBuilder stringbuilder = new StringBuilder();
711
        int i = 0;
712
        int j = reverse ? text.length() - 1 : 0;
713
        int k = reverse ? -1 : 1;
714
        boolean flag = false;
715
        boolean flag1 = false;
716
717
        for (int l = j; l >= 0 && l < text.length() && i < width; l += k)
718
        {
719
            char c0 = text.charAt(l);
720
            int i1 = this.getCharWidth(c0);
721
722
            if (flag)
723
            {
724
                flag = false;
725
726
                if (c0 != 108 && c0 != 76)
727
                {
728
                    if (c0 == 114 || c0 == 82)
729
                    {
730
                        flag1 = false;
731
                    }
732
                }
733
                else
734
                {
735
                    flag1 = true;
736
                }
737
            }
738
            else if (i1 < 0)
739
            {
740
                flag = true;
741
            }
742
            else
743
            {
744
                i += i1;
745
746
                if (flag1)
747
                {
748
                    ++i;
749
                }
750
            }
751
752
            if (i > width)
753
            {
754
                break;
755
            }
756
757
            if (reverse)
758
            {
759
                stringbuilder.insert(0, (char)c0);
760
            }
761
            else
762
            {
763
                stringbuilder.append(c0);
764
            }
765
        }
766
767
        return stringbuilder.toString();
768
    }
769
770
    /**
771
     * Remove all newline characters from the end of the string
772
     */
773
    private String trimStringNewline(String text)
774
    {
775
        while (text != null && text.endsWith("\n"))
776
        {
777
            text = text.substring(0, text.length() - 1);
778
        }
779
780
        return text;
781
    }
782
783
    /**
784
     * Splits and draws a String with wordwrap (maximum length is parameter k)
785
     */
786
    public void drawSplitString(String str, int x, int y, int wrapWidth, int textColor)
787
    {
788
        this.resetStyles();
789
        this.textColor = textColor;
790
        str = this.trimStringNewline(str);
791
        this.renderSplitString(str, x, y, wrapWidth, false);
792
    }
793
794
    /**
795
     * Perform actual work of rendering a multi-line string with wordwrap and with darker drop shadow color if flag is
796
     * set
797
     */
798
    private void renderSplitString(String str, int x, int y, int wrapWidth, boolean addShadow)
799
    {
800
        for (String s : this.listFormattedStringToWidth(str, wrapWidth))
801
        {
802
            this.renderStringAligned(s, x, y, wrapWidth, this.textColor, addShadow);
803
            y += this.FONT_HEIGHT;
804
        }
805
    }
806
807
    /**
808
     * Returns the width of the wordwrapped String (maximum length is parameter k)
809
     *  
810
     * @param str The string to split
811
     * @param maxLength The maximum length of a word
812
     */
813
    public int splitStringWidth(String str, int maxLength)
814
    {
815
        return this.FONT_HEIGHT * this.listFormattedStringToWidth(str, maxLength).size();
816
    }
817
818
    /**
819
     * Set unicodeFlag controlling whether strings should be rendered with Unicode fonts instead of the default.png
820
     * font.
821
     */
822
    public void setUnicodeFlag(boolean unicodeFlagIn)
823
    {
824
        this.unicodeFlag = unicodeFlagIn;
825
    }
826
827
    /**
828
     * Get unicodeFlag controlling whether strings should be rendered with Unicode fonts instead of the default.png
829
     * font.
830
     */
831
    public boolean getUnicodeFlag()
832
    {
833
        return this.unicodeFlag;
834
    }
835
836
    /**
837
     * Set bidiFlag to control if the Unicode Bidirectional Algorithm should be run before rendering any string.
838
     */
839
    public void setBidiFlag(boolean bidiFlagIn)
840
    {
841
        this.bidiFlag = bidiFlagIn;
842
    }
843
844
    public List<String> listFormattedStringToWidth(String str, int wrapWidth)
845
    {
846
        return Arrays.<String>asList(this.wrapFormattedStringToWidth(str, wrapWidth).split("\n"));
847
    }
848
849
    /**
850
     * Inserts newline and formatting into a string to wrap it within the specified width.
851
     */
852
    String wrapFormattedStringToWidth(String str, int wrapWidth)
853
    {
854
        int i = this.sizeStringToWidth(str, wrapWidth);
855
856
        if (str.length() <= i)
857
        {
858
            return str;
859
        }
860
        else
861
        {
862
            String s = str.substring(0, i);
863
            char c0 = str.charAt(i);
864
            boolean flag = c0 == 32 || c0 == 10;
865
            String s1 = getFormatFromString(s) + str.substring(i + (flag ? 1 : 0));
866
            return s + "\n" + this.wrapFormattedStringToWidth(s1, wrapWidth);
867
        }
868
    }
869
870
    /**
871
     * Determines how many characters from the string will fit into the specified width.
872
     */
873
    private int sizeStringToWidth(String str, int wrapWidth)
874
    {
875
        int i = str.length();
876
        int j = 0;
877
        int k = 0;
878
        int l = -1;
879
880
        for (boolean flag = false; k < i; ++k)
881
        {
882
            char c0 = str.charAt(k);
883
884
            switch (c0)
885
            {
886
                case '\n':
887
                    --k;
888
                    break;
889
890
                case ' ':
891
                    l = k;
892
893
                default:
894
                    j += this.getCharWidth(c0);
895
896
                    if (flag)
897
                    {
898
                        ++j;
899
                    }
900
901
                    break;
902
903
                case '\u00a7':
904
                    if (k < i - 1)
905
                    {
906
                        ++k;
907
                        char c1 = str.charAt(k);
908
909
                        if (c1 != 108 && c1 != 76)
910
                        {
911
                            if (c1 == 114 || c1 == 82 || isFormatColor(c1))
912
                            {
913
                                flag = false;
914
                            }
915
                        }
916
                        else
917
                        {
918
                            flag = true;
919
                        }
920
                    }
921
            }
922
923
            if (c0 == 10)
924
            {
925
                ++k;
926
                l = k;
927
                break;
928
            }
929
930
            if (j > wrapWidth)
931
            {
932
                break;
933
            }
934
        }
935
936
        return k != i && l != -1 && l < k ? l : k;
937
    }
938
939
    /**
940
     * Checks if the char code is a hexadecimal character, used to set colour.
941
     */
942
    private static boolean isFormatColor(char colorChar)
943
    {
944
        return colorChar >= 48 && colorChar <= 57 || colorChar >= 97 && colorChar <= 102 || colorChar >= 65 && colorChar <= 70;
945
    }
946
947
    /**
948
     * Checks if the char code is O-K...lLrRk-o... used to set special formatting.
949
     */
950
    private static boolean isFormatSpecial(char formatChar)
951
    {
952
        return formatChar >= 107 && formatChar <= 111 || formatChar >= 75 && formatChar <= 79 || formatChar == 114 || formatChar == 82;
953
    }
954
955
    /**
956
     * Digests a string for nonprinting formatting characters then returns a string containing only that formatting.
957
     */
958
    public static String getFormatFromString(String text)
959
    {
960
        String s = "";
961
        int i = -1;
962
        int j = text.length();
963
964
        while ((i = text.indexOf(167, i + 1)) != -1)
965
        {
966
            if (i < j - 1)
967
            {
968
                char c0 = text.charAt(i + 1);
969
970
                if (isFormatColor(c0))
971
                {
972
                    s = "\u00a7" + c0;
973
                }
974
                else if (isFormatSpecial(c0))
975
                {
976
                    s = s + "\u00a7" + c0;
977
                }
978
            }
979
        }
980
981
        return s;
982
    }
983
984
    /**
985
     * Get bidiFlag that controls if the Unicode Bidirectional Algorithm should be run before rendering any string
986
     */
987
    public boolean getBidiFlag()
988
    {
989
        return this.bidiFlag;
990
    }
991
992
    public int getColorCode(char character)
993
    {
994
        return this.colorCode["0123456789abcdef".indexOf(character)];
995
    }
996
}
997

Deobfuscated MCP 1.8.9 source of FontRenderer from Marcelektro/MavenMCP-1.8.9 (net/minecraft/client/gui/FontRenderer.java), stored verbatim.