-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
render.go
97 lines (81 loc) · 2.1 KB
/
render.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
package textboard
import (
"context"
"fmt"
"image"
"image/color"
"image/draw"
"strings"
"go.uber.org/zap"
"github.com/robbydyer/sports/internal/board"
"github.com/robbydyer/sports/internal/logo"
"github.com/robbydyer/sports/internal/rgbrender"
)
const logoCacheDir = "/tmp/sportsmatrix_logos/newslogos"
func (s *TextBoard) renderLogo(ctx context.Context, canvas board.Canvas) error {
s.Lock()
defer s.Unlock()
zeroed := rgbrender.ZeroedBounds(canvas.Bounds())
if s.config.halfSizeLogo {
var err error
zeroed, err = rgbrender.AlignPosition(rgbrender.CenterCenter, zeroed, zeroed.Dx()/2, zeroed.Dy()/2)
if err != nil {
return err
}
}
key := fmt.Sprintf("%s_%dx%d", strings.ReplaceAll(s.api.HTTPPathPrefix(), "/", ""), zeroed.Dx(), zeroed.Dy())
l, ok := s.logos[key]
if !ok {
g := func(ctx context.Context) (image.Image, error) {
return s.api.GetLogo(ctx)
}
l = logo.New(key, g, logoCacheDir, zeroed, &logo.Config{
Abbrev: "news",
XSize: zeroed.Dx(),
YSize: zeroed.Dy(),
Pt: &logo.Pt{
X: 0,
Y: 0,
Zoom: 1,
},
})
}
i, err := l.GetThumbnail(ctx, zeroed)
if err != nil {
return err
}
draw.Draw(canvas, zeroed, i, image.Point{}, draw.Over)
return nil
}
func (s *TextBoard) doRender(canvas board.Canvas, text string) error {
zeroed := rgbrender.ZeroedBounds(canvas.Bounds())
lengths, err := s.writer.MeasureStrings(canvas, []string{text})
if err != nil {
return err
}
if len(lengths) < 1 {
return fmt.Errorf("failed to measure text")
}
bounds := image.Rect(zeroed.Min.X, zeroed.Min.Y, zeroed.Min.X+lengths[0], zeroed.Max.Y)
s.log.Debug("writing headline",
zap.String("text", text),
zap.Int("pix length", lengths[0]),
zap.Int("X", bounds.Min.X),
zap.Int("Y", bounds.Min.Y),
zap.Int("X", bounds.Max.X),
zap.Int("Y", bounds.Max.Y),
zap.Int("canvas X", zeroed.Min.X),
zap.Int("canvas Y", zeroed.Min.Y),
zap.Int("canvas X", zeroed.Max.X),
zap.Int("canvas Y", zeroed.Max.Y),
)
canvas.SetWidth(bounds.Dx())
_ = s.writer.WriteAligned(
rgbrender.CenterCenter,
canvas,
bounds,
[]string{text},
color.White,
)
return nil
}