-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfont.go
59 lines (49 loc) · 1.12 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
package rog
import (
"image"
"image/draw"
"io"
"os"
)
type FontData struct {
Image image.Image
Width, Height int
CellWidth, CellHeight int
mapping map[rune]int
}
func (fd *FontData) Map(ch rune) (int, bool) {
if fd.mapping == nil {
return int(ch), true
}
position, ok := fd.mapping[ch]
return position, ok
}
func ReadFont(r io.Reader, cellWidth, cellHeight int, maps string) *FontData {
m, _, err := image.Decode(r)
if err != nil {
panic(err)
}
b := m.Bounds()
newm := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(newm, newm.Bounds(), m, b.Min, draw.Src)
var mapping map[rune]int
if len(maps) > 0 {
mapping = make(map[rune]int)
i := 0
for _, v := range maps {
mapping[v] = i
i++
}
}
width := m.Bounds().Max.X / cellWidth
height := m.Bounds().Max.Y / cellHeight
return &FontData{newm, width, height, cellWidth, cellHeight, mapping}
}
func Font(path string, cellWidth, cellHeight int, maps string) *FontData {
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
return ReadFont(file, cellWidth, cellHeight, maps)
}