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

Conversation

@mdebbar
Copy link
Contributor

@mdebbar mdebbar commented Mar 9, 2022

Before this PR: The justify alignment occurs during the painting of text on canvas2d.
After this PR: Do all the justify alignment calculation during layout.

Benefits:

To achieve this, we have to move the box-positioning logic out of the LineBuilder. It has to happen after all the lines have been constructed so that justify alignment can be calculated.

Fixes flutter/flutter#96533

@mdebbar mdebbar added the platform-web Code specifically for the web engine label Mar 9, 2022
@mdebbar mdebbar force-pushed the justify_layout branch 2 times, most recently from 610ae0c to 8093f30 Compare March 15, 2022 16:50
@skia-gold
Copy link

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

@mdebbar mdebbar marked this pull request as ready for review March 16, 2022 20:39
@mdebbar mdebbar requested a review from yjbanov March 16, 2022 20:39

int spaceBoxesToJustify = line.spaceBoxCount;
// If the last box is a space box, we can't use it to justify text.
if (lastBox is SpanBox && lastBox.isSpaceOnly) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it guaranteed that there's no more than one space-only box?

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! I'll account for multiple trailing spaces.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

box.startOffset = cumulativeWidth;
box.lineWidth = line.width;
if (box is SpanBox && box.isSpaceOnly) {
box._width += justifyPerSpaceBox;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to skip increasing the width of the last space-only box? Or maybe the last space-only box should have zero width if the text is justified? 🤔

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 intuition! I was going by what the browser does. But now I checked Flutter, and you're right, it collapses the width of trailing spaces!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FWIW, Flutter always collapses trailing spaces (regardless of which alignment is used).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I (partially) lied.

During text painting/rendering, Flutter treats trailing spaces as if they were zero-width (e.g. no background).

During text editing (cursor placement & selection highlighting), Flutter treats trailing spaces the same as other spaces.

Left Right Justify
image image image

I believe what's happening during text editing is they have no special case for trailing spaces, but they clamp the results of getBoxesForRange to the width of the paragraph.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done (minus the clamping part).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Filed an issue to implement the clamping: flutter/flutter#100567

box.startOffset = startOffset + cumulativeWidth;
box.lineWidth = line.width;
if (box is SpanBox && box.isSpaceOnly) {
box._width += justifyPerSpaceBox;
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto w.r.t. last space-only box

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@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 #31937 at sha 170decf

@flutter-dashboard
Copy link

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

Changes reported for pull request #31937 at sha f902654

@mdebbar mdebbar added the waiting for tree to go green This PR is approved and tested, but waiting for the tree to be green to land. label Mar 22, 2022
@fluttergithubbot fluttergithubbot merged commit 52a8b37 into flutter:main Mar 22, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 22, 2022
@mdebbar mdebbar deleted the justify_layout branch January 17, 2023 18:24
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 waiting for tree to go green This PR is approved and tested, but waiting for the tree to be green to land. will affect goldens

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[web] TextAlign.justify is broken in DOM mode

4 participants