Skip to content

Support linearGradient and radialGradient fills - #150

Open
Niksac wants to merge 3 commits into
dompdf:masterfrom
Niksac:svg-gradients
Open

Niksac wants to merge 3 commits into
dompdf:masterfrom
Niksac:svg-gradients

Conversation

@Niksac

@Niksac Niksac commented Nov 15, 2026

Copy link
Copy Markdown

Adds linear and radial gradients as fill paint of shapes. Closes #16.

Changes

  • Style::parseColor() returns a paint reference for url(#id), with the fallback color if any.
  • Document::getGradient() resolves a reference, including href inheritance and stop styles from attributes, style sheets and style. Gradients are collected in a separate pass over the file, because they can be defined after the element that uses them.
  • Shape resolves the geometry: gradientUnits, gradientTransform, spreadMethod, lengths with units, focal point.
  • SurfaceCpdf::fillGradient() clips to the path and paints the gradient. The path is recorded to get its bounding box and to draw it again for the stroke.
  • The bundled Cpdf gets gradient() and clip(), the same code as in Support linear-gradient() and radial-gradient() as background images (CPDF) dompdf#3822.

Limitations

  • Strokes and text use the average color of the stops.
  • No color-interpolation="linearRGB", no references to other files, no SurfacePDFLib.
  • A focal point outside of the circle is moved to its edge (SVG 1.1).

Compatibility

Gradients are painted if the canvas has a gradient() method. With the Cpdf of a dompdf version without it, the average color is used, so both PRs can be merged independently.

The existing classes Tag\LinearGradient, Tag\RadialGradient and Gradient\Stop are not used. I left them in place and can remove them if you prefer.

Disclaimer:
Most of the code was written by AI (Claude) and reviewed / tested by me.

@bsweeney bsweeney added this to the 2.0.0 milestone Nov 16, 2026
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.

Missing linear/radial gradient support

2 participants