2020-05-15 13:42:24 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2020-05-22 15:42:02 +00:00
|
|
|
"fmt"
|
2020-05-15 13:42:24 +00:00
|
|
|
"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)
|
|
|
|
}
|
2020-05-22 15:42:02 +00:00
|
|
|
|
|
|
|
func (f *Fonts) TextTexture(fontName string, color color.Color, text string) (Texture, error) {
|
|
|
|
font := f.Font(fontName)
|
|
|
|
if font == nil {
|
|
|
|
return nil, fmt.Errorf("no font with name '%s'", fontName)
|
|
|
|
}
|
|
|
|
return f.render.TextTexture(font, color, text)
|
|
|
|
}
|