diff --git a/draw2d/arc.go b/draw2d/arc.go index ec184e8..69db1ea 100644 --- a/draw2d/arc.go +++ b/draw2d/arc.go @@ -55,13 +55,13 @@ func arcAdder(adder raster.Adder, x, y, rx, ry, start, angle, scale float64) ras if (angle < end-da/4) != clockWise { curX = x + math.Cos(end)*rx curY = y + math.Sin(end)*ry - return floatToPoint(curX, curY) + return raster.Point{raster.Fix32(curX * 256), raster.Fix32(curY * 256)} } curX = x + math.Cos(angle)*rx curY = y + math.Sin(angle)*ry angle += da - adder.Add1(floatToPoint(curX, curY)) + adder.Add1(raster.Point{raster.Fix32(curX * 256), raster.Fix32(curY * 256)}) } - return floatToPoint(curX, curY) + return raster.Point{raster.Fix32(curX * 256), raster.Fix32(curY * 256)} } diff --git a/draw2d/image.go b/draw2d/image.go index 829df46..4a7ea57 100644 --- a/draw2d/image.go +++ b/draw2d/image.go @@ -139,7 +139,7 @@ func (gc *ImageGraphicContext) Stroke2(paths ...*PathStorage) { pathConverter.Convert(paths...) mta := NewMatrixTransformAdder(gc.Current.Tr, gc.strokeRasterizer) - raster.Stroke(mta, *rasterPath, raster.Fix32(gc.Current.LineWidth*256), gc.Current.Cap.capper(), gc.Current.Join.joiner()) + raster.Stroke(mta, *rasterPath, raster.Fix32(gc.Current.LineWidth*256), gc.Current.Cap.Convert(), gc.Current.Join.Convert()) gc.paint(gc.strokeRasterizer, gc.Current.StrokeColor) } @@ -204,7 +204,7 @@ func (gc *ImageGraphicContext) FillStroke2(paths ...*PathStorage) { pathConverter.Convert(paths...) mta := NewMatrixTransformAdder(gc.Current.Tr, gc.strokeRasterizer) - raster.Stroke(mta, *rasterPath, raster.Fix32(gc.Current.LineWidth*256), gc.Current.Cap.capper(), gc.Current.Join.joiner()) + raster.Stroke(mta, *rasterPath, raster.Fix32(gc.Current.LineWidth*256), gc.Current.Cap.Convert(), gc.Current.Join.Convert()) gc.paint(gc.fillRasterizer, gc.Current.FillColor) gc.paint(gc.strokeRasterizer, gc.Current.StrokeColor) @@ -241,7 +241,7 @@ func (f FillRule) UseNonZeroWinding() bool { return false } -func (c Cap) capper() raster.Capper { +func (c Cap) Convert() raster.Capper { switch c { case RoundCap: return raster.RoundCapper @@ -253,7 +253,7 @@ func (c Cap) capper() raster.Capper { return raster.RoundCapper } -func (j Join) joiner() raster.Joiner { +func (j Join) Convert() raster.Joiner { switch j { case RoundJoin: return raster.RoundJoiner diff --git a/draw2d/path_adder.go b/draw2d/path_adder.go index d5a7de9..63fdba5 100644 --- a/draw2d/path_adder.go +++ b/draw2d/path_adder.go @@ -9,16 +9,10 @@ import ( type VertexAdder struct { - command VertexCommand - adder raster.Adder + command VertexCommand + adder raster.Adder } - -func floatToPoint(x, y float64) raster.Point { - return raster.Point{raster.Fix32(x * 256), raster.Fix32(y * 256)} -} - - func NewVertexAdder(adder raster.Adder) *VertexAdder { return &VertexAdder{VertexNoCommand, adder} } @@ -40,7 +34,7 @@ func (vertexAdder *VertexAdder) Vertex(x, y float64) { type PathAdder struct { adder raster.Adder - lastPoint raster.Point + firstPoint raster.Point ApproximationScale float64 } @@ -53,37 +47,29 @@ func (pathAdder *PathAdder) Convert(paths ...*PathStorage) { for _, path := range paths { j := 0 for _, cmd := range path.commands { - j = j + pathAdder.ConvertCommand(cmd, path.vertices[j:]...) + switch cmd { + case MoveTo: + 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)}) + 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)}) + 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)}) + 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) + pathAdder.adder.Add1(lastPoint) + j += 6 + case Close: + pathAdder.adder.Add1(pathAdder.firstPoint) + } } } } -func (pathAdder *PathAdder) ConvertCommand(cmd PathCmd, vertices ...float64) int { - switch cmd { - case MoveTo: - pathAdder.lastPoint = floatToPoint(vertices[0], vertices[1]) - pathAdder.adder.Start(pathAdder.lastPoint) - return 2 - case LineTo: - pathAdder.lastPoint = floatToPoint(vertices[0], vertices[1]) - pathAdder.adder.Add1(pathAdder.lastPoint) - return 2 - case QuadCurveTo: - pathAdder.lastPoint = floatToPoint(vertices[2], vertices[3]) - pathAdder.adder.Add2(floatToPoint(vertices[0], vertices[1]), pathAdder.lastPoint) - return 4 - case CubicCurveTo: - pathAdder.lastPoint = floatToPoint(vertices[4], vertices[5]) - pathAdder.adder.Add3(floatToPoint(vertices[0], vertices[1]), floatToPoint(vertices[2], vertices[3]), pathAdder.lastPoint) - return 6 - case ArcTo: - pathAdder.lastPoint = arcAdder(pathAdder.adder, vertices[0], vertices[1], vertices[2], vertices[3], vertices[4], vertices[5], pathAdder.ApproximationScale) - pathAdder.adder.Add1(pathAdder.lastPoint) - return 6 - case Close: - pathAdder.adder.Add1(pathAdder.lastPoint) - return 0 - } - return 0 -} diff --git a/draw2d/transform.go b/draw2d/transform.go index 865732a..d14ed4c 100644 --- a/draw2d/transform.go +++ b/draw2d/transform.go @@ -26,6 +26,15 @@ func (tr MatrixTransform) Transform(points ...*float64) { } } +func (tr MatrixTransform) TransformArray(points []float64) { + for i, j := 0, 1; j < len(points); i, j = i+2, j+2 { + x := points[i] + y := points[j] + points[i] = x*tr[0] + y*tr[2] + tr[4] + points[j] = x*tr[1] + y*tr[3] + tr[5] + } +} + func (tr MatrixTransform) TransformRectangle(x0, y0, x2, y2 *float64) { x1 := *x2 y1 := *y0 diff --git a/draw2dgl/gc.go b/draw2dgl/gc.go index 3bd5aeb..50a0507 100644 --- a/draw2dgl/gc.go +++ b/draw2dgl/gc.go @@ -110,6 +110,7 @@ func NewGLPainter() *GLPainter { return p } + type GraphicContext struct { *draw2d.StackGraphicContext painter *GLPainter @@ -117,6 +118,23 @@ type GraphicContext struct { strokeRasterizer *raster.Rasterizer } +type GLVertex struct { + x, y float64 +} + + +func NewGLVertex() *GLVertex { + return &GLVertex{} +} + +func (glVertex *GLVertex) NextCommand(cmd draw2d.VertexCommand) { + +} + +func (glVertex *GLVertex) Vertex(x, y float64) { + gl.Vertex2d(x, y) +} + /** * Create a new Graphic context from an image */ @@ -150,7 +168,6 @@ func (gc *GraphicContext) DrawImage(img image.Image) { } - func (gc *GraphicContext) FillString(text string) (cursor float64) { return 0 } @@ -163,7 +180,7 @@ func (gc *GraphicContext) paint(rasterizer *raster.Rasterizer, color image.Color gc.painter.Flush() } -func (gc *GraphicContext) Stroke(paths ...*draw2d.PathStorage) { +func (gc *GraphicContext) Stroke(paths ...*draw2d.PathStorage){ paths = append(paths, gc.Current.Path) gc.strokeRasterizer.UseNonZeroWinding = true @@ -194,6 +211,19 @@ func (gc *GraphicContext) Fill(paths ...*draw2d.PathStorage) { gc.paint(gc.fillRasterizer, gc.Current.FillColor) gc.Current.Path.Clear() } +/* +func (gc *GraphicContext) Fill(paths ...*draw2d.PathStorage) { + paths = append(paths, gc.Current.Path) + gc.fillRasterizer.UseNonZeroWinding = gc.Current.FillRule.UseNonZeroWinding() + + pathConverter := draw2d.NewPathAdder(draw2d.NewMatrixTransformAdder(gc.Current.Tr, gc.fillRasterizer)) + pathConverter.ApproximationScale = gc.Current.Tr.GetScale() + pathConverter.Convert(paths...) + + gc.paint(gc.fillRasterizer, gc.Current.FillColor) + gc.Current.Path.Clear() +} +*/ func (gc *GraphicContext) FillStroke(paths ...*draw2d.PathStorage) { gc.fillRasterizer.UseNonZeroWinding = gc.Current.FillRule.UseNonZeroWinding()