Skip to content

Commit 293b18c

Browse files
authored
[vector_graphics_compiler] Fix tspan transform applied twice (flutter#13129)
A `tspan` without its own `x`/`y` inside a translated `g` was drawn with the translation applied twice. The parent `text` has `x`/`y`, so the compiler applies the transform to its position. The `tspan` has no position, so it kept the transform as well. The renderer then applied that transform on top of a position that already had it. Now a `tspan` without its own position follows its parent: if the parent already applied the transform, the `tspan` does not apply it again. The bug is in `vector_graphics_compiler`, which `flutter_svg` uses to parse SVGs. Fixes flutter#189925 ## 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.
1 parent 2f7b187 commit 293b18c

5 files changed

Lines changed: 57 additions & 7 deletions

File tree

‎packages/vector_graphics_compiler/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.3.2
2+
3+
* Fixes a `tspan` without its own position being drawn with its parent group
4+
transform applied twice.
5+
16
## 1.3.1
27

38
* Adds support for SVG alpha masks (`mask-type="alpha"`).

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ class TextPositionNode extends ParentNode {
236236
final bool reset;
237237

238238
/// Computes a [TextPosition] to encode for this node.
239-
TextPosition computeTextPosition(Rect bounds, AffineMatrix transform) {
239+
///
240+
/// [parent] is the [TextPosition] of the enclosing text or tspan, if any.
241+
TextPosition computeTextPosition(Rect bounds, AffineMatrix transform, {TextPosition? parent}) {
240242
final AffineMatrix computedTransform = concatTransform(transform);
241243

242244
double? x = attributes.x?.calculate(bounds.width);
@@ -246,6 +248,16 @@ class TextPositionNode extends ParentNode {
246248

247249
final bool hasXY = x != null && y != null;
248250
final bool hasDxDy = dx != null && dy != null;
251+
// A tspan without its own position continues from the parent's position.
252+
// If the parent already applied the transform to its position (its
253+
// `transform` is null), the transform must not be applied again.
254+
final bool inheritsConsumedTransform =
255+
parent != null &&
256+
parent.transform == null &&
257+
x == null &&
258+
y == null &&
259+
dx == null &&
260+
dy == null;
249261
final bool consumeTransform =
250262
computedTransform == AffineMatrix.identity ||
251263
(computedTransform.encodableInRect && (hasXY || hasDxDy));
@@ -272,7 +284,7 @@ class TextPositionNode extends ParentNode {
272284
dx: dx,
273285
dy: dy,
274286
reset: reset,
275-
transform: consumeTransform ? null : computedTransform,
287+
transform: (inheritsConsumedTransform || consumeTransform) ? null : computedTransform,
276288
);
277289
}
278290

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class ResolvingVisitor extends Visitor<Node, AffineMatrix> {
2525
final Set<String> _activePatterns = <String>{};
2626
int _deferredExpansionCount = 0;
2727

28+
/// The position of the text or tspan currently being resolved, if any.
29+
TextPosition? _currentTextPosition;
30+
2831
@override
2932
Node visitClipNode(ClipNode clipNode, AffineMatrix data) {
3033
final AffineMatrix childTransform = clipNode.concatTransform(data);
@@ -137,11 +140,22 @@ class ResolvingVisitor extends Visitor<Node, AffineMatrix> {
137140
@override
138141
Node visitTextPositionNode(TextPositionNode textPositionNode, AffineMatrix data) {
139142
final AffineMatrix nextTransform = textPositionNode.concatTransform(data);
143+
final TextPosition? parentTextPosition = _currentTextPosition;
144+
final TextPosition textPosition = textPositionNode.computeTextPosition(
145+
_bounds,
146+
data,
147+
parent: parentTextPosition,
148+
);
140149

141-
return ResolvedTextPositionNode(textPositionNode.computeTextPosition(_bounds, data), <Node>[
142-
for (final Node child in textPositionNode.children)
143-
child.applyAttributes(textPositionNode.attributes).accept(this, nextTransform),
144-
]);
150+
_currentTextPosition = textPosition;
151+
try {
152+
return ResolvedTextPositionNode(textPosition, <Node>[
153+
for (final Node child in textPositionNode.children)
154+
child.applyAttributes(textPositionNode.attributes).accept(this, nextTransform),
155+
]);
156+
} finally {
157+
_currentTextPosition = parentTextPosition;
158+
}
145159
}
146160

147161
@override

‎packages/vector_graphics_compiler/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: vector_graphics_compiler
22
description: A compiler to convert SVGs to the binary format used by `package:vector_graphics`.
33
repository: https://github.com/flutter/packages/tree/main/packages/vector_graphics_compiler
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+vector_graphics%22
5-
version: 1.3.1
5+
version: 1.3.2
66

77
executables:
88
vector_graphics_compiler:

‎packages/vector_graphics_compiler/test/parser_test.dart‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,25 @@ ${[for (var i = 2; i <= 30; i++) ' <pattern id="lvl$i" width="10" height="10"
414414
]);
415415
});
416416

417+
test('tspan without position does not apply the group transform twice', () {
418+
final VectorInstructions instructions = parseWithoutOptimizers('''
419+
<svg width="400" height="100" xmlns="http://www.w3.org/2000/svg">
420+
<g transform="translate(10,10)">
421+
<g transform="translate(10,10)">
422+
<text x="15" y="25">bare text</text>
423+
<text x="15" y="50"><tspan>text in tspan</tspan></text>
424+
</g>
425+
</g>
426+
</svg>
427+
''');
428+
429+
expect(instructions.textPositions, const <TextPosition>[
430+
TextPosition(x: 35, y: 45, reset: true),
431+
TextPosition(x: 35, y: 70, reset: true),
432+
TextPosition(),
433+
]);
434+
});
435+
417436
test('Non-standard root font-weight="regular" uses initial weight', () {
418437
final VectorInstructions instructions = parseWithoutOptimizers('''
419438
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">

0 commit comments

Comments
 (0)