Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text: Fix text align for autosize fields #18494

Merged
merged 7 commits into from
Nov 15, 2024

Conversation

kjarosh
Copy link
Member

@kjarosh kjarosh commented Nov 6, 2024

Fixes #18030, progresses #18490, fixes #10660, fixes #1440, fixes #3156.

Supersedes #18092, supersedes #12598.

Basically, when horizontal autosize is effective, we don't know the width of the text to be laid out and we have to calculate it dynamically.

Before this PR, the width was assumed to be the width of the field, which made non-left-aligned text display weirdly.

This PR lays out text two times when autosize is on: the first time to calculate the proper text width, and the second time to provide the final layout knowing the proper width.

Note that we could do it more efficiently by doing multi-pass layout, first pass – basic horizontal layout + vertical layout, second pass – full horizontal layout; but that requires rewriting most of the current layout code. We could do that in the future when we have more test coverage for text fields though (and we're absolutely sure that's how it works)!

Note 2: this PR moves some code around, so make sure to review commit-by-commit and don't be afraid of the number of lines changed.

@kjarosh kjarosh added text Issues relating to text rendering/input A-core Area: Core player, where no other category fits T-fix Type: Bug fix (in something that's supposed to work already) waiting-on-review Waiting on review from a Ruffle team member labels Nov 6, 2024
Copy link
Contributor

@Dinnerbone Dinnerbone left a comment

Choose a reason for hiding this comment

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

Nice work!

num_ticks = 1

[image_comparisons.output]
tolerance = 128
Copy link
Contributor

Choose a reason for hiding this comment

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

To be sure, why such a high tolerance?

Copy link
Member Author

Choose a reason for hiding this comment

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

It might seem that the tolerance is pretty high, but for tests which check the placement of black rectangles over white background, it basically means a half-pixel tolerance. It's pretty common that we're off by half a pixel, e.g. in cases of border corners (different line rasterization algorithms), or glyph placement (font hinting probably?).

Copy link
Contributor

Choose a reason for hiding this comment

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

But is it everywhere that's wrong, or just some places? Can we reduce the tolerance and increase the outliers? I'm a little worried by "every pixel can be half wrong" 😅

Copy link
Member Author

@kjarosh kjarosh Nov 6, 2024

Choose a reason for hiding this comment

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

I could alternatively do low tolerance + max_outliers, but TBH after making so many text tests I got bored of counting these outliers and making sure they are at the right places.

Edit: I didn't see the comment above when writing this.

Copy link
Member Author

@kjarosh kjarosh Nov 6, 2024

Choose a reason for hiding this comment

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

But is it everywhere that's wrong, or just some places? Can we reduce the tolerance and increase the outliers? I'm a little worried by "every pixel can be half wrong" 😅

In this case it's 144 outliers mainly due to border corners being off. When I do low tolerance + 144 outliers it's difficult to know whether there are so many outliers due to small variations in tolerance scattered all over the place (like here), or due to big variations in tolerance in one place (e.g. the glyph is wrongly positioned). So both approaches have their cons. :/

Copy link
Member Author

Choose a reason for hiding this comment

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

The solution would be to provide max outliers with max tolerance for each one, so that we could say "there are max 144 outliers, but each one shouldn't be more off than 128".

@Dinnerbone Dinnerbone enabled auto-merge (rebase) November 14, 2024 16:32
This makes it easier to pass parameters down and reduces
the number of parameters in methods.
This refactor moves code from lower_from_text_spans to LayoutContext.
This refactor moves code from lower_from_text_spans to LayoutContext.
This patch moves code around so that LayoutBox impl is not divided in half.
When horizontal autosize is effective, text width is calculated
dynamically and is equal to the width of the longest line in text.

That's why this patch makes the text width optional, and in case
it's None, the text is laid out two times—first time in order to calculate
the proper text width, and the second time knowing what width should be used.
This test verifies how text align is handled when
autosize and/or word wrap is set.
@Dinnerbone Dinnerbone merged commit 22498f7 into ruffle-rs:master Nov 15, 2024
22 checks passed
@kjarosh kjarosh deleted the autosize-align branch November 15, 2024 16:55
@kjarosh kjarosh removed the waiting-on-review Waiting on review from a Ruffle team member label Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-core Area: Core player, where no other category fits newsworthy T-fix Type: Bug fix (in something that's supposed to work already) text Issues relating to text rendering/input
Projects
None yet
3 participants