-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.go
65 lines (54 loc) · 1.16 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
package bitmapfont
import (
"bytes"
"fmt"
"io"
)
type Font struct {
info Info
Common Common
page Page
chars map[uint8]Char
kernings []Kerning
}
func NewFont() *Font {
return &Font{chars: map[uint8]Char{}}
}
func (f *Font) addChar(c Char) {
f.chars[c.Id] = c
}
func (f *Font) addKerning(k Kerning) {
f.kernings = append(f.kernings, k)
}
func (f *Font) String() string {
buf := new(bytes.Buffer)
io.WriteString(buf, "Font(")
fmt.Fprintf(buf, "face=%s, size=%d, bold=%t, #chars=%d, #kernings=%d", f.info.Face, f.info.Size, f.info.Bld, len(f.chars), len(f.kernings))
io.WriteString(buf, ")")
return buf.String()
}
func (f *Font) CharAt(c uint8) Char {
ch, ok := f.chars[c]
if !ok {
return f.chars[" "[0]]
}
return ch
}
func (f *Font) Scales() (float32, float32) {
return f.Common.ScaleW, f.Common.ScaleH
}
func (f *Font) LineHeight() float32 {
return f.Common.LineHeight
}
func (f *Font) Base() float32 {
return float32(f.Common.Base)
}
func (f *Font) AmountBetween(left, right uint8) float32 {
// linear search for now
for _, each := range f.kernings {
if each.First == left && each.Second == right {
return each.Amount
}
}
return 0
}