/
bdf.go
143 lines (133 loc) · 2.96 KB
/
bdf.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2018 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bdf
import (
"bufio"
"fmt"
"image"
"image/color"
"io"
"strconv"
"strings"
)
type Glyph struct {
Encoding int
Width int
Height int
X int
Y int
Bitmap [][]byte
ShiftX int
ShiftY int
}
func (g *Glyph) ColorModel() color.Model {
return color.AlphaModel
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
func (g *Glyph) Bounds() image.Rectangle {
return image.Rect(0, 0, g.Width+abs(g.X), 16)
}
func (g *Glyph) At(x, y int) color.Color {
x -= g.ShiftX
y -= g.ShiftY
// TODO: Should g.X and g.Y be used?
y += g.Height - 14
if x < 0 || y < 0 || x >= g.Width || y >= g.Height {
return color.Alpha{}
}
bits := g.Bitmap[y][x/8]
if (bits>>uint(7-x%8))&1 != 0 {
return color.Alpha{0xff}
}
return color.Alpha{}
}
func Parse(f io.Reader) ([]*Glyph, error) {
glyphs := []*Glyph{}
var current *Glyph
s := bufio.NewScanner(f)
for s.Scan() {
line := s.Text()
if strings.HasPrefix(line, "STARTCHAR ") {
if current != nil {
panic("not reach")
}
current = &Glyph{}
continue
}
if strings.HasPrefix(line, "ENCODING ") {
idx, err := strconv.ParseInt(line[len("ENCODING "):], 10, 32)
if err != nil {
return nil, err
}
current.Encoding = int(idx)
continue
}
if strings.HasPrefix(line, "BBX ") {
tokens := strings.Split(line, " ")
w, err := strconv.ParseInt(tokens[1], 10, 32)
if err != nil {
return nil, err
}
h, err := strconv.ParseInt(tokens[2], 10, 32)
if err != nil {
return nil, err
}
x, err := strconv.ParseInt(tokens[3], 10, 32)
if err != nil {
return nil, err
}
y, err := strconv.ParseInt(tokens[4], 10, 32)
if err != nil {
return nil, err
}
current.Width = int(w)
current.Height = int(h)
current.X = int(x)
current.Y = int(y)
}
if strings.HasPrefix(line, "BITMAP") {
current.Bitmap = [][]byte{}
continue
}
if strings.HasPrefix(line, "ENDCHAR") {
glyphs = append(glyphs, current)
current = nil
continue
}
if current == nil {
continue
}
if current.Bitmap == nil {
continue
}
if len(line)%2 != 0 {
return nil, fmt.Errorf("bdf: len(line) must be even")
}
bits := []byte{}
for ; len(line) > 0; line = line[2:] {
b, err := strconv.ParseInt(line[:2], 16, 32)
if err != nil {
return nil, err
}
bits = append(bits, byte(b))
}
current.Bitmap = append(current.Bitmap, bits)
}
return glyphs, nil
}