-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuprint.go
50 lines (42 loc) · 950 Bytes
/
uprint.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
package uprint
import (
"image/color"
"tinygo.org/x/drivers"
)
// New inits font and driver.
// Accepted fonts are: 'font6x8', 'font7x10', 'font11x18', 'font16x26'.
func New(d drivers.Displayer, fname string) *Display {
return &Display{
d: d,
f: fonts[fname],
}
}
type Display struct {
d drivers.Displayer
f font
}
func (t *Display) isSet(n uint16, bit int) bool {
val := n & (1 << (16 - bit))
return (val > 0)
}
func (t *Display) insertChar(char byte, x, y int, c color.RGBA) {
char -= 32 // "space" ascii code
var c1 color.RGBA
for yi, b := range t.f.data[char] {
for xi := 0; xi < t.f.width; xi++ {
if !t.isSet(b, xi) {
c1 = color.RGBA{}
} else {
c1 = c
}
t.d.SetPixel(int16(x+xi), int16(y+yi), c1)
}
}
}
// Print prints string at specific coordinates.
func (t *Display) Print(s string, x, y int, c color.RGBA) {
for _, char := range []byte(s) {
t.insertChar(char, x, y, c)
x += t.f.width
}
}