-
Notifications
You must be signed in to change notification settings - Fork 335
/
Writer.hx
85 lines (74 loc) · 2.15 KB
/
Writer.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
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
package hxd.fmt.bfnt;
import haxe.io.Output;
@:access(h2d.Font)
class Writer {
static inline var VERSION : Int = 1;
// V1 format:
// [BFNT][0x00][0x01]
// [name_len:u2][name:name_len][size:i2][tile_len:u2][tile_path:tile_len][lineHeight:i2][baseLine:i2]
// [defaultChar:i4] or [useNullChar:0x00000000]
// [glyph]
// [id:i4][x:u2][y:u2][width:u2][height:u2][xoffset:i2][yoffset:i2][xadvance:i2]
// [kerning]
// [prevChar:i4][advance:i2]
// [kerning-terminator:0x00000000] or [kerning]
// [glyph-terminator:0x00000000] or [glyph]
var out : Output;
public function new( out : Output ) {
this.out = out;
}
public function write( font : h2d.Font ) {
out.writeString("BFNT");
out.writeByte(0);
out.writeByte(VERSION);
writeString(font.name);
out.writeInt16(font.size);
writeString(font.tilePath);
out.writeInt16(Std.int(font.lineHeight));
out.writeInt16(Std.int(font.baseLine));
if (font.defaultChar != font.nullChar) {
var found = false;
for ( k in font.glyphs.keys() ) {
if ( font.glyphs.get(k) == font.defaultChar ) {
out.writeInt32(k);
found = true;
break;
}
}
if (!found) out.writeInt32(0);
} else {
out.writeInt32(0);
}
for ( id in font.glyphs.keys() ) {
if (id == 0) continue; // Safety measure if for some reason there's actually a character with ID 0.
var glyph = font.glyphs.get(id);
var t = glyph.t;
out.writeInt32(id);
out.writeUInt16(Std.int(t.x));
out.writeUInt16(Std.int(t.y));
out.writeUInt16(Std.int(t.width));
out.writeUInt16(Std.int(t.height));
out.writeInt16(Std.int(t.dx));
out.writeInt16(Std.int(t.dy));
out.writeInt16(Std.int(glyph.width));
var kern = @:privateAccess glyph.kerning;
while ( kern != null ) {
if (kern.prevChar != 0) {
out.writeInt32(kern.prevChar);
out.writeInt16(Std.int(kern.offset));
}
kern = kern.next;
}
out.writeInt32(0);
}
out.writeInt32(0);
}
inline function writeString( s : String ) {
if ( s == null ) s = "";
var bytes = haxe.io.Bytes.ofString(s);
if ( bytes.length > 0xFFFF )
throw "Invalid string: Size over 0xFFFF";
out.writeUInt16(bytes.length);
out.write(bytes);
}
}