/
font.go
178 lines (156 loc) · 4.23 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package render
import (
"image"
"path/filepath"
"runtime"
"strings"
"github.com/golang/freetype/truetype"
"golang.org/x/image/colornames"
"golang.org/x/image/font"
"github.com/oakmound/oak/dlog"
"github.com/oakmound/oak/fileutil"
)
var (
fontdir string
defaultHinting font.Hinting
defaultSize float64
defaultDPI float64
defaultColor image.Image
defaultFontFile string
// DefFontGenerator is a default font generator of no options
DefFontGenerator = FontGenerator{}
loadedFonts = make(map[string]*truetype.Font)
)
// A FontGenerator stores information that can be used to create a font
type FontGenerator struct {
File string
Color image.Image
Size float64
Hinting string
DPI float64
}
// DefFont returns a font built of the parameters set by SetFontDefaults.
func DefFont() *Font {
return DefFontGenerator.Generate()
}
// Generate creates a font from the FontGenerator. Any parameters not supplied
// will be filled in with defaults set through SetFontDefaults.
func (fg *FontGenerator) Generate() *Font {
dir := fontdir
// Replace zero values with defaults
if fg.File == "" {
if defaultFontFile != "" {
fg.File = defaultFontFile
} else {
_, curFile, _, _ := runtime.Caller(1)
dir = filepath.Join(filepath.Dir(curFile), "default_assets", "font")
fg.File = "luxisr.ttf"
}
}
if fg.Size == 0 {
fg.Size = defaultSize
}
if fg.DPI == 0 {
fg.DPI = defaultDPI
}
if fg.Color == nil {
fg.Color = defaultColor
}
return &Font{
FontGenerator: *fg,
Drawer: font.Drawer{
// Color and hinting zero values are replaced
// by their respective parse functions in the
// zero case.
Src: fg.Color,
Face: truetype.NewFace(LoadFont(dir, fg.File), &truetype.Options{
Size: fg.Size,
DPI: fg.DPI,
Hinting: parseFontHinting(fg.Hinting),
}),
},
}
}
// Copy creates a copy of this FontGenerator
func (fg *FontGenerator) Copy() *FontGenerator {
newFg := new(FontGenerator)
*newFg = *fg
return newFg
}
// A Font is obtained as the result of FontGenerator.Generate(). It's used to
// create text type renderables.
type Font struct {
FontGenerator
font.Drawer
}
// Refresh regenerates this font
func (f *Font) Refresh() {
*f = *f.Generate()
}
// Copy returns a copy of this font
func (f *Font) Copy() *Font {
return f.Generate()
}
// Reset sets the font to being a default font
func (f *Font) Reset() {
// Generate will return all defaults with no args
f.FontGenerator = FontGenerator{}
*f = *f.Generate()
}
// SetFontDefaults updates the default font parameters with the passed in arguments
func SetFontDefaults(wd, assetPath, fontPath, hinting, color, file string, size, dpi float64) {
fontdir = filepath.Join(
wd,
assetPath,
fontPath)
defaultHinting = parseFontHinting(hinting)
defaultSize = size
defaultDPI = dpi
defaultColor = FontColor(color)
defaultFontFile = file
}
func parseFontHinting(hintType string) (faceHinting font.Hinting) {
hintType = strings.ToLower(hintType)
switch hintType {
case "none":
faceHinting = font.HintingNone
case "vertical":
faceHinting = font.HintingVertical
case "full":
faceHinting = font.HintingFull
default:
dlog.Error("Unable to parse font hinting, ", hintType)
fallthrough
case "":
faceHinting = font.HintingNone
}
return faceHinting
}
// FontColor accesses x/image/colornames and returns an image.Image for the input
// string. If the string is not defined in x/image/colornames, it will return defaultColor
// as defined by SetFontDefaults. The set of colors as defined by x/image/colornames matches
// the set of colors as defined by the SVG 1.1 spec.
func FontColor(s string) image.Image {
s = strings.ToLower(s)
if c, ok := colornames.Map[s]; ok {
return image.NewUniform(c)
}
return defaultColor
}
// LoadFont loads in a font file and stores it with the given name. This is necessary before using the fonttype for a Font
func LoadFont(dir string, fontFile string) *truetype.Font {
if _, ok := loadedFonts[fontFile]; !ok {
fontBytes, err := fileutil.ReadFile(filepath.Join(dir, fontFile))
if err != nil {
dlog.Error(err.Error())
return nil
}
font, err := truetype.Parse(fontBytes)
if err != nil {
dlog.Error(err.Error())
return nil
}
loadedFonts[fontFile] = font
}
return loadedFonts[fontFile]
}