gg/text.go

70 lines
1.3 KiB
Go
Raw Normal View History

2024-01-13 18:17:34 +03:00
package gg
import (
//"strings"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"github.com/hajimehoshi/ebiten/v2/text"
"github.com/hajimehoshi/ebiten/v2"
"io"
//"fmt"
)
//type FontOptions = font.Options
var (
FontHintingNone = font.HintingNone
)
type Face = font.Face
type TtfFont = opentype.Font
type TtfFaceOptions = opentype.FaceOptions
type TtfFace = opentype.Face
func MakeFaceFromTtf(src io.ReaderAt, opts *TtfFaceOptions) (Face, error) {
fnt, err := ParseTtfFont(src)
if err != nil {
return nil, err
}
face, err := NewTtfFace(fnt, opts)
if err != nil {
return nil, err
}
return face, nil
}
func ParseTtfFont(src io.ReaderAt) (*TtfFont, error) {
return opentype.ParseReaderAt(src)
}
func NewTtfFace(fnt *TtfFont, opts *TtfFaceOptions) (Face, error) {
return opentype.NewFace(fnt, opts)
}
// The type implements basic drawable text.
// (Now needs to implement rotation, scaling etc, cause now only position)
type Text struct {
Object
Transform
Text string
Face Face
Colority
Visibility
}
func (txt *Text) Draw(c *Context) []EVertex {
m := txt.Matrix()
m.Concat(c.Camera.RealMatrix())
//x, y := txt.Position.XY()
//text.Draw(c.Image)
text.DrawWithOptions(c.Image, txt.Text, txt.Face,
&ebiten.DrawImageOptions{
GeoM: m,
},
)
return nil
}