text/v2: bug fix: given options were unexpectedly modified

Closes #2954
This commit is contained in:
Hajime Hoshi 2024-04-07 22:14:53 +09:00
parent 85c0f4444b
commit 2b9e307ec2
2 changed files with 23 additions and 6 deletions

View File

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

View File

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