From 6d31bfac59e8bff86014f7d458ee1a8ace77b92a Mon Sep 17 00:00:00 2001 From: Drahoslav Date: Sun, 24 Dec 2017 16:05:15 +0100 Subject: [PATCH] Use fill-rule attribute in svg context --- draw2dsvg/converters.go | 7 +++++++ draw2dsvg/gc.go | 16 +++++++++++----- draw2dsvg/svg.go | 4 +++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/draw2dsvg/converters.go b/draw2dsvg/converters.go index 1564f96..7b5c6a3 100644 --- a/draw2dsvg/converters.go +++ b/draw2dsvg/converters.go @@ -28,6 +28,13 @@ func toSvgArray(nums []float64) string { return strings.Join(arr, ",") } +func toSvgFillRule(rule draw2d.FillRule) string { + return map[draw2d.FillRule]string{ + draw2d.FillRuleEvenOdd: "evenodd", + draw2d.FillRuleWinding: "nonzero", + }[rule] +} + func toSvgPathDesc(p *draw2d.Path) string { parts := make([]string, len(p.Components)) ps := p.Points diff --git a/draw2dsvg/gc.go b/draw2dsvg/gc.go index 1d741b5..3ef6adb 100644 --- a/draw2dsvg/gc.go +++ b/draw2dsvg/gc.go @@ -8,6 +8,7 @@ import ( "github.com/llgcode/draw2d" "github.com/llgcode/draw2d/draw2dbase" "image" + "strings" ) const () @@ -71,15 +72,17 @@ func (gc *GraphicContext) FillStroke(paths ...*draw2d.Path) { func (gc *GraphicContext) drawPaths(drawType drawType, paths ...*draw2d.Path) { paths = append(paths, gc.Current.Path) - svgPaths := make([]Path, len(paths)) + svgPath := Path{} + group := Group{} - group := Group{ - Paths: svgPaths, - } + svgPathsDesc := make([]string, len(paths)) + // multiple pathes has to be joined to single svg path description + // because fill-rule wont work for whole group for i, path := range paths { - svgPaths[i].Desc = toSvgPathDesc(path) + svgPathsDesc[i] = toSvgPathDesc(path) } + svgPath.Desc = strings.Join(svgPathsDesc, " ") if drawType&stroked == stroked { group.Stroke = toSvgRGBA(gc.Current.StrokeColor) @@ -94,8 +97,11 @@ func (gc *GraphicContext) drawPaths(drawType drawType, paths ...*draw2d.Path) { if drawType&filled == filled { group.Fill = toSvgRGBA(gc.Current.FillColor) + group.FillRule = toSvgFillRule(gc.Current.FillRule) } + group.Paths = []Path{svgPath} + gc.svg.Groups = append(gc.svg.Groups, group) } diff --git a/draw2dsvg/svg.go b/draw2dsvg/svg.go index 025847a..ec16e1b 100644 --- a/draw2dsvg/svg.go +++ b/draw2dsvg/svg.go @@ -37,7 +37,9 @@ type Text struct { /* shared attrs */ type FillStroke struct { - Fill string `xml:"fill,attr,omitempty"` + Fill string `xml:"fill,attr,omitempty"` + FillRule string `xml:"fill-rule,attr,omitempty"` + Stroke string `xml:"stroke,attr,omitempty"` StrokeWidth string `xml:"stroke-width,attr,omitempty"` StrokeLinecap string `xml:"stroke-linecap,attr,omitempty"`