From 41906115f4650d8aad0c27d010839c7506781d31 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 2 Jun 2023 01:58:02 +0900 Subject: [PATCH] examples/blend: use the face's metrics to calculate the position For centering, using bounds is slightly inaccurate as the bounds don't consider left/right-side bearings. Also, using bounds for heights is slightly inaccurate as baselines for texts on the same line would not be the same. Updates #2143 --- examples/blend/main.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/blend/main.go b/examples/blend/main.go index bf5eb1113..e9ea741ab 100644 --- a/examples/blend/main.go +++ b/examples/blend/main.go @@ -22,7 +22,9 @@ import ( _ "image/png" "log" + "golang.org/x/image/font" "golang.org/x/image/font/inconsolata" + "golang.org/x/image/math/fixed" "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/examples/resources/images/blend" @@ -174,10 +176,16 @@ func maxSide(a, b *ebiten.Image) int { ) } +func fixed26_6ToFloat64(x fixed.Int26_6) float64 { + return float64(x>>6) + float64(x&((1<<6)-1))/float64(1<<6) +} + // drawCenteredText is a util function for drawing blend mode description. func drawCenteredText(screen *ebiten.Image, cx, cy float64, s string) { - bounds := text.BoundString(inconsolata.Bold8x16, s) - x, y := int(cx)-bounds.Min.X-bounds.Dx()/2, int(cy)-bounds.Min.Y-bounds.Dy()/2 + advance := font.MeasureString(inconsolata.Bold8x16, s) + m := inconsolata.Bold8x16.Metrics() + height := m.Ascent + m.Descent + x, y := int(cx-fixed26_6ToFloat64(advance)/2), int(cy-fixed26_6ToFloat64(height)/2+fixed26_6ToFloat64(m.Ascent)) text.Draw(screen, s, inconsolata.Bold8x16, x, y, color.Black) }