/
text.go
96 lines (87 loc) · 1.91 KB
/
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
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
92
93
94
95
96
package elements
import (
"github.com/enorith/image"
"github.com/enorith/image/assets"
"github.com/fogleman/gg"
"github.com/sirupsen/logrus"
"golang.org/x/image/font/opentype"
"io/ioutil"
"strings"
)
type Text struct {
Style
Content string
Wrapped bool
}
func (t Text) Draw(ctx *gg.Context) error {
if t.FontSize == 0 {
t.FontSize = image.DefaultFontSize
}
if t.LineSpacing == 0 {
t.LineSpacing = image.DefaultLineSpacing
}
if t.Width == 0 {
t.Width = float64(ctx.Width())
}
if t.Color == nil {
ctx.SetColor(image.DefaultColor)
} else {
ctx.SetColor(t.Color)
}
if t.FontFace != nil {
ctx.SetFontFace(t.FontFace)
} else if t.FontFile != "" {
e := loadFont(ctx, t.FontFile, t.FontSize)
if e != nil {
logrus.Error("load font error ", e)
return e
}
} else {
e := loadDefaultFont(ctx, t.FontSize)
if e != nil {
logrus.Error("load font error ", e)
return e
}
}
if t.Wrapped {
ctx.DrawStringWrapped(t.Content, t.Left, t.Top, t.Anchor[0], t.Anchor[1], t.Width, t.LineSpacing, t.Align)
} else {
ctx.DrawStringAnchored(t.Content, t.Left, t.Top, t.Anchor[0], t.Anchor[1])
}
ctx.Fill()
return nil
}
func loadDefaultFont(ctx *gg.Context, points float64) error {
b, e := assets.ReadFile("fonts/" + image.DefaultFont)
if e != nil {
return e
}
return setFontOpenType(ctx, b, points)
}
func loadFont(ctx *gg.Context, fontPath string, points float64) error {
if strings.HasSuffix(fontPath, "otf") {
b, e := ioutil.ReadFile(fontPath)
if e != nil {
return e
}
err := setFontOpenType(ctx, b, points)
if err != nil {
return err
}
return nil
} else {
return ctx.LoadFontFace(fontPath, points)
}
}
func setFontOpenType(ctx *gg.Context, b []byte, points float64) error {
f, e := opentype.Parse(b)
if e != nil {
return e
}
face, e := opentype.NewFace(f, &opentype.FaceOptions{Size: points, DPI: 72})
if e != nil {
return e
}
ctx.SetFontFace(face)
return nil
}