The svg2pdf engine handles SVG clip paths by distinguishing between 'simple' and 'complex' paths to balance PDF compatibility and rendering accuracy.
Simple Clip Paths
A clip path is considered simple if it meets these criteria:
- It contains no nested clip paths (except for the root clip path itself).
- All shapes within the clip path use the
FillRule::NonZero rule, OR it uses the FillRule::EvenOdd rule with exactly one shape.
Simple clip paths are rendered using native PDF clip path operators (clip_nonzero() or clip_even_odd()). This is efficient and widely supported.
Complex Clip Paths
If a clip path is too complex for native PDF operators (e.g., nested clip paths with varying transforms or multiple EvenOdd shapes), the engine falls back to using soft masks (Alpha masks).
Note: While soft masks ensure conformance with the SVG specification, they are more computationally expensive and may occasionally encounter rendering issues in certain browsers like Safari.
Implementation Logic Summary
- Check Complexity: Evaluate the
ClipPath root group. - Branching:
- If Simple: Flatten the path segments, apply the base transform, and use native PDF clipping.
- If Complex: Create a Form XObject containing the clipped content and return a graphics state reference that applies it as a
MaskType::Alpha soft mask.