Skip to content

Commit

Permalink
text/v2: bug fix: given options were unexpectedly modified
Browse files Browse the repository at this point in the history
Closes #2954
  • Loading branch information
hajimehoshi committed Apr 7, 2024
1 parent 85c0f44 commit 2b9e307
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 8 additions & 6 deletions text/v2/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
15 changes: 15 additions & 0 deletions text/v2/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 2b9e307

Please sign in to comment.