Skip to content

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

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Shalini-Ashokan
Copy link
Contributor

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.

using (var layout = new CanvasTextLayout(device, fontsource.Glyph, textFormat, (float)fontsource.Size, (float)fontsource.Size))

Validated the behavior in the following platforms

  • Android
  • Windows
  • iOS
  • Mac

Issues Fixed

Fixes #30004

Output ScreenShot

Before After
Before-Fix After-Fix

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Jun 19, 2025
Copy link
Contributor

Hey there @@Shalini-Ashokan! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service dotnet-policy-service bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Jun 19, 2025
@jsuarezruiz jsuarezruiz added platform/windows area-image Image loading, sources, caching labels Jun 20, 2025
@jsuarezruiz
Copy link
Contributor

/azp run MAUI-UITests-public

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@Shalini-Ashokan Shalini-Ashokan marked this pull request as ready for review June 20, 2025 07:22
@Copilot Copilot AI review requested due to automatic review settings June 20, 2025 07:22
@Shalini-Ashokan Shalini-Ashokan requested a review from a team as a code owner June 20, 2025 07:22
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes how FontImageSource glyphs are centered within an Image control on Windows by using the full layout bounds and explicit size when creating the text layout rather than zero dimensions, and updates related padding/offset calculations. It also adds a UI test and host app page to verify the alignment.

  • Use fontSize for the layout’s width/height instead of zero
  • Switch from DrawBounds to LayoutBounds when computing canvas size and offsets
  • Add shared UI test and host app scenario for Issue 30004

Reviewed Changes

Copilot reviewed 3 out of 5 changed files in this pull request and generated 1 comment.

File Description
src/Core/src/ImageSources/FontImageSourceService/FontImageSourceService.Windows.cs Create CanvasTextLayout with full fontSize, use LayoutBounds for size and offset
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue30004.cs Add UI test that waits for “FontImage” and captures screenshot
src/Controls/tests/TestCases.HostApp/Issues/Issue30004.cs Add host app ContentPage to display centered FontImageSource examples
Comments suppressed due to low confidence (1)

src/Controls/tests/TestCases.HostApp/Issues/Issue30004.cs:13

  • To enable the UI test to target the Image control itself (rather than the Label), add a unique AutomationId to the Image, e.g. AutomationId = "FontImageIcon".
					new Image() { Source = new FontImageSource() {  FontFamily = "FA", Glyph = "\xf7a4", Color = Colors.Black, Size = 50}, Margin = 4, Background= Colors.Red, WidthRequest=100, HeightRequest=100, HorizontalOptions = LayoutOptions.Center},

Comment on lines +83 to +91
var canvasWidth = (float)layout.LayoutBounds.Width + 2;
var canvasHeight = (float)layout.LayoutBounds.Height + 2;

var canvasImageSource = new CanvasImageSource(device, canvasWidth, canvasHeight, dpi);
using (var ds = canvasImageSource.CreateDrawingSession(UI.Colors.Transparent))
{
// offset by 1px as we added a 1px padding
var x = (layout.DrawBounds.X * -1) + 1;
var y = (layout.DrawBounds.Y * -1) + 1;
var x = (layout.LayoutBounds.X * -1) + 1;
var y = (layout.LayoutBounds.Y * -1) + 1;
Copy link
Preview

Copilot AI Jun 20, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider extracting the 1px padding into a named constant (e.g. const float Padding = 1;) to avoid magic numbers and improve readability.

Copilot uses AI. Check for mistakes.

public void VerifyFontImageAreCenterAlign()
{
App.WaitForElement("FontImage");
VerifyScreenshot();
Copy link
Contributor

Choose a reason for hiding this comment

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

Pending snapshots.
image
Could you commit the images?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jsuarezruiz, I have committed the images.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-image Image loading, sources, caching community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/windows
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"FontImageSource not center-aligned inside Image control in .NET MAUI"
2 participants