/
glyphs_truetype.go
90 lines (75 loc) · 1.82 KB
/
glyphs_truetype.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
package glyphs
import (
"io/ioutil"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
)
// Truetype is our Truetype data.
type Truetype struct {
truetype *truetype.Font
size float64
width, height, ascent int
Normal font.Face
// How do we manage bold? italics? etc?
// bold font.Face
// italics font.Face
}
// LoadTruetype loads a Face from the provided path.
func LoadTruetype(path string) (Glyphs, error) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return LoadTruetypeFromBytes(bytes)
}
// LoadTruetypeFromBytes loads a Face from the provided bytes of TTF data.
func LoadTruetypeFromBytes(ttf []byte) (Glyphs, error) {
tt, err := truetype.Parse(ttf)
if err != nil {
return nil, err
}
face := &Truetype{
truetype: tt,
}
return face, nil
}
// Type returns FaceTruetype.
func (f *Truetype) Type() Type {
return TruetypeType
}
// SetSize sets the glyph size to the provided size.
func (f *Truetype) SetSize(size float64) {
if f.size == size {
return
}
f.size = size
f.rebuild()
}
// Width gets the width of the font.
func (f *Truetype) Width() int {
return f.width
}
// Height gets the width of the font.
func (f *Truetype) Height() int {
return f.height
}
// Ascent returns the ascent of the font.
func (f *Truetype) Ascent() int {
return f.ascent
}
// rebuild rebuilds all the font variants.
func (f *Truetype) rebuild() {
f.Normal = truetype.NewFace(f.truetype, &truetype.Options{
Size: f.size,
DPI: 72, // FIXME
Hinting: font.HintingFull, // FIXME
})
metrics := f.Normal.Metrics()
f.height = (metrics.Ascent + metrics.Descent).Round()
if advance, ok := f.Normal.GlyphAdvance('M'); ok {
f.width = advance.Round()
} else {
f.width = f.height / 2
}
f.ascent = metrics.Ascent.Round()
}