maui icon indicating copy to clipboard operation
maui copied to clipboard

[Windows] Fixes center alignment issue of FontImageSource inside Image control

Open Shalini-Ashokan opened this issue 6 months ago • 11 comments

[!NOTE] Are you waiting for the changes in this PR to be merged? It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Issue Details

A FontImageSource inside an Image control results in the icon glyph not being properly centered within its bounding box, causing visual misalignment both vertically and horizontally.

Root Cause

Font images were not visually centered due to using the glyph’s tight bounding box and manual offsets for positioning. This ignored layout size and alignment settings.

The CanvasTextLayout was created with zero width and height, so HorizontalAlignment and VerticalAlignment had no effect. As a result, glyphs were only offset, not truly centered, leading to inconsistent appearance for various glyphs.

Description of Change

Creates the CanvasTextLayout using the full image size with both alignments set to Center. Uses LayoutBounds instead of DrawBounds to more accurately calculate layout size and position.

Reference

I resolved the issue by referring to the following code. https://github.com/dotnet/maui/blob/59ad299cdc4fd8c942df971c6186d476d66307be/src/Compatibility/Core/src/Windows/FontImageSourceHandler.cs#L43

Validated the behavior in the following platforms

  • [x] Android
  • [x] Windows
  • [x] iOS
  • [x] Mac

Issues Fixed

Fixes #30004

Output ScreenShot

Before After
Before-Fix After-Fix

Shalini-Ashokan avatar Jun 19 '25 11:06 Shalini-Ashokan

/azp run MAUI-UITests-public

jsuarezruiz avatar Jun 20 '25 06:06 jsuarezruiz

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar Jun 20 '25 06:06 azure-pipelines[bot]

/azp run MAUI-UITests-public

jsuarezruiz avatar Jun 23 '25 13:06 jsuarezruiz

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar Jun 23 '25 13:06 azure-pipelines[bot]

The test Source_FontImageSource is failing on Windows.

Snapshot different than baseline: ImageUITests_Source_FontImageSource_Ionicons.png (1.71% difference)

image

Could you review it?

@jsuarezruiz, The test Source_FontImageSource failure has been resolved. New snapshot has been committed.

Shalini-Ashokan avatar Jun 25 '25 09:06 Shalini-Ashokan

/azp run MAUI-UITests-public

jsuarezruiz avatar Jun 27 '25 05:06 jsuarezruiz

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar Jun 27 '25 05:06 azure-pipelines[bot]

/azp run MAUI-UITests-public

jsuarezruiz avatar Jul 10 '25 07:07 jsuarezruiz

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar Jul 10 '25 07:07 azure-pipelines[bot]

@Shalini-Ashokan See my simpler fix here that preserves image sizes and matches Label's rendering: https://github.com/dotnet/maui/pull/31412/files

dotMorten avatar Dec 08 '25 18:12 dotMorten

@Shalini-Ashokan See my simpler fix here that preserves image sizes and matches Label's rendering: https://github.com/dotnet/maui/pull/31412/files

@dotMorten, I ensured the font image scenarios with your fix, but it affects the FontAwesome and Ionicons icons. These icons are available in the HostApp. image

Shalini-Ashokan avatar Dec 09 '25 10:12 Shalini-Ashokan