feat: support get and set default font

This commit is contained in:
vicanso 2022-11-12 20:01:36 +08:00
parent 2ed86a81d0
commit de4250f60b
6 changed files with 24 additions and 10 deletions

19
font.go
View file

@ -32,9 +32,13 @@ import (
var fonts = sync.Map{}
var ErrFontNotExists = errors.New("font is not exists")
var defaultFontFamily = "defaultFontFamily"
func init() {
_ = InstallFont("roboto", roboto.Roboto)
name := "roboto"
_ = InstallFont(name, roboto.Roboto)
font, _ := GetFont(name)
SetDefaultFont(font)
}
// InstallFont installs the font for charts
@ -47,6 +51,19 @@ func InstallFont(fontFamily string, data []byte) error {
return nil
}
// GetDefaultFont get default font
func GetDefaultFont() (*truetype.Font, error) {
return GetFont(defaultFontFamily)
}
// SetDefaultFont set default font
func SetDefaultFont(font *truetype.Font) {
if font == nil {
return
}
fonts.Store(defaultFontFamily, font)
}
// GetFont get the font by font family
func GetFont(fontFamily string) (*truetype.Font, error) {
value, ok := fonts.Load(fontFamily)