-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtext.go
45 lines (38 loc) · 907 Bytes
/
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
package plot
// Textbox implements drawing text on plots.
type Textbox struct {
Margin Rect
Style
Lines []string
}
// NewTextbox creates a new text graphic with the specified lines.
func NewTextbox(line ...string) *Textbox {
return &Textbox{
Lines: line,
}
}
// Add adds a line to the text box.
func (box *Textbox) Add(text string) {
box.Lines = append(box.Lines, text)
}
// Draw draws text to the canvas.
func (box *Textbox) Draw(plot *Plot, canvas Canvas) {
canvas = canvas.Clip(canvas.Bounds().Inset(box.Margin))
style := box.Style
if style.IsZero() {
style = plot.Theme.Font
}
if style.Size == 0 {
style.Size = 10
}
if !box.Style.Origin.Empty() {
style.Origin = box.Style.Origin
}
lineHeight := style.Size * 1.1
at := canvas.Bounds().UnitLocation(style.Origin)
at.Y = lineHeight
for _, line := range box.Lines {
canvas.Text(line, at, &style)
at.Y += lineHeight
}
}