-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext.go
64 lines (51 loc) · 1.23 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
package goimage
import (
"image"
"image/color"
"io/ioutil"
"github.com/golang/freetype"
"golang.org/x/image/font/gofont/goregular"
)
// WriteText writes the given text using the Go font
func (g Image) WriteText(txt string, color color.Color, size float64, x, y int) error {
// Parse Go font
font, err := freetype.ParseFont(goregular.TTF)
if err != nil {
return err
}
// Create freetype context
c := freetype.NewContext()
c.SetDPI(72)
c.SetFont(font)
c.SetFontSize(size)
c.SetClip(g.rgba.Bounds())
c.SetDst(g.rgba)
c.SetSrc(image.NewUniform(color))
// Draw text
_, err = c.DrawString(txt, freetype.Pt(x, y))
return err
}
// WriteTextFont writes the given text using the given font
func (g Image) WriteTextFont(fontFile, txt string, color color.Color, size float64, x, y int) error {
// Load font
data, err := ioutil.ReadFile(fontFile)
if err != nil {
return err
}
// Parse font
font, err := freetype.ParseFont(data)
if err != nil {
return err
}
// Create freetype context
c := freetype.NewContext()
c.SetDPI(72)
c.SetFont(font)
c.SetFontSize(size)
c.SetClip(g.rgba.Bounds())
c.SetDst(g.rgba)
c.SetSrc(image.NewUniform(color))
// Draw text
_, err = c.DrawString(txt, freetype.Pt(x, y))
return err
}