zntg/ui/fonts.go
Sander Schobers 3591e22c97 Added Fonts() to context similarly as Textures().
- Fonts are now managed by context instead of the implementation specific renderers.
2020-05-15 15:42:24 +02:00

71 lines
1.3 KiB
Go

package ui
import (
"image/color"
"opslag.de/schobers/geom"
)
type Fonts struct {
render Renderer
fonts map[string]Font
}
func NewFonts(render Renderer) *Fonts {
return &Fonts{render, map[string]Font{}}
}
func (f *Fonts) AddFont(name string, font Font) {
curr := f.fonts[name]
if curr != nil {
curr.Destroy()
}
f.fonts[name] = font
}
func (f *Fonts) createFont(name string, create func() (Font, error)) (Font, error) {
font, err := create()
if err != nil {
return nil, err
}
f.AddFont(name, font)
return font, nil
}
func (f *Fonts) CreateFontPath(name, path string, size int) (Font, error) {
return f.createFont(name, func() (Font, error) {
return f.render.CreateFontPath(path, size)
})
}
func (f *Fonts) Destroy() {
for _, font := range f.fonts {
font.Destroy()
}
f.fonts = nil
}
func (f *Fonts) Font(name string) Font {
font, ok := f.fonts[name]
if ok {
return font
}
return nil
}
func (f *Fonts) Text(fontName string, p geom.PointF32, color color.Color, text string) {
font := f.Font(fontName)
if font == nil {
return
}
f.render.Text(font, p, color, text)
}
func (f *Fonts) TextAlign(fontName string, p geom.PointF32, color color.Color, text string, align HorizontalAlignment) {
font := f.Font(fontName)
if font == nil {
return
}
f.render.TextAlign(font, p, color, text, align)
}