-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextlayer.go
More file actions
47 lines (38 loc) · 1020 Bytes
/
textlayer.go
File metadata and controls
47 lines (38 loc) · 1020 Bytes
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
package touch
import (
"image"
"image/color"
)
type TextLayer struct {
BasicLayer
Color color.Color
Gravity image.Point
Padding image.Point
Text string
textFont *Font
}
func (tl *TextLayer) Init(frame image.Rectangle, fontname string, fontsize float64) {
tl.SetFrame(frame)
tl.SetFont(fontname, fontsize)
tl.Color = color.Black
tl.Gravity = GravityLeft
tl.Self = tl
}
func (tl *TextLayer) SetFont(name string, size float64) {
tl.textFont = SharedFont(name, size)
tl.Invalidate()
}
func (tl *TextLayer) SetText(text string) {
tl.Text = text
tl.Invalidate()
}
func (tl *TextLayer) NaturalSize() image.Point {
return tl.textFont.Measure(tl.Text).Add(tl.Padding.Mul(2))
}
func (tl *TextLayer) DrawIn(ctx DrawingContext) {
tl.BasicLayer.DrawIn(ctx)
layout := Layout(tl.Rectangle).InsetBy(tl.Padding.X, tl.Padding.Y)
textSize := tl.textFont.MeasureIn(tl.Text, layout.Size())
textRect := layout.Aligned(textSize, tl.Gravity)
tl.textFont.Draw(ctx.Image(), tl.Text, textRect, tl.Color)
}