-
Notifications
You must be signed in to change notification settings - Fork 22
/
text.go
85 lines (67 loc) · 1.91 KB
/
text.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
package streamdeck
import (
"image"
"image/color"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font/gofont/gomedium"
)
// WriteTextToButton is a low-level way to write text directly onto a button on the StreamDeck
func (d *Device) WriteTextToButton(btnIndex int, text string, textColour color.Color, backgroundColour color.Color) {
img := getImageWithText(text, textColour, backgroundColour, d.deviceType.imageSize.X)
d.WriteRawImageToButton(btnIndex, img)
}
func getImageWithText(text string, textColour color.Color, backgroundColour color.Color, btnSize int) image.Image {
size := float64(18)
myfont, err := truetype.Parse(gomedium.TTF)
if err != nil {
panic(err)
}
width := 0
for size = 1; size < 60; size++ {
width = getTextWidth(text, size)
if width > 90 {
size = size - 1
break
}
}
srcImg := image.NewUniform(textColour)
dstImg := getSolidColourImage(backgroundColour, btnSize)
c := freetype.NewContext()
c.SetFont(myfont)
c.SetDst(dstImg)
c.SetSrc(srcImg)
c.SetFontSize(size)
c.SetClip(dstImg.Bounds())
x := int((btnSize - width) / 2) // Horizontally centre text
y := int((float64(btnSize) / 2) + (size / 3)) // Fudged vertical centre, erm, very "heuristic"
pt := freetype.Pt(x, y)
c.DrawString(text, pt)
/*
textWidth := 7 * len(text)
fmt.Println(textWidth)
f := &font.Drawer{
Dst: dstImg,
Src: srcImg,
Face: basicfont.Face7x13,
Dot: fixed.Point26_6{fixed.Int26_6(x * 64), fixed.Int26_6(y * 64)},
}
f.DrawString(text)
*/
return dstImg
}
func getTextWidth(text string, size float64) int {
myfont, err := truetype.Parse(gomedium.TTF)
if err != nil {
panic(err)
}
// Calculate width of string
width := 0
face := truetype.NewFace(myfont, &truetype.Options{Size: size})
for _, x := range text {
awidth, _ := face.GlyphAdvance(rune(x))
iwidthf := int(float64(awidth) / 64)
width += iwidthf
}
return width
}