text.BoundString gives a position about a dot to the left #1893
-
When calculating the bounding box for a text (in order to center), we see that the sample code : package main
import (
_ "embed"
"image/color"
"log"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/text"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
)
func main() {
ebiten.RunGame(&Game{Msg: `.HI`})
}
var _ ebiten.Game = &Game{}
// Any font has a similar result though its easy to see here.
// https://github.com/adobe-fonts/source-code-pro/raw/release/TTF/SourceCodePro-Medium.ttf
//go:embed SourceCodePro-Medium.ttf
var myfont []byte
var fontFace font.Face
func init() {
tt, err := opentype.Parse(myfont)
if err != nil {
log.Fatal(err)
}
fontFace, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 72,
DPI: 72,
Hinting: font.HintingNone,
})
if err != nil {
log.Fatal(err)
}
}
type Game struct {
Msg string
}
func (*Game) Update() error { return nil }
func (g *Game) Draw(screen *ebiten.Image) {
screen.Fill(color.Gray{128})
textDims := text.BoundString(fontFace, g.Msg)
// green rect, white text // supposedly centered
ebitenutil.DrawRect(screen,
float64(screen.Bounds().Min.X+(screen.Bounds().Dx()-textDims.Dx())/2),
float64(screen.Bounds().Min.Y+(screen.Bounds().Dy()-textDims.Dy())/2),
float64(textDims.Dx()), float64(textDims.Dy()), color.RGBA{0, 0xBB, 0, 0x77})
text.Draw(screen, g.Msg, fontFace,
screen.Bounds().Min.X+(screen.Bounds().Dx()-textDims.Dx())/2,
screen.Bounds().Min.Y+(screen.Bounds().Dy()+textDims.Dy())/2,
color.White)
// reddish rect, yellow text // supposedly top left
ebitenutil.DrawRect(screen,
float64(screen.Bounds().Min.X),
float64(screen.Bounds().Min.Y),
float64(textDims.Dx()), float64(textDims.Dy()), color.RGBA{0xFF, 0x22, 0, 0x77})
text.Draw(screen, g.Msg, fontFace,
screen.Bounds().Min.X,
screen.Bounds().Min.Y+textDims.Dy(),
color.RGBA{0xFF, 0xFF, 0, 0x77})
// blue rect, red text // supposedly bottom right
ebitenutil.DrawRect(screen,
float64(screen.Bounds().Min.X+(screen.Bounds().Dx()-textDims.Dx())),
float64(screen.Bounds().Min.Y+(screen.Bounds().Dy()-textDims.Dy())),
float64(textDims.Dx()), float64(textDims.Dy()), color.RGBA{0, 0x22, 0xFF, 0x77})
text.Draw(screen, g.Msg, fontFace,
screen.Bounds().Min.X+(screen.Bounds().Dx()-textDims.Dx()),
screen.Bounds().Min.Y+(screen.Bounds().Dy()),
color.RGBA{0xFF, 0x22, 0, 0x77})
}
func (*Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return 256, 128
} Each time the text is rendered with what (I think) should be its bounding box. It seems to be always rendered too far right. Or the BoundString Rectangle is too far left ? Thank you for this great module. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
text.Draw(screen, g.Msg, fontFace,
screen.Bounds().Min.X+(screen.Bounds().Dx()-textDims.Dx())/2,
screen.Bounds().Min.Y+(screen.Bounds().Dy()+textDims.Dy())/2,
color.White) should be text.Draw(screen, g.Msg, fontFace,
screen.Bounds().Min.X+(screen.Bounds().Dx()-textDims.Dx())/2-textDims.Min.X,
screen.Bounds().Min.Y+(screen.Bounds().Dy()-textDims.Dy())/2-textDims.Min.Y,
color.White) |
Beta Was this translation helpful? Give feedback.
textDims
's origin position is the dot position. And,text.Draw
's position is also the dot position. Then, I guessshould be