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
2 changes: 1 addition & 1 deletion lib/web_ui/lib/src/engine/text/paragraph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ class EngineParagraph implements ui.Paragraph {
// code below will do a binary search to find where exactly the [offset]
// falls within the line.

final double dx = offset.dx - _alignOffset;
final double dx = offset.dx - lineMetrics.left;
final TextMeasurementService instance = _measurementService;

int low = lineMetrics.startIndex;
Expand Down
109 changes: 109 additions & 0 deletions lib/web_ui/test/paragraph_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,115 @@ void main() async {
TextMeasurementService.enableExperimentalCanvasImplementation = false;
});

test('getPositionForOffset multi-line centered', () {
TextMeasurementService.enableExperimentalCanvasImplementation = true;
TextMeasurementService.initialize(rulerCacheCapacity: 2);

final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(
fontFamily: 'Ahem',
fontStyle: FontStyle.normal,
fontWeight: FontWeight.normal,
fontSize: 10,
textDirection: TextDirection.ltr,
textAlign: TextAlign.center,
));
builder.addText('abcd\n');
builder.addText('abcdefg\n');
builder.addText('ab');
final Paragraph paragraph = builder.build();
paragraph.layout(const ParagraphConstraints(width: 100));

// First line: "abcd\n"

// At the beginning of the first line.
expect(
paragraph.getPositionForOffset(Offset(0, 5)),
TextPosition(offset: 0, affinity: TextAffinity.downstream),
);
// Above the first line.
expect(
paragraph.getPositionForOffset(Offset(0, -15)),
TextPosition(offset: 0, affinity: TextAffinity.downstream),
);
// At the end of the first line.
expect(
paragraph.getPositionForOffset(Offset(100, 5)),
TextPosition(offset: 4, affinity: TextAffinity.upstream),
);
// On the left side of "b" in the first line.
expect(
// The line is centered so it's shifted to the right by "30.0px".
paragraph.getPositionForOffset(Offset(30.0 + 14, 5)),
TextPosition(offset: 1, affinity: TextAffinity.downstream),
);
// On the right side of "b" in the first line.
expect(
// The line is centered so it's shifted to the right by "30.0px".
paragraph.getPositionForOffset(Offset(30.0 + 16, 5)),
TextPosition(offset: 2, affinity: TextAffinity.upstream),
);


// Second line: "abcdefg\n"

// At the beginning of the second line.
expect(
paragraph.getPositionForOffset(Offset(0, 15)),
TextPosition(offset: 5, affinity: TextAffinity.downstream),
);
// At the end of the second line.
expect(
paragraph.getPositionForOffset(Offset(100, 15)),
TextPosition(offset: 12, affinity: TextAffinity.upstream),
);
// On the left side of "e" in the second line.
expect(
// The line is centered so it's shifted to the right by "15.0px".
paragraph.getPositionForOffset(Offset(15.0 + 44, 15)),
TextPosition(offset: 9, affinity: TextAffinity.downstream),
);
// On the right side of "e" in the second line.
expect(
// The line is centered so it's shifted to the right by "15.0px".
paragraph.getPositionForOffset(Offset(15.0 + 46, 15)),
TextPosition(offset: 10, affinity: TextAffinity.upstream),
);


// Last (third) line: "ab"

// At the beginning of the last line.
expect(
paragraph.getPositionForOffset(Offset(0, 25)),
TextPosition(offset: 13, affinity: TextAffinity.downstream),
);
// At the end of the last line.
expect(
paragraph.getPositionForOffset(Offset(100, 25)),
TextPosition(offset: 15, affinity: TextAffinity.upstream),
);
// Below the last line.
expect(
paragraph.getPositionForOffset(Offset(0, 32)),
TextPosition(offset: 15, affinity: TextAffinity.upstream),
);
// On the left side of "b" in the last line.
expect(
// The line is centered so it's shifted to the right by "40.0px".
paragraph.getPositionForOffset(Offset(40.0 + 12, 25)),
TextPosition(offset: 14, affinity: TextAffinity.downstream),
);
// On the right side of "a" in the last line.
expect(
// The line is centered so it's shifted to the right by "40.0px".
paragraph.getPositionForOffset(Offset(40.0 + 9, 25)),
TextPosition(offset: 14, affinity: TextAffinity.upstream),
);

TextMeasurementService.clearCache();
TextMeasurementService.enableExperimentalCanvasImplementation = false;
});

testEachMeasurement('getBoxesForRange returns a box', () {
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(
fontFamily: 'Ahem',
Expand Down