...
1
2
3
4
5 package text
6
7 import (
8 "math"
9 "strings"
10
11 "gonum.org/v1/plot/font"
12 "gonum.org/v1/plot/vg"
13 )
14
15
16 type Plain struct {
17 Fonts *font.Cache
18 }
19
20 var _ Handler = (*Plain)(nil)
21
22
23 func (hdlr Plain) Cache() *font.Cache {
24 return hdlr.Fonts
25 }
26
27
28 func (hdlr Plain) Extents(fnt font.Font) font.Extents {
29 face := hdlr.Fonts.Lookup(fnt, fnt.Size)
30 return face.Extents()
31 }
32
33
34 func (hdlr Plain) Lines(txt string) []string {
35 txt = strings.TrimRight(txt, "\n")
36 return strings.Split(txt, "\n")
37 }
38
39
40
41
42
43 func (hdlr Plain) Box(txt string, fnt font.Font) (width, height, depth vg.Length) {
44 face := hdlr.Fonts.Lookup(fnt, fnt.Size)
45 ext := face.Extents()
46 width = face.Width(txt)
47 height = ext.Ascent
48 depth = ext.Descent
49
50 return width, height, depth
51 }
52
53
54
55 func (hdlr Plain) Draw(c vg.Canvas, txt string, sty Style, pt vg.Point) {
56 txt = strings.TrimRight(txt, "\n")
57 if len(txt) == 0 {
58 return
59 }
60
61 fnt := hdlr.Fonts.Lookup(sty.Font, sty.Font.Size)
62 c.SetColor(sty.Color)
63
64 if sty.Rotation != 0 {
65 c.Push()
66 c.Rotate(sty.Rotation)
67 }
68
69 sin64, cos64 := math.Sincos(sty.Rotation)
70 cos := vg.Length(cos64)
71 sin := vg.Length(sin64)
72 pt.X, pt.Y = pt.Y*sin+pt.X*cos, pt.Y*cos-pt.X*sin
73
74 lines := hdlr.Lines(txt)
75 ht := sty.Height(txt)
76 pt.Y += ht*vg.Length(sty.YAlign) - fnt.Extents().Ascent
77 for i, line := range lines {
78 xoffs := vg.Length(sty.XAlign) * fnt.Width(line)
79 n := vg.Length(len(lines) - i)
80 c.FillString(fnt, pt.Add(vg.Point{X: xoffs, Y: n * sty.Font.Size}), line)
81 }
82
83 if sty.Rotation != 0 {
84 c.Pop()
85 }
86 }
87
View as plain text