From 4edc3ea2e92e2e9506bc107bb48b2077a46e0628 Mon Sep 17 00:00:00 2001 From: mrbean-bremen Date: Tue, 1 Jan 2019 19:47:22 +0100 Subject: [PATCH] Added handling of rounded caps for dashed lines using dasharray - fixes #191 --- Source/Basic Shapes/SvgVisualElement.cs | 17 +++++++++++++++++ Tests/Svg.UnitTests/PassingTests.csv | 1 + 2 files changed, 18 insertions(+) diff --git a/Source/Basic Shapes/SvgVisualElement.cs b/Source/Basic Shapes/SvgVisualElement.cs index 4d8c32a..54c7884 100644 --- a/Source/Basic Shapes/SvgVisualElement.cs +++ b/Source/Basic Shapes/SvgVisualElement.cs @@ -256,10 +256,27 @@ namespace Svg // handle odd dash arrays by repeating them once this.StrokeDashArray.AddRange(this.StrokeDashArray); } + /* divide by stroke width - GDI behaviour that I don't quite understand yet.*/ pen.DashPattern = this.StrokeDashArray.ConvertAll(u => ((u.ToDeviceValue(renderer, UnitRenderingType.Other, this) <= 0) ? 1 : u.ToDeviceValue(renderer, UnitRenderingType.Other, this)) / ((strokeWidth <= 0) ? 1 : strokeWidth)).ToArray(); + if (this.StrokeLineCap == SvgStrokeLineCap.Round) + { + // to handle round caps, we have to adapt the dash pattern + // by increasing the dash length by the stroke width - GDI draws the rounded + // edge inside the dash line, SVG draws it outside the line + var pattern = new float[pen.DashPattern.Length]; + int offset = 1; // the values are already normalized to dash width + for ( int i = 0; i < pen.DashPattern.Length; i++) + { + pattern[i] = pen.DashPattern[i] + offset; + offset *= -1; // increase dash length, decrease spaces + } + pen.DashPattern = pattern; + pen.DashCap = DashCap.Round; + } + if (this.StrokeDashOffset != null && this.StrokeDashOffset.Value != 0) { pen.DashOffset = ((this.StrokeDashOffset.ToDeviceValue(renderer, UnitRenderingType.Other, this) <= 0) ? 1 : this.StrokeDashOffset.ToDeviceValue(renderer, UnitRenderingType.Other, this)) / diff --git a/Tests/Svg.UnitTests/PassingTests.csv b/Tests/Svg.UnitTests/PassingTests.csv index 15ea685..7930fff 100644 --- a/Tests/Svg.UnitTests/PassingTests.csv +++ b/Tests/Svg.UnitTests/PassingTests.csv @@ -174,6 +174,7 @@ __issue-109-01 __issue-116-01 __issue-123-01 __issue-143-01 +__issue-191-01 __issue-202-01 __issue-214-01 __issue-227-01 -- GitLab