blooblib/font.go

47 lines
951 B
Go

package bloob
import _ "embed"
type ch struct {
xForward int
image *Image
}
type Font struct {
chars []ch
lineHeight int
}
//go:embed "font.png"
var fontdata []byte
var DefaultFont = LoadTilesetFontBytes(fontdata, Vec2i{X: 8, Y: 14})
//go:embed "font-thin.png"
var fontdataThin []byte
var DefaultFontThin = LoadTilesetFontBytes(fontdataThin, Vec2i{X: 6, Y: 8})
func LoadTilesetFontBytes(data []byte, tilesize Vec2i) *Font {
tiles := LoadImageBytes(data).MakeTileset(tilesize)
var font Font
for i := 0; i < len(tiles); i += 1 {
c := ch{xForward: tilesize.X, image: tiles[i]}
font.chars = append(font.chars, c)
}
font.lineHeight = tilesize.Y + 2
return &font
}
func (f *Font) StringWidth(str string) int {
var l int
for i := 0; i < len(str); i += 1 {
l += f.chars[i].xForward
}
return l - 1
}
func (f *Font) StringBounds(str string) Vec2i {
//todo: multiline
return Vec2i{X: f.StringWidth(str), Y: f.lineHeight}
}