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
|
package net.tylermurphy.Minecraft.UI.Text;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import net.tylermurphy.Minecraft.Util.Constants;
import net.tylermurphy.Minecraft.Render.Data.Display;
public class MetaFile {
private static final int PAD_TOP = 0;
private static final int PAD_LEFT = 1;
private static final int PAD_BOTTOM = 2;
private static final int PAD_RIGHT = 3;
private static final int DESIRED_PADDING = 8;
private static final String SPLITTER = " ";
private static final String NUMBER_SEPARATOR = ",";
private double aspectRatio;
private double verticalPerPixelSize;
private double horizontalPerPixelSize;
private double spaceWidth;
private int[] padding;
private int paddingWidth;
private int paddingHeight;
private Map<Integer, Character> metaData = new HashMap<Integer, Character>();
private BufferedReader reader;
private Map<String, String> values = new HashMap<String, String>();
protected MetaFile(String file) {
this.aspectRatio = (double) Display.getWidth() / (double) Display.getHeight();
openFile(file);
loadPaddingData();
loadLineSizes();
int imageWidth = getValueOfVariable("scaleW");
loadCharacterData(imageWidth);
close();
}
protected double getSpaceWidth() {
return spaceWidth;
}
protected Character getCharacter(int ascii) {
return metaData.get(ascii);
}
private boolean processNextLine() {
values.clear();
String line = null;
try {
line = reader.readLine();
} catch (IOException e1) {
}
if (line == null) {
return false;
}
for (String part : line.split(SPLITTER)) {
String[] valuePairs = part.split("=");
if (valuePairs.length == 2) {
values.put(valuePairs[0], valuePairs[1]);
}
}
return true;
}
private int getValueOfVariable(String variable) {
int i = 0;
try {
i = Integer.parseInt(values.get(variable));
}catch(Exception e) {
e.printStackTrace();
}
return i;
}
private int[] getValuesOfVariable(String variable) {
String[] numbers = values.get(variable).split(NUMBER_SEPARATOR);
int[] actualValues = new int[numbers.length];
for (int i = 0; i < actualValues.length; i++) {
actualValues[i] = Integer.parseInt(numbers[i]);
}
return actualValues;
}
private void close() {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void openFile(String file) {
try {
FileReader isr = new FileReader(new File(Constants.FNT_LOCATION + file + ".fnt"));
reader = new BufferedReader(isr);
} catch (Exception e) {
e.printStackTrace();
System.err.println("Couldn't read font meta file!");
}
}
private void loadPaddingData() {
processNextLine();
this.padding = getValuesOfVariable("padding");
this.paddingWidth = padding[PAD_LEFT] + padding[PAD_RIGHT];
this.paddingHeight = padding[PAD_TOP] + padding[PAD_BOTTOM];
}
private void loadLineSizes() {
processNextLine();
int lineHeightPixels = getValueOfVariable("lineHeight") - paddingHeight;
verticalPerPixelSize = TextMeshCreator.LINE_HEIGHT / (double) lineHeightPixels;
horizontalPerPixelSize = verticalPerPixelSize / aspectRatio;
}
private void loadCharacterData(int imageWidth) {
processNextLine();
processNextLine();
while (processNextLine()) {
Character c = loadCharacter(imageWidth);
if (c != null) {
metaData.put(c.getId(), c);
}
}
}
private Character loadCharacter(int imageSize) {
int id = getValueOfVariable("id");
if (id == TextMeshCreator.SPACE_ASCII) {
this.spaceWidth = (getValueOfVariable("xadvance") - paddingWidth) * horizontalPerPixelSize;
return null;
}
double xTex = ((double) getValueOfVariable("x") + (padding[PAD_LEFT] - DESIRED_PADDING)) / imageSize;
double yTex = ((double) getValueOfVariable("y") + (padding[PAD_TOP] - DESIRED_PADDING)) / imageSize;
int width = getValueOfVariable("width") - (paddingWidth - (2 * DESIRED_PADDING));
int height = getValueOfVariable("height") - ((paddingHeight) - (2 * DESIRED_PADDING));
double quadWidth = width * horizontalPerPixelSize;
double quadHeight = height * verticalPerPixelSize;
double xTexSize = (double) width / imageSize;
double yTexSize = (double) height / imageSize;
double xOff = (getValueOfVariable("xoffset") + padding[PAD_LEFT] - DESIRED_PADDING) * horizontalPerPixelSize;
double yOff = (getValueOfVariable("yoffset") + (padding[PAD_TOP] - DESIRED_PADDING)) * verticalPerPixelSize;
double xAdvance = (getValueOfVariable("xadvance") - paddingWidth) * horizontalPerPixelSize;
return new Character(id, xTex, yTex, xTexSize, yTexSize, xOff, yOff, quadWidth, quadHeight, xAdvance);
}
}
|