-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.go
72 lines (57 loc) · 1.47 KB
/
text.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
//go:generate go run embedfont/main.go -p as1130 -f "embedfont/LED_5px_narrow.ttf"
package as1130
import (
"image"
"image/draw"
"math"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
func loadFont() (font.Face, error) {
const size = 5
file, err := fontBytes()
if err != nil {
return nil, err
}
ttf, err := truetype.Parse(file)
if err != nil {
return nil, err
}
face := truetype.NewFace(ttf, &truetype.Options{
Size: float64(size),
})
return face, nil
}
// TextFrames renders some text into a slice of frames that can be scrolled
// across a 24x5 display.
func TextFrames(text string) ([]*Frame24x5, error) {
const spacerFrames = 1
face, err := loadFont()
if err != nil {
return []*Frame24x5{}, err
}
rect := rect24x5()
f := &font.Drawer{
Src: image.NewUniform(On),
Face: face,
Dot: fixed.P(rect.Max.X*spacerFrames, rect.Max.Y),
}
partialFrames := float64(f.MeasureString(text)>>6) / float64(rect.Max.X)
wholeFrames := int(math.Ceil(partialFrames)) + spacerFrames
wideFrame := Frame24x5{Gray: image.NewGray(
image.Rect(0, 0, rect.Max.X*wholeFrames, rect.Max.Y),
)}
f.Dst = wideFrame
f.DrawString(text)
advance := rect.Max.X
frames := make([]*Frame24x5, wholeFrames)
for i, _ := range frames {
frame := NewFrame24x5()
draw.Draw(frame, frame.Bounds(), wideFrame.SubImage(rect), rect.Min, draw.Src)
frames[i] = frame
rect.Min.X += advance
rect.Max.X += advance
}
return frames, nil
}