op/paint: remove support for PaintOp.Rect

PaintOp.Rect is the wrong abstraction; it implies a clip operation
better handled by package clip, and not all paints need it (colors).
Furthermore, it's awkward to specify a PaintOp that fills up the
current clip area, regardless of its size.

Redefine PathOp to mean "fill current clip area".

API change. Replace uses of PaintOp.Rect with a TransformOp applied
before the PaintOp.

Leave a TODO for the PathOp infinity area.

Fixes gio#167

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-11-05 16:09:55 +01:00
parent afb52194d1
commit 94d242d18c
19 changed files with 93 additions and 106 deletions
+31 -11
View File
@@ -35,10 +35,10 @@ func TestTransformMacro(t *testing.T) {
stack := op.Push(o)
op.Offset(f32.Pt(0, 10)).Add(o)
// Actually create the text clip-path
// Apply the clip-path.
c.Add(o)
paint.PaintOp{Rect: f32.Rect(0, 0, 10, 10)}.Add(o)
paint.PaintOp{}.Add(o)
stack.Pop()
c2 := m2.Stop()
@@ -124,7 +124,7 @@ func constSqCirc() op.CallOp {
func drawChild(ops *op.Ops, text op.CallOp) op.CallOp {
r1 := op.Record(ops)
text.Add(ops)
paint.PaintOp{Rect: f32.Rect(0, 0, 10, 10)}.Add(ops)
paint.PaintOp{}.Add(ops)
return r1.Stop()
}
@@ -159,7 +159,7 @@ func TestBuildOffscreen(t *testing.T) {
s := op.Push(o)
op.Offset(f32.Pt(0, off)).Add(o)
txt.Add(o)
paint.PaintOp{Rect: f32.Rect(0, 0, 40, 40)}.Add(o)
paint.PaintOp{}.Add(o)
s.Pop()
}
@@ -184,7 +184,8 @@ func TestBuildOffscreen(t *testing.T) {
func TestNegativeOverlaps(t *testing.T) {
run(t, func(ops *op.Ops) {
clip.RRect{Rect: f32.Rect(50, 50, 100, 100)}.Add(ops)
paint.PaintOp{Rect: f32.Rect(0, 120, 100, 122)}.Add(ops)
clip.Rect(image.Rect(0, 120, 100, 122)).Add(ops)
paint.PaintOp{}.Add(ops)
}, func(r result) {
r.expect(60, 60, colornames.White)
r.expect(60, 110, colornames.White)
@@ -207,6 +208,8 @@ var gradients = []Gradient{
}
func TestLinearGradient(t *testing.T) {
t.Skip("linear gradients don't support transformations")
const gradienth = 8
// 0.5 offset from ends to ensure that the center of the pixel
// aligns with gradient from and to colors.
@@ -214,7 +217,7 @@ func TestLinearGradient(t *testing.T) {
samples := []int{0, 12, 32, 64, 96, 115, 127}
run(t, func(ops *op.Ops) {
gr := pixelAligned
gr := f32.Rect(0, 0, 128, gradienth)
for _, g := range gradients {
paint.LinearGradientOp{
Stop1: f32.Pt(gr.Min.X, gr.Min.Y),
@@ -222,7 +225,12 @@ func TestLinearGradient(t *testing.T) {
Stop2: f32.Pt(gr.Max.X, gr.Min.Y),
Color2: g.To,
}.Add(ops)
paint.PaintOp{Rect: gr}.Add(ops)
st := op.Push(ops)
clip.RRect{Rect: gr}.Add(ops)
op.Affine(f32.Affine2D{}.Offset(pixelAligned.Min)).Add(ops)
scale(pixelAligned.Dx()/128, 1).Add(ops)
paint.PaintOp{}.Add(ops)
st.Pop()
gr = gr.Add(f32.Pt(0, gradienth))
}
}, func(r result) {
@@ -247,7 +255,10 @@ func TestLinearGradientAngled(t *testing.T) {
Stop2: f32.Pt(0, 0),
Color2: colornames.Red,
}.Add(ops)
paint.PaintOp{Rect: f32.Rect(0, 0, 64, 64)}.Add(ops)
st := op.Push(ops)
clip.Rect(image.Rect(0, 0, 64, 64)).Add(ops)
paint.PaintOp{}.Add(ops)
st.Pop()
paint.LinearGradientOp{
Stop1: f32.Pt(64, 64),
@@ -255,7 +266,10 @@ func TestLinearGradientAngled(t *testing.T) {
Stop2: f32.Pt(128, 0),
Color2: colornames.Green,
}.Add(ops)
paint.PaintOp{Rect: f32.Rect(64, 0, 128, 64)}.Add(ops)
st = op.Push(ops)
clip.Rect(image.Rect(64, 0, 128, 64)).Add(ops)
paint.PaintOp{}.Add(ops)
st.Pop()
paint.LinearGradientOp{
Stop1: f32.Pt(64, 64),
@@ -263,7 +277,10 @@ func TestLinearGradientAngled(t *testing.T) {
Stop2: f32.Pt(128, 128),
Color2: colornames.Blue,
}.Add(ops)
paint.PaintOp{Rect: f32.Rect(64, 64, 128, 128)}.Add(ops)
st = op.Push(ops)
clip.Rect(image.Rect(64, 64, 128, 128)).Add(ops)
paint.PaintOp{}.Add(ops)
st.Pop()
paint.LinearGradientOp{
Stop1: f32.Pt(64, 64),
@@ -271,7 +288,10 @@ func TestLinearGradientAngled(t *testing.T) {
Stop2: f32.Pt(0, 128),
Color2: colornames.Magenta,
}.Add(ops)
paint.PaintOp{Rect: f32.Rect(0, 64, 64, 128)}.Add(ops)
st = op.Push(ops)
clip.Rect(image.Rect(0, 64, 64, 128)).Add(ops)
paint.PaintOp{}.Add(ops)
st.Pop()
}, func(r result) {})
}