Skip to content

Commit c63648d

Browse files
authored
[vector_graphics_compiler] Support SVG alpha masks (mask-type="alpha") (flutter#13025)
[vector_graphics_compiler] Support SVG alpha masks (`mask-type="alpha"`) Fixes flutter#158734 This PR adds support for SVG alpha masks (`mask-type="alpha"` and `style="mask-type: alpha"`) in `vector_graphics_compiler`, split out from flutter/packages#11909 per review feedback. ### Summary of Changes * **SVG Parser & AST (`vector_graphics_compiler`)**: * Parses the `mask-type` attribute (and inline `style="mask-type: ..."` property) on `<mask>` elements in `SvgParser`. * Propagates `isAlphaMask` through `ResolvedMaskNode` and `MaskNode`. * Emits `BlendMode.dstIn` for alpha masks instead of `BlendMode.srcIn` with a luminance-to-alpha color filter matrix (`BlendMode.modulate`). * **MaskingOptimizer Compatibility**: * Updates `MaskingOptimizer` so single-path masks using gradients with translucent stops (`stop-opacity < 1.0`) are preserved rather than replaced with solid geometry intersection (which previously discarded the gradient's alpha channel). ## 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=1bad96cc-81aa-438f-98f6-cb909289144a
1 parent 3b733d2 commit c63648d

19 files changed

Lines changed: 303 additions & 11 deletions

‎packages/vector_graphics_compiler/CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 1.3.1
22

3+
* Adds support for SVG alpha masks (`mask-type="alpha"`).
34
* Updates minimum supported SDK version to Flutter 3.41/Dart 3.11.
45

56
## 1.3.0

‎packages/vector_graphics_compiler/lib/src/paint.dart‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,3 +1493,14 @@ const FontWeight normalFontWeight = FontWeight.w400;
14931493

14941494
/// A commonly used font weight that is heavier than normal.
14951495
const FontWeight boldFontWeight = FontWeight.w700;
1496+
1497+
/// The type of mask to apply.
1498+
///
1499+
/// Corresponds to the SVG `mask-type` attribute.
1500+
enum MaskType {
1501+
/// An alpha mask where the alpha channel of the mask determines the opacity.
1502+
alpha,
1503+
1504+
/// A luminance mask where the luminance of the mask determines the opacity.
1505+
luminance,
1506+
}

‎packages/vector_graphics_compiler/lib/src/svg/_tessellator_ffi.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class Tessellator extends Visitor<Node, void>
6161
child: maskNode.child.accept(this, data),
6262
mask: maskNode.mask,
6363
blendMode: maskNode.blendMode,
64+
maskType: maskNode.maskType,
6465
);
6566
}
6667

‎packages/vector_graphics_compiler/lib/src/svg/clipping_optimizer.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class ClippingOptimizer extends Visitor<_Result, Node> with ErrorOnUnResolvedNod
120120
child: childResult.node,
121121
mask: maskNode.mask,
122122
blendMode: maskNode.blendMode,
123+
maskType: maskNode.maskType,
123124
);
124125
final result = _Result(newMaskNode);
125126
result.children.add(childResult.node);

