Skip to content

Commit

Permalink
Account for rounding errors in gradient stop positions
Browse files Browse the repository at this point in the history
- if the first/last value is not exactly 0/1,
  an exception is raised by Drawing2D
- could happen due to rounding errors
- see svg-net#966
  • Loading branch information
mrbean-bremen committed Apr 4, 2022
1 parent 51597d3 commit 37695f6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Source/Painting/SvgGradientServer.Drawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,21 @@ protected ColorBlend GetColorBlend(ISvgRenderer renderer, float opacity, bool ra
i++;
}

blend.Positions[i] = position;
// always set the first position to 0 to account for rounding errors
blend.Positions[i] = i == 0 ? 0.0f : position;
blend.Colors[i] = colour;

// Insert this colour after itself at position 0
if (insertEnd && i == colourBlends - 2)
{
i++;
blend.Colors[i] = colour;
}

if (i == colourBlends - 1)
{
// always set the last position to 1 to account for rounding errors
blend.Positions[i] = 1.0f;
blend.Colors[i] = colour;
}
}

Expand Down
Binary file added Tests/W3CTestSuite/png/__issue-966-01.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions Tests/W3CTestSuite/svg/__issue-966-01.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions doc/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The release versions are NuGet releases.

### Fixes
* fixed issue for `SvgTextBase.Bounds` with blank `SvgTextSpan` (see [PR #963](https://github.com/svg-net/SVG/pull/963))
* fixed crash for certain gradient values (see [#966](https://github.com/svg-net/SVG/issues/966))

## [Version 3.4.1](https://www.nuget.org/packages/Svg/3.4.1) (2022-03-19)

Expand Down

0 comments on commit 37695f6

Please sign in to comment.