1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-14 08:16:06 -05:00

d2font: fixed bug, when was unable to create new file doing just &Font{}.Marshal()

This commit is contained in:
M. Sz 2021-03-28 16:20:04 +02:00
parent ecab467a0f
commit d72b3e3345
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
// comes from https://d2mods.info/forum/viewtopic.php?t=42044
result := &FontGlyph{
unknown1: []byte{0},
unknown2: []byte{1, 0, 0},
unknown3: []byte{0, 0, 0, 0},
frame: frame,
width: width,
height: height,
frame: frame,
width: width,
height: height,
}
return result
@ -19,12 +16,9 @@ func Create(frame, width, height int) *FontGlyph {
// FontGlyph represents a single font glyph
type FontGlyph struct {
unknown1 []byte
unknown2 []byte
unknown3 []byte
frame int
width int
height int
frame int
width int
height int
}
// SetSize sets glyph's size to w, h
@ -59,15 +53,15 @@ func (fg *FontGlyph) FrameIndex() int {
// Unknown1 returns unknowns bytes
func (fg *FontGlyph) Unknown1() []byte {
return fg.unknown1
return []byte{0}
}
// Unknown2 returns unknowns bytes
func (fg *FontGlyph) Unknown2() []byte {
return fg.unknown2
return []byte{1, 0, 0}
}
// Unknown3 returns unknowns bytes
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
type Font struct {
unknownHeaderBytes []byte
sheet d2interface.Animation
table []byte
Glyphs map[rune]*d2fontglyph.FontGlyph
color color.Color
sheet d2interface.Animation
table []byte
Glyphs map[rune]*d2fontglyph.FontGlyph
color color.Color
}
// Load loads a new font from byte slice
@ -52,10 +51,7 @@ func Load(data []byte) (*Font, error) {
color: color.White,
}
font.unknownHeaderBytes, err = sr.ReadBytes(unknownHeaderBytesCount)
if err != nil {
return nil, err
}
sr.SkipBytes(unknownHeaderBytesCount)
err = font.initGlyphs(sr)
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 {
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()
if err != nil {
return err
break
}
// byte of 0
@ -194,7 +191,13 @@ func (f *Font) Marshal() []byte {
sw := d2datautils.CreateStreamWriter()
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 {
sw.PushUint16(uint16(c))