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

Commit 866cc36

Browse files
Add a constructor for GlyphInfo. (#48971)
Needed for flutter/flutter#139717 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent d202141 commit 866cc36

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

lib/ui/text.dart

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,9 @@ class FontVariation {
12011201
/// * [Paragraph.getClosestGlyphInfoForOffset], which finds the [GlyphInfo] of
12021202
/// the glyph(s) onscreen that's closest to the given [Offset].
12031203
final class GlyphInfo {
1204+
/// Creates a [GlyphInfo] with the specified values.
1205+
GlyphInfo(this.graphemeClusterLayoutBounds, this.graphemeClusterCodeUnitRange, this.writingDirection);
1206+
12041207
GlyphInfo._(double left, double top, double right, double bottom, int graphemeStart, int graphemeEnd, bool isLTR)
12051208
: graphemeClusterLayoutBounds = Rect.fromLTRB(left, top, right, bottom),
12061209
graphemeClusterCodeUnitRange = TextRange(start: graphemeStart, end: graphemeEnd),
@@ -1209,7 +1212,7 @@ final class GlyphInfo {
12091212
/// The layout bounding rect of the associated character, in the paragraph's
12101213
/// coordinates.
12111214
///
1212-
/// This is **not** the tight bounding box that encloses the character's outline.
1215+
/// This is **not** a tight bounding box that encloses the character's outline.
12131216
/// The vertical extent reported is derived from the font metrics (instead of
12141217
/// glyph metrics), and the horizontal extent is the horizontal advance of the
12151218
/// character.
@@ -3017,18 +3020,24 @@ abstract class Paragraph {
30173020
List<TextBox> getBoxesForPlaceholders();
30183021

30193022
/// Returns the text position closest to the given offset.
3023+
///
3024+
/// This method always returns a [TextPosition] for any given [offset], even
3025+
/// when the [offset] is not close to any text, or when the paragraph is empty.
3026+
/// This is useful for determining the text to select when the user drags the
3027+
/// text selection handle.
3028+
///
3029+
/// See also:
3030+
///
3031+
/// * [getClosestGlyphInfoForOffset], which returns more information about
3032+
/// the closest character to an [Offset].
30203033
TextPosition getPositionForOffset(Offset offset);
30213034

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

30343043
/// Returns the [GlyphInfo] located at the given UTF-16 `codeUnitOffset` in

testing/dart/text_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ void testTextRange() {
193193
});
194194
}
195195

196+
void testGlyphInfo() {
197+
test('constructor', () {
198+
const Rect testRect = Rect.fromLTWH(1, 2, 3, 4);
199+
const TextRange testRange = TextRange(start: 5, end: 6);
200+
const TextDirection testDirection = TextDirection.ltr;
201+
final GlyphInfo info = GlyphInfo(testRect, testRange, testDirection);
202+
expect(info.graphemeClusterLayoutBounds, testRect);
203+
expect(info.graphemeClusterCodeUnitRange, testRange);
204+
expect(info.writingDirection, testDirection);
205+
});
206+
}
207+
196208
void testLoadFontFromList() {
197209
test('loadFontFromList will send platform message after font is loaded', () async {
198210
late String message;
@@ -336,6 +348,7 @@ void main() {
336348
testTextStyle();
337349
testTextHeightBehavior();
338350
testTextRange();
351+
testGlyphInfo();
339352
testLoadFontFromList();
340353
testFontFeatureClass();
341354
testFontVariation();

0 commit comments

Comments
 (0)