draw2d/path_converter.go

148 lines
3.7 KiB
Go
Raw Normal View History

2011-04-27 08:06:14 +00:00
// Copyright 2010 The draw2d Authors. All rights reserved.
// created: 06/12/2010 by Laurent Le Goff
2012-04-17 09:03:56 +00:00
2011-04-27 08:06:14 +00:00
package draw2d
import (
2015-04-23 08:05:48 +00:00
"github.com/llgcode/draw2d/curve"
2011-04-27 08:06:14 +00:00
"math"
)
type PathConverter struct {
converter LineBuilder
2015-04-23 08:05:48 +00:00
ApproximationScale float64
startX, startY, x, y float64
2011-04-27 08:06:14 +00:00
}
func NewPathConverter(converter LineBuilder) *PathConverter {
2015-04-23 08:05:48 +00:00
return &PathConverter{converter, 1, 0, 0, 0, 0}
2011-04-27 08:06:14 +00:00
}
2015-04-23 16:12:31 +00:00
func (c *PathConverter) Convert(paths ...*Path) {
2011-04-27 08:06:14 +00:00
for _, path := range paths {
2015-04-23 08:05:48 +00:00
i := 0
2011-04-27 08:06:14 +00:00
for _, cmd := range path.commands {
2015-04-23 08:05:48 +00:00
switch cmd {
case MoveTo:
c.x, c.y = path.vertices[i], path.vertices[i+1]
c.startX, c.startY = c.x, c.y
if i != 0 {
2015-04-23 14:18:21 +00:00
c.converter.End()
}
c.converter.MoveTo(c.x, c.y)
2015-04-23 08:05:48 +00:00
i += 2
case LineTo:
c.x, c.y = path.vertices[i], path.vertices[i+1]
c.converter.LineTo(c.x, c.y)
2015-04-23 15:43:26 +00:00
c.converter.LineJoin()
2015-04-23 08:05:48 +00:00
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]
c.converter.LineTo(c.x, c.y)
2015-04-23 08:05:48 +00:00
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]
c.converter.LineTo(c.x, c.y)
2015-04-23 08:05:48 +00:00
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)
c.converter.LineTo(c.x, c.y)
2015-04-23 08:05:48 +00:00
i += 6
case Close:
c.converter.LineTo(c.startX, c.startY)
c.converter.Close()
2015-04-23 08:05:48 +00:00
}
2011-04-27 08:06:14 +00:00
}
2015-04-23 14:18:21 +00:00
c.converter.End()
2011-04-27 08:06:14 +00:00
}
}
2015-04-23 08:05:48 +00:00
func (c *PathConverter) convertCommand(cmd PathCmd, vertices ...float64) int {
2011-04-27 08:06:14 +00:00
return 0
}
func (c *PathConverter) MoveTo(x, y float64) *PathConverter {
c.x, c.y = x, y
c.startX, c.startY = c.x, c.y
2015-04-23 14:18:21 +00:00
c.converter.End()
c.converter.MoveTo(c.x, c.y)
2011-04-27 08:06:14 +00:00
return c
}
func (c *PathConverter) RMoveTo(dx, dy float64) *PathConverter {
c.MoveTo(c.x+dx, c.y+dy)
return c
}
func (c *PathConverter) LineTo(x, y float64) *PathConverter {
c.x, c.y = x, y
c.converter.LineTo(c.x, c.y)
2015-04-23 15:43:26 +00:00
c.converter.LineJoin()
2011-04-27 08:06:14 +00:00
return c
}
func (c *PathConverter) RLineTo(dx, dy float64) *PathConverter {
c.LineTo(c.x+dx, c.y+dy)
return c
}
func (c *PathConverter) QuadCurveTo(cx, cy, x, y float64) *PathConverter {
2015-04-23 08:05:48 +00:00
curve.TraceQuad(c.converter, []float64{c.x, c.y, cx, cy, x, y}, 0.5)
2011-04-27 08:06:14 +00:00
c.x, c.y = x, y
c.converter.LineTo(c.x, c.y)
2011-04-27 08:06:14 +00:00
return c
}
func (c *PathConverter) RQuadCurveTo(dcx, dcy, dx, dy float64) *PathConverter {
c.QuadCurveTo(c.x+dcx, c.y+dcy, c.x+dx, c.y+dy)
return c
}
func (c *PathConverter) CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64) *PathConverter {
2015-04-23 08:05:48 +00:00
curve.TraceCubic(c.converter, []float64{c.x, c.y, cx1, cy1, cx2, cy2, x, y}, 0.5)
2011-04-27 08:06:14 +00:00
c.x, c.y = x, y
c.converter.LineTo(c.x, c.y)
2011-04-27 08:06:14 +00:00
return c
}
func (c *PathConverter) RCubicCurveTo(dcx1, dcy1, dcx2, dcy2, dx, dy float64) *PathConverter {
c.CubicCurveTo(c.x+dcx1, c.y+dcy1, c.x+dcx2, c.y+dcy2, c.x+dx, c.y+dy)
return c
}
func (c *PathConverter) ArcTo(cx, cy, rx, ry, startAngle, angle float64) *PathConverter {
endAngle := startAngle + angle
clockWise := true
if angle < 0 {
clockWise = false
}
// normalize
if clockWise {
for endAngle < startAngle {
endAngle += math.Pi * 2.0
}
} else {
for startAngle < endAngle {
startAngle += math.Pi * 2.0
}
}
startX := cx + math.Cos(startAngle)*rx
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)
c.converter.LineTo(c.x, c.y)
2011-04-27 08:06:14 +00:00
return c
}
func (c *PathConverter) RArcTo(dcx, dcy, rx, ry, startAngle, angle float64) *PathConverter {
c.ArcTo(c.x+dcx, c.y+dcy, rx, ry, startAngle, angle)
return c
}
func (c *PathConverter) Close() *PathConverter {
c.converter.Close()
2011-04-27 08:06:14 +00:00
return c
}