From 4fa829a373f7302065dd651a7baa77bfc0e8b7a1 Mon Sep 17 00:00:00 2001 From: Laurent Le Goff Date: Thu, 23 Apr 2015 16:18:21 +0200 Subject: [PATCH] replace LineEndMarker by End() Method --- dasher.go | 11 +++++----- demux_converter.go | 30 -------------------------- draw2dgl/gc.go | 2 +- image.go | 2 +- line.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++ path_adder.go | 3 +++ path_converter.go | 6 +++--- stroker.go | 40 ++++++++++++++++++---------------- transform.go | 4 ++++ vertex2d.go | 23 -------------------- 10 files changed, 93 insertions(+), 82 deletions(-) delete mode 100644 demux_converter.go create mode 100644 line.go delete mode 100644 vertex2d.go diff --git a/dasher.go b/dasher.go index ba2686b..d1c49af 100644 --- a/dasher.go +++ b/dasher.go @@ -21,11 +21,12 @@ func NewDashConverter(dash []float64, dashOffset float64, converter LineBuilder) return &dasher } +func (dasher *DashVertexConverter) End() { + dasher.next.End() +} + func (dasher *DashVertexConverter) NextCommand(cmd LineMarker) { dasher.command = cmd - if dasher.command == LineEndMarker { - dasher.next.NextCommand(LineEndMarker) - } } func (dasher *DashVertexConverter) LineTo(x, y float64) { @@ -57,7 +58,7 @@ func (dasher *DashVertexConverter) lineTo(x, y float64) { dasher.next.LineTo(lx, ly) } else { // gap - dasher.next.NextCommand(LineEndMarker) + dasher.next.End() dasher.next.MoveTo(lx, ly) } d = d - rest @@ -71,7 +72,7 @@ func (dasher *DashVertexConverter) lineTo(x, y float64) { dasher.next.LineTo(x, y) } else { // gap - dasher.next.NextCommand(LineEndMarker) + dasher.next.End() dasher.next.MoveTo(x, y) } if dasher.distance >= dasher.dash[dasher.currentDash] { diff --git a/demux_converter.go b/demux_converter.go deleted file mode 100644 index 6315842..0000000 --- a/demux_converter.go +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2010 The draw2d Authors. All rights reserved. -// created: 13/12/2010 by Laurent Le Goff - -package draw2d - -type DemuxConverter struct { - converters []LineBuilder -} - -func NewDemuxConverter(converters ...LineBuilder) *DemuxConverter { - return &DemuxConverter{converters} -} - -func (dc *DemuxConverter) NextCommand(cmd LineMarker) { - for _, converter := range dc.converters { - converter.NextCommand(cmd) - } -} - -func (dc *DemuxConverter) MoveTo(x, y float64) { - for _, converter := range dc.converters { - converter.MoveTo(x, y) - } -} - -func (dc *DemuxConverter) LineTo(x, y float64) { - for _, converter := range dc.converters { - converter.LineTo(x, y) - } -} diff --git a/draw2dgl/gc.go b/draw2dgl/gc.go index 2833dcc..b0a0a07 100644 --- a/draw2dgl/gc.go +++ b/draw2dgl/gc.go @@ -223,7 +223,7 @@ func (gc *GraphicContext) FillStroke(paths ...*draw2d.PathStorage) { stroker := draw2d.NewLineStroker(gc.Current.Cap, gc.Current.Join, draw2d.NewVertexMatrixTransform(gc.Current.Tr, draw2d.NewVertexAdder(gc.strokeRasterizer))) stroker.HalfLineWidth = gc.Current.LineWidth / 2 - demux := draw2d.NewDemuxConverter(filler, stroker) + demux := draw2d.NewLineBuilders(filler, stroker) paths = append(paths, gc.Current.Path) pathConverter := draw2d.NewPathConverter(demux) pathConverter.ApproximationScale = gc.Current.Tr.GetScale() // From agg code diff --git a/image.go b/image.go index 9f2ee20..952dc2c 100644 --- a/image.go +++ b/image.go @@ -317,7 +317,7 @@ func (gc *ImageGraphicContext) FillStroke(paths ...*PathStorage) { stroker := NewLineStroker(gc.Current.Cap, gc.Current.Join, NewVertexMatrixTransform(gc.Current.Tr, NewVertexAdder(gc.strokeRasterizer))) stroker.HalfLineWidth = gc.Current.LineWidth / 2 - demux := NewDemuxConverter(filler, stroker) + demux := NewLineBuilders(filler, stroker) paths = append(paths, gc.Current.Path) pathConverter := NewPathConverter(demux) pathConverter.ApproximationScale = gc.Current.Tr.GetScale() diff --git a/line.go b/line.go new file mode 100644 index 0000000..85a2142 --- /dev/null +++ b/line.go @@ -0,0 +1,54 @@ +// Copyright 2010 The draw2d Authors. All rights reserved. +// created: 21/11/2010 by Laurent Le Goff + +package draw2d + +type LineMarker byte + +const ( + LineNoneMarker LineMarker = iota + // Mark the current point of the line as a join to it can draw some specific join Bevel, Miter, Rount + LineJoinMarker + // Mark the current point of the line as closed so it draw a line from the current + // position to the point specified by the last start marker. + LineCloseMarker +) + +type LineBuilder interface { + NextCommand(cmd LineMarker) + MoveTo(x, y float64) + LineTo(x, y float64) + End() +} + +type LineBuilders struct { + builders []LineBuilder +} + +func NewLineBuilders(builders ...LineBuilder) *LineBuilders { + return &LineBuilders{builders} +} + +func (dc *LineBuilders) NextCommand(cmd LineMarker) { + for _, converter := range dc.builders { + converter.NextCommand(cmd) + } +} + +func (dc *LineBuilders) MoveTo(x, y float64) { + for _, converter := range dc.builders { + converter.MoveTo(x, y) + } +} + +func (dc *LineBuilders) LineTo(x, y float64) { + for _, converter := range dc.builders { + converter.LineTo(x, y) + } +} + +func (dc *LineBuilders) End() { + for _, converter := range dc.builders { + converter.End() + } +} diff --git a/path_adder.go b/path_adder.go index aa85b9d..19bc48f 100644 --- a/path_adder.go +++ b/path_adder.go @@ -27,6 +27,9 @@ func (vertexAdder *VertexAdder) LineTo(x, y float64) { vertexAdder.adder.Add1(raster.Point{raster.Fix32(x * 256), raster.Fix32(y * 256)}) } +func (vertexAdder *VertexAdder) End() { +} + type PathAdder struct { adder raster.Adder firstPoint raster.Point diff --git a/path_converter.go b/path_converter.go index 1c88695..3b22c1c 100644 --- a/path_converter.go +++ b/path_converter.go @@ -27,7 +27,7 @@ func (c *PathConverter) Convert(paths ...*PathStorage) { c.x, c.y = path.vertices[i], path.vertices[i+1] c.startX, c.startY = c.x, c.y if i != 0 { - c.converter.NextCommand(LineEndMarker) + c.converter.End() } c.converter.MoveTo(c.x, c.y) i += 2 @@ -67,7 +67,7 @@ func (c *PathConverter) Convert(paths ...*PathStorage) { c.converter.LineTo(c.startX, c.startY) } } - c.converter.NextCommand(LineEndMarker) + c.converter.End() } } @@ -78,7 +78,7 @@ func (c *PathConverter) convertCommand(cmd PathCmd, vertices ...float64) int { func (c *PathConverter) MoveTo(x, y float64) *PathConverter { c.x, c.y = x, y c.startX, c.startY = c.x, c.y - c.converter.NextCommand(LineEndMarker) + c.converter.End() c.converter.MoveTo(c.x, c.y) return c } diff --git a/stroker.go b/stroker.go index ef693a8..1871349 100644 --- a/stroker.go +++ b/stroker.go @@ -44,28 +44,30 @@ func NewLineStroker(c Cap, j Join, converter LineBuilder) *LineStroker { func (l *LineStroker) NextCommand(command LineMarker) { l.command = command - if command == LineEndMarker { - if len(l.vertices) > 1 { - l.Next.MoveTo(l.vertices[0], l.vertices[1]) - for i, j := 2, 3; j < len(l.vertices); i, j = i+2, j+2 { - l.Next.LineTo(l.vertices[i], l.vertices[j]) - l.Next.NextCommand(LineNoneMarker) - } - } - for i, j := len(l.rewind)-2, len(l.rewind)-1; j > 0; i, j = i-2, j-2 { +} + +func (l *LineStroker) End() { + if len(l.vertices) > 1 { + l.Next.MoveTo(l.vertices[0], l.vertices[1]) + for i, j := 2, 3; j < len(l.vertices); i, j = i+2, j+2 { + l.Next.LineTo(l.vertices[i], l.vertices[j]) l.Next.NextCommand(LineNoneMarker) - l.Next.LineTo(l.rewind[i], l.rewind[j]) } - if len(l.vertices) > 1 { - l.Next.NextCommand(LineNoneMarker) - l.Next.LineTo(l.vertices[0], l.vertices[1]) - } - l.Next.NextCommand(LineEndMarker) - // reinit vertices - l.vertices = l.vertices[0:0] - l.rewind = l.rewind[0:0] - l.x, l.y, l.nx, l.ny = 0, 0, 0, 0 } + for i, j := len(l.rewind)-2, len(l.rewind)-1; j > 0; i, j = i-2, j-2 { + l.Next.NextCommand(LineNoneMarker) + l.Next.LineTo(l.rewind[i], l.rewind[j]) + } + if len(l.vertices) > 1 { + l.Next.NextCommand(LineNoneMarker) + l.Next.LineTo(l.vertices[0], l.vertices[1]) + } + l.Next.End() + // reinit vertices + l.vertices = l.vertices[0:0] + l.rewind = l.rewind[0:0] + l.x, l.y, l.nx, l.ny = 0, 0, 0, 0 + } func (l *LineStroker) MoveTo(x, y float64) { diff --git a/transform.go b/transform.go index ac34224..21e3ed5 100644 --- a/transform.go +++ b/transform.go @@ -278,6 +278,10 @@ func (vmt *VertexMatrixTransform) LineTo(x, y float64) { vmt.Next.LineTo(u, v) } +func (vmt *VertexMatrixTransform) End() { + vmt.Next.End() +} + // this adder apply a Matrix transformation to points type MatrixTransformAdder struct { tr MatrixTransform diff --git a/vertex2d.go b/vertex2d.go deleted file mode 100644 index f07f5e8..0000000 --- a/vertex2d.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2010 The draw2d Authors. All rights reserved. -// created: 21/11/2010 by Laurent Le Goff - -package draw2d - -type LineMarker byte - -const ( - LineNoneMarker LineMarker = iota - // Mark the current point of the line as a join to it can draw some specific join Bevel, Miter, Rount - LineJoinMarker - // Mark the current point of the line as closed so it draw a line from the current - // position to the point specified by the last start marker. - LineCloseMarker - // Mark the current point of the line as finished. This ending maker allow caps to be drawn - LineEndMarker -) - -type LineBuilder interface { - NextCommand(cmd LineMarker) - MoveTo(x, y float64) - LineTo(x, y float64) -}