From 1d8a55632df0fe176831a16ac8346d4dab43693c Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Mon, 22 Jul 2024 02:31:50 -0700 Subject: [PATCH] Remove usage of runtime/debug --- game.go | 28 +--------------------------- kibodo/keyboard_test.go | 15 +++++++++++++-- messeji/textfield.go | 36 +++++------------------------------- 3 files changed, 19 insertions(+), 60 deletions(-) diff --git a/game.go b/game.go index 8cc4caa..b515dce 100644 --- a/game.go +++ b/game.go @@ -6,8 +6,6 @@ import ( "image/color" "log" "math" - "runtime/debug" - "strings" "sync" "time" @@ -159,30 +157,6 @@ func Focused() Widget { return focusedWidget } -func boundString(f font.Face, s string) (bounds fixed.Rectangle26_6, advance fixed.Int26_6) { - if strings.TrimSpace(s) == "" { - return fixed.Rectangle26_6{}, 0 - } - for i := 0; i < 100; i++ { - bounds, advance = func() (fixed.Rectangle26_6, fixed.Int26_6) { - defer func() { - err := recover() - if err != nil && i == 99 { - debug.PrintStack() - panic("failed to calculate bounds of string '" + s + "'") - } - }() - bounds, advance = font.BoundString(f, s) - return bounds, advance - }() - if !bounds.Empty() { - return bounds, advance - } - time.Sleep(10 * time.Millisecond) - } - return fixed.Rectangle26_6{}, 0 -} - func int26ToRect(r fixed.Rectangle26_6) image.Rectangle { x, y := r.Min.X, r.Min.Y w, h := r.Max.X-r.Min.X, r.Max.Y-r.Min.Y @@ -191,7 +165,7 @@ func int26ToRect(r fixed.Rectangle26_6) image.Rectangle { // BoundString returns the bounds of the provided string. func BoundString(f font.Face, s string) image.Rectangle { - bounds, _ := boundString(f, s) + bounds, _ := font.BoundString(f, s) return int26ToRect(bounds) } diff --git a/kibodo/keyboard_test.go b/kibodo/keyboard_test.go index 65c7320..01020f8 100644 --- a/kibodo/keyboard_test.go +++ b/kibodo/keyboard_test.go @@ -1,12 +1,15 @@ package kibodo import ( + "log" "runtime" "testing" "time" - "code.rocket9labs.com/tslocum/etk" "github.com/hajimehoshi/ebiten/v2" + "github.com/hajimehoshi/ebiten/v2/examples/resources/fonts" + "golang.org/x/image/font/opentype" + "golang.org/x/image/font/sfnt" ) // TODO test presses registered @@ -59,7 +62,7 @@ func BenchmarkKeyboard_Press(b *testing.B) { } func newTestKeyboard() *Keyboard { - k := NewKeyboard(etk.Style.TextFont) + k := NewKeyboard(defaultFont()) k.SetRect(0, 0, 300, 100) return k @@ -84,3 +87,11 @@ func (d *DummyGame) Layout(outsideWidth, outsideHeight int) (screenWidth, screen func NewDummyGame() *DummyGame { return &DummyGame{} } + +func defaultFont() *sfnt.Font { + f, err := opentype.Parse(fonts.MPlus1pRegular_ttf) + if err != nil { + log.Fatal(err) + } + return f +} diff --git a/messeji/textfield.go b/messeji/textfield.go index e339331..e7beecc 100644 --- a/messeji/textfield.go +++ b/messeji/textfield.go @@ -5,10 +5,8 @@ import ( "image" "image/color" "math" - "runtime/debug" "strings" "sync" - "time" "unicode" "github.com/hajimehoshi/ebiten/v2" @@ -783,7 +781,7 @@ func (f *TextField) wrapContent(withScrollBar bool) { if f.singleLine || len(f.buffer) == 0 { buffer := f.prefix + string(bytes.Join(f.buffer, nil)) + f.suffix - bounds, _ := boundString(f.face, buffer) + bounds, _ := font.BoundString(f.face, buffer) f.bufferWrapped = []string{buffer} f.wrapStart = 0 @@ -842,7 +840,7 @@ func (f *TextField) wrapContent(withScrollBar bool) { for end > start { initialEnd = end - bounds, _ := boundString(f.face, line[start:end]) + bounds, _ := font.BoundString(f.face, line[start:end]) boundsWidth := (bounds.Max.X - bounds.Min.X).Floor() if boundsWidth > availableWidth && end > start+1 { delta := (end - start) / 2 @@ -861,7 +859,7 @@ func (f *TextField) wrapContent(withScrollBar bool) { for end < l { initialEnd = end - bounds, _ := boundString(f.face, line[start:end]) + bounds, _ := font.BoundString(f.face, line[start:end]) boundsWidth := (bounds.Max.X - bounds.Min.X).Floor() if boundsWidth > availableWidth && end > start+1 { break @@ -896,7 +894,7 @@ func (f *TextField) wrapContent(withScrollBar bool) { } if boundsWidth == 0 || end != initialEnd { - bounds, _ = boundString(f.face, line[start:end]) + bounds, _ = font.BoundString(f.face, line[start:end]) boundsWidth = (bounds.Max.X - bounds.Min.X).Floor() } @@ -954,7 +952,7 @@ func (f *TextField) drawContent() (overflow bool) { } // Calculate buffer size (width for single-line fields or height for multi-line fields). if f.singleLine { - bounds, _ := boundString(f.face, f.bufferWrapped[firstVisible]) + bounds, _ := font.BoundString(f.face, f.bufferWrapped[firstVisible]) f.bufferSize = (bounds.Max.X - bounds.Min.X).Floor() } else { f.bufferSize = (len(f.bufferWrapped)) * lineHeight @@ -1143,27 +1141,3 @@ func (f *TextField) bufferModified() { func rectIsZero(r image.Rectangle) bool { return r.Dx() == 0 || r.Dy() == 0 } - -func boundString(f font.Face, s string) (bounds fixed.Rectangle26_6, advance fixed.Int26_6) { - if strings.TrimSpace(s) == "" { - return fixed.Rectangle26_6{}, 0 - } - for i := 0; i < 100; i++ { - bounds, advance = func() (fixed.Rectangle26_6, fixed.Int26_6) { - defer func() { - err := recover() - if err != nil && i == 99 { - debug.PrintStack() - panic("failed to calculate bounds of string '" + s + "'") - } - }() - bounds, advance = font.BoundString(f, s) - return bounds, advance - }() - if !bounds.Empty() { - return bounds, advance - } - time.Sleep(10 * time.Millisecond) - } - return fixed.Rectangle26_6{}, 0 -}