[skwasm] Use text position affinity when calculating word boundaries. (flutter/engine#51753)

I had failed to take position affinity into account when calculating word boundaries on skwasm. This brings skwasm's behavior in line with canvaskit's.
This commit is contained in:
Jackson Gardner
2024-03-28 12:54:12 -07:00
committed by GitHub
parent b0300e2fc2
commit 6741217138
2 changed files with 24 additions and 1 deletions

View File

@@ -233,7 +233,11 @@ class SkwasmParagraph extends SkwasmObjectWrapper<RawParagraph> implements ui.Pa
@override
ui.TextRange getWordBoundary(ui.TextPosition position) => withStackScope((StackScope scope) {
final Pointer<Int32> outRange = scope.allocInt32Array(2);
paragraphGetWordBoundary(handle, position.offset, outRange);
final int characterPosition = switch (position.affinity) {
ui.TextAffinity.upstream => position.offset - 1,
ui.TextAffinity.downstream => position.offset,
};
paragraphGetWordBoundary(handle, characterPosition, outRange);
return ui.TextRange(start: outRange[0], end: outRange[1]);
});

View File

@@ -40,6 +40,25 @@ Future<void> testMain() async {
expect(() => builder.build(), returnsNormally);
});
test('getWordBoundary respects position affinity', () {
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle());
builder.addText('hello world');
final Paragraph paragraph = builder.build();
paragraph.layout(const ParagraphConstraints(width: double.infinity));
final TextRange downstreamWordBoundary = paragraph.getWordBoundary(const TextPosition(
offset: 5,
));
expect(downstreamWordBoundary, const TextRange(start: 5, end: 6));
final TextRange upstreamWordBoundary = paragraph.getWordBoundary(const TextPosition(
offset: 5,
affinity: TextAffinity.upstream,
));
expect(upstreamWordBoundary, const TextRange(start: 0, end: 5));
});
test('build and layout a paragraph with an empty addText', () {
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle());
builder.addText('');