-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderedtext.go
55 lines (48 loc) · 1.36 KB
/
renderedtext.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
package touch
import (
"image"
"image/color"
"image/draw"
)
// RenderedText caches the alphamask and dimensions of a text string
type RenderedText struct {
Text string
MaxSize image.Point
font *Font
rendered *image.Alpha
}
func (rt *RenderedText) Invalidate() {
rt.rendered = nil
}
func (rt *RenderedText) SetFont(name string, size float64) {
f := SharedFont(name, size)
if f == rt.font {
return
}
rt.font = f
rt.rendered = nil
}
func (rt *RenderedText) Render() {
rect := image.Rectangle{
Max: rt.font.MeasureIn(rt.Text, rt.MaxSize),
}
rt.rendered = image.NewAlpha(rect)
rt.font.Draw(rt.rendered, rt.Text, rect, color.Alpha{0xFF})
}
// Prepare creates or updates the cached bitmap for the given string.
// Returns the size of the rendered image.
func (rt *RenderedText) Prepare(text string, maxSize image.Point) image.Point {
if rt.rendered == nil || !maxSize.Eq(rt.MaxSize) || text != rt.Text {
rt.MaxSize = maxSize
rt.Text = text
rt.Render()
}
return rt.rendered.Rect.Size()
}
// Draw draws the text into the given image/rect.
// If Render() or Prepare(...) have not been called since the last
// invalidation, a panic may result.
func (rt *RenderedText) Draw(img draw.Image, rect image.Rectangle, c color.Color) {
colorSrc := image.NewUniform(c)
draw.DrawMask(img, rect, colorSrc, image.Point{}, rt.rendered, image.Point{}, draw.Over)
}