Skip to content

Fix gradient coordinate percentage parsing#127

Merged
swhitty merged 1 commit into
swhitty:mainfrom
KuroiDOC:fix/gradient-percentage-coordinates
May 16, 2026
Merged

Fix gradient coordinate percentage parsing#127
swhitty merged 1 commit into
swhitty:mainfrom
KuroiDOC:fix/gradient-percentage-coordinates

Conversation

@KuroiDOC
Copy link
Copy Markdown
Contributor

@KuroiDOC KuroiDOC commented May 8, 2026

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

  • New unit tests covering percentage x1/y1/x2/y2 parsing in ParserXMLLinearGradientTests and percentage cx/cy/r/fx/fy in ParserXMLRadialGradientTests
  • Existing non-percentage gradient tests confirm no regression

Examples

Demo SVG:
gradient_demo

Safari rendering Previous rendering Fixed
Simulator Screenshot - iPhone 17 Pro - 2026-05-08 at 13 08 11 Simulator Screenshot - iPhone 17 Pro - 2026-05-08 at 11 59 05 Simulator Screenshot - iPhone 17 Pro - 2026-05-08 at 11 57 56

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.
@KuroiDOC KuroiDOC marked this pull request as ready for review May 8, 2026 11:36
@codecov
Copy link
Copy Markdown

codecov Bot commented May 16, 2026

Codecov Report

❌ Patch coverage is 96.87500% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.61%. Comparing base (3981bb8) to head (f8476b9).

Files with missing lines Patch % Lines
DOM/Sources/Parser.XML.swift 50.00% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@swhitty swhitty merged commit 523365b into swhitty:main May 16, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants