Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paragraph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ class SkwasmLineMetrics extends SkwasmObjectWrapper<RawLineMetrics> implements u

@override
int get lineNumber => lineMetricsGetLineNumber(handle);

int get startIndex => lineMetricsGetStartIndex(handle);
int get endIndex => lineMetricsGetEndIndex(handle);
}

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

@override
ui.TextRange getLineBoundary(ui.TextPosition position) {
final int lineNumber = paragraphGetLineNumberAt(handle, position.offset);
final LineMetricsHandle metricsHandle =
paragraphGetLineMetricsAtIndex(handle, lineNumber);
final ui.TextRange range = ui.TextRange(
start: lineMetricsGetStartIndex(metricsHandle),
end: lineMetricsGetEndIndex(metricsHandle),
);
lineMetricsDispose(metricsHandle);
return range;
final int offset = position.offset;
for (final SkwasmLineMetrics metrics in computeLineMetrics()) {
if (offset >= metrics.startIndex && offset <= metrics.endIndex) {
return ui.TextRange(start: metrics.startIndex, end: metrics.endIndex);
}
}
return ui.TextRange.empty;
}

@override
Expand Down
13 changes: 13 additions & 0 deletions lib/web_ui/test/ui/paragraph_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ Future<void> testMain() async {
expect(upstreamWordBoundary, const TextRange(start: 0, end: 5));
});

test('getLineBoundary at the last character position gives correct results', () {
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle());
builder.addText('hello world');

final Paragraph paragraph = builder.build();
paragraph.layout(const ParagraphConstraints(width: double.infinity));

final TextRange lineBoundary = paragraph.getLineBoundary(const TextPosition(
offset: 11,
));
expect(lineBoundary, const TextRange(start: 0, end: 11));
});

test('build and layout a paragraph with an empty addText', () {
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle());
builder.addText('');
Expand Down