op/clip: don't panic when Path is used without an initial MoveTo

A Path initialized with Begin should be ready to use with its pen at (0,
0). Make it so.

Updates gio#311

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
This commit is contained in:
Pierre Curto
2021-11-25 09:03:30 +01:00
committed by Elias Naur
parent 74490b4dfc
commit 227c5a132b
2 changed files with 38 additions and 7 deletions
+5 -3
View File
@@ -124,10 +124,12 @@ func (p *Path) Pos() f32.Point { return p.pen }
// Begin the path, storing the path data and final Op into ops.
func (p *Path) Begin(o *op.Ops) {
*p = Path{
ops: &o.Internal,
macro: op.Record(o),
contour: 1,
}
p.hash.SetSeed(pathSeed)
p.ops = &o.Internal
p.macro = op.Record(o)
// Write the TypeAux opcode
data := ops.Write(p.ops, ops.TypeAuxLen)
data[0] = byte(ops.TypeAux)
}
+33 -4
View File
@@ -1,12 +1,16 @@
// SPDX-License-Identifier: Unlicense OR MIT
package clip
package clip_test
import (
"image/color"
"testing"
"gioui.org/f32"
"gioui.org/gpu/headless"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
)
func TestOpenPathOutlinePanic(t *testing.T) {
@@ -15,10 +19,27 @@ func TestOpenPathOutlinePanic(t *testing.T) {
t.Error("Outline of an open path didn't panic")
}
}()
var p Path
var p clip.Path
p.Begin(new(op.Ops))
p.Line(f32.Pt(10, 10))
Outline{Path: p.End()}.Op()
clip.Outline{Path: p.End()}.Op()
}
func TestPathBegin(t *testing.T) {
ops := new(op.Ops)
var p clip.Path
p.Begin(ops)
p.LineTo(f32.Point{10, 10})
p.Close()
stack := clip.Outline{Path: p.End()}.Op().Push(ops)
paint.Fill(ops, color.NRGBA{A: 255})
stack.Pop()
w := newWindow(t, 100, 100)
if w == nil {
return
}
// The following should not panic.
_ = w.Frame(ops)
}
func TestTransformChecks(t *testing.T) {
@@ -28,7 +49,15 @@ func TestTransformChecks(t *testing.T) {
}
}()
var ops op.Ops
st := Op{}.Push(&ops)
st := clip.Op{}.Push(&ops)
op.Record(&ops)
st.Pop()
}
func newWindow(t testing.TB, width, height int) *headless.Window {
w, err := headless.NewWindow(width, height)
if err != nil {
t.Skipf("failed to create headless window, skipping: %v", err)
}
return w
}