op/clip: automatically close Path in Outlines

Unclosed path segments in Path will be automatically
closed by a line.

Fixes: https://todo.sr.ht/~eliasnaur/gio/320
Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
This commit is contained in:
Pierre Curto
2021-12-19 17:09:54 +01:00
committed by Elias Naur
parent 0117de71d3
commit 11bb86166a
10 changed files with 123 additions and 28 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 408 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 488 B

+54
View File
@@ -357,6 +357,60 @@ func TestImageRGBA(t *testing.T) {
})
}
func TestGapsInPath(t *testing.T) {
ops := new(op.Ops)
var p clip.Path
p.Begin(ops)
// Unclosed square 1
p.MoveTo(f32.Point{X: 10})
p.LineTo(f32.Point{X: 40})
p.LineTo(f32.Point{X: 40, Y: 30})
p.LineTo(f32.Point{X: 10, Y: 30})
// Unclosed square 2
p.MoveTo(f32.Point{X: 50})
p.LineTo(f32.Point{X: 80})
p.LineTo(f32.Point{X: 80, Y: 30})
p.LineTo(f32.Point{X: 50, Y: 30})
spec := p.End()
t.Run("Stroke", func(t *testing.T) {
run(t,
func(ops *op.Ops) {
stack := clip.Stroke{
Path: spec,
Width: 2,
}.Op().Push(ops)
paint.ColorOp{Color: color.NRGBA{R: 255, A: 255}}.Add(ops)
paint.PaintOp{}.Add(ops)
stack.Pop()
},
func(r result) {
r.expect(10, 20, color.RGBA{})
r.expect(50, 20, color.RGBA{})
},
)
})
t.Run("Outline", func(t *testing.T) {
run(t,
func(ops *op.Ops) {
stack := clip.Outline{Path: spec}.Op().Push(ops)
paint.ColorOp{Color: color.NRGBA{R: 255, A: 255}}.Add(ops)
paint.PaintOp{}.Add(ops)
stack.Pop()
},
func(r result) {
r.expect(10, 20, colornames.Red)
r.expect(20, 20, colornames.Red)
r.expect(50, 20, colornames.Red)
r.expect(60, 20, colornames.Red)
},
)
})
}
// lerp calculates linear interpolation with color b and p.
func lerp(a, b f32color.RGBA, p float32) f32color.RGBA {
return f32color.RGBA{
+13 -3
View File
@@ -11,6 +11,7 @@ import (
"image/draw"
"image/png"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"testing"
@@ -143,11 +144,20 @@ func multiRun(t *testing.T, frames ...frameT) {
func verifyRef(t *testing.T, img *image.RGBA, frame int) (ok bool) {
// ensure identical to ref data
path := filepath.Join("refs", t.Name()+".png")
if frame != 0 {
path = filepath.Join("refs", t.Name()+"_"+strconv.Itoa(frame)+".png")
var path string
if frame == 0 {
path = t.Name()
} else {
path = t.Name() + "_" + strconv.Itoa(frame)
}
path = filepath.Join("refs", path+".png")
if *dumpImages {
if err := os.MkdirAll(filepath.Dir(path), 0766); err != nil {
if !os.IsExist(err) {
t.Error(err)
return
}
}
saveImage(t, path, img)
return true
}