From 4fe5e3a43a7694e71843b4f7ce1e29f205561ca6 Mon Sep 17 00:00:00 2001 From: Mike Jensen Date: Wed, 22 Jan 2025 20:06:57 -0700 Subject: [PATCH] flattener: Remove `Close()` from interface The prior commit switched Close() to function correctly, but upon reflection it was decided that communicating this state was not likely to be useful. Instead having the flattern handle the `LineTo` logic reduces the overall complexity and how often this must be handled. Overall, this is much simpler. --- chartdraw/drawing/dasher.go | 5 ----- chartdraw/drawing/demux_flattener.go | 7 ------- chartdraw/drawing/flattener.go | 21 ++++++--------------- chartdraw/drawing/free_type_path.go | 17 ++++------------- chartdraw/drawing/raster_graphic_context.go | 8 ++++---- chartdraw/drawing/stroker.go | 7 ------- chartdraw/drawing/transformer.go | 5 ----- 7 files changed, 14 insertions(+), 56 deletions(-) diff --git a/chartdraw/drawing/dasher.go b/chartdraw/drawing/dasher.go index d4649ab..b1f5837 100644 --- a/chartdraw/drawing/dasher.go +++ b/chartdraw/drawing/dasher.go @@ -32,11 +32,6 @@ func (dasher *DashVertexConverter) MoveTo(x, y float64) { dasher.currentDash = 0 } -// Close implements the pathbuilder interface. -func (dasher *DashVertexConverter) Close() { - dasher.next.Close() -} - // End implements the pathbuilder interface. func (dasher *DashVertexConverter) End() { dasher.next.End() diff --git a/chartdraw/drawing/demux_flattener.go b/chartdraw/drawing/demux_flattener.go index 1337c47..2d27d5b 100644 --- a/chartdraw/drawing/demux_flattener.go +++ b/chartdraw/drawing/demux_flattener.go @@ -19,13 +19,6 @@ func (dc DemuxFlattener) LineTo(x, y float64) { } } -// Close implements the path builder interface. -func (dc DemuxFlattener) Close() { - for _, flattener := range dc.Flatteners { - flattener.Close() - } -} - // End implements the path builder interface. func (dc DemuxFlattener) End() { for _, flattener := range dc.Flatteners { diff --git a/chartdraw/drawing/flattener.go b/chartdraw/drawing/flattener.go index b4c5cca..6132a0e 100644 --- a/chartdraw/drawing/flattener.go +++ b/chartdraw/drawing/flattener.go @@ -12,14 +12,14 @@ type Flattener interface { MoveTo(x, y float64) // LineTo Draw a line from the current position to the point (x, y) LineTo(x, y float64) - // Close add the most recent starting point to close the path to create a polygon - Close() // End mark the current line as finished so we can draw caps End() } // Flatten convert curves into straight segments keeping join segments info func Flatten(path *Path, flattener Flattener, scale float64) { + // First Point + var startX, startY float64 // Current Point var x, y float64 var i int @@ -27,6 +27,7 @@ func Flatten(path *Path, flattener Flattener, scale float64) { switch cmp { case MoveToComponent: x, y = path.Points[i], path.Points[i+1] + startX, startY = x, y if i != 0 { flattener.End() } @@ -52,7 +53,9 @@ func Flatten(path *Path, flattener Flattener, scale float64) { flattener.LineTo(x, y) i += 6 case CloseComponent: - flattener.Close() + if x != startX || y != startY { + flattener.LineTo(startX, startY) + } } } flattener.End() @@ -60,15 +63,11 @@ func Flatten(path *Path, flattener Flattener, scale float64) { // SegmentedPath is a path of disparate point sections. type SegmentedPath struct { - startX float64 - startY float64 Points []float64 } // MoveTo implements the path interface. func (p *SegmentedPath) MoveTo(x, y float64) { - p.startX = x - p.startY = y p.Points = append(p.Points, x, y) } @@ -77,14 +76,6 @@ func (p *SegmentedPath) LineTo(x, y float64) { p.Points = append(p.Points, x, y) } -// Close implements the path interface. -func (p *SegmentedPath) Close() { - pointLen := len(p.Points) - if pointLen > 1 && (p.Points[pointLen-2] != p.startX || p.Points[pointLen-1] != p.startY) { - p.Points = append(p.Points, p.startX, p.startY) - } -} - // End implements the path interface. func (p *SegmentedPath) End() { // Nothing to do diff --git a/chartdraw/drawing/free_type_path.go b/chartdraw/drawing/free_type_path.go index 1a2fd2f..006d62a 100644 --- a/chartdraw/drawing/free_type_path.go +++ b/chartdraw/drawing/free_type_path.go @@ -7,27 +7,18 @@ import ( // FtLineBuilder is a builder for freetype raster glyphs. type FtLineBuilder struct { - startX float64 - startY float64 - Adder raster.Adder + Adder raster.Adder } // MoveTo implements the path builder interface. -func (liner *FtLineBuilder) MoveTo(x, y float64) { - liner.startX = x - liner.startY = y +func (liner FtLineBuilder) MoveTo(x, y float64) { liner.Adder.Start(fixed.Point26_6{X: fixed.Int26_6(x * 64), Y: fixed.Int26_6(y * 64)}) } // LineTo implements the path builder interface. -func (liner *FtLineBuilder) LineTo(x, y float64) { +func (liner FtLineBuilder) LineTo(x, y float64) { liner.Adder.Add1(fixed.Point26_6{X: fixed.Int26_6(x * 64), Y: fixed.Int26_6(y * 64)}) } -// Close implements the path builder interface. -func (liner *FtLineBuilder) Close() { - liner.LineTo(liner.startX, liner.startY) -} - // End implements the path builder interface. -func (liner *FtLineBuilder) End() {} +func (liner FtLineBuilder) End() {} diff --git a/chartdraw/drawing/raster_graphic_context.go b/chartdraw/drawing/raster_graphic_context.go index 9b9b4b6..48e4f05 100644 --- a/chartdraw/drawing/raster_graphic_context.go +++ b/chartdraw/drawing/raster_graphic_context.go @@ -226,7 +226,7 @@ func (rgc *RasterGraphicContext) Stroke(paths ...*Path) { rgc.strokeRasterizer.UseNonZeroWinding = true - stroker := NewLineStroker(rgc.current.Cap, Transformer{Tr: rgc.current.Tr, Flattener: &FtLineBuilder{Adder: rgc.strokeRasterizer}}) + stroker := NewLineStroker(rgc.current.Cap, Transformer{Tr: rgc.current.Tr, Flattener: FtLineBuilder{Adder: rgc.strokeRasterizer}}) stroker.HalfLineWidth = rgc.current.LineWidth / 2 var liner Flattener @@ -298,7 +298,7 @@ func (rgc *RasterGraphicContext) Fill(paths ...*Path) { rgc.fillRasterizer.UseNonZeroWinding = rgc.current.FillRule == FillRuleWinding - flattener := Transformer{Tr: rgc.current.Tr, Flattener: &FtLineBuilder{Adder: rgc.fillRasterizer}} + flattener := Transformer{Tr: rgc.current.Tr, Flattener: FtLineBuilder{Adder: rgc.fillRasterizer}} for _, p := range paths { Flatten(p, flattener, rgc.current.Tr.GetScale()) } @@ -323,9 +323,9 @@ func (rgc *RasterGraphicContext) FillStroke(paths ...*Path) { rgc.fillRasterizer.UseNonZeroWinding = rgc.current.FillRule == FillRuleWinding rgc.strokeRasterizer.UseNonZeroWinding = true - flattener := Transformer{Tr: rgc.current.Tr, Flattener: &FtLineBuilder{Adder: rgc.fillRasterizer}} + flattener := Transformer{Tr: rgc.current.Tr, Flattener: FtLineBuilder{Adder: rgc.fillRasterizer}} - stroker := NewLineStroker(rgc.current.Cap, Transformer{Tr: rgc.current.Tr, Flattener: &FtLineBuilder{Adder: rgc.strokeRasterizer}}) + stroker := NewLineStroker(rgc.current.Cap, Transformer{Tr: rgc.current.Tr, Flattener: FtLineBuilder{Adder: rgc.strokeRasterizer}}) stroker.HalfLineWidth = rgc.current.LineWidth / 2 var liner Flattener diff --git a/chartdraw/drawing/stroker.go b/chartdraw/drawing/stroker.go index 8b94a3e..fa1b38b 100644 --- a/chartdraw/drawing/stroker.go +++ b/chartdraw/drawing/stroker.go @@ -44,13 +44,6 @@ func (l *LineStroker) line(x1, y1, x2, y2 float64) { } } -// Close implements the path builder interface. -func (l *LineStroker) Close() { - if len(l.vertices) > 1 && (l.vertices[0] != l.rewind[0] || l.vertices[1] != l.rewind[1]) { - l.appendVertex(l.vertices[0], l.vertices[1], l.rewind[0], l.rewind[1]) - } -} - // End implements the path builder interface. func (l *LineStroker) End() { if len(l.vertices) > 1 { diff --git a/chartdraw/drawing/transformer.go b/chartdraw/drawing/transformer.go index fcd444a..92ba330 100644 --- a/chartdraw/drawing/transformer.go +++ b/chartdraw/drawing/transformer.go @@ -23,11 +23,6 @@ func (t Transformer) LineTo(x, y float64) { t.Flattener.LineTo(u, v) } -// Close implements the path builder interface. -func (t Transformer) Close() { - t.Flattener.Close() -} - // End implements the path builder interface. func (t Transformer) End() { t.Flattener.End()