1
0
Fork 0
mirror of https://github.com/Luzifer/streamdeck.git synced 2024-12-29 22:21:24 +00:00

Add support for multi-line strings

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2020-06-07 00:21:17 +02:00
parent 5282f7304e
commit 8e3a5d6ed1
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

View file

@ -210,19 +210,23 @@ func (d *displayElementExec) StopLoopDisplay() error {
func (displayElementExec) drawText(c *freetype.Context, text string, textColor color.Color, fontsize float64, border int) error { func (displayElementExec) drawText(c *freetype.Context, text string, textColor color.Color, fontsize float64, border int) error {
c.SetSrc(image.NewUniform(color.RGBA{0x0, 0x0, 0x0, 0x0})) // Transparent for text size guessing c.SetSrc(image.NewUniform(color.RGBA{0x0, 0x0, 0x0, 0x0})) // Transparent for text size guessing
var ( textLines := strings.Split(text, "\n")
err error
ext fixed.Point26_6
)
for { for {
c.SetFontSize(fontsize) c.SetFontSize(fontsize)
ext, err = c.DrawString(text, freetype.Pt(0, 0)) var maxX fixed.Int26_6
for _, tl := range textLines {
ext, err := c.DrawString(tl, freetype.Pt(0, 0))
if err != nil { if err != nil {
return errors.Wrap(err, "Unable to measure text") return errors.Wrap(err, "Unable to measure text")
} }
if ext.X > maxX {
maxX = ext.X
}
}
if int(float64(ext.X)/64) > sd.IconSize()-2*border || int(c.PointToFixed(fontsize/2.0)/64) > sd.IconSize()-2*border { if int(float64(maxX)/64) > sd.IconSize()-2*border || (int(c.PointToFixed(fontsize)/64))*len(textLines)+(len(textLines)-1)*2 > sd.IconSize()-2*border {
fontsize -= 2 fontsize -= 2
continue continue
} }
@ -230,14 +234,31 @@ func (displayElementExec) drawText(c *freetype.Context, text string, textColor c
break break
} }
var (
yTotal = (int(c.PointToFixed(fontsize)/64))*len(textLines) + len(textLines)*2
yLineTop = int(float64(sd.IconSize())/2.0 - float64(yTotal)/2.0)
)
for _, tl := range textLines {
c.SetSrc(image.NewUniform(color.RGBA{0x0, 0x0, 0x0, 0x0})) // Transparent for text size guessing
ext, err := c.DrawString(tl, freetype.Pt(0, 0))
if err != nil {
return errors.Wrap(err, "Unable to measure text")
}
c.SetSrc(image.NewUniform(textColor)) c.SetSrc(image.NewUniform(textColor))
xcenter := (float64(sd.IconSize()-2*border) / 2.0) - (float64(int(float64(ext.X)/64)) / 2.0) + float64(border) xcenter := (float64(sd.IconSize()-2*border) / 2.0) - (float64(int(float64(ext.X)/64)) / 2.0) + float64(border)
ycenter := (float64(sd.IconSize()-2*border) / 2.0) + (float64(c.PointToFixed(fontsize/2.0)/64) / 2.0) + float64(border) ylower := yLineTop + int(c.PointToFixed(fontsize)/64)
_, err = c.DrawString(text, freetype.Pt(int(xcenter), int(ycenter))) if _, err = c.DrawString(tl, freetype.Pt(int(xcenter), int(ylower))); err != nil {
return errors.Wrap(err, "Unable to draw text")
}
return err yLineTop += int(c.PointToFixed(fontsize)/64) + 2
}
return nil
} }
func (displayElementExec) getImageFromDisk(filename string) (image.Image, error) { func (displayElementExec) getImageFromDisk(filename string) (image.Image, error) {