-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
56 lines (47 loc) · 1.15 KB
/
image.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
package unifont
import (
"image"
"image/color"
)
type unifontImage struct {
pix []byte
width uint8
m int
}
func monochromeAlphaConversion(c color.Color) color.Color {
if _, _, _, a := c.RGBA(); a == 0xFFFF {
return color.Alpha16{A: 0xFFFF}
} else {
return color.Alpha16{A: 0}
}
}
var (
monochromeAlphaModel = color.ModelFunc(monochromeAlphaConversion)
)
func (i *unifontImage) ColorModel() color.Model {
return monochromeAlphaModel
}
func (i *unifontImage) Bounds() image.Rectangle {
return image.Rect(0, 0, int(i.width)*i.m, unifontHeight*i.m)
}
func (i *unifontImage) At(x, y int) color.Color {
if x < 0 || y < 0 || x >= int(i.width)*i.m || y >= unifontHeight*i.m {
return color.Alpha16{}
}
offset := x/i.m + y/i.m*int(i.width)
byteOffset := offset >> 3
bitOffset := 7 - (offset & 7)
val := i.pix[byteOffset] & (1 << bitOffset)
if val != 0 {
return color.Alpha16{A: 0xFFFF}
} else {
return color.Alpha16{A: 0}
}
}
func (f *Unifont) glyphImage(g *glyph, multiplier int) *unifontImage {
return &unifontImage{
pix: f.chardata[g.offset : g.offset+uint32(g.width)*unifontHeight>>3],
width: g.width,
m: multiplier,
}
}