From 6bfd980044cffbd80d3d727af2e5442978b219a5 Mon Sep 17 00:00:00 2001 From: Pierre Curto Date: Sat, 13 Nov 2021 17:58:23 +0100 Subject: [PATCH] op/clip: don't panic on zero-sized Ellipse When the rectangle used in an Ellipse has no width or height, the path would panic with "path not closed". Use an empty rectangle path in that case instead. Signed-off-by: Pierre Curto --- op/clip/shapes.go | 6 +++++- op/clip/shapes_test.go | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 op/clip/shapes_test.go diff --git a/op/clip/shapes.go b/op/clip/shapes.go index e8acb6a5..46b9e88e 100644 --- a/op/clip/shapes.go +++ b/op/clip/shapes.go @@ -158,10 +158,14 @@ func (e Ellipse) Push(ops *op.Ops) Stack { // path constructs a path for the ellipse. func (e Ellipse) path(o *op.Ops) PathSpec { + bounds := f32.Rectangle(e) + if bounds.Dx() == 0 || bounds.Dy() == 0 { + return PathSpec{shape: ops.Rect} + } + var p Path p.Begin(o) - bounds := f32.Rectangle(e) center := bounds.Max.Add(bounds.Min).Mul(.5) diam := bounds.Dx() r := diam * .5 diff --git a/op/clip/shapes_test.go b/op/clip/shapes_test.go new file mode 100644 index 00000000..9e502ba6 --- /dev/null +++ b/op/clip/shapes_test.go @@ -0,0 +1,18 @@ +package clip_test + +import ( + "image/color" + "testing" + + "gioui.org/f32" + "gioui.org/op" + "gioui.org/op/clip" + "gioui.org/op/paint" +) + +func TestZeroEllipse(t *testing.T) { + p := f32.Pt(1.0, 2.0) + e := clip.Ellipse{Min: p, Max: p} + ops := new(op.Ops) + paint.FillShape(ops, color.NRGBA{R: 255, A: 255}, e.Op(ops)) +}