op/clip: replace Rect and RoundRect with Rect type

Remembering the order of the corners in the RoundRect is difficult,
which suggest that RoundRect should be a struct with named fields.

Do that, and make Rect the special case where corner radii are all
zero.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-11-18 14:33:28 +01:00
parent 101b65f4e5
commit 7299d1c875
4 changed files with 46 additions and 30 deletions
+29 -16
View File
@@ -284,25 +284,38 @@ func (p *Path) End() Op {
}
}
// Rect returns the clip area of a rectangle.
func Rect(ops *op.Ops, r f32.Rectangle) Op {
ri := image.Rectangle{
Min: image.Point{X: int(r.Min.X), Y: int(r.Min.Y)},
Max: image.Point{X: int(r.Max.X), Y: int(r.Max.Y)},
}
// Optimize pixel-aligned rectangles to just its bounds.
if r == toRectF(ri) {
return Op{bounds: r}
}
return RoundRect(ops, r, 0, 0, 0, 0)
}
// RoundRect returns the clip area of a rectangle with rounded
// corners defined by their radii. The origin is in the upper left
// Rect represents the clip area of a rectangle with rounded
// corners.The origin is in the upper left
// corner.
// Specify a square with corner radii equal to half the square size to
// construct a circular clip area.
func RoundRect(ops *op.Ops, r f32.Rectangle, se, sw, nw, ne float32) Op {
type Rect struct {
Rect f32.Rectangle
// The corner radii.
SE, SW, NW, NE float32
}
// Op returns the Op for the rectangle.
func (rr Rect) Op(ops *op.Ops) Op {
r := rr.Rect
// Optimize for the common pixel aligned rectangle with no
// corner rounding.
if rr.SE == 0 && rr.SW == 0 && rr.NW == 0 && rr.NE == 0 {
ri := image.Rectangle{
Min: image.Point{X: int(r.Min.X), Y: int(r.Min.Y)},
Max: image.Point{X: int(r.Max.X), Y: int(r.Max.Y)},
}
// Optimize pixel-aligned rectangles to just its bounds.
if r == toRectF(ri) {
return Op{bounds: r}
}
}
return roundRect(ops, r, rr.SE, rr.SW, rr.NW, rr.NE)
}
// roundRect returns the clip area of a rectangle with rounded
// corners defined by their radii.
func roundRect(ops *op.Ops, r f32.Rectangle, se, sw, nw, ne float32) Op {
size := r.Size()
// https://pomax.github.io/bezierinfo/#circles_cubic.
w, h := float32(size.X), float32(size.Y)