Skip to content

fix(usvg): inherit gradientTransform and patternTransform via href - #1135

Open
killerdevildog wants to merge 1 commit into
linebender:mainfrom
killerdevildog:fix-inherit-paint-server-transform-via-href
Open

killerdevildog wants to merge 1 commit into
linebender:mainfrom
killerdevildog:fix-inherit-paint-server-transform-via-href

Conversation

@killerdevildog

Copy link
Copy Markdown

Fixes #1134.

A gradient or pattern that references another one through href did not inherit its gradientTransform or patternTransform. For example, <linearGradient id="lg2" xlink:href="#lg1"/>, where lg1 has gradientTransform="rotate(90 0.5 0.5)", rendered left to right instead of top to bottom.

This regressed in 9101f30 (first released in v0.37.0), which replaced resolve_attr(node, AId::GradientTransform).attribute(..) with node.resolve_transform(..) to support transform-origin. resolve_transform only reads the element itself, so the href lookup was lost and the GradientTransform entries in resolve_lg_attr and resolve_rg_attr became unused. Resolving the defining element with resolve_attr first restores inheritance and keeps transform-origin support. A transform set on the element itself still takes precedence. When the transform is inherited, transform-origin is read from the same element that defines the transform.

Before After
Before: resvg main renders the gradient left to right After: resvg with the fix renders the gradient top to bottom

This PR changes only crates/usvg/src/parser/paint_server.rs.

Validation: the reproducer from #1134 fails before the change and passes afterward; resvg's output now matches rsvg-convert 2.58 to within 1/255 per color channel (before the change it differed by up to 255). Local paired cases covering linear, radial and pattern templates, a radial gradient using a linear template, a two-level href chain, and an element that overrides the inherited transform each render pixel-identically to the same transform set directly on the element. cargo test --release -p resvg -p usvg passes (1731 integration tests, no reference image changes) and cargo fmt --check is clean. The local regression tests are not included in this PR. Tested on Linux only.

A gradient or pattern that references another one through `href`/`xlink:href` did not inherit its `gradientTransform` or `patternTransform`. Stops, coordinates, units and `spreadMethod` were inherited, but the transform was silently dropped. For example, `<linearGradient id="lg2" xlink:href="#lg1"/>`, where `lg1` has `gradientTransform="rotate(90 0.5 0.5)"`, rendered left-to-right instead of top-to-bottom, and a referencing pattern lost its rotation.

This is a regression from 9101f30 ("Add support for the `transform-origin` attribute"), which replaced `resolve_attr(node, AId::GradientTransform).attribute(..)` with `node.resolve_transform(..)`. `resolve_transform` only reads the attribute from the element itself, so the `GradientTransform` entries in `resolve_lg_attr` and `resolve_rg_attr` became unused.

SVG 2 lists `gradientTransform` among the attributes copied from a template `linearGradient` or `radialGradient`, and `patternTransform` among those copied from a template `pattern` (https://www.w3.org/TR/SVG2/pservers.html). SVG 1.1 likewise states that attributes defined on the referenced element but not on this element are inherited.

Resolve the element that defines the transform with `resolve_attr` before calling `resolve_transform`, in `convert_linear`, `convert_radial` and `convert_pattern`. A transform set on the element itself still takes precedence over the referenced one.

Signed-off-by: killerdevildog <quaylynrimer11@gmail.com>
Signed-off-by: killerdevildog <software.quaylynrimer@gmail.com>
@luisbg

luisbg commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Fix looks good to me but we should have a test added to avoid this issue regressing in the future.

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.

gradientTransform and patternTransform are not inherited through href

2 participants