-
Notifications
You must be signed in to change notification settings - Fork 1
/
font.go
55 lines (46 loc) · 1.17 KB
/
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
46
47
48
49
50
51
52
53
54
55
package main
import (
"github.com/golang/freetype/truetype"
"github.com/markbates/pkger"
"golang.org/x/image/font"
"io/ioutil"
"log"
)
var fontCache = make(map[float64]font.Face)
var arcadeTruetypeFont *truetype.Font
func init() {
// Read font.
pix, err := pkger.Open("/assets/arcadepix.ttf")
defer pix.Close()
mustFont(err)
bs, err := ioutil.ReadAll(pix)
mustFont(err)
// Parse font data into fontFace.
arcadeTruetypeFont, err = truetype.Parse(bs)
mustFont(err)
}
// Font returns the arcade font for the given size. If it is not yet generated, it will be loaded and created.
func Font(size float64) font.Face {
if f, ok := fontCache[size]; ok {
return f
}
loadedFont := createFont(size)
fontCache[size] = loadedFont
return loadedFont
}
// createFont creates the actual font face for the given size.
func createFont(size float64) font.Face {
const dpi = 72
loadedFont := truetype.NewFace(arcadeTruetypeFont, &truetype.Options{
Size: size,
DPI: dpi,
Hinting: font.HintingFull,
})
return loadedFont
}
// mustFont checks if an error occured while loading a file.
func mustFont(err error) {
if err != nil {
log.Fatal("Unable to load font:", err)
}
}