~whereswaldon/gio-x

ca9a80220515e7ded2556082c56d9923028509ae — Sebastien Binet 11 months ago 2383213
stroke: add handling of large angles to ArcTo

Signed-off-by: Sebastien Binet <binet@cern.ch>
3 files changed, 27 insertions(+), 5 deletions(-)

M stroke/clip_test.go
M stroke/refs/TestStrokedPathArc.png
M stroke/stroke.go
M stroke/clip_test.go => stroke/clip_test.go +7 -2
@@ 333,11 333,15 @@ func TestStrokedPathArc(t *testing.T) {
			Segments: []Segment{
				MoveTo(f32.Pt(0, 65)),
				LineTo(f32.Pt(20, 65)),
				ArcTo(f32.Pt(70, 65), +math.Pi/3),
				ArcTo(f32.Pt(70, 65), +math.Pi/6),
				LineTo(f32.Pt(70, 65)),
				LineTo(f32.Pt(20, 65)),
				ArcTo(f32.Pt(70, 65), -math.Pi/2),
				ArcTo(f32.Pt(70, 65), -math.Pi/6),
				LineTo(f32.Pt(70, 65)),
				LineTo(f32.Pt(70, 115)),
				ArcTo(f32.Pt(70, 65), -7*math.Pi/6),
				LineTo(f32.Pt(70, 65)),
				LineTo(f32.Pt(70, 15)),
			},
		}
		cl := Stroke{


@@ 352,6 356,7 @@ func TestStrokedPathArc(t *testing.T) {
		r.expect(0, 0, transparent)
		r.expect(70, 65, colornames.Red)
		r.expect(35, 65, colornames.Red)
		r.expect(120, 65, colornames.Red)
	})
}


M stroke/refs/TestStrokedPathArc.png => stroke/refs/TestStrokedPathArc.png +0 -0
M stroke/stroke.go => stroke/stroke.go +20 -3
@@ 4,6 4,8 @@
package stroke

import (
	"math"

	"gioui.org/f32"
	"gioui.org/op"
	"gioui.org/op/clip"


@@ 163,9 165,20 @@ func (s Stroke) Op(ops *op.Ops) clip.Op {
			contour = append(contour, stroke.Segment{stroke.Point(pen), stroke.Point(seg.args[0]), stroke.Point(seg.args[1]), stroke.Point(seg.args[2])})
			pen = seg.args[2]
		case segOpArcTo:
			out := stroke.ArcSegment(stroke.Point(pen), stroke.Point(seg.args[0]), seg.args[1].X)
			contour = append(contour, out)
			pen = f32.Point(out.End)
			var (
				start  = stroke.Point(pen)
				center = stroke.Point(seg.args[0])
				angle  = seg.args[1].X
			)
			switch {
			case absF32(angle) > math.Pi:
				contour = stroke.AppendArc(contour, start, center, angle)
				pen = f32.Point(contour[len(contour)-1].End)
			default:
				out := stroke.ArcSegment(start, center, angle)
				contour = append(contour, out)
				pen = f32.Point(out.End)
			}
		}
	}
	if len(contour) > 0 {


@@ 219,3 232,7 @@ func (s Stroke) Op(ops *op.Ops) clip.Op {

	return clip.Outline{Path: outline.End()}.Op()
}

func absF32(x float32) float32 {
	return math.Float32frombits(math.Float32bits(x) &^ (1 << 31))
}