Start path package

This commit is contained in:
Laurent Le Goff 2015-04-27 12:16:18 +02:00
parent d6812fd8e6
commit 61a6e03fdb
4 changed files with 26 additions and 22 deletions

9
gc.go
View file

@ -4,6 +4,7 @@
package draw2d package draw2d
import ( import (
"github.com/llgcode/draw2d/path"
"image" "image"
"image/color" "image/color"
) )
@ -16,7 +17,7 @@ const (
) )
type GraphicContext interface { type GraphicContext interface {
PathBuilder path.PathBuilder
// Create a new path // Create a new path
BeginPath() BeginPath()
GetMatrixTransform() MatrixTransform GetMatrixTransform() MatrixTransform
@ -49,7 +50,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 ...*Path) Stroke(paths ...*path.Path)
Fill(paths ...*Path) Fill(paths ...*path.Path)
FillStroke(paths ...*Path) FillStroke(paths ...*path.Path)
} }

View file

@ -5,6 +5,7 @@ package draw2d
import ( import (
"errors" "errors"
"github.com/llgcode/draw2d/path"
"image" "image"
"image/color" "image/color"
"image/draw" "image/draw"
@ -275,7 +276,7 @@ func (gc *ImageGraphicContext) paint(rasterizer *raster.Rasterizer, color color.
} }
/**** second method ****/ /**** second method ****/
func (gc *ImageGraphicContext) Stroke(paths ...*Path) { func (gc *ImageGraphicContext) Stroke(paths ...*path.Path) {
paths = append(paths, gc.Current.Path) paths = append(paths, gc.Current.Path)
gc.strokeRasterizer.UseNonZeroWinding = true gc.strokeRasterizer.UseNonZeroWinding = true
@ -295,7 +296,7 @@ func (gc *ImageGraphicContext) Stroke(paths ...*Path) {
} }
/**** second method ****/ /**** second method ****/
func (gc *ImageGraphicContext) Fill(paths ...*Path) { func (gc *ImageGraphicContext) Fill(paths ...*path.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 +309,7 @@ func (gc *ImageGraphicContext) Fill(paths ...*Path) {
} }
/* second method */ /* second method */
func (gc *ImageGraphicContext) FillStroke(paths ...*Path) { func (gc *ImageGraphicContext) FillStroke(paths ...*path.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

@ -5,6 +5,7 @@ package draw2d
import ( import (
"code.google.com/p/freetype-go/freetype/raster" "code.google.com/p/freetype-go/freetype/raster"
"github.com/llgcode/draw2d/path"
) )
type VertexAdder struct { type VertexAdder struct {
@ -42,29 +43,29 @@ 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 ...*Path) { func (pathAdder *PathAdder) Convert(paths ...*path.Path) {
for _, path := range paths { for _, apath := range paths {
j := 0 j := 0
for _, cmd := range path.commands { for _, cmd := range apath.Components {
switch cmd { switch cmd {
case MoveTo: case path.MoveToCmp:
pathAdder.firstPoint = raster.Point{raster.Fix32(path.vertices[j] * 256), raster.Fix32(path.vertices[j+1] * 256)} pathAdder.firstPoint = raster.Point{raster.Fix32(apath.Points[j] * 256), raster.Fix32(apath.Points[j+1] * 256)}
pathAdder.adder.Start(pathAdder.firstPoint) pathAdder.adder.Start(pathAdder.firstPoint)
j += 2 j += 2
case LineTo: case path.LineToCmp:
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(apath.Points[j] * 256), raster.Fix32(apath.Points[j+1] * 256)})
j += 2 j += 2
case QuadCurveTo: case path.QuadCurveToCmp:
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(apath.Points[j] * 256), raster.Fix32(apath.Points[j+1] * 256)}, raster.Point{raster.Fix32(apath.Points[j+2] * 256), raster.Fix32(apath.Points[j+3] * 256)})
j += 4 j += 4
case CubicCurveTo: case path.CubicCurveToCmp:
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(apath.Points[j] * 256), raster.Fix32(apath.Points[j+1] * 256)}, raster.Point{raster.Fix32(apath.Points[j+2] * 256), raster.Fix32(apath.Points[j+3] * 256)}, raster.Point{raster.Fix32(apath.Points[j+4] * 256), raster.Fix32(apath.Points[j+5] * 256)})
j += 6 j += 6
case ArcTo: case path.ArcToCmp:
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, apath.Points[j], apath.Points[j+1], apath.Points[j+2], apath.Points[j+3], apath.Points[j+4], apath.Points[j+5], pathAdder.ApproximationScale)
pathAdder.adder.Add1(lastPoint) pathAdder.adder.Add1(lastPoint)
j += 6 j += 6
case Close: case path.CloseCmp:
pathAdder.adder.Add1(pathAdder.firstPoint) pathAdder.adder.Add1(pathAdder.firstPoint)
} }
} }

View file

@ -4,6 +4,7 @@
package draw2d package draw2d
import ( import (
"github.com/llgcode/draw2d/path"
"image" "image"
"image/color" "image/color"
@ -16,7 +17,7 @@ type StackGraphicContext struct {
type ContextStack struct { type ContextStack struct {
Tr MatrixTransform Tr MatrixTransform
Path *Path Path *path.Path
LineWidth float64 LineWidth float64
Dash []float64 Dash []float64
DashOffset float64 DashOffset float64