Fix getOffsetForCaret crash (#146669)

Fixes a crash in Google tests (b/333560406) related to a decomposed complex character.
This commit is contained in:
LongCatIsLooong 2024-04-13 04:58:02 +08:00 committed by GitHub
parent 65e8ec2140
commit 7db26b09bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 2 deletions

View file

@ -1468,10 +1468,15 @@ class TextPainter {
final _LineCaretMetrics metrics;
final List<TextBox> boxes = cachedLayout.paragraph
.getBoxesForRange(graphemeRange.start, graphemeRange.end, boxHeightStyle: ui.BoxHeightStyle.strut);
if (boxes.isNotEmpty) {
final TextBox box = boxes.single;
final bool ahchorToLeft = switch (glyphInfo.writingDirection) {
TextDirection.ltr => anchorToLeadingEdge,
TextDirection.rtl => !anchorToLeadingEdge,
};
final TextBox box = ahchorToLeft ? boxes.first : boxes.last;
metrics = _LineCaretMetrics(
offset: Offset(anchorToLeadingEdge ? box.start : box.end, box.top),
offset: Offset(ahchorToLeft ? box.left : box.right, box.top),
writingDirection: box.direction,
);
} else {

View file

@ -1682,6 +1682,20 @@ void main() {
});
}, skip: kIsWeb && !isSkiaWeb); // [intended] strut spport for HTML renderer https://github.com/flutter/flutter/issues/32243.
test('getOffsetForCaret does not crash on decomposed characters', () {
final TextPainter painter = TextPainter(
textDirection: TextDirection.ltr,
text: const TextSpan(
text: '각',
style: TextStyle(fontSize: 10),
),
)..layout(maxWidth: 1); // Force the jamo characters to soft wrap.
expect(
() => painter.getOffsetForCaret(const TextPosition(offset: 0), Rect.zero),
returnsNormally,
);
});
test('TextPainter dispatches memory events', () async {
await expectLater(
await memoryEvents(() => TextPainter().dispose(), TextPainter),