Paragraph.GetWordBoundary respects text affinity (flutter/engine#36176)

This commit is contained in:
chunhtai
2022-09-19 15:00:15 -07:00
committed by GitHub
parent 83a2d5f9ba
commit 44595a130f
4 changed files with 71 additions and 8 deletions

View File

@@ -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<int> 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<int> boundary = _getWordBoundary(characterPosition);
return TextRange(start: boundary[0], end: boundary[1]);
}

View File

@@ -775,7 +775,16 @@ class CkParagraph extends SkiaObject<SkParagraph> 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);
}

View File

@@ -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(),

View File

@@ -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();
}