-
Notifications
You must be signed in to change notification settings - Fork 11
/
font.go
104 lines (87 loc) · 2.06 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package strife
import (
"fmt"
"github.com/veandco/go-sdl2/sdl"
ttf "github.com/veandco/go-sdl2/ttf"
)
var fontLoaderInitialized = false
type fontStyle int
// types of font style
const (
plain fontStyle = iota
underline
bold
strike
)
// glyph is a wrapper over a texture with dimension stored
type glyph struct {
tex *sdl.Texture
dim []int32
}
// glpyInfo contains the rune value, colour as a hex
// and the font style.
type glyphInfo struct {
val rune
col uint32
style fontStyle
}
// asKey pretty prints the glpyhInfo
func (g *glyphInfo) asKey() string {
return string(g.val) + fmt.Sprintf("%d", g.col) + fmt.Sprintf("%d", int(g.style))
}
// encode will build a glpyhInfo object from the given
// values
func encode(col uint32, style fontStyle, val rune) glyphInfo {
return glyphInfo{
val: val, col: col, style: style,
}
}
// Font is a TrueTypeFont, stores the path
// as well as the texture cache for the glyphs
type Font struct {
*ttf.Font
path string
texCache map[string]*glyph
}
// DeriveFont will create a new font object from
// this font of a different size.
func (f *Font) DeriveFont(size int) (*Font, error) {
return LoadFont(f.path, size)
}
func (f *Font) hasGlyph(g glyphInfo) (*glyph, bool) {
if val, ok := f.texCache[g.asKey()]; ok {
return val, true
}
return nil, false
}
func (f *Font) cache(g glyphInfo, texture *sdl.Texture, dim []int32) *glyph {
// todo cache collision?
glyph := &glyph{texture, dim}
texture.SetBlendMode(sdl.BLENDMODE_BLEND)
f.texCache[g.asKey()] = glyph
return glyph
}
// LoadFont will try and load the font from the given
// path of the given size.
func LoadFont(path string, size int) (*Font, error) {
if !fontLoaderInitialized {
ttf.Init()
}
font, err := ttf.OpenFont(path, size)
if err != nil {
return nil, fmt.Errorf("Failed to load font at '%s'", path)
}
return &Font{
font,
path,
map[string]*glyph{},
}, nil
}
// Destroy will destroy the given font
// as well as clear the texture cache.
func (f *Font) Destroy() {
for _, glyph := range f.texCache {
glyph.tex.Destroy()
}
f.Font.Close()
}