zntg/ui/fonts.go
2018-09-22 16:31:09 +02:00

66 lines
1.2 KiB
Go

package ui
import (
"github.com/spf13/afero"
"opslag.de/schobers/fs/vfs"
"opslag.de/schobers/zntg/allg5"
)
type Fonts interface {
Register(name, path string, size int) error
Get(name string) *allg5.Font
Destroy()
}
type fonts struct {
fts map[string]*allg5.Font
dir vfs.CopyDir
}
func newFonts(dir vfs.CopyDir) *fonts {
return &fonts{make(map[string]*allg5.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 := allg5.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) *allg5.Font {
return fts.fts[name]
}
func (fts *fonts) Destroy() {
for _, f := range fts.fts {
f.Destroy()
}
fts.dir.Destroy()
}