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

Beginning with 11.2.5 Buttons and TextBlocks does not show whitespaces #18372

Open
CodeDevAM opened this issue Mar 5, 2025 · 8 comments · May be fixed by #18438
Open

Beginning with 11.2.5 Buttons and TextBlocks does not show whitespaces #18372

CodeDevAM opened this issue Mar 5, 2025 · 8 comments · May be fixed by #18438

Comments

@CodeDevAM
Copy link

Describe the bug

Beginning with 11.2.5 Buttons and TextBlocks does not show whitespaces. e.g. Strings like ' ' or 'Name: ' are not shown correctly.

This has a significant (negative) impact on the UI and the layout of my apps.

To Reproduce

Create a simple button with ' ' or 'Name: ' as content.

Expected behavior

Display whitespaces on Buttons and TextBlocks as before of 11.2.5. Especially leading or trailing whitespaces.

Avalonia version

11.2.5

OS

Windows

Additional context

No response

@Patrick8639
Copy link

Same here, very annoying.
I've reverted to 11.2.4.

@Patrick8639
Copy link

In 11.2.4, the problem also appears when using browser [wasm] app.

@robloo
Copy link
Contributor

robloo commented Mar 14, 2025

It's likely that the PR #18310 had some unintended effect here.

cc @xoofx @Gillibald

@Gillibald
Copy link
Contributor

Gillibald commented Mar 14, 2025

Y I think I will revert that PR and introduce a different fix. WidthIncludingTrailingWhitespace is also broken in 11.2.4. It does not produce the correct results.

With Courier New

 <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
   <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Background="Magenta" FontSize="44" Text="aaaa"/>
   <TextBlock Margin="0 10 0 0" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Magenta" FontSize="44" Text="a a "/>
   <TextBlock Margin="0 10 0 0" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Magenta" FontSize="44" Text="    "/>
 </StackPanel>

This is the expected result.
Image

This is with current Avalonia
Image

@Gillibald Gillibald self-assigned this Mar 14, 2025
@xoofx
Copy link
Contributor

xoofx commented Mar 14, 2025

I would prefer that we start from the current code that is more readable and fixed another annoying issue and fix it from there. As the repro case above should be easy to add as a unit test, I can have a look right now if that's ok?

@Gillibald
Copy link
Contributor

Feel free to have a look

Here is my fix:

        private void UpdateMetrics(TextLineImpl currentLine, ref bool first)
        {
            _metrics.InkBounds = _metrics.InkBounds.Union(new Rect(new Point(0, _metrics.Bounds.Bottom) + currentLine.InkBounds.Position, currentLine.InkBounds.Size));
            _metrics.Bounds = _metrics.Bounds.Union(new Rect(new Point(0, _metrics.Bounds.Bottom) + currentLine.Bounds.Position, currentLine.Bounds.Size));

            _metrics.MinTextWidth = Math.Max(_metrics.MinTextWidth, currentLine.Bounds.Width);
            _metrics.MinTextWidth = Math.Max(_metrics.MinTextWidth, currentLine.InkBounds.Width);

            _metrics.Height = _metrics.Bounds.Height;
            _metrics.Width = _metrics.InkBounds.Width;
            _metrics.WidthIncludingTrailingWhitespace = Math.Max(_metrics.WidthIncludingTrailingWhitespace, _metrics.Bounds.Left + currentLine.WidthIncludingTrailingWhitespace);
            _metrics.Extent = _metrics.InkBounds.Height;
            _metrics.OverhangLeading = Math.Max(0, _metrics.Bounds.Left - _metrics.InkBounds.Left);
            _metrics.OverhangTrailing = Math.Max(0, _metrics.InkBounds.Right - _metrics.Bounds.Right);
            _metrics.OverhangAfter = Math.Max(0, _metrics.InkBounds.Bottom - _metrics.Bounds.Bottom);

            // 6) Capture the baseline from the first line.
            if (first)
            {
                _metrics.Baseline = currentLine.Baseline;
                first = false;
            }
        }

This is the previous implementation with a fix for the WidthIncludingTrailingWhitespace issue.

Compare it to your version, and you will realise that all overhang values have been calculated incorrectly. Extend, OverhandLeading, OverhangTrailing and OverhangAfter represent the black pixels drawn outside the logical bounds. Your implementation just builds the overall maximum.

xoofx added a commit to xoofx/Avalonia that referenced this issue Mar 14, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
xoofx added a commit to xoofx/Avalonia that referenced this issue Mar 14, 2025
@xoofx xoofx linked a pull request Mar 14, 2025 that will close this issue
3 tasks
@xoofx
Copy link
Contributor

xoofx commented Mar 14, 2025

Created a fix via #18438

Compare it to your version, and you will realise that all overhang values have been calculated incorrectly. Extend, OverhandLeading, OverhangTrailing and OverhangAfter represent the black pixels drawn outside the logical bounds. Your implementation just builds the overall maximum.

I don't think that the previous code was correct either regarding these. We should reflect the OverhandLeading/Trailing/After given by the actual line metrics and not recompute something that has no link to the original line metrics.

The problem was with MinTextWidth that was previously taking into account the WidthIncludingTrailingWhitespace, but the previous code was hiding this through bounds, which is not desirable because we don't understand what is in the box (e.g. the Width of Bounds from the LineMetrics is actually WidthIncludingTrailingWhitespace)

@Gillibald
Copy link
Contributor

Yes, the metrics were never correct. Each line reports its metrics relative to the line's origin. Each line can have a different origin. So you can't just take the maximum OverhangLeading etc. You need to calculate this value in respect to the ink and logical bounding box.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants