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

[web] tighten paragraph bounds estimate#31272

Merged
yjbanov merged 2 commits intoflutter:mainfrom
yjbanov:infinite-width-text
Apr 19, 2022
Merged

[web] tighten paragraph bounds estimate#31272
yjbanov merged 2 commits intoflutter:mainfrom
yjbanov:infinite-width-text

Conversation

@yjbanov
Copy link
Contributor

@yjbanov yjbanov commented Feb 4, 2022

Use the actual text bounds when estimating paragraph paint bounds instead of the paragraph size. Paragraph size can frequently be bigger than the text. Common situations:

  • Paragraph is laid out with double.infinity width constraint. While the constraint is infinite, that doesn't make the text infinitely wide. The text will still only take as much space as it needs.
  • When TextAlign.center is used the text is shifted towards the center of the paragraph, so the left and right bounds of the text are padded away from the left and right walls of the paragraph.

In the example below center alignment is used. Notice that the text bounds (green box) is smaller than the paragraph bounds (black box):

canvas_paragraph_bounds

Fixes: flutter/flutter#97756

@flutter-dashboard flutter-dashboard bot added the platform-web Code specifically for the web engine label Feb 4, 2022
@yjbanov yjbanov force-pushed the infinite-width-text branch from b4d5782 to a2f7d8f Compare February 14, 2022 23:35
@skia-gold
Copy link

Gold has detected about 3 new digest(s) on patchset 2.
View them at https://flutter-engine-gold.skia.org/cl/github/31272

@yjbanov yjbanov force-pushed the infinite-width-text branch from a2f7d8f to c959048 Compare February 15, 2022 00:54
@flutter-dashboard
Copy link

Golden file changes have been found for this pull request. Click here to view and triage (e.g. because this is an intentional change).

If you are still iterating on this change and are not ready to resolve the images on the Flutter Gold dashboard, consider marking this PR as a draft pull request above. You will still be able to view image results on the dashboard, commenting will be silenced, and the check will not try to resolve itself until marked ready for review.

Changes reported for pull request #31272 at sha c959048

@skia-gold
Copy link

Gold has detected about 4 new digest(s) on patchset 2.
View them at https://flutter-engine-gold.skia.org/cl/github/31272

@ditman
Copy link
Member

ditman commented Feb 16, 2022

Yikes! I clicked in the wrong spot in the flutter-gold UI. I left the image as "?"

Copy link
Member

@ditman ditman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🚀


_paintBounds = ui.Rect.fromLTRB(
boundsLeft,
0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the top always 0? What happens if the font has tall ascenders or something like that, does that get computed earlier with the line heights?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

The previous version already used the (0, paragraph.height) range for the vertical bounds (see recording_canvas.dart, where offset.dy was used as the top and top + engineParagraph.height as the bottom). That's still the case after this PR.

What is changing are the horizontal bounds. Your concern is still valid for the horizontal case (what if a glyph paints outside its layout bounds due to some swash). Currently dart:ui doesn't provide this information to the framework, so this problem already exists (e.g. the framework may clip or prune contents, thinking that it's not visible). This can probably be worked around by the developer by using extra padding if they know that they are using some font that results in this kind of out-of-bounds painting, and I think horizontal extrusions are less common than vertical ones due to the risk of overlapping with adjacent glyphs.

Maybe this is largely solvable by adding some padding proactively? For example, we could add a certain % of the font size on both sides. WDYT? @mdebbar I'm curious about your thoughts on the topic too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Yegor. Weird characters that paint outside of their bounds are already broken because of clipping, etc.

At this time, I would say it's fair to assume that the top paint bound is 0, especially that there's no concept of vertical alignment (yet) in paragraphs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know enough about this, so 🚀

underTest.endRecording();
expect(paragraph.width, 110);
expect(paragraph.height, 60);
expect(underTest.pictureBounds, const Rect.fromLTRB(0, 0, 100, 60));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is expected to render:

┌───────┐
│A      │
│AAAAA  │
│AAA    │
└───────┘

And the one below:

┌───────┐
│   A   │
│ AAAAA │
│  AAA  │
└───────┘

(Same height, but the bounding box around As is smaller than the container paragraph, also the paragraph needs to be narrow enough to cause a carriage return around the "AAAAA" bit that is 100px wide)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly documented in the general_golden_test, genius :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL, we used the exact same example :) You get the bonus points for using better ASCII art though.

Copy link
Contributor

@mdebbar mdebbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@flutter-dashboard
Copy link

This pull request executed golden file tests, but it has not been updated in a while (20+ days). Test results from Gold expire after as many days, so this pull request will need to be updated with a fresh commit in order to get results from Gold.

@yjbanov yjbanov force-pushed the infinite-width-text branch from c959048 to bc06c77 Compare March 31, 2022 01:14
@flutter-dashboard
Copy link

Golden file changes are available for triage from new commit, Click here to view.

Changes reported for pull request #31272 at sha bc06c77

@yjbanov yjbanov force-pushed the infinite-width-text branch from bc06c77 to 498624f Compare April 18, 2022 23:21
@skia-gold
Copy link

Gold has detected about 1 new digest(s) on patchset 2.
View them at https://flutter-engine-gold.skia.org/cl/github/31272

@skia-gold
Copy link

Gold has detected about 4 new digest(s) on patchset 2.
View them at https://flutter-engine-gold.skia.org/cl/github/31272

@yjbanov yjbanov merged commit 5ee2c3b into flutter:main Apr 19, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 19, 2022
justinmc pushed a commit to justinmc/engine that referenced this pull request Apr 20, 2022
* [web] tighten paragraph bounds estimate
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

platform-web Code specifically for the web engine will affect goldens

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[web] paragraph incorrectly estimates its paint bounds

4 participants