Hochwasser/render/text.go

43 lines
870 B
Go
Raw Normal View History

2020-02-14 22:29:58 +01:00
package render
import (
"image"
"golang.org/x/image/draw"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
)
func pt(p fixed.Point26_6) image.Point {
return image.Point{
X: int(p.X+32) >> 6,
Y: int(p.Y+32) >> 6,
}
}
2022-01-02 08:07:49 +01:00
func RenderText(text string, scale float64, texture, bgTex image.Image) *image.NRGBA {
2020-02-14 22:29:58 +01:00
face := basicfont.Face7x13
stringBounds, _ := font.BoundString(face, text)
b := image.Rectangle{pt(stringBounds.Min), pt(stringBounds.Max)}
img := image.NewNRGBA(b)
2020-12-28 00:58:50 +01:00
if bgTex != nil {
draw.Draw(img, b, bgTex, image.Point{}, draw.Src)
2020-02-15 13:58:01 +01:00
}
2020-02-14 22:29:58 +01:00
d := font.Drawer{
Dst: img,
2020-12-28 00:58:50 +01:00
Src: texture,
2020-02-14 22:29:58 +01:00
Face: face,
}
d.DrawString(text)
// normalize bounds to start at 0,0
img.Rect = img.Bounds().Sub(img.Bounds().Min)
// scale up, as this font is quite small
2022-01-02 08:07:49 +01:00
return ScaleImage(img, scale, scale, false)
2020-02-14 22:29:58 +01:00
}