Skip to content

Commit

Permalink
Fix: Incorrect clip region issue (#492)
Browse files Browse the repository at this point in the history
- Small refactoring in SvgClipPath
- Fix incorrect clip region issue, fixes #363
- Add test images
  • Loading branch information
H1Gdev authored and mrbean-bremen committed Jun 20, 2019
1 parent e68fc46 commit a424206
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Source/Basic Shapes/SvgVisualElement.cs
Expand Up @@ -363,7 +363,7 @@ protected internal virtual void SetClip(ISvgRenderer renderer)
if (this.ClipPath != null)
{
SvgClipPath clipPath = this.OwnerDocument.GetElementById<SvgClipPath>(this.ClipPath.ToString());
if (clipPath != null) renderer.SetClip(clipPath.GetClipRegion(this), CombineMode.Intersect);
if (clipPath != null) renderer.SetClip(clipPath.GetClipRegion(this, renderer), CombineMode.Intersect);
}

var clip = this.Clip;
Expand Down
67 changes: 28 additions & 39 deletions Source/Clipping and Masking/SvgClipPath.cs
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using Svg.Transforms;

namespace Svg
{
Expand All @@ -15,7 +11,7 @@ public sealed class SvgClipPath : SvgElement
{
private SvgCoordinateUnits _clipPathUnits = SvgCoordinateUnits.Inherit;

private bool _pathDirty = true;
private GraphicsPath _path;

/// <summary>
/// Specifies the coordinate system for the clipping path.
Expand All @@ -27,30 +23,28 @@ public SvgCoordinateUnits ClipPathUnits
set { _clipPathUnits = value; Attributes["clipPathUnits"] = value; }
}

private GraphicsPath cachedClipPath = null;

/// <summary>
/// Gets this <see cref="SvgClipPath"/>'s region to be used as a clipping region.
/// </summary>
/// <param name="owner"></param>
/// <param name="renderer"></param>
/// <returns>A new <see cref="Region"/> containing the <see cref="Region"/> to be used for clipping.</returns>
public Region GetClipRegion(SvgVisualElement owner)
public Region GetClipRegion(SvgVisualElement owner, ISvgRenderer renderer)
{
if (cachedClipPath == null || this._pathDirty)
if (_path == null || IsPathDirty)
{
cachedClipPath = new GraphicsPath();
_path = new GraphicsPath();

foreach (SvgElement element in this.Children)
{
this.CombinePaths(cachedClipPath, element);
}
foreach (var element in Children)
CombinePaths(_path, element, renderer);

this._pathDirty = false;
IsPathDirty = false;
}

var result = cachedClipPath;
var result = _path;
if (ClipPathUnits == SvgCoordinateUnits.ObjectBoundingBox)
{
result = (GraphicsPath)cachedClipPath.Clone();
result = (GraphicsPath)_path.Clone();
using (var transform = new Matrix())
{
var bounds = owner.Bounds;
Expand All @@ -68,31 +62,28 @@ public Region GetClipRegion(SvgVisualElement owner)
/// </summary>
/// <param name="path"></param>
/// <param name="element"></param>
private void CombinePaths(GraphicsPath path, SvgElement element)
/// <param name="renderer"></param>
private void CombinePaths(GraphicsPath path, SvgElement element, ISvgRenderer renderer)
{
var graphicsElement = element as SvgVisualElement;

if (graphicsElement != null && graphicsElement.Path(null) != null)
if (graphicsElement != null)
{
path.FillMode = (graphicsElement.ClipRule == SvgClipRule.NonZero) ? FillMode.Winding : FillMode.Alternate;
var childPath = graphicsElement.Path(renderer);
if (childPath != null)
{
path.FillMode = graphicsElement.ClipRule == SvgClipRule.NonZero ? FillMode.Winding : FillMode.Alternate;

GraphicsPath childPath = graphicsElement.Path(null);
if (graphicsElement.Transforms != null)
foreach (var transform in graphicsElement.Transforms)
childPath.Transform(transform.Matrix);

if (graphicsElement.Transforms != null)
{
foreach (SvgTransform transform in graphicsElement.Transforms)
{
childPath.Transform(transform.Matrix);
}
if (childPath.PointCount > 0)
path.AddPath(childPath, false);
}

if (childPath.PointCount > 0) path.AddPath(childPath, false);
}

foreach (SvgElement child in element.Children)
{
this.CombinePaths(path, child);
}
foreach (var child in element.Children)
CombinePaths(path, child, renderer);
}

/// <summary>
Expand All @@ -104,7 +95,7 @@ private void CombinePaths(GraphicsPath path, SvgElement element)
protected override void AddElement(SvgElement child, int index)
{
base.AddElement(child, index);
this._pathDirty = true;
IsPathDirty = true;
}

/// <summary>
Expand All @@ -115,7 +106,7 @@ protected override void AddElement(SvgElement child, int index)
protected override void RemoveElement(SvgElement child)
{
base.RemoveElement(child);
this._pathDirty = true;
IsPathDirty = true;
}

/// <summary>
Expand All @@ -124,10 +115,8 @@ protected override void RemoveElement(SvgElement child)
/// <param name="renderer">The <see cref="ISvgRenderer"/> object to render to.</param>
protected override void Render(ISvgRenderer renderer)
{
// Do nothing
}


public override SvgElement DeepCopy()
{
return DeepCopy<SvgClipPath>();
Expand All @@ -136,7 +125,7 @@ public override SvgElement DeepCopy()
public override SvgElement DeepCopy<T>()
{
var newObj = base.DeepCopy<T>() as SvgClipPath;
newObj.ClipPathUnits = this.ClipPathUnits;
newObj.ClipPathUnits = ClipPathUnits;
return newObj;
}
}
Expand Down
2 changes: 2 additions & 0 deletions Tests/Svg.UnitTests/PassingTests.csv
Expand Up @@ -206,6 +206,7 @@ __issue-338-01_stroke_width
__issue-342-01
__issue-345-01
__issue-354-01
__issue-363-01
__issue-385-01_Test_text-anchor-middle
__issue-398-01
__issue-419-01
Expand All @@ -222,5 +223,6 @@ __pull_request-433-01
__pull_request-444-01
__pull_request-462-01
__pull_request-471-01
__pull_request-492-01
__Telefunken_FuBK_test_pattern
__title
2 changes: 2 additions & 0 deletions Tests/W3CTestSuite/PassingTests.txt
Expand Up @@ -236,6 +236,7 @@ __issue-342-01.svg
__issue-345-01.svg
__issue-354-01.svg
__issue-385-01_Test_text-anchor-middle.svg
__issue-363-01.svg
__issue-419-01.svg
__issue-436-01.svg
__issue-437-01.svg
Expand All @@ -248,6 +249,7 @@ __pull_request-374-01.svg
__pull_request-414-01.svg
__pull_request-433-01.svg
__pull_request-444-01.svg
__pull_request-492-01.svg
__AJ_Digital_Camera.svg
__Telefunken_FuBK_test_pattern.svg
__tiger.svg
Expand Down
Binary file added Tests/W3CTestSuite/png/__pull_request-492-01.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions Tests/W3CTestSuite/svg/__pull_request-492-01.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a424206

Please sign in to comment.