-
Notifications
You must be signed in to change notification settings - Fork 12
/
concrete.go
101 lines (86 loc) · 2.15 KB
/
concrete.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
package tinyfont // import "tinygo.org/x/tinyfont"
import (
"image/color"
"tinygo.org/x/drivers"
)
// Glyph is a struct that implements Glypher interface.
type Glyph struct {
Rune rune
Width uint8
Height uint8
XAdvance uint8
XOffset int8
YOffset int8
Bitmaps []byte
}
// Font is a struct that implements Fonter interface.
type Font struct {
BBox [4]int8 // width, height, minX, minY
Glyphs []Glyph
YAdvance uint8
EmptyGlyph Glyph
}
// Draw sets a single glyph in the buffer of the display.
func (glyph Glyph) Draw(display drivers.Displayer, x int16, y int16, color color.RGBA) {
bitmapOffset := 0
bitmap := byte(0)
if len(glyph.Bitmaps) > 0 {
bitmap = glyph.Bitmaps[bitmapOffset]
}
bit := uint8(0)
for j := int16(0); j < int16(glyph.Height); j++ {
for i := int16(0); i < int16(glyph.Width); i++ {
if (bitmap & 0x80) != 0x00 {
display.SetPixel(x+int16(glyph.XOffset)+i, y+int16(glyph.YOffset)+j, color)
}
bitmap <<= 1
bit++
if bit > 7 {
bitmapOffset++
if bitmapOffset < len(glyph.Bitmaps) {
bitmap = glyph.Bitmaps[bitmapOffset]
}
bit = 0
}
}
}
}
// Info returns glyph information.
func (glyph Glyph) Info() GlyphInfo {
return GlyphInfo{
Rune: glyph.Rune,
Width: glyph.Width,
Height: glyph.Height,
XAdvance: glyph.XAdvance,
XOffset: glyph.XOffset,
YOffset: glyph.YOffset,
}
}
// GetYAdvance returns YAdvance of f.
func (f *Font) GetYAdvance() uint8 {
return f.YAdvance
}
var emptyBitmap [1]byte
// GetGlyph returns the glyph corresponding to the specified rune in the font.
func (font *Font) GetGlyph(r rune) Glypher {
s := 0
e := len(font.Glyphs) - 1
for s <= e {
m := (s + e) / 2
if font.Glyphs[m].Info().Rune < r {
s = m + 1
} else {
e = m - 1
}
}
if s == len(font.Glyphs) || font.Glyphs[s].Info().Rune != r {
font.EmptyGlyph.Width = 0
font.EmptyGlyph.Height = 0
font.EmptyGlyph.XAdvance = font.Glyphs[0].Info().XAdvance
font.EmptyGlyph.XOffset = font.Glyphs[0].Info().XOffset
font.EmptyGlyph.YOffset = font.Glyphs[0].Info().YOffset
font.EmptyGlyph.Bitmaps = emptyBitmap[:]
return &(font.EmptyGlyph)
}
return &(font.Glyphs[s])
}