mirror of
https://github.com/Luzifer/streamdeck.git
synced 2024-12-20 17:51:21 +00:00
Add support for multi-line strings
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
5282f7304e
commit
8e3a5d6ed1
1 changed files with 34 additions and 13 deletions
|
@ -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 {
|
||||
c.SetSrc(image.NewUniform(color.RGBA{0x0, 0x0, 0x0, 0x0})) // Transparent for text size guessing
|
||||
|
||||
var (
|
||||
err error
|
||||
ext fixed.Point26_6
|
||||
)
|
||||
textLines := strings.Split(text, "\n")
|
||||
|
||||
for {
|
||||
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 {
|
||||
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
|
||||
continue
|
||||
}
|
||||
|
@ -230,14 +234,31 @@ func (displayElementExec) drawText(c *freetype.Context, text string, textColor c
|
|||
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))
|
||||
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue