Replace LineCloseMarker by Close() Method

This commit is contained in:
Laurent Le Goff 2015-04-23 17:08:20 +02:00
parent 4fa829a373
commit 42d0eb260f
6 changed files with 38 additions and 44 deletions

View file

@ -21,10 +21,6 @@ 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
}
@ -41,6 +37,14 @@ func (dasher *DashVertexConverter) MoveTo(x, y float64) {
dasher.currentDash = 0
}
func (dasher *DashVertexConverter) Close() {
dasher.next.Close()
}
func (dasher *DashVertexConverter) End() {
dasher.next.End()
}
func (dasher *DashVertexConverter) lineTo(x, y float64) {
rest := dasher.dash[dasher.currentDash] - dasher.distance
for rest < 0 {

14
line.go
View file

@ -9,15 +9,17 @@ 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 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)
// 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()
}
@ -47,6 +49,12 @@ func (dc *LineBuilders) LineTo(x, y float64) {
}
}
func (dc *LineBuilders) Close() {
for _, converter := range dc.builders {
converter.Close()
}
}
func (dc *LineBuilders) End() {
for _, converter := range dc.builders {
converter.End()

View file

@ -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) Close() {
}
func (vertexAdder *VertexAdder) End() {
}

View file

@ -33,38 +33,26 @@ func (c *PathConverter) Convert(paths ...*PathStorage) {
i += 2
case LineTo:
c.x, c.y = path.vertices[i], path.vertices[i+1]
if c.startX == c.x && c.startY == c.y {
c.converter.NextCommand(LineCloseMarker)
}
c.converter.LineTo(c.x, c.y)
c.converter.NextCommand(LineJoinMarker)
i += 2
case QuadCurveTo:
curve.TraceQuad(c.converter, path.vertices[i-2:], 0.5)
c.x, c.y = path.vertices[i+2], path.vertices[i+3]
if c.startX == c.x && c.startY == c.y {
c.converter.NextCommand(LineCloseMarker)
}
c.converter.LineTo(c.x, c.y)
i += 4
case CubicCurveTo:
curve.TraceCubic(c.converter, path.vertices[i-2:], 0.5)
c.x, c.y = path.vertices[i+4], path.vertices[i+5]
if c.startX == c.x && c.startY == c.y {
c.converter.NextCommand(LineCloseMarker)
}
c.converter.LineTo(c.x, c.y)
i += 6
case ArcTo:
c.x, c.y = arc(c.converter, path.vertices[i], path.vertices[i+1], path.vertices[i+2], path.vertices[i+3], path.vertices[i+4], path.vertices[i+5], c.ApproximationScale)
if c.startX == c.x && c.startY == c.y {
c.converter.NextCommand(LineCloseMarker)
}
c.converter.LineTo(c.x, c.y)
i += 6
case Close:
c.converter.NextCommand(LineCloseMarker)
c.converter.LineTo(c.startX, c.startY)
c.converter.Close()
}
}
c.converter.End()
@ -90,9 +78,6 @@ func (c *PathConverter) RMoveTo(dx, dy float64) *PathConverter {
func (c *PathConverter) LineTo(x, y float64) *PathConverter {
c.x, c.y = x, y
if c.startX == c.x && c.startY == c.y {
c.converter.NextCommand(LineCloseMarker)
}
c.converter.LineTo(c.x, c.y)
c.converter.NextCommand(LineJoinMarker)
return c
@ -106,9 +91,6 @@ func (c *PathConverter) RLineTo(dx, dy float64) *PathConverter {
func (c *PathConverter) QuadCurveTo(cx, cy, x, y float64) *PathConverter {
curve.TraceQuad(c.converter, []float64{c.x, c.y, cx, cy, x, y}, 0.5)
c.x, c.y = x, y
if c.startX == c.x && c.startY == c.y {
c.converter.NextCommand(LineCloseMarker)
}
c.converter.LineTo(c.x, c.y)
return c
}
@ -121,9 +103,6 @@ func (c *PathConverter) RQuadCurveTo(dcx, dcy, dx, dy float64) *PathConverter {
func (c *PathConverter) CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64) *PathConverter {
curve.TraceCubic(c.converter, []float64{c.x, c.y, cx1, cy1, cx2, cy2, x, y}, 0.5)
c.x, c.y = x, y
if c.startX == c.x && c.startY == c.y {
c.converter.NextCommand(LineCloseMarker)
}
c.converter.LineTo(c.x, c.y)
return c
}
@ -153,9 +132,6 @@ func (c *PathConverter) ArcTo(cx, cy, rx, ry, startAngle, angle float64) *PathCo
startY := cy + math.Sin(startAngle)*ry
c.MoveTo(startX, startY)
c.x, c.y = arc(c.converter, cx, cy, rx, ry, startAngle, angle, c.ApproximationScale)
if c.startX == c.x && c.startY == c.y {
c.converter.NextCommand(LineCloseMarker)
}
c.converter.LineTo(c.x, c.y)
return c
}
@ -166,7 +142,6 @@ func (c *PathConverter) RArcTo(dcx, dcy, rx, ry, startAngle, angle float64) *Pat
}
func (c *PathConverter) Close() *PathConverter {
c.converter.NextCommand(LineCloseMarker)
c.converter.LineTo(c.startX, c.startY)
c.converter.Close()
return c
}

View file

@ -78,16 +78,22 @@ func (l *LineStroker) LineTo(x, y float64) {
switch l.command {
case LineJoinMarker:
l.joinLine(l.x, l.y, l.nx, l.ny, x, y)
case LineCloseMarker:
l.line(l.x, l.y, x, y)
l.joinLine(l.x, l.y, l.nx, l.ny, x, y)
l.closePolygon()
// case LineCloseMarker:
// l.line(l.x, l.y, x, y)
// l.joinLine(l.x, l.y, l.nx, l.ny, x, y)
// l.closePolygon()
default:
l.line(l.x, l.y, x, y)
}
l.command = LineNoneMarker
}
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
if len(l.vertices)+s >= cap(l.vertices) {
@ -106,12 +112,6 @@ func (l *LineStroker) appendVertex(vertices ...float64) {
}
func (l *LineStroker) closePolygon() {
if len(l.vertices) > 1 {
l.appendVertex(l.vertices[0], l.vertices[1], l.rewind[0], l.rewind[1])
}
}
func (l *LineStroker) line(x1, y1, x2, y2 float64) {
dx := (x2 - x1)
dy := (y2 - y1)

View file

@ -278,6 +278,10 @@ func (vmt *VertexMatrixTransform) LineTo(x, y float64) {
vmt.Next.LineTo(u, v)
}
func (vmt *VertexMatrixTransform) Close() {
vmt.Next.Close()
}
func (vmt *VertexMatrixTransform) End() {
vmt.Next.End()
}