-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
39 lines (33 loc) · 900 Bytes
/
helpers.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
package main
import (
"image"
"image/color"
"io/ioutil"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
)
func loadFont(fontFilePath string) (*truetype.Font, error) {
b, err := ioutil.ReadFile(fontFilePath)
if err != nil {
return nil, err
}
fontHandler, err := truetype.Parse(b)
if err != nil {
return nil, err
}
return fontHandler, nil
}
func addLabel(img *image.RGBA, x, y int, fontSize float64, label string, fontHandler *truetype.Font) error {
fontContext := freetype.NewContext()
fontContext.SetDPI(72)
fontContext.SetFont(fontHandler)
fontContext.SetFontSize(fontSize)
fontContext.SetDst(img)
fontContext.SetClip(img.Bounds())
fontContext.SetSrc(image.NewUniform(color.Gray16{0x3030}))
pt := freetype.Pt(x, y+int(fontContext.PointToFixed(fontSize)>>6))
if _, err := fontContext.DrawString(label, pt); err != nil {
return err
}
return nil
}