-
Notifications
You must be signed in to change notification settings - Fork 335
/
Reader.hx
51 lines (39 loc) · 1.35 KB
/
Reader.hx
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
package hxd.fmt.bfnt;
import haxe.io.Input;
@:access(h2d.Font)
class Reader {
var i : Input;
public function new( i : Input ) {
this.i = i;
}
public function read( resolveTile: String -> h2d.Tile ) : h2d.Font {
if (i.readString(4) != "BFNT" || i.readByte() != 0) throw "Not a BFNT file!";
var font : h2d.Font = null;
switch (i.readByte()) {
case 1:
font = new h2d.Font(i.readString(i.readUInt16()), i.readInt16());
font.tilePath = i.readString(i.readUInt16());
var tile = font.tile = resolveTile(font.tilePath);
font.lineHeight = i.readInt16();
font.baseLine = i.readInt16();
var defaultChar = i.readInt32();
var id : Int;
while ( ( id = i.readInt32() ) != 0 ) {
var t = tile.sub(i.readUInt16(), i.readUInt16(), i.readUInt16(), i.readUInt16(), i.readInt16(), i.readInt16());
var glyph = new h2d.Font.FontChar(t, i.readInt16());
font.glyphs.set(id, glyph);
if (id == defaultChar) font.defaultChar = glyph;
var prevChar : Int;
while ( ( prevChar = i.readInt32() ) != 0 ) {
glyph.addKerning(prevChar, i.readInt16());
}
}
case ver:
throw "Unknown BFNT version: " + ver;
}
return font;
}
public static inline function parse(bytes : haxe.io.Bytes, resolveTile : String -> h2d.Tile ) : h2d.Font {
return new Reader(new haxe.io.BytesInput(bytes)).read(resolveTile);
}
}