From b9f11553e7ad80ed92607209af6101974fb865ec Mon Sep 17 00:00:00 2001 From: Yegor Date: Mon, 18 Apr 2022 17:09:10 -0700 Subject: [PATCH] [web] tighten paragraph bounds estimate (flutter/engine#31272) * [web] tighten paragraph bounds estimate --- .../lib/src/engine/html/recording_canvas.dart | 13 +++-- .../lib/src/engine/text/canvas_paragraph.dart | 3 + .../lib/src/engine/text/layout_service.dart | 27 ++++++++- .../test/engine/recording_canvas_test.dart | 53 +++++++++++++++++ .../html/paragraph/general_golden_test.dart | 57 +++++++++++++++++++ .../html/recording_canvas_golden_test.dart | 38 ++++++++++--- 6 files changed, 173 insertions(+), 18 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/html/recording_canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/html/recording_canvas.dart index 13bfed38d5..92c32052a3 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/html/recording_canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/html/recording_canvas.dart @@ -595,17 +595,18 @@ class RecordingCanvas { renderStrategy.hasArbitraryPaint = true; } renderStrategy.hasParagraphs = true; - final double left = offset.dx; - final double top = offset.dy; final PaintDrawParagraph command = PaintDrawParagraph(engineParagraph, offset); + + final ui.Rect paragraphBounds = engineParagraph.paintBounds; _paintBounds.growLTRB( - left, - top, - left + engineParagraph.width, - top + engineParagraph.height, + offset.dx + paragraphBounds.left, + offset.dy + paragraphBounds.top, + offset.dx + paragraphBounds.right, + offset.dy + paragraphBounds.bottom, command, ); + _commands.add(command); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text/canvas_paragraph.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text/canvas_paragraph.dart index 77f8f4ca00..991377e221 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text/canvas_paragraph.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text/canvas_paragraph.dart @@ -76,6 +76,9 @@ class CanvasParagraph implements ui.Paragraph { @override bool get didExceedMaxLines => _layoutService.didExceedMaxLines; + /// The bounds that contain the text painted inside this paragraph. + ui.Rect get paintBounds => _layoutService.paintBounds; + /// Whether this paragraph has been laid out or not. bool isLaidOut = false; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text/layout_service.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text/layout_service.dart index ab3cc21eca..e4a0201787 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text/layout_service.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text/layout_service.dart @@ -47,6 +47,10 @@ class TextLayoutService { final List lines = []; + /// The bounds that contain the text painted inside this paragraph. + ui.Rect get paintBounds => _paintBounds; + ui.Rect _paintBounds = ui.Rect.zero; + // *** Convenient shortcuts used during layout *** // int? get maxLines => paragraph.paragraphStyle.maxLines; @@ -204,10 +208,12 @@ class TextLayoutService { } } - // ************************************************** // - // *** PARAGRAPH BASELINE & HEIGHT & LONGEST LINE *** // - // ************************************************** // + // ***************************************************************** // + // *** PARAGRAPH BASELINE & HEIGHT & LONGEST LINE & PAINT BOUNDS *** // + // ***************************************************************** // + double boundsLeft = double.infinity; + double boundsRight = double.negativeInfinity; for (final EngineLineMetrics line in lines) { height += line.height; if (alphabeticBaseline == -1.0) { @@ -218,7 +224,22 @@ class TextLayoutService { if (longestLineWidth < line.width) { longestLine = line; } + + final double left = line.left; + if (left < boundsLeft) { + boundsLeft = left; + } + final double right = left + line.width; + if (right > boundsRight) { + boundsRight = right; + } } + _paintBounds = ui.Rect.fromLTRB( + boundsLeft, + 0, + boundsRight, + height, + ); // ********************** // // *** POSITION BOXES *** // diff --git a/engine/src/flutter/lib/web_ui/test/engine/recording_canvas_test.dart b/engine/src/flutter/lib/web_ui/test/engine/recording_canvas_test.dart index 6097a7dd1f..69ec08efce 100644 --- a/engine/src/flutter/lib/web_ui/test/engine/recording_canvas_test.dart +++ b/engine/src/flutter/lib/web_ui/test/engine/recording_canvas_test.dart @@ -7,6 +7,7 @@ import 'package:test/test.dart'; import 'package:ui/src/engine.dart'; import 'package:ui/ui.dart'; +import '../html/paragraph/text_scuba.dart'; import '../mock_engine_canvas.dart'; void main() { @@ -14,6 +15,9 @@ void main() { } void testMain() { + debugEmulateFlutterTesterEnvironment = true; + setUpStableTestFonts(); + late RecordingCanvas underTest; late MockEngineCanvas mockCanvas; const Rect screenRect = Rect.largest; @@ -23,6 +27,55 @@ void testMain() { mockCanvas = MockEngineCanvas(); }); + group('paragraph bounds', () { + Paragraph _paragraphForBoundsTest(TextAlign alignment) { + final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle( + fontFamily: 'Ahem', + fontSize: 20, + textAlign: alignment, + )); + builder.addText('A AAAAA AAA'); + return builder.build(); + } + + test('not laid out', () { + final Paragraph paragraph = _paragraphForBoundsTest(TextAlign.start); + underTest.drawParagraph(paragraph, Offset.zero); + underTest.endRecording(); + expect(underTest.pictureBounds, Rect.zero); + }); + + test('finite width', () { + final Paragraph paragraph = _paragraphForBoundsTest(TextAlign.start); + paragraph.layout(const ParagraphConstraints(width: 110)); + underTest.drawParagraph(paragraph, Offset.zero); + underTest.endRecording(); + expect(paragraph.width, 110); + expect(paragraph.height, 60); + expect(underTest.pictureBounds, const Rect.fromLTRB(0, 0, 100, 60)); + }); + + test('finite width center-aligned', () { + final Paragraph paragraph = _paragraphForBoundsTest(TextAlign.center); + paragraph.layout(const ParagraphConstraints(width: 110)); + underTest.drawParagraph(paragraph, Offset.zero); + underTest.endRecording(); + expect(paragraph.width, 110); + expect(paragraph.height, 60); + expect(underTest.pictureBounds, const Rect.fromLTRB(5, 0, 105, 60)); + }); + + test('infinite width', () { + final Paragraph paragraph = _paragraphForBoundsTest(TextAlign.start); + paragraph.layout(const ParagraphConstraints(width: double.infinity)); + underTest.drawParagraph(paragraph, Offset.zero); + underTest.endRecording(); + expect(paragraph.width, double.infinity); + expect(paragraph.height, 20); + expect(underTest.pictureBounds, const Rect.fromLTRB(0, 0, 220, 20)); + }); + }); + group('drawDRRect', () { final RRect rrect = RRect.fromLTRBR(10, 10, 50, 50, const Radius.circular(3)); final SurfacePaint somePaint = SurfacePaint() diff --git a/engine/src/flutter/lib/web_ui/test/html/paragraph/general_golden_test.dart b/engine/src/flutter/lib/web_ui/test/html/paragraph/general_golden_test.dart index b456be371b..184ad3597c 100644 --- a/engine/src/flutter/lib/web_ui/test/html/paragraph/general_golden_test.dart +++ b/engine/src/flutter/lib/web_ui/test/html/paragraph/general_golden_test.dart @@ -583,4 +583,61 @@ Future testMain() async { testForegroundStyle(canvas); return takeScreenshot(canvas, bounds, 'canvas_paragraph_foreground_style_dom'); }); + + test('paragraph bounds hug the text inside the paragraph', () async { + const Rect bounds = Rect.fromLTWH(0, 0, 150, 100); + + final CanvasParagraphBuilder builder = CanvasParagraphBuilder(EngineParagraphStyle( + fontFamily: 'Ahem', + fontSize: 20, + textAlign: TextAlign.center, + )); + + // Expected layout with center-alignment is something like this: + // + // _________________ + // | A | + // | |AAAAA| | + // | | AAA | | + // |----|-----|----| + // | |<--->| | + // | 100 | + // | | + // |<------------->| + // 110 + // + // The width of the paragraph is bigger than the actual content because the + // longest line "AAAAA" is 100px, which is smaller than 110px specified in + // the constraint. After the layout and centering the paint bounds would + // "hug" the text inside the paragraph more tightly than the box allocated + // for the paragraph. + builder.addText('A AAAAA AAA'); + + final CanvasParagraph paragraph = builder.build(); + paragraph.layout(const ParagraphConstraints(width: 110)); + final BitmapCanvas canvas = BitmapCanvas(bounds, RenderStrategy()); + canvas.translate(20, 20); + canvas.drawParagraph(paragraph, Offset.zero); + canvas.drawRect( + Rect.fromLTRB( + 0, + 0, + paragraph.width, + paragraph.height, + ), + SurfacePaintData() + ..style = PaintingStyle.stroke + ..strokeWidth = 1, + ); + + canvas.drawRect( + paragraph.paintBounds, + SurfacePaintData() + ..color = const Color(0xFF00FF00) + ..style = PaintingStyle.stroke + ..strokeWidth = 1, + ); + + await takeScreenshot(canvas, bounds, 'canvas_paragraph_bounds'); + }); } diff --git a/engine/src/flutter/lib/web_ui/test/html/recording_canvas_golden_test.dart b/engine/src/flutter/lib/web_ui/test/html/recording_canvas_golden_test.dart index 8e1025e6b6..0aa72ad4e3 100644 --- a/engine/src/flutter/lib/web_ui/test/html/recording_canvas_golden_test.dart +++ b/engine/src/flutter/lib/web_ui/test/html/recording_canvas_golden_test.dart @@ -14,12 +14,19 @@ import 'package:web_engine_tester/golden_tester.dart'; import '../common.dart'; import '../matchers.dart'; +import 'paragraph/text_scuba.dart'; void main() { internalBootstrapBrowserTest(() => testMain); } Future testMain() async { + setUpAll(() async { + debugEmulateFlutterTesterEnvironment = true; + }); + + setUpStableTestFonts(); + const double screenWidth = 600.0; const double screenHeight = 800.0; const Rect screenRect = Rect.fromLTWH(0, 0, screenWidth, screenHeight); @@ -67,13 +74,6 @@ Future testMain() async { } } - setUpAll(() async { - debugEmulateFlutterTesterEnvironment = true; - await webOnlyInitializePlatform(); - fontCollection.debugRegisterTestFonts(); - await fontCollection.ensureFontsLoaded(); - }); - test('Empty canvas reports correct paint bounds', () async { final RecordingCanvas rc = RecordingCanvas(const Rect.fromLTWH(1, 2, 300, 400)); @@ -315,9 +315,16 @@ Future testMain() async { paragraph.layout(const ParagraphConstraints(width: widthConstraint)); rc.drawParagraph(paragraph, const Offset(textLeft, textTop)); rc.endRecording(); + expect( + rc.pictureBounds!.width, + lessThan(widthConstraint), + reason: 'The given width constraint $widthConstraint is more than the ' + 'test string needs, so the width of the visible text is actually ' + 'smaller than the given width.', + ); expect( rc.pictureBounds, - const Rect.fromLTRB(textLeft, textTop, textLeft + widthConstraint, 21.0), + Rect.fromLTRB(textLeft, textTop, textLeft + paragraph.maxIntrinsicWidth, 21.0), ); await _checkScreenshot(rc, 'draw_paragraph'); }, // TODO(mdebbar): https://github.com/flutter/flutter/issues/65789 @@ -334,9 +341,22 @@ Future testMain() async { paragraph.layout(const ParagraphConstraints(width: widthConstraint)); rc.drawParagraph(paragraph, const Offset(textLeft, textTop)); rc.endRecording(); + + const double fontWidth = 14; + const int lettersInLongestWord = 9; + const double longestLineWidth = lettersInLongestWord * fontWidth; + expect( + rc.pictureBounds!.width, + lessThan(widthConstraint), + reason: 'The test string "A short sentence." is broken up into two lines, ' + '"A short" and "sentence.". The longest line contains ' + '$lettersInLongestWord characters, each ${fontWidth}px wide. ' + 'That line is ${longestLineWidth}px wide, which is less than ' + '$widthConstraint.', + ); expect( rc.pictureBounds, - const Rect.fromLTRB(textLeft, textTop, textLeft + widthConstraint, 35.0), + const Rect.fromLTRB(textLeft, textTop, textLeft + longestLineWidth, 35.0), ); await _checkScreenshot(rc, 'draw_paragraph_multi_line'); }, // TODO(mdebbar): https://github.com/flutter/flutter/issues/65789