Skip to content

SVG: read the context-fill and context-stroke paints - #2198

Open
RavSinghChandan wants to merge 5 commits into
py-pdf:masterfrom
RavSinghChandan:svg-context-fill
Open

RavSinghChandan wants to merge 5 commits into
py-pdf:masterfrom
RavSinghChandan:svg-context-fill

Conversation

@RavSinghChandan

@RavSinghChandan RavSinghChandan commented Nov 13, 2026 •

Copy link
Copy Markdown

The icons of Firefox set fill="context-fill" fill-opacity="context-fill-opacity" on the root <svg>, as in the icons of Firefox-UI-Fix. They fail with ValueError: unsupported color specification context-fill.

These SVG 2 paints take the fill or stroke of the context element. For an element drawn by a <use>, that is the <use>. For the SVG itself, it is the document drawing it: Firefox lets the page set it. So context-fill, context-stroke, context-fill-opacity and context-stroke-opacity are now inherited: inside a <use> they take its paint, and at the root the colors set on the FPDF, as currentColor does without a color property.

test_svg_context_fill_is_the_color_of_the_fpdf and test_svg_context_fill_is_the_paint_of_the_use fail without the change.

The icons of Firefox set fill="context-fill" on the root <svg>, which
failed with ValueError. These paints and their opacities are now those
of the <use> that draws the element, or else are inherited, as from the
colors set on the FPDF.

@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.

I noticed one case to address before merging: mapping both context-fill and context-stroke to INHERIT loses the distinction between them. For example, with , a referenced path using fill="context-stroke" should have a lime fill, but currently renders red. An intervening group’s fill can also override the color.

Could we preserve these as distinct values and resolve them against the referencing ? Regression tests for swapped fill/stroke and an intervening group would help cover this.

@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.

I found one remaining inheritance case:

<defs>
  <path id="p" d="M0 0h5v5z" fill="context-stroke"/>
</defs>
<g fill="red" stroke="lime">
  <use href="#p"/>
</g>

The path fills red instead of lime. The context lookup misses presentation attributes inherited from the parent; writing those colors in style="..." works. Could we handle inherited presentation attributes too and add regression coverage, including opacity?

One small lint fix: reassigning value in the new test triggers pylint’s redefined-loop-name. A separate variable for the converted expected color should resolve it.

@RavSinghChandan

Copy link
Copy Markdown
Author

Thanks, fixed. context-fill and context-stroke (and their opacities) are now resolved against the referencing : an element that uses them is built again with the fill, stroke and opacities of the , so fill="context-stroke" is lime in your example, and a group in between no longer overrides it. The definition keeps its context paints for other s, and outside a they are still inherited. test_svg_context_paints_of_the_use and test_svg_context_fill_through_a_group cover swapped fill/stroke, a style property, the opacities, an intervening group and two s with different colors.

@RavSinghChandan

Copy link
Copy Markdown
Author

Thanks, fixed. When the doesn't set a fill, stroke or opacity, the context lookup now walks up its ancestors and reads presentation attributes as well as styles, so your example fills lime. test_svg_context_paints_inherited_by_the_use covers the attributes (with stroke-opacity) and the style form, through an extra . The test now uses a separate variable for the expected color.

This branch has not been deployed

No deployments
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