Skip to content

Commit

Permalink
feat(dynamicicon): Search system font dirs if font file is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Aug 25, 2024
1 parent bea9b62 commit 5fe298f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion config_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ units = 'mg/dL'
enabled = false
# Hex code used to render text.
font-color = '#fff'
# If left blank, an embedded font will be used.
# Font path or filename of a system font. If left blank, an embedded font will be used.
font-file = ''
# Maximum font size in points.
max-font-size = 40.0
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
fyne.io/systray v1.11.0
github.com/Masterminds/sprig/v3 v3.2.3
github.com/emersion/go-autostart v0.0.0-20210130080809-00ed301c8e9a
github.com/flopp/go-findfont v0.1.0
github.com/goki/freetype v1.0.5
github.com/hhsnopek/etag v0.0.0-20171206181245-aea95f647346
github.com/knadh/koanf/providers/file v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/emersion/go-autostart v0.0.0-20210130080809-00ed301c8e9a h1:M88ob4TyD
github.com/emersion/go-autostart v0.0.0-20210130080809-00ed301c8e9a/go.mod h1:buzQsO8HHkZX2Q45fdfGH1xejPjuDQaXH8btcYMFzPM=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/flopp/go-findfont v0.1.0 h1:lPn0BymDUtJo+ZkV01VS3661HL6F4qFlkhcJN55u6mU=
github.com/flopp/go-findfont v0.1.0/go.mod h1:wKKxRDjD024Rh7VMwoU90i6ikQRCr+JTHB5n4Ejkqvw=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 h1:TQcrn6Wq+sKGkpyPvppOz99zsMBaUOKXq6HSv655U1c=
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Config struct {
type DynamicIcon struct {
Enabled bool `toml:"enabled"`
FontColor HexColor `toml:"font-color" comment:"Hex code used to render text."`
FontFile string `toml:"font-file" comment:"If left blank, an embedded font will be used."`
FontFile string `toml:"font-file" comment:"Font path or filename of a system font. If left blank, an embedded font will be used."`
MaxFontSize float64 `toml:"max-font-size" comment:"Maximum font size in points."`
}

Expand Down
14 changes: 13 additions & 1 deletion internal/dynamicicon/dynamicicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sync"
"time"

"github.com/flopp/go-findfont"
"github.com/gabe565/nightscout-menu-bar/internal/config"
"github.com/gabe565/nightscout-menu-bar/internal/nightscout"
"github.com/goki/freetype/truetype"
Expand Down Expand Up @@ -67,7 +68,18 @@ func (d *DynamicIcon) Generate(p *nightscout.Properties) ([]byte, error) {

var err error
if b, err = os.ReadFile(path); err != nil {
return nil, err
if !os.IsNotExist(err) {
return nil, err
}

path, findErr := findfont.Find(d.config.DynamicIcon.FontFile)
if findErr != nil {
return nil, errors.Join(err, findErr)
}

if b, err = os.ReadFile(path); err != nil {
return nil, err
}
}
}

Expand Down

0 comments on commit 5fe298f

Please sign in to comment.