From aeef2d5c4b27c5c882092435cd160d4b27215d75 Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Thu, 18 Feb 2021 20:20:38 +0100 Subject: [PATCH 1/6] font format: NewFontGlyph method --- d2common/d2fileformats/d2font/defaults.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 d2common/d2fileformats/d2font/defaults.go diff --git a/d2common/d2fileformats/d2font/defaults.go b/d2common/d2fileformats/d2font/defaults.go new file mode 100644 index 00000000..44b5a8e5 --- /dev/null +++ b/d2common/d2fileformats/d2font/defaults.go @@ -0,0 +1,17 @@ +package d2font + +// NewFontGlyph creates a new font glyph +func NewFontGlyph(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, 0}, + unknown2: []byte{1, 0, 0}, + unknown3: []byte{0, 0, 0, 0, 0}, + frame: frame, + width: width, + height: height, + } + + return result +} From 93389d595d761fa88b7c23da67acc1c244862952 Mon Sep 17 00:00:00 2001 From: se16n Date: Thu, 18 Feb 2021 22:40:09 +0300 Subject: [PATCH 2/6] Update description of project location path on windows --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f8879321..34a269a8 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Feel free to contribute! To pull the project down, run `go get github.com/OpenDiablo2/OpenDiablo2` -On windows this folder will most likely be in `C:\users\(you)\go\src\github.com\OpenDiablo2\OpenDiablo2` +On windows this folder will most likely be in `%USERPROFILE%\go\src\github.com\OpenDiablo2\OpenDiablo2` In the root folder, run `go get -d` to pull down all dependencies. From b75f82fc8c0fd13fa9a720c7a89daab2c69b4e64 Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Thu, 18 Feb 2021 21:05:16 +0100 Subject: [PATCH 3/6] font format: refactor: moved font glyph into new package --- .../d2font/d2fontglyph/font_glyph.go | 73 ++++++++++++++++ d2common/d2fileformats/d2font/defaults.go | 17 ---- d2common/d2fileformats/d2font/font.go | 86 +++++-------------- 3 files changed, 94 insertions(+), 82 deletions(-) create mode 100644 d2common/d2fileformats/d2font/d2fontglyph/font_glyph.go delete mode 100644 d2common/d2fileformats/d2font/defaults.go diff --git a/d2common/d2fileformats/d2font/d2fontglyph/font_glyph.go b/d2common/d2fileformats/d2font/d2fontglyph/font_glyph.go new file mode 100644 index 00000000..3ee8e6eb --- /dev/null +++ b/d2common/d2fileformats/d2font/d2fontglyph/font_glyph.go @@ -0,0 +1,73 @@ +// Package d2fontglyph represents a single font glyph +package d2fontglyph + +// Create creates a new font glyph +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, 0}, + unknown2: []byte{1, 0, 0}, + unknown3: []byte{0, 0, 0, 0, 0}, + frame: frame, + width: width, + height: height, + } + + return result +} + +// FontGlyph represents a single font glyph +type FontGlyph struct { + unknown1 []byte + unknown2 []byte + unknown3 []byte + frame int + width int + height int +} + +// SetSize sets glyph's size to w, h +func (fg *FontGlyph) SetSize(w, h int) { + fg.width, fg.height = w, h +} + +// Size returns glyph's size +func (fg *FontGlyph) Size() (w, h int) { + return fg.width, fg.height +} + +// Width returns font width +func (fg *FontGlyph) Width() int { + return fg.width +} + +// Height returns glyph's height +func (fg *FontGlyph) Height() int { + return fg.height +} + +// SetFrameIndex sets frame index to idx +func (fg *FontGlyph) SetFrameIndex(idx int) { + fg.frame = idx +} + +// FrameIndex returns glyph's frame +func (fg *FontGlyph) FrameIndex() int { + return fg.frame +} + +// Unknown1 returns unknowns bytes +func (fg *FontGlyph) Unknown1() []byte { + return fg.unknown1 +} + +// Unknown2 returns unknowns bytes +func (fg *FontGlyph) Unknown2() []byte { + return fg.unknown2 +} + +// Unknown3 returns unknowns bytes +func (fg *FontGlyph) Unknown3() []byte { + return fg.unknown3 +} diff --git a/d2common/d2fileformats/d2font/defaults.go b/d2common/d2fileformats/d2font/defaults.go deleted file mode 100644 index 44b5a8e5..00000000 --- a/d2common/d2fileformats/d2font/defaults.go +++ /dev/null @@ -1,17 +0,0 @@ -package d2font - -// NewFontGlyph creates a new font glyph -func NewFontGlyph(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, 0}, - unknown2: []byte{1, 0, 0}, - unknown3: []byte{0, 0, 0, 0, 0}, - frame: frame, - width: width, - height: height, - } - - return result -} diff --git a/d2common/d2fileformats/d2font/font.go b/d2common/d2fileformats/d2font/font.go index 0edcd2de..d18f188f 100644 --- a/d2common/d2fileformats/d2font/font.go +++ b/d2common/d2fileformats/d2font/font.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2datautils" + "github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2font/d2fontglyph" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2math" ) @@ -22,42 +23,12 @@ const ( unknown3BytesCount = 4 ) -// FontGlyph represents a single font glyph -type FontGlyph struct { - unknown1 []byte - unknown2 []byte - unknown3 []byte - frame int - width int - height int -} - -// SetSize sets glyph's size to w, h -func (fg *FontGlyph) SetSize(w, h int) { - fg.width, fg.height = w, h -} - -// Size returns glyph's size -func (fg *FontGlyph) Size() (w, h int) { - return fg.width, fg.height -} - -// SetFrameIndex sets frame index to idx -func (fg *FontGlyph) SetFrameIndex(idx int) { - fg.frame = idx -} - -// FrameIndex returns glyph's frame -func (fg *FontGlyph) FrameIndex() int { - return fg.frame -} - // Font represents a displayable font type Font struct { unknownHeaderBytes []byte sheet d2interface.Animation table []byte - Glyphs map[rune]*FontGlyph + Glyphs map[rune]*d2fontglyph.FontGlyph color color.Color } @@ -100,7 +71,7 @@ func (f *Font) SetBackground(sheet d2interface.Animation) { _, h := f.sheet.GetFrameBounds() for i := range f.Glyphs { - f.Glyphs[i].SetSize(f.Glyphs[i].width, h) + f.Glyphs[i].SetSize(f.Glyphs[i].Width(), h) } } @@ -123,8 +94,8 @@ func (f *Font) GetTextMetrics(text string) (width, height int) { lineWidth = 0 lineHeight = 0 } else if glyph, ok := f.Glyphs[c]; ok { - lineWidth += glyph.width - lineHeight = d2math.MaxInt(lineHeight, glyph.height) + lineWidth += glyph.Width() + lineHeight = d2math.MaxInt(lineHeight, glyph.Height()) } } @@ -152,16 +123,16 @@ func (f *Font) RenderText(text string, target d2interface.Surface) error { continue } - if err := f.sheet.SetCurrentFrame(glyph.frame); err != nil { + if err := f.sheet.SetCurrentFrame(glyph.FrameIndex()); err != nil { return err } f.sheet.Render(target) - lineHeight = d2math.MaxInt(lineHeight, glyph.height) + lineHeight = d2math.MaxInt(lineHeight, glyph.Height()) lineLength++ - target.PushTranslation(glyph.width, 0) + target.PushTranslation(glyph.Width(), 0) } target.PopN(lineLength) @@ -174,7 +145,7 @@ func (f *Font) RenderText(text string, target d2interface.Surface) error { } func (f *Font) initGlyphs(sr *d2datautils.StreamReader) error { - glyphs := make(map[rune]*FontGlyph) + glyphs := make(map[rune]*d2fontglyph.FontGlyph) for i := 12; i < len(f.table); i += 14 { code, err := sr.ReadUInt16() @@ -182,48 +153,33 @@ func (f *Font) initGlyphs(sr *d2datautils.StreamReader) error { return err } - var glyph FontGlyph - // two bytes of 0 - glyph.unknown1, err = sr.ReadBytes(unknown1BytesCount) - if err != nil { - return err - } + sr.SkipBytes(unknown1BytesCount) width, err := sr.ReadByte() if err != nil { return err } - glyph.width = int(width) - height, err := sr.ReadByte() if err != nil { return err } - glyph.height = int(height) - // 1, 0, 0 - glyph.unknown2, err = sr.ReadBytes(unknown2BytesCount) - if err != nil { - return err - } + sr.SkipBytes(unknown2BytesCount) frame, err := sr.ReadUInt16() if err != nil { return err } - glyph.frame = int(frame) - // 1, 0, 0, character code repeated, and further 0. - glyph.unknown3, err = sr.ReadBytes(unknown3BytesCount) - if err != nil { - return err - } + sr.SkipBytes(unknown3BytesCount) - glyphs[rune(code)] = &glyph + glyph := d2fontglyph.Create(int(width), int(height), int(frame)) + + glyphs[rune(code)] = glyph } f.Glyphs = glyphs @@ -240,12 +196,12 @@ func (f *Font) Marshal() []byte { for c, i := range f.Glyphs { sw.PushUint16(uint16(c)) - sw.PushBytes(i.unknown1...) - sw.PushBytes(byte(i.width)) - sw.PushBytes(byte(i.height)) - sw.PushBytes(i.unknown2...) - sw.PushUint16(uint16(i.frame)) - sw.PushBytes(i.unknown3...) + sw.PushBytes(i.Unknown1()...) + sw.PushBytes(byte(i.Width())) + sw.PushBytes(byte(i.Height())) + sw.PushBytes(i.Unknown2()...) + sw.PushUint16(uint16(i.FrameIndex())) + sw.PushBytes(i.Unknown3()...) } return sw.GetBytes() From a9ccda187363faa19b55d74add851d3e641c3f9c Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Fri, 19 Feb 2021 07:42:25 +0100 Subject: [PATCH 4/6] hothotfix: fixed argument order in call to d2fontglyph.Create --- d2common/d2fileformats/d2font/font.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/d2common/d2fileformats/d2font/font.go b/d2common/d2fileformats/d2font/font.go index d18f188f..bb9beedb 100644 --- a/d2common/d2fileformats/d2font/font.go +++ b/d2common/d2fileformats/d2font/font.go @@ -177,7 +177,7 @@ func (f *Font) initGlyphs(sr *d2datautils.StreamReader) error { // 1, 0, 0, character code repeated, and further 0. sr.SkipBytes(unknown3BytesCount) - glyph := d2fontglyph.Create(int(width), int(height), int(frame)) + glyph := d2fontglyph.Create(int(frame), int(width), int(height)) glyphs[rune(code)] = glyph } From 7574293624a224b5f9ed08413e1931519d421437 Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Fri, 19 Feb 2021 08:48:20 +0100 Subject: [PATCH 5/6] hotfix: font format: - removed magic numbers - corrected unknown1 bytes count in d2fontglyph.Create --- d2common/d2fileformats/d2font/d2fontglyph/font_glyph.go | 2 +- d2common/d2fileformats/d2font/font.go | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/d2common/d2fileformats/d2font/d2fontglyph/font_glyph.go b/d2common/d2fileformats/d2font/d2fontglyph/font_glyph.go index 3ee8e6eb..0f6df70b 100644 --- a/d2common/d2fileformats/d2font/d2fontglyph/font_glyph.go +++ b/d2common/d2fileformats/d2font/d2fontglyph/font_glyph.go @@ -6,7 +6,7 @@ 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, 0}, + unknown1: []byte{0}, unknown2: []byte{1, 0, 0}, unknown3: []byte{0, 0, 0, 0, 0}, frame: frame, diff --git a/d2common/d2fileformats/d2font/font.go b/d2common/d2fileformats/d2font/font.go index bb9beedb..7ffd6032 100644 --- a/d2common/d2fileformats/d2font/font.go +++ b/d2common/d2fileformats/d2font/font.go @@ -16,6 +16,8 @@ const ( ) const ( + numHeaderBytes = 12 + bytesPerGlyph = 14 signatureBytesCount = 5 unknownHeaderBytesCount = 7 unknown1BytesCount = 1 @@ -147,13 +149,13 @@ 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 := 12; i < len(f.table); i += 14 { + for i := numHeaderBytes; i < len(f.table); i += bytesPerGlyph { code, err := sr.ReadUInt16() if err != nil { return err } - // two bytes of 0 + // byte of 0 sr.SkipBytes(unknown1BytesCount) width, err := sr.ReadByte() From a4f12f6ebed381b15d96783baa12dd033dbc9241 Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Fri, 19 Feb 2021 10:31:11 +0100 Subject: [PATCH 6/6] hotfix: removed unnecessary 0 from d2fontglyph.unknown3 bytes list --- d2common/d2fileformats/d2font/d2fontglyph/font_glyph.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/d2common/d2fileformats/d2font/d2fontglyph/font_glyph.go b/d2common/d2fileformats/d2font/d2fontglyph/font_glyph.go index 0f6df70b..11eda03e 100644 --- a/d2common/d2fileformats/d2font/d2fontglyph/font_glyph.go +++ b/d2common/d2fileformats/d2font/d2fontglyph/font_glyph.go @@ -8,7 +8,7 @@ func Create(frame, width, height int) *FontGlyph { result := &FontGlyph{ unknown1: []byte{0}, unknown2: []byte{1, 0, 0}, - unknown3: []byte{0, 0, 0, 0, 0}, + unknown3: []byte{0, 0, 0, 0}, frame: frame, width: width, height: height,