/
font.go
359 lines (307 loc) · 9.11 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package render
import (
"fmt"
"image"
"image/color"
"image/draw"
"io/ioutil"
"log"
"engo.io/engo"
"engo.io/gl"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
var (
dpi = float64(72)
)
// Font keeps track of a specific Font. Fonts are explicit instances of a font file,
// including the Size and Color. A separate font will have to be generated to get
// different sizes and colors of the same font file.
type Font struct {
URL string
Size float64
BG color.Color
FG color.Color
TTF *truetype.Font
face font.Face
}
// Create is for loading fonts from the disk, given a location
func (f *Font) Create() error {
// Read and parse the font
ttfBytes, err := ioutil.ReadFile(f.URL)
if err != nil {
return err
}
ttf, err := freetype.ParseFont(ttfBytes)
if err != nil {
return err
}
f.TTF = ttf
f.face = truetype.NewFace(f.TTF, &truetype.Options{
Size: f.Size,
DPI: dpi,
Hinting: font.HintingFull,
})
return nil
}
// CreatePreloaded is for loading fonts which have already been defined (and loaded) within Preload
func (f *Font) CreatePreloaded() error {
fontres, err := engo.Files.Resource(f.URL)
if err != nil {
return err
}
fnt, ok := fontres.(FontResource)
if !ok {
return fmt.Errorf("preloaded font is not of type `*truetype.Font`: %s", f.URL)
}
f.TTF = fnt.Font
f.face = truetype.NewFace(f.TTF, &truetype.Options{
Size: f.Size,
DPI: dpi,
Hinting: font.HintingFull,
})
return nil
}
// TextDimensions returns the total width, total height and total line size
// of the input string written out in the Font.
func (f *Font) TextDimensions(text string) (int, int, int) {
fnt := f.TTF
size := f.Size
var (
totalWidth = fixed.Int26_6(0)
totalHeight = fixed.Int26_6(size)
maxYBearing = fixed.Int26_6(0)
)
fupe := fixed.Int26_6(fnt.FUnitsPerEm())
for _, char := range text {
idx := fnt.Index(char)
hm := fnt.HMetric(fupe, idx)
vm := fnt.VMetric(fupe, idx)
g := truetype.GlyphBuf{}
err := g.Load(fnt, fupe, idx, font.HintingNone)
if err != nil {
log.Println(err)
return 0, 0, 0
}
totalWidth += hm.AdvanceWidth
yB := (vm.TopSideBearing * fixed.Int26_6(size)) / fupe
if yB > maxYBearing {
maxYBearing = yB
}
dY := (vm.AdvanceHeight * fixed.Int26_6(size)) / fupe
if dY > totalHeight {
totalHeight = dY
}
}
// Scale to actual pixel size
totalWidth *= fixed.Int26_6(size)
totalWidth /= fupe
return int(totalWidth), int(totalHeight), int(maxYBearing)
}
// RenderNRGBA returns an *image.NRGBA in the Font based on the input string.
func (f *Font) RenderNRGBA(text string) *image.NRGBA {
width, height, yBearing := f.TextDimensions(text)
font := f.TTF
size := f.Size
if size <= 0 {
panic("Font size cannot be <= 0")
}
// Default colors
if f.FG == nil {
f.FG = color.NRGBA{0, 0, 0, 0}
}
if f.BG == nil {
f.BG = color.NRGBA{0, 0, 0, 0}
}
// Colors
fg := image.NewUniform(f.FG)
bg := image.NewUniform(f.BG)
// Create the font context
c := freetype.NewContext()
nrgba := image.NewNRGBA(image.Rect(0, 0, width, height))
draw.Draw(nrgba, nrgba.Bounds(), bg, image.ZP, draw.Src)
c.SetDPI(dpi)
c.SetFont(font)
c.SetFontSize(size)
c.SetClip(nrgba.Bounds())
c.SetDst(nrgba)
c.SetSrc(fg)
// Draw the text.
pt := fixed.P(0, yBearing)
_, err := c.DrawString(text, pt)
if err != nil {
log.Println(err)
return nil
}
return nrgba
}
// Render returns a Texture in the Font based on the input string.
func (f *Font) Render(text string) Texture {
nrgba := f.RenderNRGBA(text)
// Create texture
imObj := NewImageObject(nrgba)
return NewTextureSingle(imObj)
}
// generateFontAtlas generates the font atlas for this given font, using the first `c` Unicode characters.
func (f *Font) generateFontAtlas(c int) FontAtlas {
atlas := FontAtlas{
XLocation: make([]float32, c),
YLocation: make([]float32, c),
Width: make([]float32, c),
Height: make([]float32, c),
}
currentX := float32(0)
currentY := float32(0)
// Default colors
if f.FG == nil {
f.FG = color.NRGBA{0, 0, 0, 0}
}
if f.BG == nil {
f.BG = color.NRGBA{0, 0, 0, 0}
}
d := &font.Drawer{}
d.Src = image.NewUniform(f.FG)
d.Face = truetype.NewFace(f.TTF, &truetype.Options{
Size: f.Size,
DPI: dpi,
Hinting: font.HintingNone,
})
lineHeight := d.Face.Metrics().Height
lineBuffer := float32(lineHeight.Ceil()) / 2
xBuffer := float32(10)
for i := 0; i < c; i++ {
_, adv, ok := d.Face.GlyphBounds(rune(i))
if !ok {
continue
}
currentX += xBuffer
atlas.Width[i] = float32(adv.Ceil())
atlas.Height[i] = float32(lineHeight.Ceil()) + lineBuffer
atlas.XLocation[i] = currentX
atlas.YLocation[i] = currentY
currentX += float32(adv.Ceil()) + xBuffer
if currentX > atlas.TotalWidth {
atlas.TotalWidth = currentX
}
if currentX > 1024 || i >= c-1 {
currentX = 0
currentY += float32(lineHeight.Ceil()) + lineBuffer
atlas.TotalHeight += float32(lineHeight.Ceil()) + lineBuffer
}
}
// Create texture
actual := image.NewNRGBA(image.Rect(0, 0, int(atlas.TotalWidth), int(atlas.TotalHeight)))
draw.Draw(actual, actual.Bounds(), image.NewUniform(f.BG), image.ZP, draw.Src)
d.Dst = actual
for i := 0; i < c; i++ {
_, _, ok := d.Face.GlyphBounds(rune(i))
if !ok {
continue
}
d.Dot = fixed.P(int(atlas.XLocation[i]), int(atlas.YLocation[i]+float32(lineHeight.Ceil())))
d.DrawBytes([]byte{byte(i)})
}
imObj := NewImageObject(actual)
atlas.Texture = NewTextureSingle(imObj).id
return atlas
}
// A FontAtlas is a representation of some of the Font characters, as an image
type FontAtlas struct {
Texture *gl.Texture
// XLocation contains the X-coordinate of the starting position of all characters
XLocation []float32
// YLocation contains the Y-coordinate of the starting position of all characters
YLocation []float32
// Width contains the width in pixels of all the characters, including the spacing between characters
Width []float32
// Height contains the height in pixels of all the characters
Height []float32
// TotalWidth is the total amount of pixels the `FontAtlas` is wide; useful for determining the `Viewport`,
// which is relative to this value.
TotalWidth float32
// TotalHeight is the total amount of pixels the `FontAtlas` is high; useful for determining the `Viewport`,
// which is relative to this value.
TotalHeight float32
}
// Text represents a string drawn onto the screen, as used by the `TextShader`.
type Text struct {
// Font is the reference to the font you're using to render this. This includes the color, as well as the font size.
Font *Font
// Text is the actual text you want to draw. This may include newlines (\n).
Text string
// LineSpacing is the amount of additional spacing there is between the lines (when `Text` consists of multiple lines),
// relative to the `Size` of the `Font`.
LineSpacing float32
// LetterSpacing is the amount of additional spacing there is between the characters, relative to the `Size` of
// the `Font`.
LetterSpacing float32
// RightToLeft is an experimental variable used to indicate that subsequent characters come to the left of the
// previous character.
RightToLeft bool
}
// Texture returns nil because the Text is generated from a FontAtlas. This implements the common.Drawable interface.
func (t Text) Texture() *gl.Texture { return nil }
// Width returns the width of the Text generated from a FontAtlas. This implements the common.Drawable interface.
func (t Text) Width() float32 {
atlas, ok := atlasCache[*t.Font]
if !ok {
// Generate texture first
atlas = t.Font.generateFontAtlas(200)
atlasCache[*t.Font] = atlas
}
var currentX float32
var greatestX float32
for _, char := range t.Text {
// TODO: this might not work for all characters
switch {
case char == '\n':
if currentX > greatestX {
greatestX = currentX
}
currentX = 0
continue
case char < 32: // all system stuff should be ignored
continue
}
currentX += atlas.Width[char] + float32(t.Font.Size)*t.LetterSpacing
}
if currentX > greatestX {
return currentX
}
return greatestX
}
// Height returns the height the Text generated from a FontAtlas. This implements the common.Drawable interface.
func (t Text) Height() float32 {
atlas, ok := atlasCache[*t.Font]
if !ok {
// Generate texture first
atlas = t.Font.generateFontAtlas(200)
atlasCache[*t.Font] = atlas
}
var currentY float32
var totalY float32
var tallest float32
for _, char := range t.Text {
// TODO: this might not work for all characters
switch {
case char == '\n':
totalY += tallest
tallest = float32(0)
continue
case char < 32: // all system stuff should be ignored
continue
}
currentY = atlas.Height[char] + t.LineSpacing*atlas.Height[char]
if currentY > tallest {
tallest = currentY
}
}
return totalY + tallest
}
// View returns 0, 0, 1, 1 because the Text is generated from a FontAtlas. This implements the common.Drawable interface.
func (t Text) View() (float32, float32, float32, float32) { return 0, 0, 1, 1 }
// Close does nothing because the Text is generated from a FontAtlas. There is no underlying texture to close.
// This implements the common.Drawable interface.
func (t Text) Close() {}