zntg/ui/fonts.go

66 lines
1.2 KiB
Go
Raw Normal View History

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