You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 8041cca
Browse filesBrowse the repository at this point in the historyBrowse files
[various] Support SVG filters (blur, offset, feMerge) (flutter#11909)
[various] Support SVG filters (blur, offset, feMerge)
Fixesflutter/flutter#158592
This PR introduces support for basic SVG filters (`feGaussianBlur`, `feOffset`, and `feMerge`) to the `vector_graphics` ecosystem. (Alpha mask support has been split out into a separate PR per review feedback.)
Key Features:
* Added parsing, resolution, and encoding of SVG filters including `feGaussianBlur`, `feOffset`, and `feMerge` in `vector_graphics_compiler`.
* Updated the binary format and `vector_graphics_codec` to support encoding and decoding paint blurs.
* Updated `vector_graphics` to render paint blurs using Flutter's `ui.ImageFilter.blur`.
* Enhanced `SourceAlpha` filters to preserve opacity variations in gradient stops by mapping gradient colors to black while maintaining their alpha values.
Implementation Details:
* **Filter Chaining**: Filters are parsed into a directed acyclic graph (DAG) of primitives. The resolver traces these primitives from the filter outputs back to `SourceGraphic`/`SourceAlpha` to determine the necessary rendering layers.
* **Multi-layer Filter Resolution**: Handles multi-layer filters (e.g. from `feMerge`) by duplicating the target rendering nodes for each layer in the AST.
* **Text Filter Resolution**: Resolves filters at the `TextPositionNode` level (rather than leaf `TextNode`s) to maintain correct text chunk alignment and cursor positioning.
* **Nested Filter Propagation**: Enhanced nested parent nodes (groups), inner path filters, and text position nodes to correctly inherit and propagate the outer `SourceAlpha` state down the tree.
* **Blur Scaling**: Scales the blur standard deviations by the accumulated transform's scale factors at both the group and path/text levels to ensure correct rendering when scaled (since the compiler bakes geometry transforms at compile time, the blur sigmas must also be scaled to match).
* **Compiler Optimizer & Tessellator Compatibility**:
* Preserves `blurSigmaX`/`blurSigmaY` when `TessellationVisitor` and `PathOps` convert `PathNode`s to `VerticesNode`s.
* Prevents `MaskingOptimizer`, `ClippingOptimizer`, and `OverdrawOptimizer` from collapsing or intersecting paths that carry a blur filter (since blurred paint extends beyond geometric path bounds).
* **Spec Compliance**:
* Supports both comma and space-separated `stdDeviation` values.
* Defaults omitted `stdDeviation` attributes to `0.0` (as per SVG spec).
* Automatically resolves default `in` attributes for `feMergeNode`s (falling back to the previous sibling primitive's result or `SourceGraphic`).
* Preserves original fill/stroke opacity when applying `SourceAlpha` filters.
* Handles self-closing `<feMerge />` elements.
* **Robustness, Performance & Optimizations**:
* Added cycle detection during filter tracing to prevent stack overflow on circular filter references.
* Skips rendering node recreation for filter layers that do not affect the paint (e.g. plain `SourceGraphic` branches in a merge).
* Wrapped current layer state restoration in `try-finally` blocks inside parent node and text position node visitors to guarantee robust recovery in case of exceptions.
* Caches blackened gradients in `ResolvingVisitor` to prevent redundant object allocations for shared SVG gradients.
* Guarded against setting paint `imageFilter` when blur standard deviations are `0.0` to avoid unnecessary rendering overhead in Flutter.
* Defensively clamped blur standard deviations to non-negative values in the rendering listener to prevent crashes when processing malformed SVG files.
## Pre-Review Checklist
**Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.
[^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
TAG=agy
CONV=27cb188a-9ad9-4434-8918-8ef71bdba17c
0 commit comments