Skip to content

Commit 739e3bd

Browse files
authored
Update hasTrailingSpaces (#149698)
This PR addresses an issue with TextPainter's caret position calculation for text containing full-width spaces. Currently, the caret position is not accurately calculated for strings with full-width spaces. To resolve this, the following changes have been made: Corrected the logic for caret position calculation when full-width spaces are present in the text. Added and updated test cases to ensure accurate caret position calculation. These changes ensure that the caret position for text with full-width spaces is computed correctly. This issue was introduced by the commit [a0a854a](flutter/flutter@a0a854a). Related Issue: [#149099](flutter/flutter#149099)
1 parent ee10d2f commit 739e3bd

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,14 @@ class _TextLayout {
348348
final int lastLineIndex = _paragraph.numberOfLines - 1;
349349
assert(lastLineIndex >= 0);
350350
final ui.LineMetrics lineMetrics = _paragraph.getLineMetricsAt(lastLineIndex)!;
351-
// SkParagraph currently treats " " and "\t" as white spaces. Trailing white
352-
// spaces don't contribute to the line width and thus require special handling
351+
// Trailing white spaces don't contribute to the line width and thus require special handling
353352
// when they're present.
354353
// Luckily they have the same bidi embedding level as the paragraph as per
355354
// https://unicode.org/reports/tr9/#L1, so we can anchor the caret to the
356355
// last logical trailing space.
357356
final bool hasTrailingSpaces = switch (rawString.codeUnitAt(rawString.length - 1)) {
358357
0x9 || // horizontal tab
358+
0x3000 || // ideographic space
359359
0x20 => true, // space
360360
_ => false,
361361
};

packages/flutter/test/painting/text_painter_test.dart

+13
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,19 @@ void main() {
144144
painter.layout();
145145
caretOffset = painter.getOffsetForCaret(ui.TextPosition(offset: text.length), ui.Rect.zero);
146146
expect(caretOffset.dx, painter.width);
147+
148+
// Test with trailing full-width space
149+
const String textWithFullWidthSpace = 'A\u{3000}';
150+
checkCaretOffsetsLtr(textWithFullWidthSpace);
151+
painter.text = const TextSpan(text: textWithFullWidthSpace);
152+
painter.layout();
153+
caretOffset = painter.getOffsetForCaret(const ui.TextPosition(offset: 0), ui.Rect.zero);
154+
expect(caretOffset.dx, 0);
155+
caretOffset = painter.getOffsetForCaret(const ui.TextPosition(offset: 1), ui.Rect.zero);
156+
expect(caretOffset.dx, painter.width / 2);
157+
caretOffset = painter.getOffsetForCaret(const ui.TextPosition(offset: textWithFullWidthSpace.length), ui.Rect.zero);
158+
expect(caretOffset.dx, painter.width);
159+
147160
painter.dispose();
148161
});
149162

0 commit comments

Comments
 (0)