Skip to content

Commit

Permalink
Support h/v centering and restricted image bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed May 27, 2021
1 parent a92734b commit 9acdc9d
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/muesli/streamdeck"
"github.com/nfnt/resize"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)

var (
Expand Down Expand Up @@ -52,6 +51,7 @@ func (w *BaseWidget) ActionHold() *ActionConfig {
}

func (w *BaseWidget) TriggerAction() {
// just a stub
}

func NewWidget(index uint8, id string, action *ActionConfig, actionHold *ActionConfig, config map[string]string) Widget {
Expand Down Expand Up @@ -87,6 +87,7 @@ func NewWidget(index uint8, id string, action *ActionConfig, actionHold *ActionC
mode: config["mode"],
fillColor: config["fillColor"],
}

default:
// unknown widget ID
fmt.Println("Unknown widget with ID:", id)
Expand All @@ -95,7 +96,7 @@ func NewWidget(index uint8, id string, action *ActionConfig, actionHold *ActionC
return nil
}

func drawImage(img *image.RGBA, path string, size uint, x uint, y uint) error {
func drawImage(img *image.RGBA, path string, size int, pt image.Point) error {
f, err := os.Open(path)
if err != nil {
return err
Expand All @@ -107,15 +108,24 @@ func drawImage(img *image.RGBA, path string, size uint, x uint, y uint) error {
return err
}

icon = resize.Resize(size, size, icon, resize.Bilinear)
draw.Draw(img, image.Rect(int(x), int(y), int(x+size), int(y+size)), icon, image.Point{0, 0}, draw.Src)
if pt.X < 0 {
xcenter := float64(img.Bounds().Dx()/2.0) - (float64(size) / 2.0)
pt = image.Pt(int(xcenter), pt.Y)
}
if pt.Y < 0 {
ycenter := float64(img.Bounds().Dy()/2.0) - (float64(size) / 2.0)
pt = image.Pt(pt.X, int(ycenter))
}

icon = resize.Resize(uint(size), uint(size), icon, resize.Bilinear)
draw.Draw(img, image.Rect(pt.X, pt.Y, pt.X+size, pt.Y+size), icon, image.Point{0, 0}, draw.Src)

return nil
}

func drawString(img *image.RGBA, ttf *truetype.Font, text string, fontsize float64, pt fixed.Point26_6) {
func drawString(img *image.RGBA, bounds image.Rectangle, ttf *truetype.Font, text string, fontsize float64, pt image.Point) {
c := freetype.NewContext()
c.SetDPI(124)
c.SetDPI(float64(dev.DPI))
c.SetFont(ttf)
c.SetSrc(image.NewUniform(color.RGBA{0, 0, 0, 0}))
c.SetDst(img)
Expand All @@ -125,25 +135,21 @@ func drawString(img *image.RGBA, ttf *truetype.Font, text string, fontsize float

// find text entent
c.SetSrc(image.NewUniform(color.RGBA{0, 0, 0, 0}))
extent, _ := c.DrawString(text, freetype.Pt(0, 0))
actwidth := int(float64(extent.X) / 64)
actheight := c.PointToFixed(fontsize/2.0) / 64
xcenter := (float64(img.Bounds().Dx()) / 2.0) - (float64(actwidth) / 2.0)
ycenter := (float64(58) / 2.0) + (float64(actheight) / 2.0)

if pt.X < 0 {
oldy := pt.Y
pt = freetype.Pt(int(xcenter), 0)
pt.Y = oldy
extent, _ := c.DrawString(text, freetype.Pt(0, 0))
actwidth := int(float64(extent.X) / 64.0)
xcenter := float64(bounds.Dx())/2.0 - (float64(actwidth) / 2.0)
pt = image.Pt(int(xcenter), pt.Y)
}
if pt.Y < 0 {
oldx := pt.X
pt = freetype.Pt(0, int(ycenter))
pt.X = oldx
actheight := int(float64(fontsize) * 72.0 / float64(dev.DPI))
ycenter := float64(bounds.Dy()/2.0) + float64(actheight)
pt = image.Pt(pt.X, bounds.Min.Y+int(ycenter))
}

c.SetSrc(image.NewUniform(color.RGBA{255, 255, 255, 255}))
if _, err := c.DrawString(text, pt); err != nil {
if _, err := c.DrawString(text, freetype.Pt(pt.X, pt.Y)); err != nil {
log.Fatal(err)
}
}
Expand Down

0 comments on commit 9acdc9d

Please sign in to comment.