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

Add a constructor for GlyphInfo. #48971

Merged
merged 1 commit into from
Dec 13, 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
23 changes: 16 additions & 7 deletions lib/ui/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,9 @@ class FontVariation {
/// * [Paragraph.getClosestGlyphInfoForOffset], which finds the [GlyphInfo] of
/// the glyph(s) onscreen that's closest to the given [Offset].
final class GlyphInfo {
/// Creates a [GlyphInfo] with the specified values.
GlyphInfo(this.graphemeClusterLayoutBounds, this.graphemeClusterCodeUnitRange, this.writingDirection);

GlyphInfo._(double left, double top, double right, double bottom, int graphemeStart, int graphemeEnd, bool isLTR)
: graphemeClusterLayoutBounds = Rect.fromLTRB(left, top, right, bottom),
graphemeClusterCodeUnitRange = TextRange(start: graphemeStart, end: graphemeEnd),
Expand All @@ -1209,7 +1212,7 @@ final class GlyphInfo {
/// The layout bounding rect of the associated character, in the paragraph's
/// coordinates.
///
/// This is **not** the tight bounding box that encloses the character's outline.
/// This is **not** a tight bounding box that encloses the character's outline.
/// The vertical extent reported is derived from the font metrics (instead of
/// glyph metrics), and the horizontal extent is the horizontal advance of the
/// character.
Expand Down Expand Up @@ -3017,18 +3020,24 @@ abstract class Paragraph {
List<TextBox> getBoxesForPlaceholders();

/// Returns the text position closest to the given offset.
///
/// This method always returns a [TextPosition] for any given [offset], even
/// when the [offset] is not close to any text, or when the paragraph is empty.
/// This is useful for determining the text to select when the user drags the
/// text selection handle.
///
/// See also:
///
/// * [getClosestGlyphInfoForOffset], which returns more information about
/// the closest character to an [Offset].
TextPosition getPositionForOffset(Offset offset);

/// Returns the [GlyphInfo] of the glyph closest to the given `offset` in the
/// paragraph coordinate system, or null if the glyph is not in the visible
/// range.
/// paragraph coordinate system, or null if if the text is empty, or is
/// entirely clipped or ellipsized away.
///
/// This method first finds the line closest to `offset.dy`, and then returns
/// the [GlyphInfo] of the closest glyph(s) within that line.
///
/// This method can be used to implement per-glyph hit-testing. The returned
/// [GlyphInfo] can help determine whether the given `offset` directly hits a
/// glyph in the paragraph.
GlyphInfo? getClosestGlyphInfoForOffset(Offset offset);

/// Returns the [GlyphInfo] located at the given UTF-16 `codeUnitOffset` in
Expand Down
13 changes: 13 additions & 0 deletions testing/dart/text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ void testTextRange() {
});
}

void testGlyphInfo() {
test('constructor', () {
const Rect testRect = Rect.fromLTWH(1, 2, 3, 4);
const TextRange testRange = TextRange(start: 5, end: 6);
const TextDirection testDirection = TextDirection.ltr;
final GlyphInfo info = GlyphInfo(testRect, testRange, testDirection);
expect(info.graphemeClusterLayoutBounds, testRect);
expect(info.graphemeClusterCodeUnitRange, testRange);
expect(info.writingDirection, testDirection);
});
}

void testLoadFontFromList() {
test('loadFontFromList will send platform message after font is loaded', () async {
late String message;
Expand Down Expand Up @@ -336,6 +348,7 @@ void main() {
testTextStyle();
testTextHeightBehavior();
testTextRange();
testGlyphInfo();
testLoadFontFromList();
testFontFeatureClass();
testFontVariation();
Expand Down