Reader.hx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package hxd.fmt.bfnt;
  2. import haxe.io.Input;
  3. @:access(h2d.Font)
  4. class Reader {
  5. var i : Input;
  6. public function new( i : Input ) {
  7. this.i = i;
  8. }
  9. public function read( resolveTile: String -> h2d.Tile ) : h2d.Font {
  10. if (i.readString(4) != "BFNT" || i.readByte() != 0) throw "Not a BFNT file!";
  11. var font : h2d.Font = null;
  12. switch (i.readByte()) {
  13. case 1:
  14. font = new h2d.Font(i.readString(i.readUInt16()), i.readInt16());
  15. font.tilePath = i.readString(i.readUInt16());
  16. var tile = font.tile = resolveTile(font.tilePath);
  17. font.lineHeight = i.readInt16();
  18. font.baseLine = i.readInt16();
  19. var defaultChar = i.readInt32();
  20. var id : Int;
  21. while ( ( id = i.readInt32() ) != 0 ) {
  22. var t = tile.sub(i.readUInt16(), i.readUInt16(), i.readUInt16(), i.readUInt16(), i.readInt16(), i.readInt16());
  23. var glyph = new h2d.Font.FontChar(t, i.readInt16());
  24. font.glyphs.set(id, glyph);
  25. if (id == defaultChar) font.defaultChar = glyph;
  26. var prevChar : Int;
  27. while ( ( prevChar = i.readInt32() ) != 0 ) {
  28. glyph.addKerning(prevChar, i.readInt16());
  29. }
  30. }
  31. case ver:
  32. throw "Unknown BFNT version: " + ver;
  33. }
  34. return font;
  35. }
  36. public static inline function parse(bytes : haxe.io.Bytes, resolveTile : String -> h2d.Tile ) : h2d.Font {
  37. return new Reader(new haxe.io.BytesInput(bytes)).read(resolveTile);
  38. }
  39. }