From 41809b913202ced89ab24f320bf08d35be0fca59 Mon Sep 17 00:00:00 2001 From: Laurent Le Goff Date: Thu, 23 Apr 2015 17:43:26 +0200 Subject: [PATCH] remove LineMarker --- dasher.go | 9 +++--- line.go | 23 ++++++-------- path_adder.go | 7 ++--- path_converter.go | 4 +-- stroker.go | 78 +++++++++++++++++------------------------------ transform.go | 9 +++--- 6 files changed, 50 insertions(+), 80 deletions(-) diff --git a/dasher.go b/dasher.go index ac70396..a9a1ce2 100644 --- a/dasher.go +++ b/dasher.go @@ -4,7 +4,6 @@ package draw2d type DashVertexConverter struct { - command LineMarker next LineBuilder x, y, distance float64 dash []float64 @@ -21,10 +20,6 @@ func NewDashConverter(dash []float64, dashOffset float64, converter LineBuilder) return &dasher } -func (dasher *DashVertexConverter) NextCommand(cmd LineMarker) { - dasher.command = cmd -} - func (dasher *DashVertexConverter) LineTo(x, y float64) { dasher.lineTo(x, y) } @@ -36,6 +31,10 @@ func (dasher *DashVertexConverter) MoveTo(x, y float64) { dasher.currentDash = 0 } +func (dasher *DashVertexConverter) LineJoin() { + dasher.next.LineJoin() +} + func (dasher *DashVertexConverter) Close() { dasher.next.Close() } diff --git a/line.go b/line.go index b6b6c47..5838aad 100644 --- a/line.go +++ b/line.go @@ -3,19 +3,14 @@ package draw2d -type LineMarker byte - -const ( - // Mark the current point of the line as a join to it can draw some specific join Bevel, Miter, Rount - LineJoinMarker LineMarker = iota -) - +// LineBuilder defines drawing line methods type LineBuilder interface { - NextCommand(cmd LineMarker) // MoveTo Start a New line from the point (x, y) MoveTo(x, y float64) // LineTo Draw a line from the current position to the point (x, y) LineTo(x, y float64) + // LineJoin add the most recent starting point to close the path to create a polygon + LineJoin() // 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 @@ -30,12 +25,6 @@ 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) @@ -48,6 +37,12 @@ func (dc *LineBuilders) LineTo(x, y float64) { } } +func (dc *LineBuilders) LineJoin() { + for _, converter := range dc.builders { + converter.LineJoin() + } +} + func (dc *LineBuilders) Close() { for _, converter := range dc.builders { converter.Close() diff --git a/path_adder.go b/path_adder.go index 0037269..696cf89 100644 --- a/path_adder.go +++ b/path_adder.go @@ -15,10 +15,6 @@ func NewVertexAdder(adder raster.Adder) *VertexAdder { return &VertexAdder{adder} } -func (vertexAdder *VertexAdder) NextCommand(cmd LineMarker) { - -} - func (vertexAdder *VertexAdder) MoveTo(x, y float64) { vertexAdder.adder.Start(raster.Point{raster.Fix32(x * 256), raster.Fix32(y * 256)}) } @@ -27,6 +23,9 @@ func (vertexAdder *VertexAdder) LineTo(x, y float64) { vertexAdder.adder.Add1(raster.Point{raster.Fix32(x * 256), raster.Fix32(y * 256)}) } +func (vertexAdder *VertexAdder) LineJoin() { +} + func (vertexAdder *VertexAdder) Close() { } diff --git a/path_converter.go b/path_converter.go index 29356a5..90bf415 100644 --- a/path_converter.go +++ b/path_converter.go @@ -34,7 +34,7 @@ func (c *PathConverter) Convert(paths ...*PathStorage) { case LineTo: c.x, c.y = path.vertices[i], path.vertices[i+1] c.converter.LineTo(c.x, c.y) - c.converter.NextCommand(LineJoinMarker) + c.converter.LineJoin() i += 2 case QuadCurveTo: curve.TraceQuad(c.converter, path.vertices[i-2:], 0.5) @@ -79,7 +79,7 @@ func (c *PathConverter) RMoveTo(dx, dy float64) *PathConverter { func (c *PathConverter) LineTo(x, y float64) *PathConverter { c.x, c.y = x, y c.converter.LineTo(c.x, c.y) - c.converter.NextCommand(LineJoinMarker) + c.converter.LineJoin() return c } diff --git a/stroker.go b/stroker.go index 63f779c..e140254 100644 --- a/stroker.go +++ b/stroker.go @@ -27,7 +27,6 @@ type LineStroker struct { vertices []float64 rewind []float64 x, y, nx, ny float64 - command LineMarker } func NewLineStroker(c Cap, j Join, converter LineBuilder) *LineStroker { @@ -41,8 +40,34 @@ func NewLineStroker(c Cap, j Join, converter LineBuilder) *LineStroker { return l } -func (l *LineStroker) NextCommand(command LineMarker) { - l.command = command +func (l *LineStroker) MoveTo(x, y float64) { + l.x, l.y = x, y +} + +func (l *LineStroker) LineTo(x, y float64) { + l.line(l.x, l.y, x, y) +} + +func (l *LineStroker) LineJoin() { + +} + +func (l *LineStroker) line(x1, y1, x2, y2 float64) { + dx := (x2 - x1) + dy := (y2 - y1) + d := vectorDistance(dx, dy) + if d != 0 { + nx := dy * l.HalfLineWidth / d + ny := -(dx * l.HalfLineWidth / d) + l.appendVertex(x1+nx, y1+ny, x2+nx, y2+ny, x1-nx, y1-ny, x2-nx, y2-ny) + l.x, l.y, l.nx, l.ny = x2, y2, nx, ny + } +} + +func (l *LineStroker) Close() { + if len(l.vertices) > 1 { + l.appendVertex(l.vertices[0], l.vertices[1], l.rewind[0], l.rewind[1]) + } } func (l *LineStroker) End() { @@ -66,55 +91,8 @@ func (l *LineStroker) End() { } -func (l *LineStroker) MoveTo(x, y float64) { - l.x, l.y = x, y -} - -func (l *LineStroker) LineTo(x, y float64) { - switch l.command { - case LineJoinMarker: - l.joinLine(l.x, l.y, l.nx, l.ny, x, y) - default: - l.line(l.x, l.y, x, y) - } -} - -func (l *LineStroker) Close() { - if len(l.vertices) > 1 { - l.appendVertex(l.vertices[0], l.vertices[1], l.rewind[0], l.rewind[1]) - } -} - func (l *LineStroker) appendVertex(vertices ...float64) { s := len(vertices) / 2 l.vertices = append(l.vertices, vertices[:s]...) l.rewind = append(l.rewind, vertices[s:]...) } - -func (l *LineStroker) line(x1, y1, x2, y2 float64) { - dx := (x2 - x1) - dy := (y2 - y1) - d := vectorDistance(dx, dy) - if d != 0 { - nx := dy * l.HalfLineWidth / d - ny := -(dx * l.HalfLineWidth / d) - l.appendVertex(x1+nx, y1+ny, x2+nx, y2+ny, x1-nx, y1-ny, x2-nx, y2-ny) - l.x, l.y, l.nx, l.ny = x2, y2, nx, ny - } -} - -func (l *LineStroker) joinLine(x1, y1, nx1, ny1, x2, y2 float64) { - dx := (x2 - x1) - dy := (y2 - y1) - d := vectorDistance(dx, dy) - - if d != 0 { - nx := dy * l.HalfLineWidth / d - ny := -(dx * l.HalfLineWidth / d) - /* l.join(x1, y1, x1 + nx, y1 - ny, nx, ny, x1 + ny2, y1 + nx2, nx2, ny2) - l.join(x1, y1, x1 - ny1, y1 - nx1, nx1, ny1, x1 - ny2, y1 - nx2, nx2, ny2)*/ - - l.appendVertex(x1+nx, y1+ny, x2+nx, y2+ny, x1-nx, y1-ny, x2-nx, y2-ny) - l.x, l.y, l.nx, l.ny = x2, y2, nx, ny - } -} diff --git a/transform.go b/transform.go index 1f4ffa0..f2c2927 100644 --- a/transform.go +++ b/transform.go @@ -261,11 +261,6 @@ func NewVertexMatrixTransform(tr MatrixTransform, converter LineBuilder) *Vertex return &VertexMatrixTransform{tr, converter} } -// Vertex Matrix Transform -func (vmt *VertexMatrixTransform) NextCommand(command LineMarker) { - vmt.Next.NextCommand(command) -} - func (vmt *VertexMatrixTransform) MoveTo(x, y float64) { u := x*vmt.tr[0] + y*vmt.tr[2] + vmt.tr[4] v := x*vmt.tr[1] + y*vmt.tr[3] + vmt.tr[5] @@ -278,6 +273,10 @@ func (vmt *VertexMatrixTransform) LineTo(x, y float64) { vmt.Next.LineTo(u, v) } +func (vmt *VertexMatrixTransform) LineJoin() { + vmt.Next.LineJoin() +} + func (vmt *VertexMatrixTransform) Close() { vmt.Next.Close() }