rename Path to PathBuilder

This commit is contained in:
Laurent Le Goff 2015-04-23 18:12:31 +02:00
parent fef7265145
commit 06178b5d2d
9 changed files with 39 additions and 39 deletions

View file

@ -9,7 +9,7 @@ import (
//high level path creation //high level path creation
func Rect(path Path, x1, y1, x2, y2 float64) { func Rect(path PathBuilder, x1, y1, x2, y2 float64) {
path.MoveTo(x1, y1) path.MoveTo(x1, y1)
path.LineTo(x2, y1) path.LineTo(x2, y1)
path.LineTo(x2, y2) path.LineTo(x2, y2)
@ -17,7 +17,7 @@ func Rect(path Path, x1, y1, x2, y2 float64) {
path.Close() path.Close()
} }
func RoundRect(path Path, x1, y1, x2, y2, arcWidth, arcHeight float64) { func RoundRect(path PathBuilder, x1, y1, x2, y2, arcWidth, arcHeight float64) {
arcWidth = arcWidth / 2 arcWidth = arcWidth / 2
arcHeight = arcHeight / 2 arcHeight = arcHeight / 2
path.MoveTo(x1, y1+arcHeight) path.MoveTo(x1, y1+arcHeight)
@ -31,12 +31,12 @@ func RoundRect(path Path, x1, y1, x2, y2, arcWidth, arcHeight float64) {
path.Close() path.Close()
} }
func Ellipse(path Path, cx, cy, rx, ry float64) { func Ellipse(path PathBuilder, cx, cy, rx, ry float64) {
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 PathBuilder, cx, cy, radius float64) {
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

@ -182,7 +182,7 @@ func (gc *GraphicContext) paint(rasterizer *raster.Rasterizer, color color.Color
gc.painter.Flush() gc.painter.Flush()
} }
func (gc *GraphicContext) Stroke(paths ...*draw2d.PathStorage) { func (gc *GraphicContext) Stroke(paths ...*draw2d.Path) {
paths = append(paths, gc.Current.Path) paths = append(paths, gc.Current.Path)
gc.strokeRasterizer.UseNonZeroWinding = true gc.strokeRasterizer.UseNonZeroWinding = true
@ -202,7 +202,7 @@ func (gc *GraphicContext) Stroke(paths ...*draw2d.PathStorage) {
gc.Current.Path.Clear() gc.Current.Path.Clear()
} }
func (gc *GraphicContext) Fill(paths ...*draw2d.PathStorage) { func (gc *GraphicContext) Fill(paths ...*draw2d.Path) {
paths = append(paths, gc.Current.Path) paths = append(paths, gc.Current.Path)
gc.fillRasterizer.UseNonZeroWinding = gc.Current.FillRule.UseNonZeroWinding() gc.fillRasterizer.UseNonZeroWinding = gc.Current.FillRule.UseNonZeroWinding()
@ -214,7 +214,7 @@ func (gc *GraphicContext) Fill(paths ...*draw2d.PathStorage) {
gc.Current.Path.Clear() gc.Current.Path.Clear()
} }
func (gc *GraphicContext) FillStroke(paths ...*draw2d.PathStorage) { func (gc *GraphicContext) FillStroke(paths ...*draw2d.Path) {
gc.fillRasterizer.UseNonZeroWinding = gc.Current.FillRule.UseNonZeroWinding() gc.fillRasterizer.UseNonZeroWinding = gc.Current.FillRule.UseNonZeroWinding()
gc.strokeRasterizer.UseNonZeroWinding = true gc.strokeRasterizer.UseNonZeroWinding = true

8
gc.go
View file

@ -16,7 +16,7 @@ const (
) )
type GraphicContext interface { type GraphicContext interface {
Path PathBuilder
// Create a new path // Create a new path
BeginPath() BeginPath()
GetMatrixTransform() MatrixTransform GetMatrixTransform() MatrixTransform
@ -49,7 +49,7 @@ type GraphicContext interface {
FillStringAt(text string, x, y float64) (cursor float64) FillStringAt(text string, x, y float64) (cursor float64)
StrokeString(text string) (cursor float64) StrokeString(text string) (cursor float64)
StrokeStringAt(text string, x, y float64) (cursor float64) StrokeStringAt(text string, x, y float64) (cursor float64)
Stroke(paths ...*PathStorage) Stroke(paths ...*Path)
Fill(paths ...*PathStorage) Fill(paths ...*Path)
FillStroke(paths ...*PathStorage) FillStroke(paths ...*Path)
} }

View file

@ -275,7 +275,7 @@ func (gc *ImageGraphicContext) paint(rasterizer *raster.Rasterizer, color color.
} }
/**** second method ****/ /**** second method ****/
func (gc *ImageGraphicContext) Stroke(paths ...*PathStorage) { func (gc *ImageGraphicContext) Stroke(paths ...*Path) {
paths = append(paths, gc.Current.Path) paths = append(paths, gc.Current.Path)
gc.strokeRasterizer.UseNonZeroWinding = true gc.strokeRasterizer.UseNonZeroWinding = true
@ -295,7 +295,7 @@ func (gc *ImageGraphicContext) Stroke(paths ...*PathStorage) {
} }
/**** second method ****/ /**** second method ****/
func (gc *ImageGraphicContext) Fill(paths ...*PathStorage) { func (gc *ImageGraphicContext) Fill(paths ...*Path) {
paths = append(paths, gc.Current.Path) paths = append(paths, gc.Current.Path)
gc.fillRasterizer.UseNonZeroWinding = gc.Current.FillRule.UseNonZeroWinding() gc.fillRasterizer.UseNonZeroWinding = gc.Current.FillRule.UseNonZeroWinding()
@ -308,7 +308,7 @@ func (gc *ImageGraphicContext) Fill(paths ...*PathStorage) {
} }
/* second method */ /* second method */
func (gc *ImageGraphicContext) FillStroke(paths ...*PathStorage) { func (gc *ImageGraphicContext) FillStroke(paths ...*Path) {
gc.fillRasterizer.UseNonZeroWinding = gc.Current.FillRule.UseNonZeroWinding() gc.fillRasterizer.UseNonZeroWinding = gc.Current.FillRule.UseNonZeroWinding()
gc.strokeRasterizer.UseNonZeroWinding = true gc.strokeRasterizer.UseNonZeroWinding = true

View file

@ -4,7 +4,7 @@
package draw2d package draw2d
// PathBuilder define method that create path // PathBuilder define method that create path
type Path interface { type PathBuilder interface {
// Return the current point of the current path // Return the current point of the current path
LastPoint() (x, y float64) LastPoint() (x, y float64)

View file

@ -42,7 +42,7 @@ func NewPathAdder(adder raster.Adder) *PathAdder {
return &PathAdder{adder, raster.Point{0, 0}, 1} return &PathAdder{adder, raster.Point{0, 0}, 1}
} }
func (pathAdder *PathAdder) Convert(paths ...*PathStorage) { func (pathAdder *PathAdder) Convert(paths ...*Path) {
for _, path := range paths { for _, path := range paths {
j := 0 j := 0
for _, cmd := range path.commands { for _, cmd := range path.commands {

View file

@ -18,7 +18,7 @@ func NewPathConverter(converter LineBuilder) *PathConverter {
return &PathConverter{converter, 1, 0, 0, 0, 0} return &PathConverter{converter, 1, 0, 0, 0, 0}
} }
func (c *PathConverter) Convert(paths ...*PathStorage) { func (c *PathConverter) Convert(paths ...*Path) {
for _, path := range paths { for _, path := range paths {
i := 0 i := 0
for _, cmd := range path.commands { for _, cmd := range path.commands {

View file

@ -19,32 +19,32 @@ const (
Close Close
) )
type PathStorage struct { type Path struct {
commands []PathCmd commands []PathCmd
vertices []float64 vertices []float64
x, y float64 x, y float64
} }
func NewPathStorage() (p *PathStorage) { func NewPathStorage() (p *Path) {
p = new(PathStorage) p = new(Path)
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 *Path) 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 *Path) appendToPath(cmd PathCmd, vertices ...float64) {
p.commands = append(p.commands, cmd) p.commands = append(p.commands, cmd)
p.vertices = append(p.vertices, vertices...) p.vertices = append(p.vertices, vertices...)
} }
func (src *PathStorage) Copy() (dest *PathStorage) { func (src *Path) Copy() (dest *Path) {
dest = new(PathStorage) dest = new(Path)
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))
@ -52,31 +52,31 @@ func (src *PathStorage) Copy() (dest *PathStorage) {
return dest return dest
} }
func (p *PathStorage) LastPoint() (x, y float64) { func (p *Path) LastPoint() (x, y float64) {
return p.x, p.y return p.x, p.y
} }
func (p *PathStorage) IsEmpty() bool { func (p *Path) IsEmpty() bool {
return len(p.commands) == 0 return len(p.commands) == 0
} }
func (p *PathStorage) Close() { func (p *Path) Close() {
p.appendToPath(Close) p.appendToPath(Close)
} }
func (p *PathStorage) MoveTo(x, y float64) { func (p *Path) MoveTo(x, y float64) {
p.appendToPath(MoveTo, x, y) p.appendToPath(MoveTo, x, y)
p.x = x p.x = x
p.y = y p.y = y
} }
func (p *PathStorage) RMoveTo(dx, dy float64) { func (p *Path) RMoveTo(dx, dy float64) {
x, y := p.LastPoint() x, y := p.LastPoint()
p.MoveTo(x+dx, y+dy) p.MoveTo(x+dx, y+dy)
} }
func (p *PathStorage) LineTo(x, y float64) { func (p *Path) LineTo(x, y float64) {
if len(p.commands) == 0 { //special case when no move has been done if len(p.commands) == 0 { //special case when no move has been done
p.MoveTo(0, 0) p.MoveTo(0, 0)
} }
@ -85,12 +85,12 @@ func (p *PathStorage) LineTo(x, y float64) {
p.y = y p.y = y
} }
func (p *PathStorage) RLineTo(dx, dy float64) { func (p *Path) RLineTo(dx, dy float64) {
x, y := p.LastPoint() x, y := p.LastPoint()
p.LineTo(x+dx, y+dy) p.LineTo(x+dx, y+dy)
} }
func (p *PathStorage) QuadCurveTo(cx, cy, x, y float64) { func (p *Path) QuadCurveTo(cx, cy, x, y float64) {
if len(p.commands) == 0 { //special case when no move has been done if len(p.commands) == 0 { //special case when no move has been done
p.MoveTo(0, 0) p.MoveTo(0, 0)
} }
@ -99,12 +99,12 @@ func (p *PathStorage) QuadCurveTo(cx, cy, x, y float64) {
p.y = y p.y = y
} }
func (p *PathStorage) RQuadCurveTo(dcx, dcy, dx, dy float64) { func (p *Path) RQuadCurveTo(dcx, dcy, dx, dy float64) {
x, y := p.LastPoint() x, y := p.LastPoint()
p.QuadCurveTo(x+dcx, y+dcy, x+dx, y+dy) p.QuadCurveTo(x+dcx, y+dcy, x+dx, y+dy)
} }
func (p *PathStorage) CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64) { func (p *Path) CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64) {
if len(p.commands) == 0 { //special case when no move has been done if len(p.commands) == 0 { //special case when no move has been done
p.MoveTo(0, 0) p.MoveTo(0, 0)
} }
@ -113,12 +113,12 @@ func (p *PathStorage) CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64) {
p.y = y p.y = y
} }
func (p *PathStorage) RCubicCurveTo(dcx1, dcy1, dcx2, dcy2, dx, dy float64) { func (p *Path) RCubicCurveTo(dcx1, dcy1, dcx2, dcy2, dx, dy float64) {
x, y := p.LastPoint() x, y := p.LastPoint()
p.CubicCurveTo(x+dcx1, y+dcy1, x+dcx2, y+dcy2, x+dx, y+dy) p.CubicCurveTo(x+dcx1, y+dcy1, x+dcx2, y+dcy2, x+dx, y+dy)
} }
func (p *PathStorage) ArcTo(cx, cy, rx, ry, startAngle, angle float64) { func (p *Path) ArcTo(cx, cy, rx, ry, startAngle, angle float64) {
endAngle := startAngle + angle endAngle := startAngle + angle
clockWise := true clockWise := true
if angle < 0 { if angle < 0 {
@ -146,12 +146,12 @@ func (p *PathStorage) ArcTo(cx, cy, rx, ry, startAngle, angle float64) {
p.y = cy + math.Sin(endAngle)*ry p.y = cy + math.Sin(endAngle)*ry
} }
func (p *PathStorage) RArcTo(dcx, dcy, rx, ry, startAngle, angle float64) { func (p *Path) RArcTo(dcx, dcy, rx, ry, startAngle, angle float64) {
x, y := p.LastPoint() x, y := p.LastPoint()
p.ArcTo(x+dcx, y+dcy, rx, ry, startAngle, angle) p.ArcTo(x+dcx, y+dcy, rx, ry, startAngle, angle)
} }
func (p *PathStorage) String() string { func (p *Path) String() string {
s := "" s := ""
j := 0 j := 0
for _, cmd := range p.commands { for _, cmd := range p.commands {

View file

@ -16,7 +16,7 @@ type StackGraphicContext struct {
type ContextStack struct { type ContextStack struct {
Tr MatrixTransform Tr MatrixTransform
Path *PathStorage Path *Path
LineWidth float64 LineWidth float64
Dash []float64 Dash []float64
DashOffset float64 DashOffset float64