-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmap.go
296 lines (246 loc) · 6.27 KB
/
bitmap.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package font
import (
"errors"
"image"
"image/color"
"log"
)
type Bitmap struct {
// Data is the raw bitmap data bytes
Data []uint16
// Size is the size per glyph, in pixels
Size image.Point
// Advance size
Advance int
// VerticalSpacing
VerticalSpacing int
// Alpha 8-bit alpha value
Alpha color.Alpha
// CodePoints for glyphs in the font
CodePoint []rune
// Encoding name
Encoding string
// Replacement character
Replacement rune
// bounds of the bitmap (cached)
bounds *image.Rectangle
}
// At is an alias for AlphaAt, to satisfy the image.Image interface
func (bmp *Bitmap) At(x, y int) color.Color {
return bmp.AlphaAt(x, y)
}
// AlphaAt returns the alpha value at (x, y)
func (bmp *Bitmap) AlphaAt(x, y int) color.Alpha {
//log.Printf("alpha at %d,%d", x, y)
if y > len(bmp.Data) || x > bmp.Size.X {
return color.Alpha{}
}
b := bmp.Data[y]
o := bmp.Size.X - 1
if b&(1<<uint16(o-x)) != 0 {
return bmp.Alpha
}
return color.Alpha{}
}
// Bounds of the image
func (bmp *Bitmap) Bounds() image.Rectangle {
if bmp.bounds == nil {
bmp.bounds = &image.Rectangle{Max: image.Point{
X: bmp.Size.X,
Y: len(bmp.Data),
}}
log.Printf("%T bounds %v", bmp, bmp.bounds)
}
return *bmp.bounds
}
func (bmp *Bitmap) ColorModel() color.Model {
return color.AlphaModel
}
// DataOffset returns the byte offset for a pixel at (x, y)
func (bmp *Bitmap) DataOffset(x, y int) int {
/*
Out bitmap image data is as follows, for a bitmap with 7x6 glyphs:
00000000 00000000 00000000 00000000 00000000 00000000, // glyph0
00110011 01001100 00101010 01010101 01110000 00001111, // glyph1
... // glyphN
And we're going to pretend, that in our image, the glyphs are layed out
vertically, like so:
00000000 \
00000000 \
00000000 \__ glyph 0
00000000 /
00000000 /
00000000 /
00110011 \
01001100 \
00101010 \__ glyph 1
01010101 /
01110000 /
00001111 /
Thus, for a pixel at (x, y) = (4, 9):
00000000
00000000
00000000
00000000
00000000
00000000
00110011
01001100
00101010
01010101 < y
01110000
00001111
^
x
We need to transform to the contiguous memory point marked at p:
00000000 00000000 00000000 00000000 00000000 00000000, // glyph0
00110011 01001100 00101010 01010p01 01110000 00001111, // glyph1
*/
return y
}
func (bmp *Bitmap) Face(width, height int) (face Face, ok bool) {
if bmp.Size.X == width && bmp.Size.Y == height {
return bmp, true
}
return nil, false
}
func (bmp *Bitmap) Glyph(p image.Point, r rune) (dr image.Rectangle, mask image.Image, maskp image.Point, advance int, ok bool) {
for i, cp := range bmp.CodePoint {
if cp == r {
maskp.Y = i * bmp.Size.Y
ok = true
break
}
}
if !ok {
log.Printf("glyph %q at %s not found", r, p)
return image.Rectangle{}, nil, image.Point{}, 0, false
}
dr = image.Rectangle{
Min: p,
Max: p.Add(bmp.Size),
}
return dr, bmp, maskp, bmp.Size.X, true
}
func (bmp *Bitmap) Glyphs() int {
return len(bmp.Data) / bmp.Size.Y
}
func (bmp *Bitmap) GlyphSize() image.Point {
return bmp.Size
}
func (bmp *Bitmap) Sizes() []image.Point {
return []image.Point{bmp.Size}
}
func (bmp *Bitmap) copy() *Bitmap {
out := new(Bitmap)
*out = *bmp
out.Data = make([]uint16, len(bmp.Data))
copy(out.Data, bmp.Data)
out.CodePoint = make([]rune, len(bmp.CodePoint))
copy(out.CodePoint, bmp.CodePoint)
return out
}
// Scale up by factor 2
func (bmp *Bitmap) Scale() (*Bitmap, error) {
if bmp.Size.X > 8 {
// unless we're using a larger storage unit, we ...
return nil, errors.New("font: can't scale beyond 16 horizontal pixels")
}
out := bmp.copy()
out.Size = bmp.Size.Mul(2)
out.Advance = bmp.Advance << 1
out.Data = make([]uint16, len(bmp.Data)<<1)
out.bounds = &image.Rectangle{Max: image.Point{
X: out.Size.X,
Y: len(out.Data),
}}
log.Printf("%T bounds %v", bmp, bmp.bounds)
var (
o = bmp.Size.X - 1
mask uint16
bit uint16
)
for i, bits := range bmp.Data {
for x := 0; x < bmp.Size.X; x++ {
bit = uint16(o - x)
mask = 1 << bit
out.Data[(i<<1)+0] |= (bits & mask) << (bit + 0)
out.Data[(i<<1)+0] |= (bits & mask) << (bit + 1)
}
out.Data[(i<<1)+1] = out.Data[(i<<1)+0]
}
return out, nil
}
// Smooth edge transitions
/*
This smooths hard edges using a simple algorithm. It finds diagonal transitions
and fills the gaps. Eg:
#. ##
.# -> smoothed to -> ##
Or:
.# ##
#. -> smoothed to -> ##
This is to smooth after scaling up the font, so the resulting effect will be
as follows:
#. ##.. ##..
.# -> scaled up to -> ##.. -> smoothed to -> ###.
..## .###
..## ..##
Or:
.# ..## ..##
#. -> scaled up to -> ..## -> smoothed to -> .###
##.. ###.
##.. ##..
*/
func (bmp *Bitmap) Smooth() *Bitmap {
out := bmp.copy()
var (
tl, tr, bl, br bool
mask1, mask2 uint16
)
for i := range out.CodePoint {
o := i * out.Size.Y
for y := 0; y < out.Size.Y-1; y++ {
for x := 0; x < out.Size.X-1; x++ {
mask1 = 1 << uint(out.Size.X-1-x)
mask2 = 1 << uint(out.Size.X-2-x)
tl = out.Data[o+y]&mask1 != 0
tr = out.Data[o+y]&mask2 != 0
bl = out.Data[o+y+1]&mask1 != 0
br = out.Data[o+y+1]&mask2 != 0
// #. to ##
// .# ##
if tl && br && !tr && !bl {
out.Data[o+y] |= mask2
out.Data[o+y+1] |= mask1
}
// .# to ##
// #. ##
if tr && bl && !tl && !br {
out.Data[o+y] |= mask1
out.Data[o+y+1] |= mask2
}
}
}
}
return out
}
// BitmapFont is a Font implementation using bitmap fonts
type BitmapFont []*Bitmap
// Face returns the face for the given font size
func (font BitmapFont) Face(width, height int) (face Face, ok bool) {
for _, bmp := range font {
if bmp.Size.X == width && bmp.Size.Y == height {
return bmp, true
}
}
return nil, false
}
// Sizes returns the available face sizes
func (font BitmapFont) Sizes() []image.Point {
var sizes = make([]image.Point, len(font))
for i, face := range font {
sizes[i] = face.Size
}
return sizes
}