diff --git a/engine/src/flutter/lib/ui/text.dart b/engine/src/flutter/lib/ui/text.dart index e8898feacb..fb2627e3a1 100644 --- a/engine/src/flutter/lib/ui/text.dart +++ b/engine/src/flutter/lib/ui/text.dart @@ -2238,13 +2238,12 @@ enum TextAffinity { /// A position in a string of text. /// -/// A TextPosition can be used to locate a position in a string in code (using -/// the [offset] property), and it can also be used to locate the same position -/// visually in a rendered string of text (using [offset] and, when needed to -/// resolve ambiguity, [affinity]). +/// A TextPosition can be used to describe a caret position in between +/// characters. The [offset] points to the position between `offset - 1` and +/// `offset` characters of the string, and the [affinity] is used to describe +/// which character this position affiliates with. /// -/// The location of an offset in a rendered string is ambiguous in two cases. -/// One happens when rendered text is forced to wrap. In this case, the offset +/// One use case is when rendered text is forced to wrap. In this case, the offset /// where the wrap occurs could visually appear either at the end of the first /// line or the beginning of the second line. The second way is with /// bidirectional text. An offset at the interface between two different text @@ -2829,8 +2828,23 @@ class Paragraph extends NativeFieldWrapperClass1 { /// have word breaks on both sides. In such cases, this method will return /// [offset, offset+1]. Word boundaries are defined more precisely in Unicode /// Standard Annex #29 http://www.unicode.org/reports/tr29/#Word_Boundaries + /// + /// The [TextPosition] is treated as caret position, its [TextPosition.affinity] + /// is used to determine which character this position points to. For example, + /// the word boundary at `TextPosition(offset: 5, affinity: TextPosition.upstream)` + /// of the `string = 'Hello word'` will return range (0, 5) because the position + /// points to the character 'o' instead of the space. TextRange getWordBoundary(TextPosition position) { - final List boundary = _getWordBoundary(position.offset); + final int characterPosition; + switch (position.affinity) { + case TextAffinity.upstream: + characterPosition = position.offset - 1; + break; + case TextAffinity.downstream: + characterPosition = position.offset; + break; + } + final List boundary = _getWordBoundary(characterPosition); return TextRange(start: boundary[0], end: boundary[1]); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/text.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/text.dart index 104ada6655..67d9630cc6 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/text.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/text.dart @@ -775,7 +775,16 @@ class CkParagraph extends SkiaObject implements ui.Paragraph { @override ui.TextRange getWordBoundary(ui.TextPosition position) { final SkParagraph paragraph = _ensureInitialized(_lastLayoutConstraints!); - final SkTextRange skRange = paragraph.getWordBoundary(position.offset); + final int characterPosition; + switch (position.affinity) { + case ui.TextAffinity.upstream: + characterPosition = position.offset - 1; + break; + case ui.TextAffinity.downstream: + characterPosition = position.offset; + break; + } + final SkTextRange skRange = paragraph.getWordBoundary(characterPosition); return ui.TextRange(start: skRange.start, end: skRange.end); } diff --git a/engine/src/flutter/lib/web_ui/test/canvaskit/canvaskit_api_test.dart b/engine/src/flutter/lib/web_ui/test/canvaskit/canvaskit_api_test.dart index 301da66d03..0f5811d83c 100644 --- a/engine/src/flutter/lib/web_ui/test/canvaskit/canvaskit_api_test.dart +++ b/engine/src/flutter/lib/web_ui/test/canvaskit/canvaskit_api_test.dart @@ -1342,6 +1342,22 @@ void _canvasTests() { ); }); + test('Paragraph converts caret position to charactor position', () { + final CkParagraphBuilder builder = CkParagraphBuilder( + CkParagraphStyle(), + ); + builder.addText('Hello there'); + final CkParagraph paragraph = builder.build(); + paragraph.layout(const ui.ParagraphConstraints(width: 100)); + ui.TextRange range = paragraph.getWordBoundary(const ui.TextPosition(offset: 5, affinity: ui.TextAffinity.upstream)); + expect(range.start, 0); + expect(range.end, 5); + + range = paragraph.getWordBoundary(const ui.TextPosition(offset: 5)); + expect(range.start, 5); + expect(range.end, 6); + }); + test('Paragraph dispose', () { final CkParagraphBuilder builder = CkParagraphBuilder( CkParagraphStyle(), diff --git a/engine/src/flutter/testing/dart/text_test.dart b/engine/src/flutter/testing/dart/text_test.dart index 13bb1645bb..e0f9c825ec 100644 --- a/engine/src/flutter/testing/dart/text_test.dart +++ b/engine/src/flutter/testing/dart/text_test.dart @@ -288,6 +288,29 @@ void testFontVariation() { }); } +void testGetWordBoundary() { + test('GetWordBoundary', () async { + final Uint8List fontData = await readFile('RobotoSlab-VariableFont_wght.ttf'); + await loadFontFromList(fontData, fontFamily: 'RobotoSerif'); + + final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle( + fontFamily: 'RobotoSerif', + fontSize: 40.0, + )); + builder.addText('Hello team'); + final Paragraph paragraph = builder.build(); + paragraph.layout(const ParagraphConstraints(width: double.infinity)); + + TextRange range = paragraph.getWordBoundary(const TextPosition(offset: 5, affinity: TextAffinity.upstream)); + expect(range.start, 0); + expect(range.end, 5); + + range = paragraph.getWordBoundary(const TextPosition(offset: 5)); + expect(range.start, 5); + expect(range.end, 6); + }); +} + void main() { testFontWeight(); testParagraphStyle(); @@ -297,4 +320,5 @@ void main() { testLoadFontFromList(); testFontFeatureClass(); testFontVariation(); + testGetWordBoundary(); }