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

Fix a bug in the HTML render's getClosestGlyphInfo implementation #48774

Merged
merged 1 commit into from
Dec 14, 2023
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
11 changes: 7 additions & 4 deletions lib/web_ui/lib/src/engine/text/layout_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,11 @@ class TextLayoutService {
}

ui.GlyphInfo? getClosestGlyphInfo(ui.Offset offset) {
final LayoutFragment? fragment = _findLineForY(offset.dy)
?.closestFragmentAtOffset(offset.dx);
final ParagraphLine? line = _findLineForY(offset.dy);
if (line == null) {
return null;
}
final LayoutFragment? fragment = line.closestFragmentAtOffset(offset.dx - line.left);
if (fragment == null) {
return null;
}
Expand All @@ -431,8 +434,8 @@ class TextLayoutService {
|| fragment.line.left + fragment.line.width <= dx
|| switch (fragment.textDirection!) {
// If dx is closer to the trailing edge, no need to check other fragments.
ui.TextDirection.ltr => dx >= (fragment.left + fragment.right) / 2,
ui.TextDirection.rtl => dx <= (fragment.left + fragment.right) / 2,
ui.TextDirection.ltr => dx >= line.left + (fragment.left + fragment.right) / 2,
ui.TextDirection.rtl => dx <= line.left + (fragment.left + fragment.right) / 2,
};
final ui.GlyphInfo candidate1 = fragment.getClosestCharacterBox(dx);
if (closestGraphemeStartInFragment) {
Expand Down
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 @@ -380,7 +380,7 @@ class ParagraphLine {

final double? minDistance = closestFragment?.distance;
if (minDistance == null || minDistance > distance) {
closestFragment = (fragment: fragment, distance: distance);
closestFragment = (fragment: fragment, distance: distance);
}
}
return closestFragment?.fragment;
Expand Down
23 changes: 23 additions & 0 deletions lib/web_ui/test/html/text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,29 @@ Future<void> testMain() async {
expect(bottomRight?.writingDirection, TextDirection.ltr);
});

test('Basic glyph metrics - hit test - center aligned text in separate fragments', () {
const double fontSize = 10.0;
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(
fontSize: fontSize,
textAlign: TextAlign.center,
fontFamily: 'FlutterTest',
))..addText('12345\n')
..addText('1')
..addText('2')
..addText('3');
final Paragraph paragraph = builder.build();
paragraph.layout(const ParagraphConstraints(width: 50));

final GlyphInfo? bottomCenter = paragraph.getClosestGlyphInfoForOffset(const Offset(25.0, 99.0));
final GlyphInfo? expected = paragraph.getGlyphInfoAt(7);
expect(bottomCenter, equals(expected));
expect(bottomCenter, isNot(paragraph.getGlyphInfoAt(8)));

expect(bottomCenter?.graphemeClusterLayoutBounds, const Rect.fromLTWH(20, 10, 10, 10));
expect(bottomCenter?.graphemeClusterCodeUnitRange, const TextRange(start: 7, end: 8));
expect(bottomCenter?.writingDirection, TextDirection.ltr);
});

test('Glyph metrics with grapheme split into different runs', () {
const double fontSize = 10;
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(
Expand Down