Skip to content

drawing: do not fail on an arc whose scaled-up radii look too small after rounding - #2163

Merged
andersonhc merged 3 commits into
py-pdf:masterfrom
RavSinghChandan:svg-arc-rounding
Nov 12, 2026
Merged

andersonhc merged 3 commits into
py-pdf:masterfrom
RavSinghChandan:svg-arc-rounding

Conversation

@RavSinghChandan

Copy link
Copy Markdown

Five icons of Font Awesome 7.3.1, among them brands/instagram.svg, fail with:

ValueError: math domain error

The ring of the Instagram icon is made of two half circles, a74.6 74.6 0 1 1 .6 149.2, whose end points are exactly a diameter apart. The path is relative, so after the earlier arcs the start point is 224.30000244424667, and the radii come out a tiny bit too small to reach the end point. _approximate_arc() then scales them up, as the SVG implementation notes say, and for such radii the value under the square root in the center computation is 0, the center being the midpoint. With rounding errors it is slightly negative, beyond what the existing round(..., 8) absorbs for coordinates of this size, so math.sqrt() fails.

The value is now clamped at 0. All 2161 Font Awesome 7.3.1 icons load, and the Instagram icon renders as in a browser.

The new case in svg_path_render_tests is the two rings of that icon; it fails on master. The SVG tests, black, pylint, mypy and pyright pass

Comment thread CHANGELOG.md Resolved
@andersonhc

Copy link
Copy Markdown
Collaborator

The new regression test is failing because one coordinate rounds slightly differently across environments (287.8128 instead of 287.8127). Could we compare the numeric coordinates with a small absolute tolerance, while still checking the drawing operators exactly? That should keep the regression coverage without depending on identical floating-point results.

assert actual_numbers == pytest.approx(expected_numbers, abs=1.1e-4, rel=0)

@RavSinghChandan

Copy link
Copy Markdown
Author

Thanks, done as you suggested: test_rendering_smooth_curves now compares the operators exactly and the numbers with pytest.approx(..., abs=1.1e-4, rel=0).

@andersonhc
andersonhc merged commit 238774c into py-pdf:master Nov 12, 2026
23 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