Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/vector_graphics_compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.3.2

* Fixes a `tspan` without its own position being drawn with its parent group
transform applied twice.

## 1.3.1

* Adds support for SVG alpha masks (`mask-type="alpha"`).
Expand Down
16 changes: 14 additions & 2 deletions packages/vector_graphics_compiler/lib/src/svg/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ class TextPositionNode extends ParentNode {
final bool reset;

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

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

final bool hasXY = x != null && y != null;
final bool hasDxDy = dx != null && dy != null;
// A tspan without its own position continues from the parent's position.
// If the parent already applied the transform to its position (its
// `transform` is null), the transform must not be applied again.
final bool inheritsConsumedTransform =
parent != null &&
parent.transform == null &&
x == null &&
y == null &&
dx == null &&
dy == null;
final bool consumeTransform =
computedTransform == AffineMatrix.identity ||
(computedTransform.encodableInRect && (hasXY || hasDxDy));
Expand All @@ -272,7 +284,7 @@ class TextPositionNode extends ParentNode {
dx: dx,
dy: dy,
reset: reset,
transform: consumeTransform ? null : computedTransform,
transform: (inheritsConsumedTransform || consumeTransform) ? null : computedTransform,
);
}

Expand Down
22 changes: 18 additions & 4 deletions packages/vector_graphics_compiler/lib/src/svg/resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class ResolvingVisitor extends Visitor<Node, AffineMatrix> {
final Set<String> _activePatterns = <String>{};
int _deferredExpansionCount = 0;

/// The position of the text or tspan currently being resolved, if any.
TextPosition? _currentTextPosition;

@override
Node visitClipNode(ClipNode clipNode, AffineMatrix data) {
final AffineMatrix childTransform = clipNode.concatTransform(data);
Expand Down Expand Up @@ -137,11 +140,22 @@ class ResolvingVisitor extends Visitor<Node, AffineMatrix> {
@override
Node visitTextPositionNode(TextPositionNode textPositionNode, AffineMatrix data) {
final AffineMatrix nextTransform = textPositionNode.concatTransform(data);
final TextPosition? parentTextPosition = _currentTextPosition;
final TextPosition textPosition = textPositionNode.computeTextPosition(
_bounds,
data,
parent: parentTextPosition,
);

return ResolvedTextPositionNode(textPositionNode.computeTextPosition(_bounds, data), <Node>[
for (final Node child in textPositionNode.children)
child.applyAttributes(textPositionNode.attributes).accept(this, nextTransform),
]);
_currentTextPosition = textPosition;
try {
return ResolvedTextPositionNode(textPosition, <Node>[
for (final Node child in textPositionNode.children)
child.applyAttributes(textPositionNode.attributes).accept(this, nextTransform),
]);
} finally {
_currentTextPosition = parentTextPosition;
}
}

@override
Expand Down
2 changes: 1 addition & 1 deletion packages/vector_graphics_compiler/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: vector_graphics_compiler
description: A compiler to convert SVGs to the binary format used by `package:vector_graphics`.
repository: https://github.com/flutter/packages/tree/main/packages/vector_graphics_compiler
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+vector_graphics%22
version: 1.3.1
version: 1.3.2

executables:
vector_graphics_compiler:
Expand Down
19 changes: 19 additions & 0 deletions packages/vector_graphics_compiler/test/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,25 @@ ${[for (var i = 2; i <= 30; i++) ' <pattern id="lvl$i" width="10" height="10"
]);
});

test('tspan without position does not apply the group transform twice', () {
final VectorInstructions instructions = parseWithoutOptimizers('''
<svg width="400" height="100" xmlns="http://www.w3.org/2000/svg">
<g transform="translate(10,10)">
<g transform="translate(10,10)">
<text x="15" y="25">bare text</text>
<text x="15" y="50"><tspan>text in tspan</tspan></text>
</g>
</g>
</svg>
''');

expect(instructions.textPositions, const <TextPosition>[
TextPosition(x: 35, y: 45, reset: true),
TextPosition(x: 35, y: 70, reset: true),
TextPosition(),
]);
});

test('Non-standard root font-weight="regular" uses initial weight', () {
final VectorInstructions instructions = parseWithoutOptimizers('''
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
Expand Down
Loading