-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_image.go
209 lines (176 loc) · 4.05 KB
/
generate_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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"io/ioutil"
"log"
"os"
"path"
"time"
"github.com/disintegration/imaging"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
)
var (
width = 212
height = 104
upLeft = image.Point{0, 0}
lowRight = image.Point{width, height}
dpi float64 = 72
fontSize float64 = 10
spacing = 1.25
black = color.RGBA{0, 0, 0, 0xff}
white = color.RGBA{0xff, 0xff, 0xff, 0xff}
)
func generateImage(data *Response) (*image.Image, error) {
log.Println("Generating image...")
font, err := loadFont("Verdana.ttf")
if err != nil {
return nil, err
}
baseImage, err := loadImage("baseImage.png")
if err != nil {
return nil, err
}
// Initialize image
rgba := image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(rgba, rgba.Bounds(), *baseImage, image.Point{0, 0}, draw.Over)
// Setup font context
fCtx := newFontContext(rgba)
// Convert raw data object to display text
lines := BuildLines(data)
if err != nil {
return nil, err
}
fCtx.SetFont(font)
for _, line := range lines {
fCtx.SetFontSize(line.fontSize)
pt := freetype.Pt(line.x, line.y)
_, err = fCtx.DrawString(line.text, pt)
if err != nil {
return nil, err
}
}
// If you look at the PI with external ports at bottom, then the display
// will be presented to you with the short side on the Y axis, and the
// data cable on the right.. So we need to rotate 90 counter clockwise
// and horizontally flip the image so it appears as one would expect
rotImg := imaging.Rotate90(rgba)
mirrorImg := imaging.FlipH(rotImg)
subImg := mirrorImg.SubImage(mirrorImg.Rect)
return &subImg, nil
}
func newFontContext(destImg *image.RGBA) *freetype.Context {
c := freetype.NewContext()
c.SetDPI(dpi)
c.SetClip(destImg.Bounds())
c.SetDst(destImg)
c.SetSrc(image.Black)
return c
}
func assetPath() (string, error) {
// Get executable directory
ex, err := os.Executable()
if err != nil {
return "", err
}
dir := path.Dir(ex)
return fmt.Sprintf("%s/assets/", dir), nil
}
func loadFont(fontFile string) (*truetype.Font, error) {
ap, err := assetPath()
if err != nil {
return nil, err
}
// Read the font data
fontBytes, err := ioutil.ReadFile(ap + fontFile)
if err != nil {
return nil, err
}
font, err := freetype.ParseFont(fontBytes)
if err != nil {
return nil, err
}
return font, nil
}
func loadImage(imageFile string) (*image.Image, error) {
ap, err := assetPath()
if err != nil {
return nil, err
}
baseImageFile, err := os.Open(ap + imageFile)
if err != nil {
return nil, err
}
defer baseImageFile.Close()
baseImage, err := png.Decode(baseImageFile)
if err != nil {
return nil, err
}
return &baseImage, nil
}
// Line holds information for rendering text to a display
type Line struct {
x, y int
text string
fontSize float64
}
// BuildLines converts a Response into an array of lines
func BuildLines(data *Response) []Line {
// Metal/Currency pair title
title := Line{
fontSize: 22,
x: 109,
y: 27,
text: fmt.Sprintf("%s%s", data.Metal, data.Currency),
}
ounces := Line{
fontSize: 18,
x: 10,
y: 52,
text: fmt.Sprintf("%.2f", data.Price),
}
kilos := Line{
fontSize: 18,
x: 10,
y: 76,
text: fmt.Sprintf("%.2f", data.Price*32.15),
}
// Price change from previous close price
difference := data.Price - data.PrevClosePrice
percentage := (difference / data.PrevClosePrice) * 100
sign := ""
if data.Price > data.PrevClosePrice {
sign = "+"
}
percentChange := Line{
fontSize: 18,
x: 107,
y: 52,
text: fmt.Sprintf("%s%.2f%%", sign, percentage),
}
priceChange := Line{
fontSize: 18,
x: 114,
y: 73,
text: fmt.Sprintf("[%.2f]", difference),
}
timeT := time.Unix(data.Timestamp, 0)
updateTime := Line{
fontSize: 9,
x: 112,
y: 93,
text: timeT.Format("02/01/06 15:04:05"),
}
return []Line{
title,
ounces,
kilos,
percentChange,
priceChange,
updateTime,
}
}