mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-26 10:42:42 +01:00
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
This commit is contained in:
parent
0918877344
commit
41906115f4
@ -22,7 +22,9 @@ import (
|
|||||||
_ "image/png"
|
_ "image/png"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"golang.org/x/image/font"
|
||||||
"golang.org/x/image/font/inconsolata"
|
"golang.org/x/image/font/inconsolata"
|
||||||
|
"golang.org/x/image/math/fixed"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
"github.com/hajimehoshi/ebiten/v2/examples/resources/images/blend"
|
"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.
|
// drawCenteredText is a util function for drawing blend mode description.
|
||||||
func drawCenteredText(screen *ebiten.Image, cx, cy float64, s string) {
|
func drawCenteredText(screen *ebiten.Image, cx, cy float64, s string) {
|
||||||
bounds := text.BoundString(inconsolata.Bold8x16, s)
|
advance := font.MeasureString(inconsolata.Bold8x16, s)
|
||||||
x, y := int(cx)-bounds.Min.X-bounds.Dx()/2, int(cy)-bounds.Min.Y-bounds.Dy()/2
|
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)
|
text.Draw(screen, s, inconsolata.Bold8x16, x, y, color.Black)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user