/
text.go
121 lines (109 loc) · 2.52 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
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
package shapes
import (
. "github.com/gabz57/goledmatrix/canvas"
"github.com/gabz57/goledmatrix/canvas/fonts"
"github.com/gabz57/goledmatrix/canvas/matrix"
. "github.com/gabz57/goledmatrix/components"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"image"
"image/color"
)
type Text struct {
*Graphic
font font.Face
position Point
txt string
pixels []Pixel
bounds image.Rectangle
}
func NewText(graphic *Graphic, position Point, txt string, f fonts.MatrixFont) *Text {
text := Text{
Graphic: graphic,
position: position,
txt: txt,
font: fonts.GetFont(f),
}
text.computeBounds()
text.computePixels()
return &text
}
func (t *Text) Draw(canvas Canvas) error {
//max := canvas.Bounds().Max
for _, pixel := range t.pixels {
//if pixel.X < max.X && pixel.Y < max.Y {
canvas.Set(pixel.X, pixel.Y, pixel.C)
//}
}
return nil
}
func (t *Text) SetOffset(offset Point) {
t.Graphic.SetOffset(offset)
t.computePixels()
}
func (t *Text) SetColor(color color.Color) {
t.Layout().SetColor(color)
t.computePixels()
}
func (t *Text) SetText(txt string) {
if t.txt == txt {
return
}
t.txt = txt
t.computeBounds()
t.computePixels()
}
func (t *Text) Bounds() image.Rectangle {
return t.bounds
}
var colorPixel = color.White
var emptyPixel = color.Black
func (t *Text) computeBounds() {
t.bounds = image.Rect(
0,
0,
font.MeasureString(t.font, t.txt).Ceil(),
t.font.Metrics().Ascent.Ceil()+t.font.Metrics().Descent.Ceil())
}
func (t *Text) computePixels() {
dx := t.bounds.Dx()
dy := t.bounds.Dy()
var leds = make([]color.Color, dx*dy)
var imgDst = matrix.NewSimpleCanvas(dx, dy, &leds)
for rX := t.bounds.Min.X; rX < t.bounds.Max.X; rX++ {
for rY := t.bounds.Min.Y; rY < t.bounds.Max.Y; rY++ {
imgDst.Set(rX, rY, emptyPixel)
}
}
d := font.Drawer{
Dst: imgDst,
Src: &image.Uniform{C: colorPixel},
Face: t.font,
Dot: fixed.Point26_6{X: 0, Y: t.font.Metrics().Height - t.font.Metrics().Descent},
}
d.DrawString(t.txt)
var pixels []Pixel = nil
position := t.position.Add(t.ComputedOffset())
for rX := 0; rX < dx; rX++ {
for rY := 0; rY < dy; rY++ {
at := imgDst.At(rX, rY)
if at != emptyPixel {
r, g, b, _ := at.RGBA()
if r != 0 && g != 0 && b != 0 {
pixels = append(pixels, Pixel{
X: position.X + rX,
Y: position.Y + rY,
C: t.Layout().Color(),
})
} else {
pixels = append(pixels, Pixel{
X: position.X + rX,
Y: position.Y + rY,
C: t.Layout().BackgroundColor(),
})
}
}
}
}
t.pixels = pixels
}