-
Notifications
You must be signed in to change notification settings - Fork 1
/
font.go
61 lines (51 loc) · 1.27 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
package main
import (
"encoding/binary"
"io"
"os"
)
type psfFont struct {
Version uint32
HeaderSize uint32
Flags uint32
NumGlyphs uint32
BytesPerGlyph uint32
Height uint32
Width uint32
GlyphBuffer []byte
}
func readPSF(f *os.File) psfFont {
header := make([]byte, 32)
_, err := f.Read(header)
if err != nil {
logger.Panicw("unable to read font header",
"err", err)
}
if header[0] != 0x72 || header[1] != 0xb5 || header[2] != 0x4a || header[3] != 0x86 {
logger.Panic("file is not a psf font")
}
p := psfFont{
Version: binary.LittleEndian.Uint32(header[4:8]),
HeaderSize: binary.LittleEndian.Uint32(header[8:12]),
Flags: binary.LittleEndian.Uint32(header[12:16]),
NumGlyphs: binary.LittleEndian.Uint32(header[16:20]),
BytesPerGlyph: binary.LittleEndian.Uint32(header[20:24]),
Height: binary.LittleEndian.Uint32(header[24:28]),
Width: binary.LittleEndian.Uint32(header[28:32]),
}
f.Seek(32, 0)
b, _ := io.ReadAll(f)
p.GlyphBuffer = b
return p
}
func (p psfFont) getChar(chr int) []byte {
if chr > 127 {
// Ignore all unicode
chr = 'a'
}
char := make([]byte, p.BytesPerGlyph)
for k := range char {
char[k] = p.GlyphBuffer[chr*int(p.BytesPerGlyph)+k]
}
return char
}