-
Notifications
You must be signed in to change notification settings - Fork 755
/
CustomFont.java
283 lines (264 loc) · 9.39 KB
/
CustomFont.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package tconstruct.client;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.Tessellator;
import org.lwjgl.opengl.GL11;
/**
*
* @author TheObliterator
*
* A class to create and draw true type
* fonts onto the Minecraft game engine.
*/
public class CustomFont
{
private int texID;
private int[] xPos;
private int[] yPos;
private int startChar;
private int endChar;
private FontMetrics metrics;
/**
* Instantiates the font, filling in default start
* and end character parameters.
*
* 'new CustomFont(ModLoader.getMinecraftInstance(),
* "Arial", 12);
*
* @param mc The Minecraft instance for the font to be bound to.
* @param fontName The name of the font to be drawn.
* @param size The size of the font to be drawn.
*/
public CustomFont(Minecraft mc, Object font, int size)
{
this(mc, font, size, 0, 4000);
}
/**
* Instantiates the font, pre-rendering a sprite
* font image by using a true type font on a
* bitmap. Then allocating that bitmap to the
* Minecraft rendering engine for later use.
*
* 'new CustomFont(ModLoader.getMinecraftInstance(),
* "Arial", 12, 32, 126);'
*
* @param mc The Minecraft instance for the font to be bound to.
* @param fontName The name of the font to be drawn.
* @param size The size of the font to be drawn.
* @param startChar The starting ASCII character id to be drawable. (Default 32)
* @param endChar The ending ASCII character id to be drawable. (Default 126)
*/
public CustomFont(Minecraft mc, Object font, int size, int startChar, int endChar)
{
this.startChar = startChar;
this.endChar = endChar;
xPos = new int[endChar - startChar];
yPos = new int[endChar - startChar];
// Create a bitmap and fill it with a transparent color as well
// as obtain a Graphics instance which can be drawn on.
// NOTE: It is CRUICIAL that the size of the image is 256x256, if
// it is not the Minecraft engine will not draw it properly.
BufferedImage img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
try
{
if (font instanceof String)
{
String fontName = (String) font;
if (fontName.contains("/"))
g.setFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontName)).deriveFont((float) size));
else
g.setFont(new Font(fontName, 0, size));
}
else if (font instanceof InputStream)
{
g.setFont(Font.createFont(Font.TRUETYPE_FONT, (InputStream) font).deriveFont((float) size));
}
else if (font instanceof File)
{
g.setFont(Font.createFont(Font.TRUETYPE_FONT, (File) font).deriveFont((float) size));
}
}
catch (Exception e)
{
e.printStackTrace();
}
g.setColor(new Color(255, 255, 255, 0));
g.fillRect(0, 0, 256, 256);
g.setColor(Color.white);
metrics = g.getFontMetrics();
// Draw the specified range of characters onto
// the new bitmap, spacing according to the font
// widths. Also allocating positions of characters
// on the bitmap to two arrays which will be used
// later when drawing.
int x = 2;
int y = 2;
for (int i = startChar; i < endChar; i++)
{
g.drawString("" + ((char) i), x, y + g.getFontMetrics().getAscent());
xPos[i - startChar] = x;
yPos[i - startChar] = y - metrics.getMaxDescent();
x += metrics.stringWidth("" + (char) i) + 2;
if (x >= 250 - metrics.getMaxAdvance())
{
x = 2;
y += metrics.getMaxAscent() + metrics.getMaxDescent() + size / 2;
}
}
// Render the finished bitmap into the Minecraft
// graphics engine.
// Sadly broken by 1.6
//texID = mc.renderEngine.allocateAndSetupTexture(img);
}
/**
* Draws a given string with an automatically
* calculated shadow below it.
* @param gui The gui/subclass to be drawn on
* @param text The string to be drawn
* @param x The x position to start drawing
* @param y The y position to start drawing
* @param color The color of the non-shadowed text (Hex)
*/
public void drawStringS (Gui gui, String text, int x, int y, int color)
{
int l = color & 0xff000000;
int shade = (color & 0xfcfcfc) >> 2;
shade += l;
drawString(gui, text, x + 1, y + 1, shade);
drawString(gui, text, x, y, color);
}
/**
* Draws a given string onto a gui/subclass.
* @param gui The gui/subclass to be drawn on
* @param text The string to be drawn
* @param x The x position to start drawing
* @param y The y position to start drawing
* @param color The color of the non-shadowed text (Hex)
*/
public void drawString (Gui gui, String text, int x, int y, int color)
{
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
GL11.glEnable(3553 /*GL_TEXTURE_2D*/);
GL11.glBindTexture(3553 /*GL_TEXTURE_2D*/, texID);
float red = (float) (color >> 16 & 0xff) / 255F;
float green = (float) (color >> 8 & 0xff) / 255F;
float blue = (float) (color & 0xff) / 255F;
float alpha = (float) (color >> 24 & 0xff) / 255F;
GL11.glColor4f(red, green, blue, alpha);
int startX = x;
for (int i = 0; i < text.length(); i++)
{
char c = text.charAt(i);
if (c == '\\')
{
char type = text.charAt(i + 1);
if (type == 'n')
{
y += metrics.getAscent() + 2;
x = startX;
}
i++;
continue;
}
drawChar(gui, c, x, y);
x += metrics.getStringBounds("" + c, null).getWidth();
}
}
/**
* Returns the created FontMetrics
* which is used to retrive various
* information about the True Type Font
* @return FontMetrics of the created font.
*/
public FontMetrics getMetrics ()
{
return metrics;
}
/**
* Gets the drawing width of a given
* string of string.
* @param text The string to be measured
* @return The width of the given string.
*/
public int getStringWidth (String text)
{
return (int) getBounds(text).getWidth();
}
/**
* Gets the drawing height of a given
* string of string.
* @param text The string to be measured
* @return The height of the given string.
*/
public int getStringHeight (String text)
{
return (int) getBounds(text).getHeight();
}
/**
* A method that returns a Rectangle that
* contains the width and height demensions
* of the given string.
* @param text The string to be measured
* @return Rectangle containing width and height that
* the text will consume when drawn.
*/
private Rectangle getBounds (String text)
{
int w = 0;
int h = 0;
int tw = 0;
for (int i = 0; i < text.length(); i++)
{
char c = text.charAt(i);
if (c == '\\')
{
char type = text.charAt(i + 1);
if (type == 'n')
{
h += metrics.getAscent() + 2;
if (tw > w)
w = tw;
tw = 0;
}
i++;
continue;
}
tw += metrics.stringWidth("" + c);
}
if (tw > w)
w = tw;
h += metrics.getAscent();
return new Rectangle(0, 0, w, h);
}
/**
* Private drawing method used within other
* drawing methods.
*/
private void drawChar (Gui gui, char c, int x, int y)
{
Rectangle2D bounds = metrics.getStringBounds("" + c, null);
drawTexturedModalRect(x, y, xPos[(byte) c - startChar], yPos[(byte) c - startChar], (int) bounds.getWidth(), (int) bounds.getHeight() + metrics.getMaxDescent());
}
public void drawTexturedModalRect (int x, int y, int u, int v, int width, int height)
{
float offsetWidth = 0.00390625F;
float offsetHeight = 0.00390625F;
Tessellator tessellator = Tessellator.instance;
tessellator.startDrawingQuads();
tessellator.addVertexWithUV((x + 0), (y + height), 0, ((u + 0) * offsetWidth), ((v + height) * offsetHeight));
tessellator.addVertexWithUV((x + width), (y + height), 0, ((u + width) * offsetWidth), ((v + height) * offsetHeight));
tessellator.addVertexWithUV((x + width), (y + 0), 0, ((u + width) * offsetWidth), ((v + 0) * offsetHeight));
tessellator.addVertexWithUV((x + 0), (y + 0), 0, ((u + 0) * offsetWidth), ((v + 0) * offsetHeight));
tessellator.draw();
}
}