forked from joejulian/gio
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:
+5
-3
@@ -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.
|
// Begin the path, storing the path data and final Op into ops.
|
||||||
func (p *Path) Begin(o *op.Ops) {
|
func (p *Path) Begin(o *op.Ops) {
|
||||||
|
*p = Path{
|
||||||
|
ops: &o.Internal,
|
||||||
|
macro: op.Record(o),
|
||||||
|
contour: 1,
|
||||||
|
}
|
||||||
p.hash.SetSeed(pathSeed)
|
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 := ops.Write(p.ops, ops.TypeAuxLen)
|
||||||
data[0] = byte(ops.TypeAux)
|
data[0] = byte(ops.TypeAux)
|
||||||
}
|
}
|
||||||
|
|||||||
+33
-4
@@ -1,12 +1,16 @@
|
|||||||
// SPDX-License-Identifier: Unlicense OR MIT
|
// SPDX-License-Identifier: Unlicense OR MIT
|
||||||
|
|
||||||
package clip
|
package clip_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"image/color"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gioui.org/f32"
|
"gioui.org/f32"
|
||||||
|
"gioui.org/gpu/headless"
|
||||||
"gioui.org/op"
|
"gioui.org/op"
|
||||||
|
"gioui.org/op/clip"
|
||||||
|
"gioui.org/op/paint"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestOpenPathOutlinePanic(t *testing.T) {
|
func TestOpenPathOutlinePanic(t *testing.T) {
|
||||||
@@ -15,10 +19,27 @@ func TestOpenPathOutlinePanic(t *testing.T) {
|
|||||||
t.Error("Outline of an open path didn't panic")
|
t.Error("Outline of an open path didn't panic")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
var p Path
|
var p clip.Path
|
||||||
p.Begin(new(op.Ops))
|
p.Begin(new(op.Ops))
|
||||||
p.Line(f32.Pt(10, 10))
|
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) {
|
func TestTransformChecks(t *testing.T) {
|
||||||
@@ -28,7 +49,15 @@ func TestTransformChecks(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
var ops op.Ops
|
var ops op.Ops
|
||||||
st := Op{}.Push(&ops)
|
st := clip.Op{}.Push(&ops)
|
||||||
op.Record(&ops)
|
op.Record(&ops)
|
||||||
st.Pop()
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user