BufferedReader reader = new BufferedReader(new InputStreamReader(fontFile.read()), 512);
try {
reader.readLine(); // info
String line = reader.readLine();
if (line == null) throw new GdxRuntimeException("Invalid font file: " + fontFile);
String[] common = line.split(" ", 4);
if (common.length < 4) throw new GdxRuntimeException("Invalid font file: " + fontFile);
if (!common[1].startsWith("lineHeight=")) throw new GdxRuntimeException("Invalid font file: " + fontFile);
lineHeight = Integer.parseInt(common[1].substring(11));
if (!common[2].startsWith("base=")) throw new GdxRuntimeException("Invalid font file: " + fontFile);
int baseLine = Integer.parseInt(common[2].substring(5));
line = reader.readLine();
if (line == null) throw new GdxRuntimeException("Invalid font file: " + fontFile);
String[] pageLine = line.split(" ", 4);
if (!pageLine[2].startsWith("file=")) throw new GdxRuntimeException("Invalid font file: " + fontFile);
String imgFilename = null;
if (pageLine[2].endsWith("\"")) {
imgFilename = pageLine[2].substring(6, pageLine[2].length() - 1);
} else {
imgFilename = pageLine[2].substring(5, pageLine[2].length());
}
imagePath = fontFile.parent().child(imgFilename).path().replaceAll("\\\\", "/");
descent = 0;
while (true) {
line = reader.readLine();
if (line == null) break;
if (line.startsWith("kernings ")) break;
if (!line.startsWith("char ")) continue;
Glyph glyph = new Glyph();
StringTokenizer tokens = new StringTokenizer(line, " =");
tokens.nextToken();
tokens.nextToken();
int ch = Integer.parseInt(tokens.nextToken());
if (ch <= Character.MAX_VALUE)
setGlyph(ch, glyph);
else
continue;
tokens.nextToken();
glyph.srcX = Integer.parseInt(tokens.nextToken());
tokens.nextToken();
glyph.srcY = Integer.parseInt(tokens.nextToken());
tokens.nextToken();
glyph.width = Integer.parseInt(tokens.nextToken());
tokens.nextToken();
glyph.height = Integer.parseInt(tokens.nextToken());
tokens.nextToken();
glyph.xoffset = Integer.parseInt(tokens.nextToken());
tokens.nextToken();
if (flip)
glyph.yoffset = Integer.parseInt(tokens.nextToken());
else
glyph.yoffset = -(glyph.height + Integer.parseInt(tokens.nextToken()));
tokens.nextToken();
glyph.xadvance = Integer.parseInt(tokens.nextToken());
descent = Math.min(baseLine + glyph.yoffset, descent);
}
while (true) {
line = reader.readLine();
if (line == null) break;
if (!line.startsWith("kerning ")) break;
StringTokenizer tokens = new StringTokenizer(line, " =");
tokens.nextToken();
tokens.nextToken();
int first = Integer.parseInt(tokens.nextToken());
tokens.nextToken();
int second = Integer.parseInt(tokens.nextToken());
if (first < 0 || first > Character.MAX_VALUE || second < 0 || second > Character.MAX_VALUE) continue;
Glyph glyph = getGlyph((char)first);
tokens.nextToken();
int amount = Integer.parseInt(tokens.nextToken());
glyph.setKerning(second, amount);
}
Glyph spaceGlyph = getGlyph(' ');
if (spaceGlyph == null) {
spaceGlyph = new Glyph();
Glyph xadvanceGlyph = getGlyph('l');
if (xadvanceGlyph == null) xadvanceGlyph = getFirstGlyph();
spaceGlyph.xadvance = xadvanceGlyph.xadvance;
setGlyph(' ', spaceGlyph);
}
spaceWidth = spaceGlyph != null ? spaceGlyph.xadvance + spaceGlyph.width : 1;
Glyph xGlyph = null;
for (int i = 0; i < xChars.length; i++) {
xGlyph = getGlyph(xChars[i]);
if (xGlyph != null) break;
}
if (xGlyph == null) xGlyph = getFirstGlyph();
xHeight = xGlyph.height;
Glyph capGlyph = null;
for (int i = 0; i < capChars.length; i++) {
capGlyph = getGlyph(capChars[i]);
if (capGlyph != null) break;
}
if (xGlyph == null) xGlyph = getFirstGlyph();
capHeight = capGlyph.height;
ascent = baseLine - capHeight;
down = -lineHeight;
if (flip) {
ascent = -ascent;
down = -down;
}
} catch (Exception ex) {
throw new GdxRuntimeException("Error loading font file: " + fontFile, ex);
} finally {
try {
reader.close();
} catch (IOException ignored) {
}