Preparing for pdf backend

- make PathStorage Commands and Vertices public, so they can be accessed by the pdf backend
- start every path with MoveTo (also before ArcTo)
This commit is contained in:
Stani 2015-06-27 01:03:41 +02:00
parent f8fb5a2052
commit d47e08f7c9
4 changed files with 40 additions and 38 deletions

View file

@ -32,11 +32,13 @@ func RoundRect(path Path, x1, y1, x2, y2, arcWidth, arcHeight float64) {
} }
func Ellipse(path Path, cx, cy, rx, ry float64) { func Ellipse(path Path, cx, cy, rx, ry float64) {
path.MoveTo(cx-rx, cy)
path.ArcTo(cx, cy, rx, ry, 0, -math.Pi*2) path.ArcTo(cx, cy, rx, ry, 0, -math.Pi*2)
path.Close() path.Close()
} }
func Circle(path Path, cx, cy, radius float64) { func Circle(path Path, cx, cy, radius float64) {
path.MoveTo(cx-radius, cy)
path.ArcTo(cx, cy, radius, radius, 0, -math.Pi*2) path.ArcTo(cx, cy, radius, radius, 0, -math.Pi*2)
path.Close() path.Close()
} }

View file

@ -43,23 +43,23 @@ func NewPathAdder(adder raster.Adder) *PathAdder {
func (pathAdder *PathAdder) Convert(paths ...*PathStorage) { func (pathAdder *PathAdder) Convert(paths ...*PathStorage) {
for _, path := range paths { for _, path := range paths {
j := 0 j := 0
for _, cmd := range path.commands { for _, cmd := range path.Commands {
switch cmd { switch cmd {
case MoveTo: case MoveTo:
pathAdder.firstPoint = raster.Point{raster.Fix32(path.vertices[j] * 256), raster.Fix32(path.vertices[j+1] * 256)} pathAdder.firstPoint = raster.Point{raster.Fix32(path.Vertices[j] * 256), raster.Fix32(path.Vertices[j+1] * 256)}
pathAdder.adder.Start(pathAdder.firstPoint) pathAdder.adder.Start(pathAdder.firstPoint)
j += 2 j += 2
case LineTo: case LineTo:
pathAdder.adder.Add1(raster.Point{raster.Fix32(path.vertices[j] * 256), raster.Fix32(path.vertices[j+1] * 256)}) pathAdder.adder.Add1(raster.Point{raster.Fix32(path.Vertices[j] * 256), raster.Fix32(path.Vertices[j+1] * 256)})
j += 2 j += 2
case QuadCurveTo: case QuadCurveTo:
pathAdder.adder.Add2(raster.Point{raster.Fix32(path.vertices[j] * 256), raster.Fix32(path.vertices[j+1] * 256)}, raster.Point{raster.Fix32(path.vertices[j+2] * 256), raster.Fix32(path.vertices[j+3] * 256)}) pathAdder.adder.Add2(raster.Point{raster.Fix32(path.Vertices[j] * 256), raster.Fix32(path.Vertices[j+1] * 256)}, raster.Point{raster.Fix32(path.Vertices[j+2] * 256), raster.Fix32(path.Vertices[j+3] * 256)})
j += 4 j += 4
case CubicCurveTo: case CubicCurveTo:
pathAdder.adder.Add3(raster.Point{raster.Fix32(path.vertices[j] * 256), raster.Fix32(path.vertices[j+1] * 256)}, raster.Point{raster.Fix32(path.vertices[j+2] * 256), raster.Fix32(path.vertices[j+3] * 256)}, raster.Point{raster.Fix32(path.vertices[j+4] * 256), raster.Fix32(path.vertices[j+5] * 256)}) pathAdder.adder.Add3(raster.Point{raster.Fix32(path.Vertices[j] * 256), raster.Fix32(path.Vertices[j+1] * 256)}, raster.Point{raster.Fix32(path.Vertices[j+2] * 256), raster.Fix32(path.Vertices[j+3] * 256)}, raster.Point{raster.Fix32(path.Vertices[j+4] * 256), raster.Fix32(path.Vertices[j+5] * 256)})
j += 6 j += 6
case ArcTo: case ArcTo:
lastPoint := arcAdder(pathAdder.adder, path.vertices[j], path.vertices[j+1], path.vertices[j+2], path.vertices[j+3], path.vertices[j+4], path.vertices[j+5], pathAdder.ApproximationScale) lastPoint := arcAdder(pathAdder.adder, path.Vertices[j], path.Vertices[j+1], path.Vertices[j+2], path.Vertices[j+3], path.Vertices[j+4], path.Vertices[j+5], pathAdder.ApproximationScale)
pathAdder.adder.Add1(lastPoint) pathAdder.adder.Add1(lastPoint)
j += 6 j += 6
case Close: case Close:

View file

@ -20,8 +20,8 @@ func NewPathConverter(converter VertexConverter) *PathConverter {
func (c *PathConverter) Convert(paths ...*PathStorage) { func (c *PathConverter) Convert(paths ...*PathStorage) {
for _, path := range paths { for _, path := range paths {
j := 0 j := 0
for _, cmd := range path.commands { for _, cmd := range path.Commands {
j = j + c.ConvertCommand(cmd, path.vertices[j:]...) j = j + c.ConvertCommand(cmd, path.Vertices[j:]...)
} }
c.converter.NextCommand(VertexStopCommand) c.converter.NextCommand(VertexStopCommand)
} }

View file

@ -20,45 +20,45 @@ const (
) )
type PathStorage struct { type PathStorage struct {
commands []PathCmd Commands []PathCmd
vertices []float64 Vertices []float64
x, y float64 x, y float64
} }
func NewPathStorage() (p *PathStorage) { func NewPathStorage() (p *PathStorage) {
p = new(PathStorage) p = new(PathStorage)
p.commands = make([]PathCmd, 0, 256) p.Commands = make([]PathCmd, 0, 256)
p.vertices = make([]float64, 0, 256) p.Vertices = make([]float64, 0, 256)
return return
} }
func (p *PathStorage) Clear() { func (p *PathStorage) Clear() {
p.commands = p.commands[0:0] p.Commands = p.Commands[0:0]
p.vertices = p.vertices[0:0] p.Vertices = p.Vertices[0:0]
return return
} }
func (p *PathStorage) appendToPath(cmd PathCmd, vertices ...float64) { func (p *PathStorage) appendToPath(cmd PathCmd, Vertices ...float64) {
if cap(p.vertices) <= len(p.vertices)+6 { if cap(p.Vertices) <= len(p.Vertices)+6 {
a := make([]PathCmd, len(p.commands), cap(p.commands)+256) a := make([]PathCmd, len(p.Commands), cap(p.Commands)+256)
b := make([]float64, len(p.vertices), cap(p.vertices)+256) b := make([]float64, len(p.Vertices), cap(p.Vertices)+256)
copy(a, p.commands) copy(a, p.Commands)
p.commands = a p.Commands = a
copy(b, p.vertices) copy(b, p.Vertices)
p.vertices = b p.Vertices = b
} }
p.commands = p.commands[0 : len(p.commands)+1] p.Commands = p.Commands[0 : len(p.Commands)+1]
p.commands[len(p.commands)-1] = cmd p.Commands[len(p.Commands)-1] = cmd
copy(p.vertices[len(p.vertices):len(p.vertices)+len(vertices)], vertices) copy(p.Vertices[len(p.Vertices):len(p.Vertices)+len(Vertices)], Vertices)
p.vertices = p.vertices[0 : len(p.vertices)+len(vertices)] p.Vertices = p.Vertices[0 : len(p.Vertices)+len(Vertices)]
} }
func (src *PathStorage) Copy() (dest *PathStorage) { func (src *PathStorage) Copy() (dest *PathStorage) {
dest = new(PathStorage) dest = new(PathStorage)
dest.commands = make([]PathCmd, len(src.commands)) dest.Commands = make([]PathCmd, len(src.Commands))
copy(dest.commands, src.commands) copy(dest.Commands, src.Commands)
dest.vertices = make([]float64, len(src.vertices)) dest.Vertices = make([]float64, len(src.Vertices))
copy(dest.vertices, src.vertices) copy(dest.Vertices, src.Vertices)
return dest return dest
} }
@ -67,7 +67,7 @@ func (p *PathStorage) LastPoint() (x, y float64) {
} }
func (p *PathStorage) IsEmpty() bool { func (p *PathStorage) IsEmpty() bool {
return len(p.commands) == 0 return len(p.Commands) == 0
} }
func (p *PathStorage) Close() *PathStorage { func (p *PathStorage) Close() *PathStorage {
@ -145,7 +145,7 @@ func (p *PathStorage) ArcTo(cx, cy, rx, ry, startAngle, angle float64) *PathStor
} }
startX := cx + math.Cos(startAngle)*rx startX := cx + math.Cos(startAngle)*rx
startY := cy + math.Sin(startAngle)*ry startY := cy + math.Sin(startAngle)*ry
if len(p.commands) > 0 { if len(p.Commands) > 0 {
p.LineTo(startX, startY) p.LineTo(startX, startY)
} else { } else {
p.MoveTo(startX, startY) p.MoveTo(startX, startY)
@ -165,22 +165,22 @@ func (p *PathStorage) RArcTo(dcx, dcy, rx, ry, startAngle, angle float64) *PathS
func (p *PathStorage) String() string { func (p *PathStorage) String() string {
s := "" s := ""
j := 0 j := 0
for _, cmd := range p.commands { for _, cmd := range p.Commands {
switch cmd { switch cmd {
case MoveTo: case MoveTo:
s += fmt.Sprintf("MoveTo: %f, %f\n", p.vertices[j], p.vertices[j+1]) s += fmt.Sprintf("MoveTo: %f, %f\n", p.Vertices[j], p.Vertices[j+1])
j = j + 2 j = j + 2
case LineTo: case LineTo:
s += fmt.Sprintf("LineTo: %f, %f\n", p.vertices[j], p.vertices[j+1]) s += fmt.Sprintf("LineTo: %f, %f\n", p.Vertices[j], p.Vertices[j+1])
j = j + 2 j = j + 2
case QuadCurveTo: case QuadCurveTo:
s += fmt.Sprintf("QuadCurveTo: %f, %f, %f, %f\n", p.vertices[j], p.vertices[j+1], p.vertices[j+2], p.vertices[j+3]) s += fmt.Sprintf("QuadCurveTo: %f, %f, %f, %f\n", p.Vertices[j], p.Vertices[j+1], p.Vertices[j+2], p.Vertices[j+3])
j = j + 4 j = j + 4
case CubicCurveTo: case CubicCurveTo:
s += fmt.Sprintf("CubicCurveTo: %f, %f, %f, %f, %f, %f\n", p.vertices[j], p.vertices[j+1], p.vertices[j+2], p.vertices[j+3], p.vertices[j+4], p.vertices[j+5]) s += fmt.Sprintf("CubicCurveTo: %f, %f, %f, %f, %f, %f\n", p.Vertices[j], p.Vertices[j+1], p.Vertices[j+2], p.Vertices[j+3], p.Vertices[j+4], p.Vertices[j+5])
j = j + 6 j = j + 6
case ArcTo: case ArcTo:
s += fmt.Sprintf("ArcTo: %f, %f, %f, %f, %f, %f\n", p.vertices[j], p.vertices[j+1], p.vertices[j+2], p.vertices[j+3], p.vertices[j+4], p.vertices[j+5]) s += fmt.Sprintf("ArcTo: %f, %f, %f, %f, %f, %f\n", p.Vertices[j], p.Vertices[j+1], p.Vertices[j+2], p.Vertices[j+3], p.Vertices[j+4], p.Vertices[j+5])
j = j + 6 j = j + 6
case Close: case Close:
s += "Close\n" s += "Close\n"