From d47e08f7c9c8518edecbf9695ab1b46436efaf37 Mon Sep 17 00:00:00 2001 From: Stani Date: Sat, 27 Jun 2015 01:03:41 +0200 Subject: [PATCH] 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) --- advanced_path.go | 2 ++ path_adder.go | 12 +++++----- path_converter.go | 4 ++-- path_storage.go | 60 +++++++++++++++++++++++------------------------ 4 files changed, 40 insertions(+), 38 deletions(-) diff --git a/advanced_path.go b/advanced_path.go index 68f1d78..2610bb8 100644 --- a/advanced_path.go +++ b/advanced_path.go @@ -32,11 +32,13 @@ func RoundRect(path Path, x1, y1, x2, y2, arcWidth, arcHeight 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.Close() } func Circle(path Path, cx, cy, radius float64) { + path.MoveTo(cx-radius, cy) path.ArcTo(cx, cy, radius, radius, 0, -math.Pi*2) path.Close() } diff --git a/path_adder.go b/path_adder.go index c5efd2b..5731bb5 100644 --- a/path_adder.go +++ b/path_adder.go @@ -43,23 +43,23 @@ func NewPathAdder(adder raster.Adder) *PathAdder { func (pathAdder *PathAdder) Convert(paths ...*PathStorage) { for _, path := range paths { j := 0 - for _, cmd := range path.commands { + for _, cmd := range path.Commands { switch cmd { 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) j += 2 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 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 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 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) j += 6 case Close: diff --git a/path_converter.go b/path_converter.go index 0ef96b8..865a466 100644 --- a/path_converter.go +++ b/path_converter.go @@ -20,8 +20,8 @@ func NewPathConverter(converter VertexConverter) *PathConverter { func (c *PathConverter) Convert(paths ...*PathStorage) { for _, path := range paths { j := 0 - for _, cmd := range path.commands { - j = j + c.ConvertCommand(cmd, path.vertices[j:]...) + for _, cmd := range path.Commands { + j = j + c.ConvertCommand(cmd, path.Vertices[j:]...) } c.converter.NextCommand(VertexStopCommand) } diff --git a/path_storage.go b/path_storage.go index c2a8870..a7e1662 100644 --- a/path_storage.go +++ b/path_storage.go @@ -20,45 +20,45 @@ const ( ) type PathStorage struct { - commands []PathCmd - vertices []float64 + Commands []PathCmd + Vertices []float64 x, y float64 } func NewPathStorage() (p *PathStorage) { p = new(PathStorage) - p.commands = make([]PathCmd, 0, 256) - p.vertices = make([]float64, 0, 256) + p.Commands = make([]PathCmd, 0, 256) + p.Vertices = make([]float64, 0, 256) return } func (p *PathStorage) Clear() { - p.commands = p.commands[0:0] - p.vertices = p.vertices[0:0] + p.Commands = p.Commands[0:0] + p.Vertices = p.Vertices[0:0] return } -func (p *PathStorage) appendToPath(cmd PathCmd, vertices ...float64) { - if cap(p.vertices) <= len(p.vertices)+6 { - a := make([]PathCmd, len(p.commands), cap(p.commands)+256) - b := make([]float64, len(p.vertices), cap(p.vertices)+256) - copy(a, p.commands) - p.commands = a - copy(b, p.vertices) - p.vertices = b +func (p *PathStorage) appendToPath(cmd PathCmd, Vertices ...float64) { + if cap(p.Vertices) <= len(p.Vertices)+6 { + a := make([]PathCmd, len(p.Commands), cap(p.Commands)+256) + b := make([]float64, len(p.Vertices), cap(p.Vertices)+256) + copy(a, p.Commands) + p.Commands = a + copy(b, p.Vertices) + p.Vertices = b } - p.commands = p.commands[0 : len(p.commands)+1] - p.commands[len(p.commands)-1] = cmd - copy(p.vertices[len(p.vertices):len(p.vertices)+len(vertices)], vertices) - p.vertices = p.vertices[0 : len(p.vertices)+len(vertices)] + p.Commands = p.Commands[0 : len(p.Commands)+1] + p.Commands[len(p.Commands)-1] = cmd + copy(p.Vertices[len(p.Vertices):len(p.Vertices)+len(Vertices)], Vertices) + p.Vertices = p.Vertices[0 : len(p.Vertices)+len(Vertices)] } func (src *PathStorage) Copy() (dest *PathStorage) { dest = new(PathStorage) - dest.commands = make([]PathCmd, len(src.commands)) - copy(dest.commands, src.commands) - dest.vertices = make([]float64, len(src.vertices)) - copy(dest.vertices, src.vertices) + dest.Commands = make([]PathCmd, len(src.Commands)) + copy(dest.Commands, src.Commands) + dest.Vertices = make([]float64, len(src.Vertices)) + copy(dest.Vertices, src.Vertices) return dest } @@ -67,7 +67,7 @@ func (p *PathStorage) LastPoint() (x, y float64) { } func (p *PathStorage) IsEmpty() bool { - return len(p.commands) == 0 + return len(p.Commands) == 0 } 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 startY := cy + math.Sin(startAngle)*ry - if len(p.commands) > 0 { + if len(p.Commands) > 0 { p.LineTo(startX, startY) } else { 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 { s := "" j := 0 - for _, cmd := range p.commands { + for _, cmd := range p.Commands { switch cmd { 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 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 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 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 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 case Close: s += "Close\n"