Remove usage of runtime/debug

This commit is contained in:
Trevor Slocum 2024-07-22 02:31:50 -07:00
parent 5a26cb4e80
commit 1d8a55632d
3 changed files with 19 additions and 60 deletions

28
game.go
View file

@ -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)
}

View file

@ -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
}

View file

@ -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
}