Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit b1dbb43

Browse files
[skwasm] Fix Paragraph.getLineBoundary (#51846)
The Skia APIs I was previously using were returning `-1` for `paragraphGetLineNumberAt` when querying at the end of the string. I changed the implementation to be identical to what CanvasKit and the native Paragraph object are doing.
1 parent c60b00a commit b1dbb43

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paragraph.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ class SkwasmLineMetrics extends SkwasmObjectWrapper<RawLineMetrics> implements u
7777

7878
@override
7979
int get lineNumber => lineMetricsGetLineNumber(handle);
80+
81+
int get startIndex => lineMetricsGetStartIndex(handle);
82+
int get endIndex => lineMetricsGetEndIndex(handle);
8083
}
8184

8285
class SkwasmParagraph extends SkwasmObjectWrapper<RawParagraph> implements ui.Paragraph {
@@ -243,15 +246,13 @@ class SkwasmParagraph extends SkwasmObjectWrapper<RawParagraph> implements ui.Pa
243246

244247
@override
245248
ui.TextRange getLineBoundary(ui.TextPosition position) {
246-
final int lineNumber = paragraphGetLineNumberAt(handle, position.offset);
247-
final LineMetricsHandle metricsHandle =
248-
paragraphGetLineMetricsAtIndex(handle, lineNumber);
249-
final ui.TextRange range = ui.TextRange(
250-
start: lineMetricsGetStartIndex(metricsHandle),
251-
end: lineMetricsGetEndIndex(metricsHandle),
252-
);
253-
lineMetricsDispose(metricsHandle);
254-
return range;
249+
final int offset = position.offset;
250+
for (final SkwasmLineMetrics metrics in computeLineMetrics()) {
251+
if (offset >= metrics.startIndex && offset <= metrics.endIndex) {
252+
return ui.TextRange(start: metrics.startIndex, end: metrics.endIndex);
253+
}
254+
}
255+
return ui.TextRange.empty;
255256
}
256257

257258
@override

lib/web_ui/test/ui/paragraph_builder_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ Future<void> testMain() async {
5959
expect(upstreamWordBoundary, const TextRange(start: 0, end: 5));
6060
});
6161

62+
test('getLineBoundary at the last character position gives correct results', () {
63+
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle());
64+
builder.addText('hello world');
65+
66+
final Paragraph paragraph = builder.build();
67+
paragraph.layout(const ParagraphConstraints(width: double.infinity));
68+
69+
final TextRange lineBoundary = paragraph.getLineBoundary(const TextPosition(
70+
offset: 11,
71+
));
72+
expect(lineBoundary, const TextRange(start: 0, end: 11));
73+
});
74+
6275
test('build and layout a paragraph with an empty addText', () {
6376
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle());
6477
builder.addText('');

0 commit comments

Comments
 (0)