Skip to content

Commit 7db26b0

Browse files
Fix getOffsetForCaret crash (#146669)
Fixes a crash in Google tests (b/333560406) related to a decomposed complex character.
1 parent 65e8ec2 commit 7db26b0

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

packages/flutter/lib/src/painting/text_painter.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,10 +1468,15 @@ class TextPainter {
14681468
final _LineCaretMetrics metrics;
14691469
final List<TextBox> boxes = cachedLayout.paragraph
14701470
.getBoxesForRange(graphemeRange.start, graphemeRange.end, boxHeightStyle: ui.BoxHeightStyle.strut);
1471+
14711472
if (boxes.isNotEmpty) {
1472-
final TextBox box = boxes.single;
1473+
final bool ahchorToLeft = switch (glyphInfo.writingDirection) {
1474+
TextDirection.ltr => anchorToLeadingEdge,
1475+
TextDirection.rtl => !anchorToLeadingEdge,
1476+
};
1477+
final TextBox box = ahchorToLeft ? boxes.first : boxes.last;
14731478
metrics = _LineCaretMetrics(
1474-
offset: Offset(anchorToLeadingEdge ? box.start : box.end, box.top),
1479+
offset: Offset(ahchorToLeft ? box.left : box.right, box.top),
14751480
writingDirection: box.direction,
14761481
);
14771482
} else {

packages/flutter/test/painting/text_painter_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,20 @@ void main() {
16821682
});
16831683
}, skip: kIsWeb && !isSkiaWeb); // [intended] strut spport for HTML renderer https://github.com/flutter/flutter/issues/32243.
16841684

1685+
test('getOffsetForCaret does not crash on decomposed characters', () {
1686+
final TextPainter painter = TextPainter(
1687+
textDirection: TextDirection.ltr,
1688+
text: const TextSpan(
1689+
text: '각',
1690+
style: TextStyle(fontSize: 10),
1691+
),
1692+
)..layout(maxWidth: 1); // Force the jamo characters to soft wrap.
1693+
expect(
1694+
() => painter.getOffsetForCaret(const TextPosition(offset: 0), Rect.zero),
1695+
returnsNormally,
1696+
);
1697+
});
1698+
16851699
test('TextPainter dispatches memory events', () async {
16861700
await expectLater(
16871701
await memoryEvents(() => TextPainter().dispose(), TextPainter),

0 commit comments

Comments
 (0)