/
fonts.go
58 lines (51 loc) · 1.18 KB
/
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
53
54
55
56
57
58
package rx
import (
"io/ioutil"
"sync"
"github.com/golang/freetype/truetype"
"github.com/hajimehoshi/ebiten"
"golang.org/x/image/font"
)
var (
fontsOnce sync.Once
sourceSansProRegular *truetype.Font
defaultFont *truetype.Font
defaultFontFace font.Face
)
// SourceSansProRegular Truetype Font
func SourceSansProRegular() *truetype.Font {
fontsOnce.Do(fontsSetup)
return sourceSansProRegular
}
// DefaultFont Truetype Font
func DefaultFont() *truetype.Font {
return SourceSansProRegular()
}
// DefaultFontFace is the 20pt 72dpi*DeviceScaleFactor() font face
//
// Warning: do not use this inside an init() function, specially on Android
func DefaultFontFace() font.Face {
fontsOnce.Do(fontsSetup)
return defaultFontFace
}
func fontsSetup() {
f, err := res.Open("public/fonts/SourceSansPro-Regular.ttf")
if err != nil {
panic(err)
}
b, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
tt, err := truetype.Parse(b)
if err != nil {
panic(err)
}
sourceSansProRegular = tt
defaultFont = tt
defaultFontFace = truetype.NewFace(defaultFont, &truetype.Options{
DPI: 72 * ebiten.DeviceScaleFactor(),
Size: 20,
Hinting: font.HintingFull,
})
}