Merge pull request #1108 from gucio321/d2font

d2font: fixed creating file bug
This commit is contained in:
Tim Sarbin 2021-03-28 10:25:43 -04:00 committed by GitHub
commit 5efe96ebe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 27 deletions

View File

@ -6,12 +6,9 @@ func Create(frame, width, height int) *FontGlyph {
// nolint:gomnd // thes bytes are constant // nolint:gomnd // thes bytes are constant
// comes from https://d2mods.info/forum/viewtopic.php?t=42044 // comes from https://d2mods.info/forum/viewtopic.php?t=42044
result := &FontGlyph{ result := &FontGlyph{
unknown1: []byte{0}, frame: frame,
unknown2: []byte{1, 0, 0}, width: width,
unknown3: []byte{0, 0, 0, 0}, height: height,
frame: frame,
width: width,
height: height,
} }
return result return result
@ -19,12 +16,9 @@ func Create(frame, width, height int) *FontGlyph {
// FontGlyph represents a single font glyph // FontGlyph represents a single font glyph
type FontGlyph struct { type FontGlyph struct {
unknown1 []byte frame int
unknown2 []byte width int
unknown3 []byte height int
frame int
width int
height int
} }
// SetSize sets glyph's size to w, h // SetSize sets glyph's size to w, h
@ -59,15 +53,15 @@ func (fg *FontGlyph) FrameIndex() int {
// Unknown1 returns unknowns bytes // Unknown1 returns unknowns bytes
func (fg *FontGlyph) Unknown1() []byte { func (fg *FontGlyph) Unknown1() []byte {
return fg.unknown1 return []byte{0}
} }
// Unknown2 returns unknowns bytes // Unknown2 returns unknowns bytes
func (fg *FontGlyph) Unknown2() []byte { func (fg *FontGlyph) Unknown2() []byte {
return fg.unknown2 return []byte{1, 0, 0}
} }
// Unknown3 returns unknowns bytes // Unknown3 returns unknowns bytes
func (fg *FontGlyph) Unknown3() []byte { func (fg *FontGlyph) Unknown3() []byte {
return fg.unknown3 return []byte{0, 0, 0, 0}
} }

View File

@ -27,11 +27,10 @@ const (
// Font represents a displayable font // Font represents a displayable font
type Font struct { type Font struct {
unknownHeaderBytes []byte sheet d2interface.Animation
sheet d2interface.Animation table []byte
table []byte Glyphs map[rune]*d2fontglyph.FontGlyph
Glyphs map[rune]*d2fontglyph.FontGlyph color color.Color
color color.Color
} }
// Load loads a new font from byte slice // Load loads a new font from byte slice
@ -52,10 +51,7 @@ func Load(data []byte) (*Font, error) {
color: color.White, color: color.White,
} }
font.unknownHeaderBytes, err = sr.ReadBytes(unknownHeaderBytesCount) sr.SkipBytes(unknownHeaderBytesCount)
if err != nil {
return nil, err
}
err = font.initGlyphs(sr) err = font.initGlyphs(sr)
if err != nil { if err != nil {
@ -149,10 +145,11 @@ func (f *Font) RenderText(text string, target d2interface.Surface) error {
func (f *Font) initGlyphs(sr *d2datautils.StreamReader) error { func (f *Font) initGlyphs(sr *d2datautils.StreamReader) error {
glyphs := make(map[rune]*d2fontglyph.FontGlyph) glyphs := make(map[rune]*d2fontglyph.FontGlyph)
for i := numHeaderBytes; i < len(f.table); i += bytesPerGlyph { // for i := numHeaderBytes; i < len(f.table); i += bytesPerGlyph {
for i := numHeaderBytes; true; i += bytesPerGlyph {
code, err := sr.ReadUInt16() code, err := sr.ReadUInt16()
if err != nil { if err != nil {
return err break
} }
// byte of 0 // byte of 0
@ -194,7 +191,13 @@ func (f *Font) Marshal() []byte {
sw := d2datautils.CreateStreamWriter() sw := d2datautils.CreateStreamWriter()
sw.PushBytes([]byte("Woo!\x01")...) sw.PushBytes([]byte("Woo!\x01")...)
sw.PushBytes(f.unknownHeaderBytes...)
// unknown header bytes - constant
sw.PushBytes([]byte{1, 0, 0, 0, 0, 1}...)
// Expected Height of character cell and Expected Width of character cell
// not used in decoder
sw.PushBytes([]byte{0, 0}...)
for c, i := range f.Glyphs { for c, i := range f.Glyphs {
sw.PushUint16(uint16(c)) sw.PushUint16(uint16(c))