tins2020/fonts.go

62 lines
1.0 KiB
Go
Raw Normal View History

2020-05-08 16:38:26 +00:00
package tins2020
import (
2020-05-09 13:32:43 +00:00
"fmt"
2020-05-08 16:38:26 +00:00
"github.com/veandco/go-sdl2/ttf"
"opslag.de/schobers/fs/vfs"
)
type Fonts struct {
dir vfs.CopyDir
fonts map[string]*ttf.Font
}
func (f *Fonts) Init(dir vfs.CopyDir) {
f.dir = dir
f.fonts = map[string]*ttf.Font{}
}
2020-05-09 13:32:43 +00:00
func (f *Fonts) Font(name string) *ttf.Font { return f.fonts[name] }
type FontDescriptor struct {
Name string
Path string
Size int
}
func (f *Fonts) LoadDesc(fonts ...FontDescriptor) error {
for _, desc := range fonts {
err := f.Load(desc.Name, desc.Path, desc.Size)
if err != nil {
return fmt.Errorf("error loading '%s'; error: %v", desc.Name, err)
}
}
return nil
}
2020-05-08 16:38:26 +00:00
func (f *Fonts) Load(name, path string, size int) error {
fontPath, err := f.dir.Retrieve(path)
if err != nil {
return err
}
font, err := ttf.OpenFont(fontPath, size)
if err != nil {
return err
}
if font, ok := f.fonts[name]; ok {
font.Close()
}
f.fonts[name] = font
return nil
}
func (f *Fonts) Destroy() {
if f.fonts == nil {
return
}
for _, f := range f.fonts {
f.Close()
}
}