From 5fe298fb2d821f17449dc69119c063adcf741b98 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Sat, 24 Aug 2024 18:16:17 -0500 Subject: [PATCH] feat(dynamicicon): Search system font dirs if font file is not found --- config_example.toml | 2 +- go.mod | 1 + go.sum | 2 ++ internal/config/config.go | 2 +- internal/dynamicicon/dynamicicon.go | 14 +++++++++++++- 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/config_example.toml b/config_example.toml index 69435ea..a11ee77 100644 --- a/config_example.toml +++ b/config_example.toml @@ -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 diff --git a/go.mod b/go.mod index 6965ca2..2aaed1c 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 53812bf..535ec4e 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/config/config.go b/internal/config/config.go index e9cbece..8f002cb 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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."` } diff --git a/internal/dynamicicon/dynamicicon.go b/internal/dynamicicon/dynamicicon.go index 10824ef..b2bb75d 100644 --- a/internal/dynamicicon/dynamicicon.go +++ b/internal/dynamicicon/dynamicicon.go @@ -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" @@ -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 + } } }