‎packages/vector_graphics_compiler/lib/src/svg/masking_optimizer.dart‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Path toVectorGraphicsPath(path_ops.Path path) {
112112
ResolvedPathNode? getSingleChild(Node node) {
113113
if (node is ResolvedPathNode) {
114114
return node;
115-
} else if (node is ParentNode && node.children.length == 1) {
115+
} else if (node is ParentNode && node is! SaveLayerNode && node.children.length == 1) {
116116
return getSingleChild(node.children.first);
117117
}
118118
return null;
@@ -215,8 +215,14 @@ class MaskingOptimizer extends Visitor<_Result, Node> with ErrorOnUnResolvedNode
215215
_Result visitResolvedMaskNode(ResolvedMaskNode maskNode, void data) {
216216
var result = _Result(maskNode);
217217
final ResolvedPathNode? singleMaskPathNode = getSingleChild(maskNode.mask);
218-
219-
if (singleMaskPathNode != null) {
218+
final bool canOptimizeSingleMaskPath =
219+
singleMaskPathNode != null &&
220+
(maskNode.maskType != MaskType.alpha ||
221+
(singleMaskPathNode.paint.fill?.shader == null &&
222+
singleMaskPathNode.paint.fill?.color.a == 255 &&
223+
singleMaskPathNode.paint.stroke == null));
224+
225+
if (canOptimizeSingleMaskPath) {
220226
masksToApply.add(singleMaskPathNode);
221227
final _Result childResult = maskNode.child.accept(this, maskNode);
222228
masksToApply.removeLast();
@@ -228,6 +234,7 @@ class MaskingOptimizer extends Visitor<_Result, Node> with ErrorOnUnResolvedNode
228234
child: childResult.node,
229235
mask: maskNode.mask,
230236
blendMode: maskNode.blendMode,
237+
maskType: maskNode.maskType,
231238
);
232239
result = _Result(newMaskNode);
233240
}
@@ -237,6 +244,7 @@ class MaskingOptimizer extends Visitor<_Result, Node> with ErrorOnUnResolvedNode
237244
child: childResult.node,
238245
mask: maskNode.mask,
239246
blendMode: maskNode.blendMode,
247+
maskType: maskNode.maskType,
240248
);
241249
result = _Result(newMaskNode);
242250
}

‎packages/vector_graphics_compiler/lib/src/svg/node.dart‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,17 @@ class ParentNode extends AttributedNode {
204204
/// Create the paint required to draw a save layer, or `null` if none is
205205
/// required.
206206
Paint? createLayerPaint() {
207-
final double? fillOpacity = attributes.fill?.opacity;
207+
final double? layerOpacity = attributes.fill?.opacity ?? attributes.opacity;
208208
final bool needsLayer =
209209
(attributes.blendMode != null) ||
210-
(fillOpacity != null && fillOpacity != 1.0 && fillOpacity != 0.0);
210+
(layerOpacity != null && layerOpacity != 1.0 && layerOpacity != 0.0);
211211

212212
if (needsLayer) {
213213
return Paint(
214214
blendMode: attributes.blendMode,
215215
fill:
216216
attributes.fill?.toFill(Rect.largest, transform) ??
217-
Fill(color: Color.opaqueBlack.withOpacity(fillOpacity ?? 1.0)),
217+
Fill(color: Color.opaqueBlack.withOpacity(layerOpacity ?? 1.0)),
218218
);
219219
}
220220
return null;

‎packages/vector_graphics_compiler/lib/src/svg/overdraw_optimizer.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ class OverdrawOptimizer extends Visitor<_Result, Node> with ErrorOnUnResolvedNod
276276
child: childResult.node,
277277
mask: maskNode.mask,
278278
blendMode: maskNode.blendMode,
279+
maskType: maskNode.maskType,
279280
);
280281
final result = _Result(newMaskNode);
281282
result.children.add(childResult.node);

‎packages/vector_graphics_compiler/lib/src/svg/parser.dart‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,20 @@ class SvgParser {
10911091
}
10921092
}
10931093

1094+
/// Parses a `mask-type` attribute value into a [MaskType].
1095+
MaskType? parseMaskType(String? maskType) {
1096+
if (maskType == null) {
1097+
return null;
1098+
}
1099+
switch (maskType.trim().toLowerCase()) {
1100+
case 'alpha':
1101+
return MaskType.alpha;
1102+
case 'luminance':
1103+
return MaskType.luminance;
1104+
}
1105+
return null;
1106+
}
1107+
10941108
double _parseRawWidthHeight(String raw) {
10951109
if (raw == '100%' || raw == '') {
10961110
return double.infinity;
@@ -1597,6 +1611,8 @@ class SvgParser {
15971611
id: id,
15981612
),
15991613
textAnchorMultiplier: parseTextAnchor(attributeMap['text-anchor']),
1614+
maskType: parseMaskType(attributeMap['mask-type']),
1615+
opacity: opacity,
16001616
);
16011617
}
16021618
}
@@ -1778,6 +1794,8 @@ class SvgAttributes {
17781794
this.dy,
17791795
this.width,
17801796
this.height,
1797+
this.maskType,
1798+
this.opacity,
17811799
});
17821800

