Fix gradient coordinate percentage parsing#127
Merged
swhitty merged 1 commit intoMay 16, 2026
Conversation
SVG linearGradient and radialGradient accept percentage values for their coordinate attributes (x1, y1, x2, y2, cx, cy, r, fx, fy, fr). The parser was silently discarding the '%' suffix and treating the numeric part as a pixel coordinate, placing gradient axes far outside the bounding box and producing incorrect colours.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #127 +/- ##
==========================================
+ Coverage 89.58% 89.61% +0.02%
==========================================
Files 161 161
Lines 12597 12651 +54
==========================================
+ Hits 11285 11337 +52
- Misses 1312 1314 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
swhitty
approved these changes
May 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
linearGradient and radialGradient elements frequently use percentage values for their geometry attributes (x1, y1, x2, y2, cx, cy, r, fx, fy, fr). Per SVG 1.1 §13.2.2, with the default gradientUnits="objectBoundingBox", 50% is equivalent to
the fraction 0.5.
SwiftDraw's scanCoordinate only recognised px/in/cm/mm/pt/pc as unit suffixes. The % was left unconsumed and the bare number was returned — so x1="3.309%" became 3.309 instead of 0.03309, placing the gradient axis far outside the bounding box
and rendering the wrong colours.
Solution
Added scanCoordinateOrPercentage() to XMLParser.Scanner — identical to scanCoordinate but consumes a trailing % and divides the value by 100. The new method is wired through AttributeValueParser and used exclusively in the linear and radial
gradient parsers.
Generic coordinate parsing is intentionally left unchanged: % on non-gradient attributes (e.g. ) is viewport-relative and requires resolution context that doesn't exist in the parser layer yet.
Testing
Examples
Demo SVG:
