-
Notifications
You must be signed in to change notification settings - Fork 0
/
fonts.go
52 lines (48 loc) · 940 Bytes
/
fonts.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
package postcards2dianne
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
)
var (
defaultFont string
fonts map[string]*truetype.Font
)
func init() {
dir := os.Getenv("POSTCARD_FONTS_DIR")
if dir == "" {
dir = "/Library/fonts" // Mac OS X default
}
fonts = make(map[string]*truetype.Font)
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if filepath.Ext(path) != ".ttf" {
return nil
}
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
ttf, err := freetype.ParseFont(b)
if err != nil {
return nil // skip, don't error tho
}
name := strings.TrimSuffix(filepath.Base(path), ".ttf")
fonts[name] = ttf
if defaultFont == "" {
defaultFont = name
}
return nil
})
if err != nil {
panic(err)
}
}