/
font.go
46 lines (37 loc) · 841 Bytes
/
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
package three
import (
"fmt"
"os"
"github.com/go-gl/mathgl/mgl32"
"github.com/tobscher/gltext"
)
// Font defines a font with its texture
type Font struct {
texture *Texture
font *gltext.Font
}
// NewFont loads a font file from path at a given scale.
// The font will be rendered to a texture which is later used
// for drawing.
func NewFont(path string, scale int32) (*Font, error) {
logger.Info(fmt.Sprintf("Loading font from: %v", path))
fd, err := os.Open(path)
if err != nil {
return nil, err
}
defer fd.Close()
f, err := gltext.LoadTruetype(fd, scale, 32, 127, gltext.LeftToRight)
if err != nil {
return nil, err
}
texture := Texture{
glTexture: f.Texture,
WrapS: ClampToEdge,
WrapT: ClampToEdge,
Repeat: mgl32.Vec2{1, 1},
}
return &Font{
font: f,
texture: &texture,
}, nil
}