From 51d237a0895206ef4ae05c8687e64a0073776879 Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Wed, 4 Dec 2019 13:27:14 -0800 Subject: [PATCH] [web] Fix text measurement when constraint width is infinite (flutter/engine#14132) --- .../lib/web_ui/lib/src/engine/text/ruler.dart | 38 ++++++++++++------- .../web_ui/test/text/measurement_test.dart | 22 +++++++++++ 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text/ruler.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text/ruler.dart index d33ee46361..7ea7cc4973 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text/ruler.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text/ruler.dart @@ -210,9 +210,25 @@ class TextDimensions { } /// Updated element style width. - void updateWidth(String cssWidth) { + void updateConstraintWidth(double width, String ellipsis) { _invalidateBoundsCache(); - _element.style.width = cssWidth; + + if (width.isInfinite) { + _element.style + ..width = null + ..whiteSpace = 'pre'; + } else if (ellipsis != null) { + // Width is finite, but we don't want to let the text soft-wrap when + // ellipsis overflow is enabled. + _element.style + ..width = '${width}px' + ..whiteSpace = 'pre'; + } else { + // Width is finite and there's no ellipsis overflow. + _element.style + ..width = '${width}px' + ..whiteSpace = 'pre-wrap'; + } } void _invalidateBoundsCache() { @@ -475,17 +491,8 @@ class ParagraphRuler { ..display = 'block' ..overflowWrap = 'break-word'; - // TODO(flutter_web): Implement the ellipsis overflow for multi-line text - // too. As a pre-requisite, we need to be able to programmatically find - // line breaks. - if (style.ellipsis == null) { - elementStyle.whiteSpace = 'pre-wrap'; - } else { - // The height measurement is affected by whether the text has the ellipsis - // overflow property or not. This is because when ellipsis is set, we may - // not render all the lines, but stop at the first line that overflows. + if (style.ellipsis != null) { elementStyle - ..whiteSpace = 'pre' ..overflow = 'hidden' ..textOverflow = 'ellipsis'; } @@ -595,7 +602,10 @@ class ParagraphRuler { // The extra 0.5 is because sometimes the browser needs slightly more space // than the size it reports back. When that happens the text may be wrap // when we thought it didn't. - constrainedDimensions.updateWidth('${constraints.width + 0.5}px'); + constrainedDimensions.updateConstraintWidth( + constraints.width + 0.5, + style.ellipsis, + ); } /// Returns text position in a paragraph that contains multiple @@ -701,7 +711,7 @@ class ParagraphRuler { ..appendText(before) ..append(rangeSpan) ..appendText(after); - constrainedDimensions.updateWidth('${constraints.width}px'); + constrainedDimensions.updateConstraintWidth(constraints.width, null); // Measure the rects of [rangeSpan]. final List> clientRects = rangeSpan.getClientRects(); diff --git a/engine/src/flutter/lib/web_ui/test/text/measurement_test.dart b/engine/src/flutter/lib/web_ui/test/text/measurement_test.dart index 1119ffef2b..3478124486 100644 --- a/engine/src/flutter/lib/web_ui/test/text/measurement_test.dart +++ b/engine/src/flutter/lib/web_ui/test/text/measurement_test.dart @@ -406,6 +406,28 @@ void main() async { } }); + testMeasurements( + 'wraps multi-line text correctly when constraint width is infinite', + (TextMeasurementService instance) { + final EngineParagraph paragraph = build(ahemStyle, '123\n456 789'); + final MeasurementResult result = + instance.measure(paragraph, infiniteConstraints); + + expect(result.isSingleLine, false); + expect(result.maxIntrinsicWidth, 70); + expect(result.minIntrinsicWidth, 30); + expect(result.width, double.infinity); + expect(result.height, 20); + + if (instance is CanvasTextMeasurementService) { + expect(result.lines, [ + line('123', hardBreak: true, width: 30.0, lineNumber: 0), + line('456 789', hardBreak: true, width: 70.0, lineNumber: 1), + ]); + } + }, + ); + testMeasurements( 'takes letter spacing into account', (TextMeasurementService instance) {