From 739e3bd0e8358d2bee3141e45ca4be6af91a36dd Mon Sep 17 00:00:00 2001 From: Takahiro Torii Date: Tue, 11 Jun 2024 01:48:11 +0900 Subject: [PATCH] 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](https://github.com/flutter/flutter/commit/a0a854a78ba3ff06c01c36eb975b9a25cbce9063). Related Issue: [#149099](https://github.com/flutter/flutter/issues/149099) --- packages/flutter/lib/src/painting/text_painter.dart | 4 ++-- .../flutter/test/painting/text_painter_test.dart | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/painting/text_painter.dart b/packages/flutter/lib/src/painting/text_painter.dart index 2bf0faca41..4b1884f73b 100644 --- a/packages/flutter/lib/src/painting/text_painter.dart +++ b/packages/flutter/lib/src/painting/text_painter.dart @@ -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, }; diff --git a/packages/flutter/test/painting/text_painter_test.dart b/packages/flutter/test/painting/text_painter_test.dart index 8534f5297b..d946807d7b 100644 --- a/packages/flutter/test/painting/text_painter_test.dart +++ b/packages/flutter/test/painting/text_painter_test.dart @@ -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(); });