krampus19/alui/fonts.go

58 lines
955 B
Go
Raw Normal View History

2019-12-19 05:59:45 +00:00
package alui
import (
"opslag.de/schobers/allg5"
)
type Fonts struct {
fonts map[string]*allg5.Font
}
type FontDescription struct {
Path string
Size int
Name string
}
func NewFonts() *Fonts {
return &Fonts{map[string]*allg5.Font{}}
}
func (f *Fonts) Destroy() {
for _, font := range f.fonts {
font.Destroy()
}
f.fonts = nil
}
2019-12-23 22:42:33 +00:00
func (f *Fonts) Len() int { return len(f.fonts) }
2019-12-19 05:59:45 +00:00
func (f *Fonts) Load(path string, size int, name string) error {
font, err := allg5.LoadTTFFont(path, size)
if err != nil {
return err
}
if old := f.fonts[name]; old != nil {
old.Destroy()
}
f.fonts[name] = font
return nil
}
func (f *Fonts) LoadFonts(descriptions ...FontDescription) error {
for _, desc := range descriptions {
err := f.Load(desc.Path, desc.Size, desc.Name)
if err != nil {
return err
}
}
return nil
}
func (f *Fonts) Get(name string) *allg5.Font {
if name == "" {
return f.Get("default")
}
return f.fonts[name]
}