diff --git a/lib/ui/text.dart b/lib/ui/text.dart index 374011ea45c66..ff4665162577f 100644 --- a/lib/ui/text.dart +++ b/lib/ui/text.dart @@ -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), @@ -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. @@ -3017,18 +3020,24 @@ abstract class Paragraph { List 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 diff --git a/testing/dart/text_test.dart b/testing/dart/text_test.dart index 5bbf5a62bba14..dd54059c8e0be 100644 --- a/testing/dart/text_test.dart +++ b/testing/dart/text_test.dart @@ -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; @@ -336,6 +348,7 @@ void main() { testTextStyle(); testTextHeightBehavior(); testTextRange(); + testGlyphInfo(); testLoadFontFromList(); testFontFeatureClass(); testFontVariation();