Skip to content

Commit 35a1e60

Browse files
authored
[vector_graphics] Draw <text> under an RTL ambient direction (flutter#13408)
`FlutterVectorGraphicsListener.onDrawText` lays out every paragraph at infinite width, using the ambient text direction. Under RTL the default `TextAlign.start` resolves to right, which puts each line at x = +∞, so no `<text>` is drawn at all. Shapes are unaffected. The listener already positions text itself (`x`, `dx`, and `text-anchor` through `maxIntrinsicWidth`), so this change sets `textAlign: TextAlign.left` on the paragraph. The text direction is still passed through, so bidi handling is unchanged. The new test renders `<text x="10">` under both directions and checks that ink is painted and starts at x ≈ 10. Without the fix, `rtl` paints no pixels. Fixes flutter/flutter#195182 ## Pre-Review Checklist [^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. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
1 parent be64cac commit 35a1e60

4 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎packages/vector_graphics/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.2.6
2+
3+
* Fixes `<text>` not being drawn when the ambient text direction is RTL.
4+
15
## 1.2.5
26

37
* Fixes a leak of the decoded picture when a `VectorGraphic` is removed before

‎packages/vector_graphics/lib/src/listener.dart‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,12 @@ class FlutterVectorGraphicsListener extends VectorGraphicsCodecListener {
688688
if (patternId != null) {
689689
paint.shader = _patterns[patternId]!.shader;
690690
}
691-
final builder = ParagraphBuilder(ParagraphStyle(textDirection: _textDirection));
691+
final builder = ParagraphBuilder(
692+
// Positioning (x, text-anchor) is done by the listener, and the paragraph
693+
// is laid out at infinite width: start alignment under RTL would place
694+
// the line at +infinity.
695+
ParagraphStyle(textDirection: _textDirection, textAlign: TextAlign.left),
696+
);
692697
builder.pushStyle(
693698
TextStyle(
694699
locale: _locale,

‎packages/vector_graphics/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: vector_graphics
22
description: A vector graphics rendering package for Flutter using a binary encoding.
33
repository: https://github.com/flutter/packages/tree/main/packages/vector_graphics
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+vector_graphics%22
5-
version: 1.2.5
5+
version: 1.2.6
66

77
environment:
88
sdk: ^3.11.0

‎packages/vector_graphics/test/listener_test.dart‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'dart:convert' show base64;
6+
import 'dart:math' as math;
67
import 'dart:typed_data';
78
import 'dart:ui' as ui;
89

@@ -70,6 +71,34 @@ void main() {
7071
expect(imageBytes.last, 0xFF000000);
7172
}, skip: kIsWeb);
7273

74+
for (final ui.TextDirection direction in ui.TextDirection.values) {
75+
test('Text is drawn under ${direction.name}', () async {
76+
final Uint8List bytes = encodeSvg(
77+
xml: '<svg width="100" height="40"><text x="10" y="30" font-size="20">AB</text></svg>',
78+
debugName: 'text',
79+
enableClippingOptimizer: false,
80+
enableMaskingOptimizer: false,
81+
enableOverdrawOptimizer: false,
82+
);
83+
final PictureInfo info = await decodeVectorGraphics(
84+
bytes.buffer.asByteData(),
85+
locale: ui.PlatformDispatcher.instance.locale,
86+
textDirection: direction,
87+
clipViewbox: true,
88+
loader: const AssetBytesLoader('test'),
89+
);
90+
final ui.Image image = info.picture.toImageSync(100, 40);
91+
final Uint32List pixels = (await image.toByteData())!.buffer.asUint32List();
92+
final int firstPainted = pixels.indexWhere((int p) => p != 0);
93+
expect(firstPainted, isNot(-1), reason: 'no text was drawn');
94+
// The test font's glyphs are filled squares, so ink starts at x.
95+
final int leftmostColumn = Iterable<int>.generate(
96+
pixels.length,
97+
).where((int i) => pixels[i] != 0).map((int i) => i % 100).reduce(math.min);
98+
expect(leftmostColumn, closeTo(10, 1));
99+
}, skip: kIsWeb);
100+
}
101+
73102
test('Scales image correctly', () async {
74103
final factory = TestPictureFactory();
75104
final listener = FlutterVectorGraphicsListener(pictureFactory: factory);

0 commit comments

Comments
 (0)