mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2025-02-10 10:36:42 -05:00
font table interpreter: moved d stuff responsible for font table into d2fileformats/d2font
This commit is contained in:
parent
5e1dbf56f0
commit
721a67b404
@ -1,7 +1,8 @@
|
|||||||
package d2asset
|
package d2font
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
"image/color"
|
"image/color"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -23,6 +24,23 @@ type Font struct {
|
|||||||
color color.Color
|
color color.Color
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load loads a new font from byte slice
|
||||||
|
func Load(data []byte, sheet d2interface.Animation) (*Font, error) {
|
||||||
|
if string(data[:5]) != "Woo!\x01" {
|
||||||
|
return nil, fmt.Errorf("invalid font table format")
|
||||||
|
}
|
||||||
|
|
||||||
|
font := &Font{
|
||||||
|
table: data,
|
||||||
|
sheet: sheet,
|
||||||
|
color: color.White,
|
||||||
|
}
|
||||||
|
|
||||||
|
font.initGlyphs()
|
||||||
|
|
||||||
|
return font, nil
|
||||||
|
}
|
||||||
|
|
||||||
// SetColor sets the fonts color
|
// SetColor sets the fonts color
|
||||||
func (f *Font) SetColor(c color.Color) {
|
func (f *Font) SetColor(c color.Color) {
|
||||||
f.color = c
|
f.color = c
|
||||||
@ -30,9 +48,6 @@ func (f *Font) SetColor(c color.Color) {
|
|||||||
|
|
||||||
// GetTextMetrics returns the dimensions of the Font element in pixels
|
// GetTextMetrics returns the dimensions of the Font element in pixels
|
||||||
func (f *Font) GetTextMetrics(text string) (width, height int) {
|
func (f *Font) GetTextMetrics(text string) (width, height int) {
|
||||||
if f.glyphs == nil {
|
|
||||||
f.initGlyphs()
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
lineWidth int
|
lineWidth int
|
@ -2,7 +2,6 @@ package d2asset
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"image/color"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -19,6 +18,7 @@ import (
|
|||||||
|
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2records"
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2records"
|
||||||
|
|
||||||
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2font"
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2txt"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2txt"
|
||||||
|
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
||||||
@ -209,11 +209,11 @@ func (am *AssetManager) LoadComposite(baseType d2enum.ObjectType, token, palette
|
|||||||
}
|
}
|
||||||
|
|
||||||
// LoadFont loads a font the resource files
|
// LoadFont loads a font the resource files
|
||||||
func (am *AssetManager) LoadFont(tablePath, spritePath, palettePath string) (*Font, error) {
|
func (am *AssetManager) LoadFont(tablePath, spritePath, palettePath string) (*d2font.Font, error) {
|
||||||
cachePath := fmt.Sprintf("%s;%s;%s", tablePath, spritePath, palettePath)
|
cachePath := fmt.Sprintf("%s;%s;%s", tablePath, spritePath, palettePath)
|
||||||
|
|
||||||
if cached, found := am.fonts.Retrieve(cachePath); found {
|
if cached, found := am.fonts.Retrieve(cachePath); found {
|
||||||
return cached.(*Font), nil
|
return cached.(*d2font.Font), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
sheet, err := am.LoadAnimation(spritePath, palettePath)
|
sheet, err := am.LoadAnimation(spritePath, palettePath)
|
||||||
@ -226,16 +226,9 @@ func (am *AssetManager) LoadFont(tablePath, spritePath, palettePath string) (*Fo
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if string(tableData[:5]) != "Woo!\x01" {
|
font, err := d2font.Load(tableData, sheet)
|
||||||
return nil, fmt.Errorf("invalid font table format: %s", tablePath)
|
if err != nil {
|
||||||
}
|
return nil, fmt.Errorf("error while loading font table %s: %v", tablePath, err)
|
||||||
|
|
||||||
am.Debugf(fmtLoadFont, tablePath, spritePath, palettePath)
|
|
||||||
|
|
||||||
font := &Font{
|
|
||||||
table: tableData,
|
|
||||||
sheet: sheet,
|
|
||||||
color: color.White,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = am.fonts.Insert(cachePath, font, defaultCacheEntryWeight)
|
err = am.fonts.Insert(cachePath, font, defaultCacheEntryWeight)
|
||||||
|
@ -4,8 +4,8 @@ import (
|
|||||||
"image/color"
|
"image/color"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2font"
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Constants defining the main shades of basic colors
|
// Constants defining the main shades of basic colors
|
||||||
@ -25,7 +25,7 @@ type Label struct {
|
|||||||
|
|
||||||
renderer d2interface.Renderer
|
renderer d2interface.Renderer
|
||||||
text string
|
text string
|
||||||
font *d2asset.Font
|
font *d2font.Font
|
||||||
surface d2interface.Surface
|
surface d2interface.Surface
|
||||||
color color.RGBA
|
color color.RGBA
|
||||||
hoverColor color.RGBA
|
hoverColor color.RGBA
|
||||||
@ -35,7 +35,7 @@ type Label struct {
|
|||||||
blinkTimer time.Time
|
blinkTimer time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func createLabel(renderer d2interface.Renderer, text string, font *d2asset.Font, col color.RGBA) (*Label, error) {
|
func createLabel(renderer d2interface.Renderer, text string, font *d2font.Font, col color.RGBA) (*Label, error) {
|
||||||
label := &Label{
|
label := &Label{
|
||||||
font: font,
|
font: font,
|
||||||
renderer: renderer,
|
renderer: renderer,
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"image/color"
|
"image/color"
|
||||||
|
|
||||||
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2font"
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
||||||
@ -532,7 +533,7 @@ func (l *Layout) createButton(renderer d2interface.Renderer, text string,
|
|||||||
return button, nil
|
return button, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Layout) loadFont(fontStyle FontStyle) (*d2asset.Font, error) {
|
func (l *Layout) loadFont(fontStyle FontStyle) (*d2font.Font, error) {
|
||||||
config := getFontStyleConfig(fontStyle)
|
config := getFontStyleConfig(fontStyle)
|
||||||
if config == nil {
|
if config == nil {
|
||||||
return nil, errors.New("invalid font style")
|
return nil, errors.New("invalid font style")
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2font"
|
||||||
|
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
||||||
@ -19,7 +19,7 @@ type Label struct {
|
|||||||
*BaseWidget
|
*BaseWidget
|
||||||
text string
|
text string
|
||||||
Alignment HorizontalAlign
|
Alignment HorizontalAlign
|
||||||
font *d2asset.Font
|
font *d2font.Font
|
||||||
Color map[int]color.Color
|
Color map[int]color.Color
|
||||||
backgroundColor color.Color
|
backgroundColor color.Color
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user