From 6c0a15c624164853af39eee1ada0ff715d1a774a Mon Sep 17 00:00:00 2001 From: Drahoslav Date: Sun, 24 Dec 2017 12:32:29 +0100 Subject: [PATCH] Use line-{width,cap,join} attributes in svg context --- draw2d.go | 16 ++++++++++++++++ draw2dsvg/gc.go | 13 ++++++++++++- draw2dsvg/svg.go | 3 +++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/draw2d.go b/draw2d.go index 92a7344..5905a4d 100644 --- a/draw2d.go +++ b/draw2d.go @@ -128,6 +128,14 @@ const ( SquareCap ) +func (cap LineCap) String() string { + return map[LineCap]string{ + RoundCap: "round", + ButtCap: "cap", + SquareCap: "square", + }[cap] +} + // LineJoin is the style of segments joint type LineJoin int @@ -140,6 +148,14 @@ const ( MiterJoin ) +func (join LineJoin) String() string { + return map[LineJoin]string{ + RoundJoin: "round", + BevelJoin: "bevel", + MiterJoin: "miter", + }[join] +} + // StrokeStyle keeps stroke style attributes // that is used by the Stroke method of a Drawer type StrokeStyle struct { diff --git a/draw2dsvg/gc.go b/draw2dsvg/gc.go index b7f175d..a542de2 100644 --- a/draw2dsvg/gc.go +++ b/draw2dsvg/gc.go @@ -76,9 +76,16 @@ func (gc *GraphicContext) drawPaths (drawType drawType, paths ...*draw2d.Path) { svgPaths[i].Desc = toSvgPathDesc(path) if drawType & stroked == stroked { svgPaths[i].Stroke = toSvgRGBA(gc.Current.StrokeColor) + svgPaths[i].StrokeWidth = toSvgLength(gc.Current.LineWidth) + svgPaths[i].StrokeLinecap = gc.Current.Cap.String() + svgPaths[i].StrokeLinejoin = gc.Current.Join.String() + } else { + svgPaths[i].Stroke = "none" } if drawType & filled == filled { svgPaths[i].Fill = toSvgRGBA(gc.Current.FillColor) + } else { + svgPaths[i].Fill = "none" } } @@ -89,7 +96,11 @@ func (gc *GraphicContext) drawPaths (drawType drawType, paths ...*draw2d.Path) { func toSvgRGBA (c color.Color) string { // TODO move elsewhere r, g, b, a := c.RGBA() - return fmt.Sprintf("rgba(%v, %v, %v, %v)", r>>8, g>>8, b>>8, float64(a>>8)/255) + return fmt.Sprintf("rgba(%v, %v, %v, %.3f)", r>>8, g>>8, b>>8, float64(a>>8)/255) +} + +func toSvgLength (l float64) string { + return fmt.Sprintf("%.4f", l) } func toSvgPathDesc (p *draw2d.Path) string { // TODO move elsewhere diff --git a/draw2dsvg/svg.go b/draw2dsvg/svg.go index 9e70575..b2dfbb3 100644 --- a/draw2dsvg/svg.go +++ b/draw2dsvg/svg.go @@ -39,4 +39,7 @@ type Text struct { type FillStroke struct { Fill string `xml:"fill,attr,omitempty"` Stroke string `xml:"stroke,attr,omitempty"` + StrokeWidth string `xml:"stroke-width,attr,omitempty"` + StrokeLinecap string `xml:"stroke-linecap,attr,omitempty"` + StrokeLinejoin string `xml:"stroke-linejoin,attr,omitempty"` } \ No newline at end of file