zntg/ui/fonts.go

66 lines
1.2 KiB
Go

package ui
import (
"github.com/spf13/afero"
"opslag.de/schobers/fs/vfs"
"opslag.de/schobers/galleg/allegro5"
)
type Fonts interface {
Register(name, path string, size int) error
Get(name string) *allegro5.Font
Destroy()
}
type fonts struct {
fts map[string]*allegro5.Font
dir vfs.CopyDir
}
func newFonts(dir vfs.CopyDir) *fonts {
return &fonts{make(map[string]*allegro5.Font), dir}
}
func NewFonts(fs afero.Fs) (Fonts, error) {
var dir, err = vfs.NewCopyDir(fs)
if nil != err {
return nil, err
}
return newFonts(dir), nil
}
func (fts *fonts) load(name, path string, size int) error {
f, err := allegro5.LoadTTFFont(path, size)
if nil != err {
return err
}
fts.fts[name] = f
return nil
}
func (fts *fonts) loadFromBox(name, path string, size int) error {
path, err := fts.dir.Retrieve(path)
if nil != err {
return err
}
return fts.load(name, path, size)
}
func (fts *fonts) Register(name, path string, size int) error {
if nil != fts.dir {
return fts.loadFromBox(name, path, size)
}
return fts.load(name, path, size)
}
func (fts *fonts) Get(name string) *allegro5.Font {
return fts.fts[name]
}
func (fts *fonts) Destroy() {
for _, f := range fts.fts {
f.Destroy()
}
fts.dir.Destroy()
}