From a826fc72168978c1057d2f1e7cb9c9c10bcc1145 Mon Sep 17 00:00:00 2001 From: Stani Date: Sat, 11 Jul 2015 01:12:09 +0200 Subject: [PATCH 1/3] fix path drawing --- draw2dpdf/gc.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/draw2dpdf/gc.go b/draw2dpdf/gc.go index 50a7137..51c0fd5 100644 --- a/draw2dpdf/gc.go +++ b/draw2dpdf/gc.go @@ -44,8 +44,13 @@ var ( // a page and set fill color to white. func NewPdf(orientationStr, unitStr, sizeStr string) *gofpdf.Fpdf { pdf := gofpdf.New(orientationStr, unitStr, sizeStr, draw2d.GetFontFolder()) + // to be compatible with draw2d + pdf.SetDrawColor(0, 0, 0) + pdf.SetFillColor(255, 255, 255) + pdf.SetLineCapStyle("round") + pdf.SetLineJoinStyle("round") + pdf.SetLineWidth(1) pdf.AddPage() - pdf.SetFillColor(255, 255, 255) // to be compatible with draw2d return pdf } @@ -176,12 +181,20 @@ func (gc *GraphicContext) Stroke(paths ...*draw2d.PathStorage) { // Fill fills the paths with the color specified by SetFillColor func (gc *GraphicContext) Fill(paths ...*draw2d.PathStorage) { - gc.draw("F", paths...) + style := "F" + if !gc.Current.FillRule.UseNonZeroWinding() { + style += "*" + } + gc.draw(style, paths...) } // FillStroke first fills the paths and than strokes them func (gc *GraphicContext) FillStroke(paths ...*draw2d.PathStorage) { - gc.draw("FD", paths...) + style := "FD" + if !gc.Current.FillRule.UseNonZeroWinding() { + style += "*" + } + gc.draw(style, paths...) } var logger = log.New(os.Stdout, "", log.Lshortfile) @@ -190,12 +203,9 @@ var logger = log.New(os.Stdout, "", log.Lshortfile) func (gc *GraphicContext) draw(style string, paths ...*draw2d.PathStorage) { paths = append(paths, gc.Current.Path) pathConverter := NewPathConverter(gc.pdf) - // pathConverter := NewPathConverter(NewPathLogger(logger, gc.pdf)) pathConverter.Convert(paths...) - if gc.Current.FillRule.UseNonZeroWinding() { - style += "*" - } gc.pdf.DrawPath(style) + gc.Current.Path.Clear() } // overwrite StackGraphicContext methods From 781a0defe5066768ac26705897251183c299e6f2 Mon Sep 17 00:00:00 2001 From: Stani Date: Sat, 11 Jul 2015 01:13:39 +0200 Subject: [PATCH 2/3] previous fix makes Save and Restore for line caps join unnecessary --- samples/linecapjoin/linecapjoin.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/samples/linecapjoin/linecapjoin.go b/samples/linecapjoin/linecapjoin.go index 6acf659..81d78f0 100644 --- a/samples/linecapjoin/linecapjoin.go +++ b/samples/linecapjoin/linecapjoin.go @@ -31,7 +31,6 @@ func Main(gc draw2d.GraphicContext, ext string) (string, error) { // Draw a line with an angle with specified line cap and join func Draw(gc draw2d.GraphicContext, cap draw2d.Cap, join draw2d.Join, x0, y0, x1, y1, offset float64) { - gc.Save() // pdf: save & restore needed to isolate caps and joins gc.SetLineCap(cap) gc.SetLineJoin(join) @@ -50,5 +49,4 @@ func Draw(gc draw2d.GraphicContext, cap draw2d.Cap, join draw2d.Join, gc.LineTo((x0+x1)/2+offset, (y0+y1)/2) gc.LineTo(x1, y1) gc.Stroke() - gc.Restore() } From 08a6c87a0be561f1d6db6a0831cd55168a58dca8 Mon Sep 17 00:00:00 2001 From: Stani Date: Sat, 11 Jul 2015 01:22:50 +0200 Subject: [PATCH 3/3] replace gc.Current.Path.Clear() with gc.Current.Path = draw2d.NewPathStorage(); fixes #61 --- draw2dgl/gc.go | 4 ++-- draw2dpdf/gc.go | 2 +- image.go | 2 +- path_storage.go | 6 ------ stack_gc.go | 2 +- 5 files changed, 5 insertions(+), 11 deletions(-) diff --git a/draw2dgl/gc.go b/draw2dgl/gc.go index 2833dcc..66e25cb 100644 --- a/draw2dgl/gc.go +++ b/draw2dgl/gc.go @@ -199,7 +199,7 @@ func (gc *GraphicContext) Stroke(paths ...*draw2d.PathStorage) { pathConverter.Convert(paths...) gc.paint(gc.strokeRasterizer, gc.Current.StrokeColor) - gc.Current.Path.Clear() + gc.Current.Path = draw2d.NewPathStorage() } func (gc *GraphicContext) Fill(paths ...*draw2d.PathStorage) { @@ -211,7 +211,7 @@ func (gc *GraphicContext) Fill(paths ...*draw2d.PathStorage) { pathConverter.Convert(paths...) gc.paint(gc.fillRasterizer, gc.Current.FillColor) - gc.Current.Path.Clear() + gc.Current.Path = draw2d.NewPathStorage() } func (gc *GraphicContext) FillStroke(paths ...*draw2d.PathStorage) { diff --git a/draw2dpdf/gc.go b/draw2dpdf/gc.go index 51c0fd5..fd9563a 100644 --- a/draw2dpdf/gc.go +++ b/draw2dpdf/gc.go @@ -205,7 +205,7 @@ func (gc *GraphicContext) draw(style string, paths ...*draw2d.PathStorage) { pathConverter := NewPathConverter(gc.pdf) pathConverter.Convert(paths...) gc.pdf.DrawPath(style) - gc.Current.Path.Clear() + gc.Current.Path = draw2d.NewPathStorage() } // overwrite StackGraphicContext methods diff --git a/image.go b/image.go index bc4d403..8e5fa72 100644 --- a/image.go +++ b/image.go @@ -269,7 +269,7 @@ func (gc *ImageGraphicContext) paint(rasterizer *raster.Rasterizer, color color. gc.painter.SetColor(color) rasterizer.Rasterize(gc.painter) rasterizer.Clear() - gc.Current.Path.Clear() + gc.Current.Path = NewPathStorage() } // Stroke strokes the paths with the color specified by SetStrokeColor diff --git a/path_storage.go b/path_storage.go index a7e1662..b19ea3d 100644 --- a/path_storage.go +++ b/path_storage.go @@ -32,12 +32,6 @@ func NewPathStorage() (p *PathStorage) { return } -func (p *PathStorage) Clear() { - 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) diff --git a/stack_gc.go b/stack_gc.go index c1ac309..1e09e8f 100644 --- a/stack_gc.go +++ b/stack_gc.go @@ -125,7 +125,7 @@ func (gc *StackGraphicContext) GetFontData() FontData { } func (gc *StackGraphicContext) BeginPath() { - gc.Current.Path.Clear() + gc.Current.Path = NewPathStorage() } func (gc *StackGraphicContext) IsEmpty() bool {