forked from hetianyi/gox
/
font.go
45 lines (39 loc) · 942 Bytes
/
font.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 fontx
import (
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"image/color"
"io/ioutil"
)
type FontConfig struct {
Font *truetype.Font
FontSize float64
Color color.Color
}
type TrueTypeFont struct {
Font *truetype.Font
}
// LoadFont loads a font file and returns *truetype.Font.
func LoadFont(fontFile string) (*TrueTypeFont, error) {
// Read the font data.
fontBytes, err := ioutil.ReadFile(fontFile)
if err != nil {
return nil, err
}
f, err := freetype.ParseFont(fontBytes)
return &TrueTypeFont{f}, err
}
func NewFreeTypeContext() *freetype.Context {
return freetype.NewContext()
}
// GetMetrics gets font metrics by options.
func (fo *TrueTypeFont) GetMetrics(fc *FontConfig) font.Metrics {
opt := &truetype.Options{
Size: fc.FontSize,
DPI: 72,
Hinting: font.HintingNone,
}
face := truetype.NewFace(fo.Font, opt)
return face.Metrics()
}