Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed font-size estimation #52

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions buttons/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ type TextButton struct {
updateHandler func(streamdeck.Button)
btnIndex int
actionHandler streamdeck.ButtonActionHandler
margin int
}

// GetImageForButton is the interface implemention to get the button's image as an image.Image
func (btn *TextButton) GetImageForButton(btnSize int) image.Image {
img := getImageWithText(btn.label, btn.textColour, btn.backgroundColour, btnSize)
img := getImageWithText(btn.label, btn.textColour, btn.backgroundColour, btnSize, btn.margin)
return img
}

Expand Down Expand Up @@ -80,19 +81,36 @@ func (btn *TextButton) Pressed() {
// background. The text will be set on a single line, and auto-sized to fill the button as best
// as possible.
func NewTextButton(label string) *TextButton {
btn := NewTextButtonWithColours(label, color.White, color.Black)
btn := NewTextButtonWithColoursAndMargin(label, color.White, color.Black, 6)
return btn
}

// NewTextButtonWithMargin creates a new TextButton with the specified text on it, in white on a
// black background. The text will be set on a single line, and auto-sized to fill the button as
// best as possible, taking into account the margin on each side.
func NewTextButtonWithMargin(label string, margin int) *TextButton {
btn := NewTextButtonWithColoursAndMargin(label, color.White, color.Black, margin)
return btn
}

// NewTextButtonWithColours creates a new TextButton with the specified text on it, in the specified
// text and background colours. The text will be set on a single line, and auto-sized to fill the
// button as best as possible.
func NewTextButtonWithColours(label string, textColour color.Color, backgroundColour color.Color) *TextButton {
btn := &TextButton{label: label, textColour: textColour, backgroundColour: backgroundColour}
btn := NewTextButtonWithColoursAndMargin(label, textColour, backgroundColour, 6)
return btn
}

func getImageWithText(text string, textColour color.Color, backgroundColour color.Color, btnSize int) image.Image {
// NewTextButtonWithColoursAndMargin creates a new TextButton with the specified text on it, in the
// specified text and background colours. The text will be set on a single line, and auto-sized to
// fill the button as best as possible, taking into account the margin on each side.
func NewTextButtonWithColoursAndMargin(label string, textColour color.Color, backgroundColour color.Color, margin int) *TextButton {
btn := &TextButton{label: label, textColour: textColour, backgroundColour: backgroundColour, margin: margin}
return btn
}


func getImageWithText(text string, textColour color.Color, backgroundColour color.Color, btnSize int, margin int) image.Image {

size := float64(18)

Expand All @@ -104,7 +122,7 @@ func getImageWithText(text string, textColour color.Color, backgroundColour colo
width := 0
for size = 1; size < 60; size++ {
width = getTextWidth(text, size)
if width > 90 {
if width >= btnSize - (margin * 2) {
size = size - 1
break
}
Expand All @@ -123,7 +141,7 @@ func getImageWithText(text string, textColour color.Color, backgroundColour colo
c.SetClip(dstImg.Bounds())

x := int((btnSize - width) / 2) // Horizontally centre text
y := int(50 + (size / 3)) // Fudged vertical centre, erm, very "heuristic"
y := int(btnSize / 2) + int(size / 3) // Fudged vertical centre, erm, very "heuristic"

pt := freetype.Pt(x, y)
c.DrawString(text, pt)
Expand Down