diff --git a/engine/src/flutter/lib/ui/text.dart b/engine/src/flutter/lib/ui/text.dart index b10ad0164c..4dc0de493b 100644 --- a/engine/src/flutter/lib/ui/text.dart +++ b/engine/src/flutter/lib/ui/text.dart @@ -3253,7 +3253,25 @@ class Paragraph extends NativeFieldWrapperClass1 { /// metrics, so use it sparingly. TextRange getLineBoundary(TextPosition position) { final List boundary = _getLineBoundary(position.offset); - return TextRange(start: boundary[0], end: boundary[1]); + final TextRange line = TextRange(start: boundary[0], end: boundary[1]); + + final List nextBoundary = _getLineBoundary(position.offset + 1); + final TextRange nextLine = TextRange(start: nextBoundary[0], end: nextBoundary[1]); + // If there is no next line, because we're at the end of the field, return + // line. + if (!nextLine.isValid) { + return line; + } + + // _getLineBoundary only considers the offset and assumes that the + // TextAffinity is upstream. In the case that TextPosition is just after a + // wordwrap (downstream), we need to return the line for the next offset. + if (position.affinity == TextAffinity.downstream && line != nextLine + && position.offset == line.end && line.end == nextLine.start) { + final List nextBoundary = _getLineBoundary(position.offset + 1); + return TextRange(start: nextBoundary[0], end: nextBoundary[1]); + } + return line; } List _getLineBoundary(int offset) native 'Paragraph_getLineBoundary'; diff --git a/engine/src/flutter/testing/dart/paragraph_test.dart b/engine/src/flutter/testing/dart/paragraph_test.dart index 61cd0bedfd..0b94f9b817 100644 --- a/engine/src/flutter/testing/dart/paragraph_test.dart +++ b/engine/src/flutter/testing/dart/paragraph_test.dart @@ -59,4 +59,151 @@ void main() { ); } }); + + test('getLineBoundary', () { + const double fontSize = 10.0; + final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle( + fontFamily: 'Ahem', + fontStyle: FontStyle.normal, + fontWeight: FontWeight.normal, + fontSize: fontSize, + )); + builder.addText('Test Ahem'); + final Paragraph paragraph = builder.build(); + paragraph.layout(ParagraphConstraints(width: fontSize * 5.0)); + + // Wraps to two lines. + expect(paragraph.height, closeTo(fontSize * 2.0, 0.001)); + + final TextPosition wrapPositionDown = TextPosition( + offset: 5, + affinity: TextAffinity.downstream, + ); + TextRange line = paragraph.getLineBoundary(wrapPositionDown); + expect(line.start, 5); + expect(line.end, 9); + + final TextPosition wrapPositionUp = TextPosition( + offset: 5, + affinity: TextAffinity.upstream, + ); + line = paragraph.getLineBoundary(wrapPositionUp); + expect(line.start, 0); + expect(line.end, 5); + + final TextPosition wrapPositionStart = TextPosition( + offset: 0, + affinity: TextAffinity.downstream, + ); + line = paragraph.getLineBoundary(wrapPositionStart); + expect(line.start, 0); + expect(line.end, 5); + + final TextPosition wrapPositionEnd = TextPosition( + offset: 9, + affinity: TextAffinity.downstream, + ); + line = paragraph.getLineBoundary(wrapPositionEnd); + expect(line.start, 5); + expect(line.end, 9); + }); + + test('getLineBoundary RTL', () { + const double fontSize = 10.0; + final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle( + fontFamily: 'Ahem', + fontStyle: FontStyle.normal, + fontWeight: FontWeight.normal, + fontSize: fontSize, + textDirection: TextDirection.rtl, + )); + builder.addText('القاهرةالقاهرة'); + final Paragraph paragraph = builder.build(); + paragraph.layout(ParagraphConstraints(width: fontSize * 5.0)); + + // Wraps to three lines. + expect(paragraph.height, closeTo(fontSize * 3.0, 0.001)); + + final TextPosition wrapPositionDown = TextPosition( + offset: 5, + affinity: TextAffinity.downstream, + ); + TextRange line = paragraph.getLineBoundary(wrapPositionDown); + expect(line.start, 5); + expect(line.end, 10); + + final TextPosition wrapPositionUp = TextPosition( + offset: 5, + affinity: TextAffinity.upstream, + ); + line = paragraph.getLineBoundary(wrapPositionUp); + expect(line.start, 0); + expect(line.end, 5); + + final TextPosition wrapPositionStart = TextPosition( + offset: 0, + affinity: TextAffinity.downstream, + ); + line = paragraph.getLineBoundary(wrapPositionStart); + expect(line.start, 0); + expect(line.end, 5); + + final TextPosition wrapPositionEnd = TextPosition( + offset: 9, + affinity: TextAffinity.downstream, + ); + line = paragraph.getLineBoundary(wrapPositionEnd); + expect(line.start, 5); + expect(line.end, 10); + }); + + test('getLineBoundary empty line', () { + const double fontSize = 10.0; + final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle( + fontFamily: 'Ahem', + fontStyle: FontStyle.normal, + fontWeight: FontWeight.normal, + fontSize: fontSize, + textDirection: TextDirection.rtl, + )); + builder.addText('Test\n\nAhem'); + final Paragraph paragraph = builder.build(); + paragraph.layout(ParagraphConstraints(width: fontSize * 5.0)); + + // Three lines due to line breaks, with the middle line being empty. + expect(paragraph.height, closeTo(fontSize * 3.0, 0.001)); + + final TextPosition emptyLinePosition = TextPosition( + offset: 5, + affinity: TextAffinity.downstream, + ); + TextRange line = paragraph.getLineBoundary(emptyLinePosition); + expect(line.start, 5); + expect(line.end, 5); + + // Since these are hard newlines, TextAffinity has no effect here. + final TextPosition emptyLinePositionUpstream = TextPosition( + offset: 5, + affinity: TextAffinity.upstream, + ); + line = paragraph.getLineBoundary(emptyLinePositionUpstream); + expect(line.start, 5); + expect(line.end, 5); + + final TextPosition endOfFirstLinePosition = TextPosition( + offset: 4, + affinity: TextAffinity.downstream, + ); + line = paragraph.getLineBoundary(endOfFirstLinePosition); + expect(line.start, 0); + expect(line.end, 4); + + final TextPosition startOfLastLinePosition = TextPosition( + offset: 6, + affinity: TextAffinity.downstream, + ); + line = paragraph.getLineBoundary(startOfLastLinePosition); + expect(line.start, 6); + expect(line.end, 10); + }); }