Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Svg.Model/Services/SvgService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ public static SKSize GetDimensions(SvgFragment svgFragment, SKRect skViewport =
if (isWidthperc && svgFragment is SvgDocument)
{
var bounds = percentViewport;
w = (bounds.Width + bounds.Left) * (svgFragment.Width.Value * 0.01f);
w = bounds.Width * (svgFragment.Width.Value * 0.01f);
}
else
{
Expand All @@ -740,7 +740,7 @@ public static SKSize GetDimensions(SvgFragment svgFragment, SKRect skViewport =
if (isHeightperc && svgFragment is SvgDocument)
{
var bounds = percentViewport;
h = (bounds.Height + bounds.Top) * (svgFragment.Height.Value * 0.01f);
h = bounds.Height * (svgFragment.Height.Value * 0.01f);
}
else
{
Expand Down
29 changes: 29 additions & 0 deletions tests/Svg.Skia.UnitTests/SKSvgTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,35 @@ public void Save_StandaloneDocumentWithExplicitPercentSize_UsesViewBoxWithoutCon
Assert.Equal(100, image.Height);
}

[Fact]
public void Save_StandaloneDocumentWithNegativeViewBoxOrigin_UsesViewBoxSize()
{
const string svgMarkup = """
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-10 -20 120 80">
<rect x="-10" y="-20" width="120" height="80" fill="red" />
</svg>
""";

var document = SvgService.FromSvg(svgMarkup);
Assert.NotNull(document);

var size = SvgService.GetDimensions(document);
Assert.Equal(120f, size.Width);
Assert.Equal(80f, size.Height);

var svg = new SKSvg();
using var input = new MemoryStream(Encoding.UTF8.GetBytes(svgMarkup));
using var _ = svg.Load(input);
using var output = new MemoryStream();

Assert.True(svg.Save(output, SkiaSharp.SKColors.Transparent));

output.Position = 0;
using var image = Image.Load<Rgba32>(output);
Assert.Equal(120, image.Width);
Assert.Equal(80, image.Height);
}

[Fact]
public void Save_StandaloneDocumentWithExplicitPercentSize_UsesConfiguredStandaloneViewport()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Svg.Skia.UnitTests/SvgAnimationControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public void SetAnimationTime_RebuildsRootViewBoxAnimations()
Assert.Equal(50f, renderedDocument.ViewBox.Width, 3);
Assert.Equal(50f, renderedDocument.ViewBox.Height, 3);
Assert.Null(svg.HitTestTopmostElement(new SKPoint(65, 65)));
Assert.Equal("target", svg.HitTestTopmostElement(new SKPoint(25, 25))?.ID);
Assert.Equal("target", svg.HitTestTopmostElement(new SKPoint(15, 15))?.ID);
}

[Fact]
Expand Down
Loading