From 569f4724754a54e1a99ee010cbfd4a45bb62826e Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 11 Jun 2023 04:11:33 +0900 Subject: [PATCH] examples: replace BoundString with MeasureString and Metrics --- examples/2048/2048/tile.go | 9 ++++----- examples/blocks/blocks/font.go | 4 ++-- examples/ui/main.go | 25 +++++++++++++------------ 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/examples/2048/2048/tile.go b/examples/2048/2048/tile.go index e56c6141a..dce737baf 100644 --- a/examples/2048/2048/tile.go +++ b/examples/2048/2048/tile.go @@ -395,10 +395,9 @@ func (t *Tile) Draw(boardImage *ebiten.Image) { f = mplusNormalFont } - bound, _ := font.BoundString(f, str) - w := (bound.Max.X - bound.Min.X).Ceil() - h := (bound.Max.Y - bound.Min.Y).Ceil() - x = x + (tileSize-w)/2 - y = y + (tileSize-h)/2 + h + w := font.MeasureString(f, str).Floor() + h := (f.Metrics().Ascent + f.Metrics().Descent).Floor() + x += (tileSize - w) / 2 + y += (tileSize-h)/2 + f.Metrics().Ascent.Floor() text.Draw(boardImage, str, f, x, y, tileColor(v)) } diff --git a/examples/blocks/blocks/font.go b/examples/blocks/blocks/font.go index 3ad7f8eb0..3466e6eed 100644 --- a/examples/blocks/blocks/font.go +++ b/examples/blocks/blocks/font.go @@ -61,8 +61,8 @@ func getArcadeFonts(scale int) font.Face { func textWidth(str string) int { maxW := 0 for _, line := range strings.Split(str, "\n") { - b, _ := font.BoundString(getArcadeFonts(1), line) - w := (b.Max.X - b.Min.X).Ceil() + a := font.MeasureString(getArcadeFonts(1), line) + w := a.Floor() if maxW < w { maxW = w } diff --git a/examples/ui/main.go b/examples/ui/main.go index bbb8ad43a..f557bf0a2 100644 --- a/examples/ui/main.go +++ b/examples/ui/main.go @@ -37,9 +37,8 @@ const ( ) var ( - uiImage *ebiten.Image - uiFont font.Face - uiFontMHeight int + uiImage *ebiten.Image + uiFont font.Face ) func init() { @@ -62,8 +61,6 @@ func init() { if err != nil { log.Fatal(err) } - b, _, _ := uiFont.GlyphBounds('M') - uiFontMHeight = (b.Max.Y - b.Min.Y).Ceil() } type imageType int @@ -186,10 +183,11 @@ func (b *Button) Draw(dst *ebiten.Image) { } drawNinePatches(dst, b.Rect, imageSrcRects[t]) - bounds, _ := font.BoundString(uiFont, b.Text) - w := (bounds.Max.X - bounds.Min.X).Ceil() + m := uiFont.Metrics() + w := font.MeasureString(uiFont, b.Text).Floor() + h := (m.Ascent + m.Descent).Floor() x := b.Rect.Min.X + (b.Rect.Dx()-w)/2 - y := b.Rect.Max.Y - (b.Rect.Dy()-uiFontMHeight)/2 + y := b.Rect.Min.Y + (b.Rect.Dy()-h)/2 + m.Ascent.Floor() text.Draw(dst, b.Text, uiFont, x, y, color.Black) } @@ -338,6 +336,8 @@ func (t *TextBox) contentOffset() (int, int) { func (t *TextBox) Draw(dst *ebiten.Image) { drawNinePatches(dst, t.Rect, imageSrcRects[imageTypeTextBox]) + // TODO: Use a sub-image of dst instead of an offscreen contentBuf. + // Using a sub-image is better in terms of performance. if t.contentBuf != nil { vw, vh := t.viewSize() w, h := t.contentBuf.Bounds().Dx(), t.contentBuf.Bounds().Dy() @@ -352,9 +352,10 @@ func (t *TextBox) Draw(dst *ebiten.Image) { } t.contentBuf.Clear() + m := uiFont.Metrics() for i, line := range strings.Split(t.Text, "\n") { x := -t.offsetX + textBoxPaddingLeft - y := -t.offsetY + i*lineHeight + lineHeight - (lineHeight-uiFontMHeight)/2 + y := -t.offsetY + i*lineHeight + (lineHeight-(m.Ascent+m.Descent).Floor())/2 + m.Ascent.Floor() if y < -lineHeight { continue } @@ -388,8 +389,7 @@ type CheckBox struct { } func (c *CheckBox) width() int { - b, _ := font.BoundString(uiFont, c.Text) - w := (b.Max.X - b.Min.X).Ceil() + w := font.MeasureString(uiFont, c.Text).Floor() return checkboxWidth + checkboxPaddingLeft + w } @@ -423,8 +423,9 @@ func (c *CheckBox) Draw(dst *ebiten.Image) { drawNinePatches(dst, r, imageSrcRects[imageTypeCheckBoxMark]) } + m := uiFont.Metrics() x := c.X + checkboxWidth + checkboxPaddingLeft - y := (c.Y + 16) - (16-uiFontMHeight)/2 + y := c.Y + (checkboxHeight-(m.Ascent+m.Descent).Floor())/2 + m.Ascent.Floor() text.Draw(dst, c.Text, uiFont, x, y, color.Black) }