op/clip: don't panic when stroking empty path

Fixes: https://todo.sr.ht/~eliasnaur/gio/367
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2022-02-22 12:04:10 +01:00
parent e9e00994cf
commit 773a7e48a0
2 changed files with 25 additions and 11 deletions
+15 -11
View File
@@ -49,18 +49,22 @@ func (p Op) add(o *op.Ops) {
path := p.path
if !path.hasSegments && p.width > 0 {
if p.path.shape != ops.Rect {
panic("only rects have empty paths")
switch p.path.shape {
case ops.Rect:
b := frect(path.bounds)
var rect Path
rect.Begin(o)
rect.MoveTo(b.Min)
rect.LineTo(f32.Pt(b.Max.X, b.Min.Y))
rect.LineTo(b.Max)
rect.LineTo(f32.Pt(b.Min.X, b.Max.Y))
rect.Close()
path = rect.End()
case ops.Path:
// Nothing to do.
default:
panic("invalid empty path for shape")
}
b := frect(path.bounds)
var rect Path
rect.Begin(o)
rect.MoveTo(b.Min)
rect.LineTo(f32.Pt(b.Max.X, b.Min.Y))
rect.LineTo(b.Max)
rect.LineTo(f32.Pt(b.Min.X, b.Max.Y))
rect.Close()
path = rect.End()
}
bo := binary.LittleEndian
if path.hasSegments {
+10
View File
@@ -62,6 +62,16 @@ func TestTransformChecks(t *testing.T) {
st.Pop()
}
func TestEmptyPath(t *testing.T) {
var ops op.Ops
p := clip.Path{}
p.Begin(&ops)
defer clip.Stroke{
Path: p.End(),
Width: 3,
}.Op().Push(&ops).Pop()
}
func newWindow(t testing.TB, width, height int) *headless.Window {
w, err := headless.NewWindow(width, height)
if err != nil {