2020-02-18 03:11:52 +00:00
|
|
|
package d2asset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image/color"
|
|
|
|
"strings"
|
|
|
|
|
2020-07-13 13:05:04 +00:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-08-11 22:01:33 +00:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
|
2020-02-18 03:11:52 +00:00
|
|
|
)
|
|
|
|
|
2020-07-13 13:05:04 +00:00
|
|
|
var _ d2interface.Font = &Font{} // Static check to confirm struct conforms to interface
|
|
|
|
|
2020-02-18 03:11:52 +00:00
|
|
|
type fontGlyph struct {
|
|
|
|
frame int
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
}
|
|
|
|
|
2020-07-01 18:09:18 +00:00
|
|
|
// Font represents a displayable font
|
2020-02-18 03:11:52 +00:00
|
|
|
type Font struct {
|
2020-07-05 17:01:44 +00:00
|
|
|
sheet d2interface.Animation
|
2020-02-18 03:11:52 +00:00
|
|
|
glyphs map[rune]fontGlyph
|
|
|
|
color color.Color
|
|
|
|
}
|
|
|
|
|
2020-07-05 19:56:48 +00:00
|
|
|
// SetColor sets the fonts color
|
2020-07-01 18:09:18 +00:00
|
|
|
func (f *Font) SetColor(c color.Color) {
|
|
|
|
f.color = c
|
2020-02-25 03:35:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 18:09:18 +00:00
|
|
|
// GetTextMetrics returns the dimensions of the Font element in pixels
|
|
|
|
func (f *Font) GetTextMetrics(text string) (width, height int) {
|
2020-02-18 03:11:52 +00:00
|
|
|
var (
|
|
|
|
lineWidth int
|
|
|
|
lineHeight int
|
|
|
|
totalWidth int
|
|
|
|
totalHeight int
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, c := range text {
|
|
|
|
if c == '\n' {
|
2020-08-05 04:03:33 +00:00
|
|
|
totalWidth = d2math.MaxInt(totalWidth, lineWidth)
|
2020-02-18 03:11:52 +00:00
|
|
|
totalHeight += lineHeight
|
|
|
|
lineWidth = 0
|
|
|
|
lineHeight = 0
|
|
|
|
} else if glyph, ok := f.glyphs[c]; ok {
|
|
|
|
lineWidth += glyph.width
|
2020-08-05 04:03:33 +00:00
|
|
|
lineHeight = d2math.MaxInt(lineHeight, glyph.height)
|
2020-02-18 03:11:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 04:03:33 +00:00
|
|
|
totalWidth = d2math.MaxInt(totalWidth, lineWidth)
|
2020-02-18 03:11:52 +00:00
|
|
|
totalHeight += lineHeight
|
|
|
|
|
|
|
|
return totalWidth, totalHeight
|
|
|
|
}
|
|
|
|
|
2020-07-08 00:16:22 +00:00
|
|
|
// RenderText prints a text using its configured style on a Surface (multi-lines are left-aligned, use label otherwise)
|
2020-06-29 04:41:58 +00:00
|
|
|
func (f *Font) RenderText(text string, target d2interface.Surface) error {
|
2020-02-18 03:11:52 +00:00
|
|
|
f.sheet.SetColorMod(f.color)
|
|
|
|
|
|
|
|
lines := strings.Split(text, "\n")
|
|
|
|
|
|
|
|
for _, line := range lines {
|
|
|
|
var (
|
|
|
|
lineHeight int
|
|
|
|
lineLength int
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, c := range line {
|
2020-07-01 18:09:18 +00:00
|
|
|
glyph, ok := f.glyphs[c]
|
|
|
|
if !ok {
|
|
|
|
continue
|
2020-02-18 03:11:52 +00:00
|
|
|
}
|
2020-07-01 18:09:18 +00:00
|
|
|
|
|
|
|
if err := f.sheet.SetCurrentFrame(glyph.frame); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.sheet.Render(target); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-05 04:03:33 +00:00
|
|
|
lineHeight = d2math.MaxInt(lineHeight, glyph.height)
|
2020-07-01 18:09:18 +00:00
|
|
|
lineLength++
|
|
|
|
|
|
|
|
target.PushTranslation(glyph.width, 0)
|
2020-02-18 03:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
target.PopN(lineLength)
|
|
|
|
target.PushTranslation(0, lineHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
target.PopN(len(lines))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|