From bd6dcc68c936b80cc848af3e691a044b7f2959be Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Tue, 8 Aug 2023 14:07:53 -0700 Subject: [PATCH] Disable HTML renderer paragraph input width flooring (flutter/engine#44478) Hopefully the flooring will no longer be needed once the framework stops doing the rounding. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../lib/src/engine/text/canvas_paragraph.dart | 6 ++--- .../lib/web_ui/test/html/text_test.dart | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) 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 fc0678982a..fe3874785c 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 @@ -94,9 +94,9 @@ class CanvasParagraph implements ui.Paragraph { // `30.5` then the TextPainter in the framework will ceil the `30.5` width // which will result in a width of `40.0` that's higher than the constraint // width. - constraints = ui.ParagraphConstraints( - width: constraints.width.floorToDouble(), - ); + if (!ui.ParagraphBuilder.shouldDisableRoundingHack) { + constraints = ui.ParagraphConstraints(width: constraints.width.floorToDouble()); + } if (constraints == _lastUsedConstraints) { return; diff --git a/engine/src/flutter/lib/web_ui/test/html/text_test.dart b/engine/src/flutter/lib/web_ui/test/html/text_test.dart index 4bb4595681..d9c7780fde 100644 --- a/engine/src/flutter/lib/web_ui/test/html/text_test.dart +++ b/engine/src/flutter/lib/web_ui/test/html/text_test.dart @@ -94,6 +94,32 @@ Future testMain() async { } }); + test('Can disable rounding hack', () { + if (!ParagraphBuilder.shouldDisableRoundingHack) { + ParagraphBuilder.setDisableRoundingHack(true); + addTearDown(() => ParagraphBuilder.setDisableRoundingHack(false)); + } + assert(ParagraphBuilder.shouldDisableRoundingHack); + const double fontSize = 1; + const String text = '12345'; + const double letterSpacing = 0.25; + const double expectedIntrinsicWidth = text.length * (fontSize + letterSpacing); + assert(expectedIntrinsicWidth.truncate() != expectedIntrinsicWidth); + final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(fontSize: fontSize, fontFamily: 'FlutterTest')); + builder.pushStyle(TextStyle(letterSpacing: letterSpacing)); + builder.addText(text); + final Paragraph paragraph = builder.build() + ..layout(const ParagraphConstraints(width: expectedIntrinsicWidth)); + + expect(paragraph.maxIntrinsicWidth, expectedIntrinsicWidth); + switch (paragraph.computeLineMetrics()) { + case [LineMetrics(width: final double width)]: + expect(width, expectedIntrinsicWidth); + case final List metrics: + expect(metrics, hasLength(1)); + } + }); + test('lay out unattached paragraph', () { final CanvasParagraph paragraph = plain(EngineParagraphStyle( fontFamily: 'sans-serif',