Skip to content

Commit

Permalink
Move font helpers to fonts.go
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed May 29, 2021
1 parent de5ab23 commit 7db449e
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 41 deletions.
105 changes: 105 additions & 0 deletions fonts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package main

import (
"image"
"image/color"
"io/ioutil"
"log"

"github.com/flopp/go-findfont"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
)

var (
ttfFont *truetype.Font
ttfThinFont *truetype.Font
ttfBoldFont *truetype.Font
)

// maxPointSize returns the maximum point size we can use to fit text inside width and height
// as well as the resulting text-width in pixels.
func maxPointSize(text string, c *freetype.Context, width, height int) (float64, int) {
// never let the font size exceed the requested height
fontsize := 72.0
for c.PointToFixed(fontsize).Round() > height {
fontsize -= 1
}

// offset initial loop iteration
fontsize += 1

// find the biggest matching font size for the requested width
var actwidth int
for actwidth = width + 1; actwidth > width; fontsize -= 1 {
c.SetFontSize(fontsize)

textExtent, err := c.DrawString(text, freetype.Pt(0, 0))
if err != nil {
return 0, 0
}

actwidth = textExtent.X.Round()
}

return fontsize, actwidth
}

func fontByName(font string) *truetype.Font {
switch font {
case "thin":
return ttfThinFont
case "regular":
return ttfFont
case "bold":
return ttfBoldFont
default:
return ttfFont
}
}

func ftContext(img *image.RGBA, ttf *truetype.Font, fontsize float64) *freetype.Context {
c := freetype.NewContext()
c.SetDPI(float64(dev.DPI))
c.SetFont(ttf)
c.SetSrc(image.NewUniform(color.RGBA{0, 0, 0, 0}))
c.SetDst(img)
c.SetClip(img.Bounds())
c.SetHinting(font.HintingFull)
c.SetFontSize(fontsize)

return c
}

func loadFont(name string) (*truetype.Font, error) {
fontPath, err := findfont.Find(name)
if err != nil {
return nil, err
}

ttf, err := ioutil.ReadFile(fontPath)
if err != nil {
return nil, err
}

return freetype.ParseFont(ttf)
}

func init() {
var err error
ttfFont, err = loadFont("Roboto-Regular.ttf")
if err != nil {
log.Fatal(err)
}

ttfThinFont, err = loadFont("Roboto-Thin.ttf")
if err != nil {
log.Fatal(err)
}

ttfBoldFont, err = loadFont("Roboto-Bold.ttf")
if err != nil {
log.Fatal(err)
}
}
41 changes: 0 additions & 41 deletions widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,14 @@ import (
"image"
"image/color"
"image/draw"
"io/ioutil"
"log"
"os"
"strconv"

"github.com/flopp/go-findfont"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"github.com/muesli/streamdeck"
"github.com/nfnt/resize"
"golang.org/x/image/font"
)

var (
ttfFont *truetype.Font
ttfThinFont *truetype.Font
ttfBoldFont *truetype.Font
)

type Widget interface {
Expand Down Expand Up @@ -167,35 +158,3 @@ func drawString(img *image.RGBA, bounds image.Rectangle, ttf *truetype.Font, tex
log.Fatal(err)
}
}

func loadFont(name string) (*truetype.Font, error) {
fontPath, err := findfont.Find(name)
if err != nil {
return nil, err
}

ttf, err := ioutil.ReadFile(fontPath)
if err != nil {
return nil, err
}

return freetype.ParseFont(ttf)
}

func init() {
var err error
ttfFont, err = loadFont("Roboto-Regular.ttf")
if err != nil {
log.Fatal(err)
}

ttfThinFont, err = loadFont("Roboto-Thin.ttf")
if err != nil {
log.Fatal(err)
}

ttfBoldFont, err = loadFont("Roboto-Bold.ttf")
if err != nil {
log.Fatal(err)
}
}

0 comments on commit 7db449e

Please sign in to comment.