Skip to content

Commit 08e467d

Browse files
authored
Clipping if only one character text overflows (#99146)
1 parent e2d1206 commit 08e467d

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

packages/flutter/lib/src/painting/text_painter.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,15 @@ class TextPainter {
586586
return _paragraph!.didExceedMaxLines;
587587
}
588588

589+
/// The distance from the left edge of the leftmost glyph to the right edge of
590+
/// the rightmost glyph in the paragraph.
591+
///
592+
/// Valid only after [layout] has been called.
593+
double get longestLine {
594+
assert(!_debugNeedsLayout);
595+
return _paragraph!.longestLine;
596+
}
597+
589598
double? _lastMinWidth;
590599
double? _lastMaxWidth;
591600

packages/flutter/lib/src/rendering/paragraph.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,12 @@ class RenderParagraph extends RenderBox
493493
@visibleForTesting
494494
bool get debugHasOverflowShader => _overflowShader != null;
495495

496+
/// Whether this paragraph currently has overflow and needs clipping.
497+
///
498+
/// Used to test this object. Not for use in production.
499+
@visibleForTesting
500+
bool get debugNeedsClipping => _needsClipping;
501+
496502
void _layoutText({ double minWidth = 0.0, double maxWidth = double.infinity }) {
497503
final bool widthMatters = softWrap || overflow == TextOverflow.ellipsis;
498504
_textPainter.layout(
@@ -644,7 +650,7 @@ class RenderParagraph extends RenderBox
644650
size = constraints.constrain(textSize);
645651

646652
final bool didOverflowHeight = size.height < textSize.height || textDidExceedMaxLines;
647-
final bool didOverflowWidth = size.width < textSize.width;
653+
final bool didOverflowWidth = size.width < textSize.width || size.width < _textPainter.longestLine;
648654
// TODO(abarth): We're only measuring the sizes of the line boxes here. If
649655
// the glyphs draw outside the line boxes, we might think that there isn't
650656
// visual overflow when there actually is visual overflow. This can become

packages/flutter/test/rendering/paragraph_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,24 @@ void main() {
326326
expect(paragraph.debugHasOverflowShader, isFalse);
327327
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/61018
328328

329+
test('one character clip test', () {
330+
// Regressing test for https://github.com/flutter/flutter/issues/99140
331+
final RenderParagraph paragraph = RenderParagraph(
332+
const TextSpan(
333+
text: '7',
334+
style: TextStyle(fontFamily: 'Ahem', fontSize: 60.0),
335+
),
336+
textDirection: TextDirection.ltr,
337+
maxLines: 1,
338+
);
339+
340+
// Lay out in a narrow box to force clipping.
341+
// The text width is 60 bigger than the constraints width.
342+
layout(paragraph, constraints: BoxConstraints.tight(const Size(50.0, 200.0)));
343+
344+
expect(paragraph.debugNeedsClipping, true);
345+
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/61018
346+
329347
test('maxLines', () {
330348
final RenderParagraph paragraph = RenderParagraph(
331349
const TextSpan(

0 commit comments

Comments
 (0)