/
text.go
59 lines (49 loc) · 1.16 KB
/
text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package gomui
import (
"image"
"image/draw"
"math"
"strings"
sfont "golang.org/x/exp/shiny/font"
"golang.org/x/image/math/fixed"
"golang.org/x/mobile/exp/sprite"
)
type TextTexture struct {
fg, bg image.Color
rgba image.RGBA
Spacing float64
Face sfont.Face
}
func NewTextTexture(bounds image.Rectangle, face sfont.Face) *TextTexture {
return &TextTexture{
fg: image.Black,
bg: image.White,
rgba: image.NewRGBA(bounds),
Face: face,
Spacing: 1.5,
}
}
func (t *TextTexture) SetColor(fg, bg image.Color) {
t.fg, t.bg = fg, bg
}
func (t *TextTexture) SetBounds(bounds image.Rectangle) {
t.rgba = image.NewRGBA(bounds)
}
func (t *TextTexture) Create(eng sprite.Engine, text string) (sprite.SubTex, error) {
draw.Draw(t.rgba, t.rgba.Bounds(), t.bg, image.ZP, draw.Src)
d := &sfont.Drawer{
Dst: t.rgba,
Src: t.fg,
Face: t.Face,
}
dy := int(math.Ceil(t.Face.Size * t.Spacing))
for i, s := range strings.Split(text, "\n") {
d.Dot = fixed.P(0, int(t.Face.Size*0.8)+dy*i)
d.DrawString(s)
}
tex, err := eng.LoadTexture(t.rgba)
if err != nil {
return sprite.SubTex{}, err
}
return sprite.SubTex{tex, t.rgba.Bounds()}, nil
}