Skip to content

Fix negative-origin viewBox image dimensions#499

Merged
wieslawsoltes merged 4 commits into
masterfrom
fix/issue-446-viewbox-dimensions
May 11, 2026
Merged

Fix negative-origin viewBox image dimensions#499
wieslawsoltes merged 4 commits into
masterfrom
fix/issue-446-viewbox-dimensions

Conversation

@wieslawsoltes
Copy link
Copy Markdown
Owner

PR Summary: Fix Negative-Origin viewBox Image Dimensions

Overview

This PR fixes incorrect output image dimensions when exporting standalone SVG documents that use percentage root width/height values with a viewBox whose min-x or min-y is non-zero.

Issue: #446

Problem

SvgService.GetDimensions() resolved percentage-sized root SVG documents by multiplying the percentage against:

  • bounds.Width + bounds.Left
  • bounds.Height + bounds.Top

For a viewBox like:

-1477.1400146484375 -2356.166015625 4876.4100341796875 2771.040008544922

that effectively used the right and bottom coordinates as dimensions. With negative viewBox origins, this produced output dimensions around 3399x415 instead of the expected 4876x2771, leaving large blank areas and clipping or misframing the rendered content.

Changes

Fix root percentage dimension resolution

Updated SvgService.GetDimensions() so root SVG percentage width and height resolve against the viewport size only:

  • bounds.Width
  • bounds.Height

This keeps viewBox position separate from viewBox size, which matches the SVG model where the third and fourth viewBox values define the viewport width and height independently of the first two origin values.

Add regression coverage

Added Save_StandaloneDocumentWithNegativeViewBoxOrigin_UsesViewBoxSize to SKSvgTests.

The test covers a compact negative-origin viewBox:

<svg width="100%" height="100%" viewBox="-10 -20 120 80">

It verifies both:

  • SvgService.GetDimensions() returns 120x80
  • SKSvg.Save() produces a 120x80 PNG

Clean nullable xml:base fallback

While reviewing the change, TryGetXmlBase() emitted a nullable assignment warning when falling through to custom attributes. The helper now assigns a non-null fallback and returns false when no xml:base value is available.

Validation

Ran:

git submodule update --init --recursive
dotnet restore tests/Svg.Skia.UnitTests/Svg.Skia.UnitTests.csproj
dotnet test tests/Svg.Skia.UnitTests/Svg.Skia.UnitTests.csproj -c Release --no-restore --filter "FullyQualifiedName~Svg.Skia.UnitTests.SKSvgTests|FullyQualifiedName~Svg.Skia.UnitTests.Issue99Tests"
dotnet format Svg.Skia.slnx --no-restore --include src/Svg.Model/Services/SvgService.cs tests/Svg.Skia.UnitTests/SKSvgTests.cs
git diff --check

Result:

  • Focused tests passed: 9/9
  • Formatting completed
  • Whitespace check passed

Notes

The full solution build was not used as the primary validation target for this focused fix because the checkout contains many unrelated projects and target frameworks. The focused test project was restored and run after initializing submodules, which exercises the changed dimension logic and export path.

…to fix/issue-446-viewbox-dimensions

# Conflicts:
#	src/Svg.Model/Services/SvgService.cs
#	tests/Svg.Skia.UnitTests/SKSvgTests.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant