diff --git a/draw2dpdf/gc.go b/draw2dpdf/gc.go index b81bdbe..3d7cc5f 100644 --- a/draw2dpdf/gc.go +++ b/draw2dpdf/gc.go @@ -190,7 +190,9 @@ func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (cursor floa // Stroke strokes the paths with the color specified by SetStrokeColor func (gc *GraphicContext) Stroke(paths ...*draw2d.PathStorage) { - gc.draw("D", paths...) + _, _, _, alphaS := gc.Current.StrokeColor.RGBA() + gc.draw("D", alphaS, paths...) + gc.Current.Path = draw2d.NewPathStorage() } // Fill fills the paths with the color specified by SetFillColor @@ -199,27 +201,43 @@ func (gc *GraphicContext) Fill(paths ...*draw2d.PathStorage) { if !gc.Current.FillRule.UseNonZeroWinding() { style += "*" } - gc.draw(style, paths...) + _, _, _, alphaF := gc.Current.FillColor.RGBA() + gc.draw(style, alphaF, paths...) + gc.Current.Path = draw2d.NewPathStorage() } // FillStroke first fills the paths and than strokes them func (gc *GraphicContext) FillStroke(paths ...*draw2d.PathStorage) { - style := "FD" + var rule string if !gc.Current.FillRule.UseNonZeroWinding() { - style += "*" + rule = "*" } - gc.draw(style, paths...) + _, _, _, alphaS := gc.Current.StrokeColor.RGBA() + _, _, _, alphaF := gc.Current.FillColor.RGBA() + if alphaS == alphaF { + gc.draw("FD"+rule, alphaF, paths...) + } else { + gc.draw("F"+rule, alphaF, paths...) + gc.draw("S", alphaS, paths...) + } + gc.Current.Path = draw2d.NewPathStorage() } var logger = log.New(os.Stdout, "", log.Lshortfile) +const alphaMax = float64(0xFFFF) + // draw fills and/or strokes paths -func (gc *GraphicContext) draw(style string, paths ...*draw2d.PathStorage) { +func (gc *GraphicContext) draw(style string, alpha uint32, paths ...*draw2d.PathStorage) { paths = append(paths, gc.Current.Path) pathConverter := NewPathConverter(gc.pdf) pathConverter.Convert(paths...) + a := float64(alpha) / alphaMax + current, blendMode := gc.pdf.GetAlpha() + if a != current { + gc.pdf.SetAlpha(a, blendMode) + } gc.pdf.DrawPath(style) - gc.Current.Path = draw2d.NewPathStorage() } // overwrite StackGraphicContext methods diff --git a/output/samples/geometry.png b/output/samples/geometry.png index 99494ff..c4ae6e3 100644 Binary files a/output/samples/geometry.png and b/output/samples/geometry.png differ diff --git a/resource/image/geometry.pdf b/resource/image/geometry.pdf index 6c5c1bc..755e4ea 100644 Binary files a/resource/image/geometry.pdf and b/resource/image/geometry.pdf differ diff --git a/samples/geometry/geometry.go b/samples/geometry/geometry.go index 8fb105f..6e96e20 100644 --- a/samples/geometry/geometry.go +++ b/samples/geometry/geometry.go @@ -80,7 +80,6 @@ func CurveRectangle(gc draw2d.GraphicContext, x0, y0, } } gc.Close() - gc.SetStrokeColor(stroke) gc.SetFillColor(fill) gc.SetLineWidth(10.0) @@ -125,9 +124,8 @@ func Arc(gc draw2d.GraphicContext, xc, yc, width, height float64) { gc.SetFillColor(color.NRGBA{255, 0x33, 0x33, 0x80}) gc.SetLineWidth(width / 20) - gc.MoveTo(xc, yc) - gc.LineTo(xc+math.Cos(startAngle)*radiusX, yc+math.Sin(startAngle)*radiusY) - gc.MoveTo(xc, yc) + gc.MoveTo(xc+math.Cos(startAngle)*radiusX, yc+math.Sin(startAngle)*radiusY) + gc.LineTo(xc, yc) gc.LineTo(xc-radiusX, yc) gc.Stroke() @@ -154,9 +152,8 @@ func ArcNegative(gc draw2d.GraphicContext, xc, yc, width, height float64) { gc.SetFillColor(color.NRGBA{255, 0x33, 0x33, 0x80}) gc.SetLineWidth(width / 20) - gc.MoveTo(xc, yc) - gc.LineTo(xc+math.Cos(startAngle)*radiusX, yc+math.Sin(startAngle)*radiusY) - gc.MoveTo(xc, yc) + gc.MoveTo(xc+math.Cos(startAngle)*radiusX, yc+math.Sin(startAngle)*radiusY) + gc.LineTo(xc, yc) gc.LineTo(xc-radiusX, yc) gc.Stroke() @@ -205,7 +202,7 @@ func FillString(gc draw2d.GraphicContext, x, y, width, height float64) { Name: "luxi", Family: draw2d.FontFamilyMono, Style: draw2d.FontStyleBold | draw2d.FontStyleItalic}) - w := gc.FillString("cou") + w := gc.FillString("Hug") gc.Translate(w+sx, 0) left, top, right, bottom := gc.GetStringBounds("cou") gc.SetStrokeColor(color.NRGBA{255, 0x33, 0x33, 0x80}) @@ -215,7 +212,7 @@ func FillString(gc draw2d.GraphicContext, x, y, width, height float64) { gc.SetFillColor(color.NRGBA{0x33, 0x33, 0xff, 0xff}) gc.SetStrokeColor(color.NRGBA{0x33, 0x33, 0xff, 0xff}) gc.SetLineWidth(height / 100) - gc.StrokeString("cou") + gc.StrokeString("Hug") gc.Restore() } diff --git a/samples/gopher2/gopher2.go b/samples/gopher2/gopher2.go index 012c3ca..232aa03 100644 --- a/samples/gopher2/gopher2.go +++ b/samples/gopher2/gopher2.go @@ -21,7 +21,7 @@ func Main(gc draw2d.GraphicContext, ext string) (string, error) { gc.SetFillColor(image.White) gc.Save() // Draw a (partial) gopher - gc.Translate(-65, 65) + gc.Translate(-60, 65) gc.Rotate(-30 * (math.Pi / 180.0)) Draw(gc, 48, 48, 240, 72) gc.Restore() @@ -41,13 +41,13 @@ func Draw(gc draw2d.GraphicContext, x, y, w, h float64) { brb := color.RGBA{0x8B, 0x45, 0x13, 0xBB} // brown transparant // round head top - gc.MoveTo(x, y+h) - gc.CubicCurveTo(x, y+h*1.05, x+w/2, y-h, x+w, y+h*1.05) + gc.MoveTo(x, y+h*1.002) + gc.CubicCurveTo(x+w/4, y-h/3, x+3*w/4, y-h/3, x+w, y+h*1.002) gc.Close() gc.SetFillColor(brb) gc.Fill() // rectangle head bottom - draw2d.RoundRect(gc, x, y+h, x+w, y+h+h, w/5, h/5) + draw2d.RoundRect(gc, x, y+h, x+w, y+h+h, h/5, h/5) gc.Fill() // left ear outside draw2d.Circle(gc, x, y+h, w/12)