17831801
/// For use in tests to construct arbitrary attributes.
@@ -1807,6 +1825,8 @@ class SvgAttributes {
18071825
this.dy,
18081826
this.width,
18091827
this.height,
1828+
this.maskType,
1829+
this.opacity,
18101830
});
18111831

18121832
/// The empty set of properties.
@@ -1962,6 +1982,12 @@ class SvgAttributes {
19621982
/// The relative y translation.
19631983
final DoubleOrPercentage? dy;
19641984

1985+
/// The `mask-type` attribute.
1986+
final MaskType? maskType;
1987+
1988+
/// The element or group-level `opacity` attribute, clamped to 0.0..1.0.
1989+
final double? opacity;
1990+
19651991
/// A copy of these attributes after absorbing a saveLayer.
19661992
///
19671993
/// Specifically, this will null out `blendMode` and any opacity related
@@ -1992,6 +2018,7 @@ class SvgAttributes {
19922018
y: y,
19932019
width: width,
19942020
height: height,
2021+
maskType: maskType,
19952022
);
19962023
}
19972024

@@ -2037,6 +2064,8 @@ class SvgAttributes {
20372064
y: y,
20382065
dx: dx,
20392066
dy: dy,
2067+
maskType: maskType ?? parent.maskType,
2068+
opacity: opacity,
20402069
);
20412070
}
20422071
}

‎packages/vector_graphics_compiler/lib/src/svg/resolver.dart‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ class ResolvingVisitor extends Visitor<Node, AffineMatrix> {
5656
final AffineMatrix childTransform = maskNode.concatTransform(data);
5757
final Node mask = resolvedMask.accept(this, childTransform);
5858

59-
return ResolvedMaskNode(child: child, mask: mask, blendMode: maskNode.blendMode);
59+
return ResolvedMaskNode(
60+
child: child,
61+
mask: mask,
62+
blendMode: maskNode.blendMode,
63+
maskType: resolvedMask.attributes.maskType,
64+
);
6065
} finally {
6166
_activeMasks.remove(maskNode.maskId);
6267
}
@@ -441,9 +446,18 @@ class ResolvedClipNode extends Node {
441446
/// A mask node with child and mask fully resolved.
442447
///
443448
/// This should only be constructed from a [MaskNode] in a [ResolvingVisitor].
449+
///
450+
/// The [mask] node tree is evaluated to generate a mask layer that is applied to
451+
/// [child]. The [maskType] indicates whether opacity is derived from the mask's
452+
/// alpha channel ([MaskType.alpha]) or luminance ([MaskType.luminance]).
444453
class ResolvedMaskNode extends Node {
445454
/// Create a new [ResolvedMaskNode].
446-
ResolvedMaskNode({required this.child, required this.mask, required this.blendMode});
455+
ResolvedMaskNode({
456+
required this.child,
457+
required this.mask,
458+
required this.blendMode,
459+
this.maskType,
460+
});
447461

448462
/// The child to apply as a mask.
449463
final Node mask;
@@ -454,6 +468,9 @@ class ResolvedMaskNode extends Node {
454468
/// The blend mode to apply when saving a layer for the mask, if any.
455469
final BlendMode? blendMode;
456470

471+
/// The mask type ([MaskType.alpha] or [MaskType.luminance]) of this mask layer, if any.
472+
final MaskType? maskType;
473+
457474
@override
458475
S accept<S, V>(Visitor<S, V> visitor, V data) {
459476
return visitor.visitResolvedMaskNode(this, data);

‎packages/vector_graphics_compiler/lib/src/svg/visitor.dart‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ class CommandBuilderVisitor extends Visitor<void, void> with ErrorOnUnResolvedNo
164164
void visitResolvedMaskNode(ResolvedMaskNode maskNode, void data) {
165165
_builder.addSaveLayer(Paint(blendMode: maskNode.blendMode, fill: const Fill()));
166166
maskNode.child.accept(this, data);
167-
_builder.addMask();
167+
if (maskNode.maskType == MaskType.alpha) {
168+
_builder.addSaveLayer(const Paint(blendMode: BlendMode.dstIn, fill: Fill()));
169+
} else {
170+
_builder.addMask();
171+
}
168172
maskNode.mask.accept(this, data);
169173
_builder.restore();
170174
_builder.restore();

0 commit comments

Comments
 (0)