Skip to content

Fix SVG C# codegen gradient output#505

Merged
wieslawsoltes merged 4 commits into
masterfrom
issue-460-codegen-gradient-fixes
May 9, 2026
Merged

Fix SVG C# codegen gradient output#505
wieslawsoltes merged 4 commits into
masterfrom
issue-460-codegen-gradient-fixes

Conversation

@wieslawsoltes
Copy link
Copy Markdown
Owner

Summary

Fixes #460.

This PR fixes two SVG C# code generation failures reported in issue #460:

  • generated font family arrays now escape embedded double quotes correctly for C# verbatim string literals
  • generated gradient code no longer emits invalid float literals such as NaNf

It also fixes the renderer path that produced non-finite gradient stop positions for zero-width geometry bounds, so retained shader models are built with valid, clamped SVG stop offsets before code generation runs.

Root Cause

The C# generator emitted verbatim strings with raw embedded quotes. For a font family such as "Black Ops One", this produced invalid generated C# like @""Black Ops One"" instead of doubling quotes inside the verbatim string.

Gradient color positions had two related problems:

  • ToFloatArray appended f to every formatted float, which made non-finite values compile as invalid C# tokens such as NaNf.
  • Gradient stop offsets were computed by converting the stop offset through ToDeviceValue(...) and dividing by skBounds.Width. For zero-width bounds, percentage stops could become 0 / 0, producing NaN positions.

Changes

  • Emit valid C# constants for non-finite floats:
    • float.NaN
    • float.PositiveInfinity
    • float.NegativeInfinity
  • Escape embedded quotes in generated verbatim string arrays.
  • Generate null gradient color positions when retained shader positions are missing or non-finite, matching SkiaSharp's optional color-position behavior.
  • Compute SVG gradient stop offsets directly from the stop offset value:
    • percentages are normalized to 0..1
    • user values are treated as already normalized
    • non-finite and out-of-range values are clamped
  • Align retained shader model and runtime Skia conversion behavior so null color positions remain valid instead of dropping the shader.
  • Add regression tests for string escaping, special float literals, non-finite gradient positions, zero-width gradient bounds, and runtime conversion of optional gradient positions.

Validation

  • dotnet format Svg.Skia.slnx --no-restore
  • dotnet test tests/Svg.Skia.UnitTests/Svg.Skia.UnitTests.csproj -f net10.0 -c Release --no-restore --filter "FullyQualifiedName~SkiaCSharpCodeGenTests" --logger "console;verbosity=normal"
  • dotnet build Svg.Skia.slnx -c Release
  • dotnet test Svg.Skia.slnx -c Release --no-restore

Full solution tests passed. The main Svg.Skia.UnitTests run ended with 1,640 passed, 593 skipped, and 0 failed.

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.

SVG C# Code Generator

1 participant