Skip to content

SVG: do not draw an element with display="none" - #2158

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

andersonhc merged 3 commits into
py-pdf:masterfrom
RavSinghChandan:svg-display-none

Conversation

@RavSinghChandan

Copy link
Copy Markdown

Elements and groups with display="none" are drawn. Inkscape stores hidden layers as <g style="display:none">, and other editors write display="none" on hidden objects, so content the author hid ends up in the PDF. tox's logo (docs/_static/img/tox.svg) has a hidden layer, and optax's favicon (docs/images/favicon.svg) has a hidden group of construction strokes, which are written into the PDF (146 kB with them, 118 kB without).

_build_group_child() now reads display (attribute, style or CSS class) and builds a hidden element into a throwaway group, so it is not drawn, but its ids are still registered and a <use> of an element inside it still works.

Added test_svg_display_none (attribute, hidden Inkscape layer, CSS class) and test_svg_display_none_referenced; the first fails on master. The full test suite passes except test_image_with_explicit_dimensions, which also fails on master in my environment; black, pylint, mypy and pyright pass

@andersonhc andersonhc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this! The hidden-layer handling looks useful, and the tests cover the common cases well.

I noticed a few cases where hidden content still appears:

  • display="none" on the root or a is missed because those elements don’t go through _build_group_child().
  • A referencing an element that itself has display="none" still draws it. This differs from referencing a visible child inside a hidden group, which your new test correctly allows.
    Could we cover these cases too? For references, we’ll need to preserve the target’s own display setting while keeping visible descendants of hidden groups available for reuse.

@RavSinghChandan

Copy link
Copy Markdown
Author

Thanks, all three are covered now: a hidden root or is not drawn, and a of an element that is itself display="none" draws nothing, while a visible element inside a hidden group can still be reused.

# Conflicts:
#	CHANGELOG.md
#	fpdf/svg.py
#	test/svg/test_svg.py
@andersonhc
andersonhc merged commit 3557240 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