From 106f3e1a578e713e3404a991d7f60c744f039566 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 7 Apr 2024 22:14:53 +0900 Subject: [PATCH] text/v2: bug fix: given options were unexpectedly modified Closes #2954 --- text/v2/layout.go | 14 ++++++++------ text/v2/text_test.go | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/text/v2/layout.go b/text/v2/layout.go index f99757f65..efe1d0b5d 100644 --- a/text/v2/layout.go +++ b/text/v2/layout.go @@ -105,16 +105,18 @@ func Draw(dst *ebiten.Image, text string, face Face, options *DrawOptions) { options = &DrawOptions{} } - geoM := options.GeoM + // Copy the options to avoid modifying the original options (#2954). + drawOp := options.DrawImageOptions + geoM := drawOp.GeoM + for _, g := range AppendGlyphs(nil, text, face, &options.LayoutOptions) { if g.Image == nil { continue } - op := &options.DrawImageOptions - op.GeoM.Reset() - op.GeoM.Translate(g.X, g.Y) - op.GeoM.Concat(geoM) - dst.DrawImage(g.Image, op) + drawOp.GeoM.Reset() + drawOp.GeoM.Translate(g.X, g.Y) + drawOp.GeoM.Concat(geoM) + dst.DrawImage(g.Image, &drawOp) } } diff --git a/text/v2/text_test.go b/text/v2/text_test.go index 707f1b1dd..46acf1106 100644 --- a/text/v2/text_test.go +++ b/text/v2/text_test.go @@ -356,3 +356,18 @@ func TestConvertToFloat(t *testing.T) { } } } + +// Issue #2954 +func TestDrawOptionsNotModified(t *testing.T) { + img := ebiten.NewImage(30, 30) + + op := &text.DrawOptions{} + text.Draw(img, "Hello", text.NewGoXFace(bitmapfont.Face), op) + + if got, want := op.GeoM, (ebiten.GeoM{}); got != want { + t.Errorf("got: %v, want: %v", got, want) + } + if got, want := op.ColorScale, (ebiten.ColorScale{}); got != want { + t.Errorf("got: %v, want: %v", got, want) + } +}