New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Text centering issues with bitmapfont #69
Comments
Isn't |
The issue persists even without a newline. I would assume that this is a relevant piece of code: Lines 267 to 341 in 507f5ce
The line width is calculated as 378 for both bitmap font and the font used in the example, but they're still aligned differently: Ultimately, both fonts/texts end up here, with text.Draw(screen, strings.Join(line, " "), t.Face, lx, ly, t.Color) It's weird that the results are quite different. So, with |
Here is a code to play around this without ebitenui: package main
import (
"fmt"
"image/color"
"log"
"github.com/hajimehoshi/bitmapfont/v2"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text"
"github.com/hajimehoshi/ebiten/v2/vector"
)
type Game struct {
}
func (g *Game) Update() error {
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
ff := bitmapfont.Face
white := color.RGBA{R: 255, G: 255, B: 255, A: 255}
green := color.RGBA{G: 255, A: 255}
offsets := []int{10, 30, 50, 70}
dy := 20
for _, dx := range offsets {
text.Draw(screen, fmt.Sprintf("Hello, offset_x=%d", dx), ff, dx, dy, white)
dy += 20
vector.StrokeLine(screen, float32(dx), 0, float32(dx), float32(200), 1, green, true)
}
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return 640, 480
}
func main() {
ebiten.SetWindowSize(640, 480)
ebiten.SetWindowTitle("bitmapfont")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
} |
This seems correct as the green line should indicate a dot position. What about using |
Hmm, interestingly, |
The I think that either bitmapfont should adjust the P.S. - the vertical alignment seems to be off too. |
The more I look at this the more I think it has something to do with the dot positioning in the bitmap font lib. For all the TTF fonts I've tested it is aligned as expected. It seems like the bitmap font is expecting the draw X position to be the end of the rune while the TTFs are expecting the draw X position to be the start of the rune. I don't think this is a problem with ebitenui. As far as a workaround in the meantime you can adjust the TextPadding to be biased on the left side:
|
OK so this seems an issue in the bitmap font: https://pkg.go.dev/golang.org/x/image/font#Face I'll fix this by updating the major version. |
I expected that the center of the dot is (0, 0) from the comment
but actually this was not with some TTF/OTF fonts (the minimum X was a positive value). This is quite confusing. |
Fonts are overly complicated. I barely understand how they work. I'm very impressed with your bitmap library but glad I don't have to work on it lol. |
@quasilyte Please try github.com/hajimehoshi/bitmapfont/v3 (v3.0.0-alpha). Thanks, |
I can confirm that v3 solves all of my problems. I also tried the scaled bitmap version (Hajime would recognize this code, ahaha). It works correctly as well. The multi-line text looks legit too. @mcarpenter622 @hajimehoshi A big thanks for the fix and investigation. :) @mcarpenter622 I'm using the bitmapfont because of its amazing multi-language support and great compatibility with pixel-art games. :) It's really neat. I think it will be useful in the upcoming Ebitengine game jam too! |
I've created an issue about the comments: golang/go#60501 |
Thanks @hajimehoshi and @quasilyte for working through this together! I'm going to go ahead and close this issue. |
Basically, replace
loadFont()
with a direct use of bitmapfont.Face.The result is off-center text. This happens with any kind of text with this font.
The font is monospaced.
It could be a bitmapfont issue or ebitenui font handling issue.
It's more likely to be a bitmapfont issue, but it's hard to debug it outside of the context.
For instance, maybe it handles glyph advance incorrectly, providing a spacing for 1 extra trailing character?
Or maybe ebitenui doesn't handle this corner case correctly with different
font.Face
implementations?The underlying
font.Face
implementation can be found here: https://github.com/hajimehoshi/bitmapfont/blob/main/internal/bitmap/bitmap.goI'll link this issue to the bitmapfont repository issue as well.
The text was updated successfully, but these errors were encountered: