-
Notifications
You must be signed in to change notification settings - Fork 781
/
Copy pathdrawText.go
91 lines (77 loc) · 1.85 KB
/
drawText.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package util
import (
"image"
"image/color"
"image/draw"
"strings"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
"golang.org/x/mobile/event/size"
"golang.org/x/mobile/exp/gl/glutil"
"golang.org/x/mobile/geom"
)
func drawText(img *image.RGBA, x, y int, s string) {
col := color.RGBA{0, 0, 0, 255}
point := fixed.Point26_6{fixed.I(x), fixed.I(y)}
d := &font.Drawer{
Dst: img,
Src: image.NewUniform(col),
Face: basicfont.Face7x13,
Dot: point,
}
d.DrawString(s)
}
// Text draws text to screen
type Text struct {
sz size.Event
images *glutil.Images
m *glutil.Image
// TODO: store *gl.Context
}
// NewText creates a Text tied to the current GL context.
func NewText(images *glutil.Images) *Text {
return &Text{
images: images,
}
}
// Draw draws text at the x, y coordinates at the scaleX, scaleY specified by user
// Draw accepts strings with newline characters
func (t *Text) Draw(sz size.Event, x, y int, scaleX, scaleY geom.Pt, s string) {
if sz.WidthPx == 0 && sz.HeightPx == 0 {
return
} else if t.sz != sz {
t.sz = sz
if t.m != nil {
t.m.Release()
}
t.m = t.images.NewImage(sz.WidthPx, sz.HeightPx)
}
// split string by newline
lines := strings.Split(s, "\n")
// draw each string on a separate line
for i, v := range lines {
drawText(t.m.RGBA, int(geom.Pt(x)/scaleX), int(geom.Pt(y)/scaleY)+i*10, v)
}
// copy img data to GL device
t.m.Upload()
t.m.Draw(
sz,
geom.Point{0, 0}, // topLeft
geom.Point{sz.WidthPt * scaleX, 0}, // topRight
geom.Point{0, sz.HeightPt * scaleY}, // bottomLeft
t.m.RGBA.Bounds(),
)
}
func (t *Text) Clear() {
if t.m != nil {
draw.Draw(t.m.RGBA, t.m.RGBA.Bounds(), image.Transparent, image.Point{}, draw.Src)
}
}
func (t *Text) Release() {
if t.m != nil {
t.m.Release()
t.m = nil
t.images = nil
}
}