From ce116abdedab45e77c42317ea5d5fb6516c043df Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Thu, 10 Feb 2022 18:59:47 +0100 Subject: [PATCH] op/clip: specify path for stroked Rects Fixes: https://todo.sr.ht/~eliasnaur/gio/358 Signed-off-by: Elias Naur --- gpu/internal/rendertest/clip_test.go | 14 +++++++++ .../rendertest/refs/TestStrokedRect.png | Bin 0 -> 564 bytes op/clip/clip.go | 28 ++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 gpu/internal/rendertest/refs/TestStrokedRect.png diff --git a/gpu/internal/rendertest/clip_test.go b/gpu/internal/rendertest/clip_test.go index 3a147312..6a5ad9ae 100644 --- a/gpu/internal/rendertest/clip_test.go +++ b/gpu/internal/rendertest/clip_test.go @@ -277,3 +277,17 @@ func TestPathInterleave(t *testing.T) { paint.ColorOp{}.Add(ops) }) } + +func TestStrokedRect(t *testing.T) { + run(t, func(o *op.Ops) { + r := clip.Rect{Min: image.Pt(50, 50), Max: image.Pt(100, 100)} + paint.FillShape(o, + color.NRGBA{R: 0xff, A: 0xFF}, + clip.Stroke{ + Path: r.Path(), + Width: 5, + }.Op(), + ) + }, func(r result) { + }) +} diff --git a/gpu/internal/rendertest/refs/TestStrokedRect.png b/gpu/internal/rendertest/refs/TestStrokedRect.png new file mode 100644 index 0000000000000000000000000000000000000000..77b74a46ed85508d5fde8049910bde7bdf58fafb GIT binary patch literal 564 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7xzrVEpar;uumf=gr-NzK0DY+&|W9 zi7_0UsCuJKCpe-lH-U2dDE}nl+*1g~EV0V3ek>5u}Cu0T+<_7u_yI0C3 ztXAFWZ=`=R_Nz|yj7jfR@`FEJeNp*$SI$58NylGBmVKM~WTh+1UU|mP4YTK;p7l(O z|CMdS?N^NJR-C+5#qS_p`d{eBeAUXe^AdYCEyHZ9cQBlrUGEXM>UqTD5Q#>2hB=G} zBu`A`ftH!`}(Yk+&K@b>e(;2u`#4G6p%xFmu6u2|Gy|s?xSh)t);-!z~JfX K=d#Wzp$P!EI=_to literal 0 HcmV?d00001 diff --git a/op/clip/clip.go b/op/clip/clip.go index c94e1a0b..a8510794 100644 --- a/op/clip/clip.go +++ b/op/clip/clip.go @@ -48,6 +48,20 @@ func (p Op) Push(o *op.Ops) Stack { 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") + } + 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 { data := ops.Write(&o.Internal, ops.TypePathLen) @@ -345,3 +359,17 @@ func (o Outline) Op() Op { outline: true, } } + +// frect converts a rectangle to a f32.Rectangle. +func frect(r image.Rectangle) f32.Rectangle { + return f32.Rectangle{ + Min: fpt(r.Min), Max: fpt(r.Max), + } +} + +// fpt converts an point to a f32.Point. +func fpt(p image.Point) f32.Point { + return f32.Point{ + X: float32(p.X), Y: float32(p.Y), + } +}