Skip to content

Commit

Permalink
perf(dynamicicon): Only keep image in memory during generation
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jan 30, 2025
1 parent 79c861c commit d407884
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions internal/dynamicicon/dynamicicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
_ "embed"
"errors"
"image"
"image/draw"
"log/slog"
"os"
"path/filepath"
Expand Down Expand Up @@ -36,15 +35,10 @@ type DynamicIcon struct {
mu sync.Mutex

font *truetype.Font
img *image.NRGBA
}

func New(conf *config.Config) *DynamicIcon {
d := &DynamicIcon{
config: conf,
img: image.NewNRGBA(image.Rectangle{Max: image.Point{X: width, Y: height}}),
}
return d
return &DynamicIcon{config: conf}
}

var ErrFontSize = errors.New("unable to determine the correct font size")
Expand Down Expand Up @@ -104,8 +98,9 @@ func (d *DynamicIcon) Generate(p *nightscout.Properties) ([]byte, error) {
}
}()

img := image.NewRGBA(image.Rect(0, 0, width, height))
drawer := &font.Drawer{
Dst: d.img,
Dst: img,
Src: image.NewUniform(d.config.DynamicIcon.FontColor),
}

Expand All @@ -129,14 +124,13 @@ func (d *DynamicIcon) Generate(p *nightscout.Properties) ([]byte, error) {

metrics := face.Metrics()

draw.Draw(d.img, d.img.Bounds(), image.Transparent, image.Point{}, draw.Src)
drawer.Dot.X = (widthF - drawer.MeasureString(bgnow)) / 2
drawer.Dot.Y = (heightF + metrics.Ascent - metrics.Descent) / 2
drawer.DrawString(bgnow)

var buf bytes.Buffer
buf.Grow(2 * bytefmt.KiB)
if err := encode(&buf, d.img); err != nil {
if err := encode(&buf, img); err != nil {
return nil, err
}

Expand Down

0 comments on commit d407884

Please sign in to comment.