Skip to content

Commit

Permalink
Update hasTrailingSpaces (#149698)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
ttorii20 authored Jun 10, 2024
1 parent ee10d2f commit 739e3bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/flutter/lib/src/painting/text_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,14 @@ class _TextLayout {
final int lastLineIndex = _paragraph.numberOfLines - 1;
assert(lastLineIndex >= 0);
final ui.LineMetrics lineMetrics = _paragraph.getLineMetricsAt(lastLineIndex)!;
// SkParagraph currently treats " " and "\t" as white spaces. Trailing white
// spaces don't contribute to the line width and thus require special handling
// Trailing white spaces don't contribute to the line width and thus require special handling
// when they're present.
// Luckily they have the same bidi embedding level as the paragraph as per
// https://unicode.org/reports/tr9/#L1, so we can anchor the caret to the
// last logical trailing space.
final bool hasTrailingSpaces = switch (rawString.codeUnitAt(rawString.length - 1)) {
0x9 || // horizontal tab
0x3000 || // ideographic space
0x20 => true, // space
_ => false,
};
Expand Down
13 changes: 13 additions & 0 deletions packages/flutter/test/painting/text_painter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ void main() {
painter.layout();
caretOffset = painter.getOffsetForCaret(ui.TextPosition(offset: text.length), ui.Rect.zero);
expect(caretOffset.dx, painter.width);

// Test with trailing full-width space
const String textWithFullWidthSpace = 'A\u{3000}';
checkCaretOffsetsLtr(textWithFullWidthSpace);
painter.text = const TextSpan(text: textWithFullWidthSpace);
painter.layout();
caretOffset = painter.getOffsetForCaret(const ui.TextPosition(offset: 0), ui.Rect.zero);
expect(caretOffset.dx, 0);
caretOffset = painter.getOffsetForCaret(const ui.TextPosition(offset: 1), ui.Rect.zero);
expect(caretOffset.dx, painter.width / 2);
caretOffset = painter.getOffsetForCaret(const ui.TextPosition(offset: textWithFullWidthSpace.length), ui.Rect.zero);
expect(caretOffset.dx, painter.width);

painter.dispose();
});

Expand Down

0 comments on commit 739e3bd

Please sign in to comment.