-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCFont.java
208 lines (175 loc) · 6.45 KB
/
CFont.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
package CreidtMe.Packege;
import net.minecraft.client.renderer.texture.DynamicTexture;
import org.lwjgl.opengl.GL11;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
public class CFont
{
private float imgSize = 512;
protected CharData[] charData = new CharData[256];
protected Font font;
protected boolean antiAlias;
protected boolean fractionalMetrics;
protected int fontHeight = -1;
protected int charOffset = 0;
protected DynamicTexture tex;
public CFont(Font font, boolean antiAlias, boolean fractionalMetrics)
{
this.font = font;
this.antiAlias = antiAlias;
this.fractionalMetrics = fractionalMetrics;
tex = setupTexture(font, antiAlias, fractionalMetrics, this.charData);
}
public DynamicTexture setupTexture(Font font, boolean antiAlias, boolean fractionalMetrics, CharData[] chars)
{
BufferedImage img = generateFontImage(font, antiAlias, fractionalMetrics, chars);
try
{
return new DynamicTexture(img);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
protected BufferedImage generateFontImage(Font font, boolean antiAlias, boolean fractionalMetrics, CharData[] chars)
{
int imgSize = (int) this.imgSize;
BufferedImage bufferedImage = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
g.setFont(font);
g.setColor(new Color(255, 255, 255, 0));
g.fillRect(0, 0, imgSize, imgSize);
g.setColor(Color.WHITE);
g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON : RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAlias ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antiAlias ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
FontMetrics fontMetrics = g.getFontMetrics();
int charHeight = 0;
int positionX = 0;
int positionY = 1;
for (int i = 0; i < chars.length; i++)
{
char ch = (char) i;
CharData charData = new CharData();
Rectangle2D dimensions = fontMetrics.getStringBounds(String.valueOf(ch), g);
charData.width = (dimensions.getBounds().width + 8);
charData.height = dimensions.getBounds().height;
if (positionX + charData.width >= imgSize)
{
positionX = 0;
positionY += charHeight;
charHeight = 0;
}
if (charData.height > charHeight)
{
charHeight = charData.height;
}
charData.storedX = positionX;
charData.storedY = positionY;
if (charData.height > this.fontHeight)
{
this.fontHeight = charData.height;
}
chars[i] = charData;
g.drawString(String.valueOf(ch), positionX + 2, positionY + fontMetrics.getAscent());
positionX += charData.width;
}
return bufferedImage;
}
public void drawChar(CharData[] chars, char c, float x, float y) throws ArrayIndexOutOfBoundsException
{
try
{
drawQuad(x, y, chars[c].width, chars[c].height, chars[c].storedX, chars[c].storedY, chars[c].width, chars[c].height);
}
catch (Exception e)
{
e.printStackTrace();
}
}
protected void drawQuad(float x, float y, float width, float height, float srcX, float srcY, float srcWidth, float srcHeight)
{
float renderSRCX = srcX / imgSize;
float renderSRCY = srcY / imgSize;
float renderSRCWidth = srcWidth / imgSize;
float renderSRCHeight = srcHeight / imgSize;
GL11.glTexCoord2f(renderSRCX + renderSRCWidth, renderSRCY);
GL11.glVertex2d(x + width, y);
GL11.glTexCoord2f(renderSRCX, renderSRCY);
GL11.glVertex2d(x, y);
GL11.glTexCoord2f(renderSRCX, renderSRCY + renderSRCHeight);
GL11.glVertex2d(x, y + height);
GL11.glTexCoord2f(renderSRCX, renderSRCY + renderSRCHeight);
GL11.glVertex2d(x, y + height);
GL11.glTexCoord2f(renderSRCX + renderSRCWidth, renderSRCY + renderSRCHeight);
GL11.glVertex2d(x + width, y + height);
GL11.glTexCoord2f(renderSRCX + renderSRCWidth, renderSRCY);
GL11.glVertex2d(x + width, y);
}
public int getStringHeight(String text)
{
return getHeight();
}
public int getHeight()
{
return (this.fontHeight - 8) / 2;
}
public int getStringWidth(String text)
{
int width = 0;
for (char c : text.toCharArray())
{
if ((c < this.charData.length) && (c >= 0))
{
width += this.charData[c].width - 8 + this.charOffset;
}
}
return width / 2;
}
public boolean isAntiAlias()
{
return this.antiAlias;
}
public void setAntiAlias(boolean antiAlias)
{
if (this.antiAlias != antiAlias)
{
this.antiAlias = antiAlias;
tex = setupTexture(this.font, antiAlias, this.fractionalMetrics, this.charData);
}
}
public boolean isFractionalMetrics()
{
return this.fractionalMetrics;
}
public void setFractionalMetrics(boolean fractionalMetrics)
{
if (this.fractionalMetrics != fractionalMetrics)
{
this.fractionalMetrics = fractionalMetrics;
tex = setupTexture(this.font, this.antiAlias, fractionalMetrics, this.charData);
}
}
public Font getFont()
{
return this.font;
}
public void setFont(Font font)
{
this.font = font;
tex = setupTexture(font, this.antiAlias, this.fractionalMetrics, this.charData);
}
protected class CharData
{
public int width;
public int height;
public int storedX;
public int storedY;
protected CharData()
{
}
}
}