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](a0a854a78b). Related Issue: [#149099](https://github.com/flutter/flutter/issues/149099)
This commit is contained in:
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user