Skip to content

SVG: accept opacity percentages and clamp an out-of-range opacity - #1998

Merged
andersonhc merged 1 commit into
py-pdf:masterfrom
RavSinghChandan:svg-opacity-percent-and-clamp
Oct 11, 2026
Merged

andersonhc merged 1 commit into
py-pdf:masterfrom
RavSinghChandan:svg-opacity-percent-and-clamp

Conversation

@RavSinghChandan

Copy link
Copy Markdown

SVG 2 and CSS allow opacities as percentages, but fill-opacity="50%" raised ValueError: could not convert string to float: '50%'. Also, fill-opacity and stroke-opacity are clamped to 0-1 as the spec says, but opacity used a bare float(), so opacity="1.5" raised ValueError: 1.5 not in range [0.0, 1.0] later on.

clamp_float() (only used for opacities) now reads a trailing % as a fraction, and opacity goes through the same clamp_float(0.0, 1.0) as the other two. Added cases to test_svg_attribute_conversion.

Checklist:

  • A unit test is covering the code added / modified by this PR
  • In case of a new feature, docstrings have been added, with also some documentation in the docs/ folder (N/A)
  • A mention of the change is present in CHANGELOG.md
  • This PR is ready to be merged

By submitting this pull request, I confirm that my contribution is made under the terms of the GNU LGPL 3.0 license.

@andersonhc
andersonhc merged commit a9888b0 into py-pdf:master Oct 11, 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