Skip to content

Commit

Permalink
flattener: Remove Close() from interface
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jentfoo committed Jan 23, 2025
1 parent 827d20b commit 4fe5e3a
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 56 deletions.
5 changes: 0 additions & 5 deletions chartdraw/drawing/dasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 0 additions & 7 deletions chartdraw/drawing/demux_flattener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 6 additions & 15 deletions chartdraw/drawing/flattener.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ 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
for _, cmp := range path.Components {
switch cmp {
case MoveToComponent:
x, y = path.Points[i], path.Points[i+1]
startX, startY = x, y
if i != 0 {
flattener.End()
}
Expand All @@ -52,23 +53,21 @@ 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()
}

// 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)
}

Expand All @@ -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
Expand Down
17 changes: 4 additions & 13 deletions chartdraw/drawing/free_type_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
8 changes: 4 additions & 4 deletions chartdraw/drawing/raster_graphic_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
Expand All @@ -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
Expand Down
7 changes: 0 additions & 7 deletions chartdraw/drawing/stroker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 0 additions & 5 deletions chartdraw/drawing/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 4fe5e3a

Please sign in to comment.