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