82 lines
1.4 KiB
Go
82 lines
1.4 KiB
Go
package fonts
|
|
|
|
import (
|
|
"bytes"
|
|
"log"
|
|
|
|
_ "embed"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
|
"golang.org/x/image/font"
|
|
"golang.org/x/image/font/opentype"
|
|
"golang.org/x/image/font/sfnt"
|
|
)
|
|
|
|
const (
|
|
FontDPI = 72
|
|
FontSizeStandard = 16
|
|
FontSizeLarge = 24
|
|
)
|
|
|
|
type FontStruct struct {
|
|
Standard font.Face
|
|
Large font.Face
|
|
New *text.GoTextFaceSource
|
|
Bitfont *text.GoTextFaceSource
|
|
}
|
|
|
|
var (
|
|
|
|
//go:embed vcrmono.ttf
|
|
vcrmono_ttf []byte
|
|
//go:embed bitbybit.ttf
|
|
bitbybit_ttf []byte
|
|
|
|
LaunchyFont FontStruct
|
|
)
|
|
|
|
func LoadFontFatal(src []byte) *sfnt.Font {
|
|
tt, err := opentype.Parse(src)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
return tt
|
|
}
|
|
|
|
func GetFaceFatal(fnt *sfnt.Font, dpi, size float64) font.Face {
|
|
var face font.Face
|
|
var err error
|
|
|
|
if dpi > 0 && size > 0 && fnt != nil {
|
|
face, err = opentype.NewFace(fnt, &opentype.FaceOptions{
|
|
Size: size,
|
|
DPI: dpi,
|
|
Hinting: font.HintingVertical,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
return face
|
|
}
|
|
|
|
func init() {
|
|
LaunchyFont = FontStruct{}
|
|
|
|
fnt := LoadFontFatal(vcrmono_ttf)
|
|
LaunchyFont.Standard = GetFaceFatal(fnt, FontDPI, FontSizeStandard)
|
|
LaunchyFont.Large = GetFaceFatal(fnt, FontDPI, FontSizeLarge)
|
|
|
|
s, err := text.NewGoTextFaceSource(bytes.NewReader(vcrmono_ttf))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
LaunchyFont.New = s
|
|
|
|
s, err = text.NewGoTextFaceSource(bytes.NewReader(bitbybit_ttf))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
LaunchyFont.Bitfont = s
|
|
}
|