/
find.go
36 lines (31 loc) · 896 Bytes
/
find.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
package fontutil
import (
"io/ioutil"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
)
// Find is a platform-agnostic way to locate font files installed on the system by name.
func Find(fontname string) (filename string, err error) {
// call to platform dependent function
return find(fontname)
}
// FindAndParse Finds a font file named by fontname, reads it, and parses it.
func FindAndParse(fontname string) (*truetype.Font, error) {
filepath, err := Find(fontname)
if err != nil {
return nil, err
}
file, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, err
}
return freetype.ParseFont(file)
}
// Must is a helper that wraps a call to FindAndParse and panics if the err is
// non-nil. It is intended for use in package variable initializations.
func Must(t *truetype.Font, e error) *truetype.Font {
if e != nil {
panic(e)
}
return t
}