Skip to content

Commit

Permalink
Lazy load fonts (#704)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Spicer <mark@tidbyt.com>
  • Loading branch information
leoadberg and betterengineering authored Jul 31, 2023
1 parent 62848ce commit 93a500c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 31 deletions.
48 changes: 31 additions & 17 deletions render/fonts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,36 @@ import (
"golang.org/x/image/font"
)

var Font = map[string]font.Face{}

func init() {
for name, dataB64 := range FontDataRaw {
data, err := base64.StdEncoding.DecodeString(dataB64)
if err != nil {
log.Printf("couldn't decode %s: %s", name, err)
continue
}

f, err := bdf.Parse(data)
if err != nil {
log.Printf("couldn't parse %s: %s", name, err)
continue
}

Font[name] = f.NewFace()
var fontCache = map[string]font.Face{}

func GetFontList() []string {
fontNames := []string{}
for key := range fontDataRaw {
fontNames = append(fontNames, key)
}
return fontNames
}

func GetFont(name string) font.Face {
if font, ok := fontCache[name]; ok {
return font
}

dataB64, ok := fontDataRaw[name]
if !ok {
log.Panicf("Unknown font '%s', the available fonts are: %v", name, GetFontList())
}

data, err := base64.StdEncoding.DecodeString(dataB64)
if err != nil {
log.Panicf("couldn't decode %s: %s", name, err)
}

f, err := bdf.Parse(data)
if err != nil {
log.Panicf("couldn't parse %s: %s", name, err)
}

fontCache[name] = f.NewFace()
return fontCache[name]
}
2 changes: 1 addition & 1 deletion render/fonts_raw.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion render/gen/embedfonts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const fontsTemplate = `// Code generated by embedfonts.go, DO NOT EDIT.
package render
var FontDataRaw = map[string]string{
var fontDataRaw = map[string]string{
{{range .}} "{{.Name}}": "{{.DataB64}}",
{{end}}
}
Expand Down
6 changes: 3 additions & 3 deletions render/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func (t *Text) PaintBounds(bounds image.Rectangle, frameIdx int) image.Rectangle
}

func (t *Text) Init() error {
face := Font[DefaultFontFace]
if t.Font != "" {
face = Font[t.Font]
if t.Font == "" {
t.Font = DefaultFontFace
}
face := GetFont(t.Font)

dc := gg.NewContext(0, 0)
dc.SetFontFace(face)
Expand Down
12 changes: 6 additions & 6 deletions render/wrappedtext.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ type WrappedText struct {
}

func (tw WrappedText) PaintBounds(bounds image.Rectangle, frameIdx int) image.Rectangle {
face := Font[DefaultFontFace]
if tw.Font != "" {
face = Font[tw.Font]
if tw.Font == "" {
tw.Font = DefaultFontFace
}
face := GetFont(tw.Font)
// The bounds provided by user or parent widget
width := tw.Width
if width == 0 {
Expand Down Expand Up @@ -99,10 +99,10 @@ func (tw WrappedText) PaintBounds(bounds image.Rectangle, frameIdx int) image.Re
}

func (tw WrappedText) Paint(dc *gg.Context, bounds image.Rectangle, frameIdx int) {
face := Font[DefaultFontFace]
if tw.Font != "" {
face = Font[tw.Font]
if tw.Font == "" {
tw.Font = DefaultFontFace
}
face := GetFont(tw.Font)
// Text alignment
align := gg.AlignLeft
if tw.Align == "center" {
Expand Down
7 changes: 4 additions & 3 deletions runtime/modules/render_runtime/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 93a500c

Please sign in to comment.