Skip to content

Commit 5c15622

Browse files
SyedAbdulAzeemSF4852PureWeen
authored andcommitted
[Windows] Fix for Argument Exception raised when the GetStringSize method of ICanvas called with default font (#29048)
* [Windows] Fix for 20419 ( Bug in ICanvas.GetStringSize when called with default font ) * [Windows] Fix for 20419 ( Bug in ICanvas.GetStringSize when called with default font ) * [Windows] Fix for 20419 ( Bug in ICanvas.GetStringSize when called with default font ) * Modified the test case * [Windows] Fix for 20419 ( Bug in ICanvas.GetStringSize when called with default font ) * [Windows] Fix for 20419 ( Bug in ICanvas.GetStringSize when called with default font ) * [Windows] Fix for 20419 ( Bug in ICanvas.GetStringSize when called with default font ) * [Windows] Fix for 20419 ( Bug in ICanvas.GetStringSize when called with default font ) * Have updated the fix by creating the CanvasTextFormat instance using the ToCanvasTextFormat extension method defined in FontExtensions * Removed unused namespace references
1 parent d0efc5f commit 5c15622

File tree

4 files changed

+66
-11
lines changed

4 files changed

+66
-11
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace Controls.TestCases.HostApp.Issues;
2+
3+
[Issue(IssueTracker.Github, 20419, "Argument Exception raised when the GetStringSize method of ICanvas called with default font", PlatformAffected.UWP)]
4+
public class Issue20419 : ContentPage
5+
{
6+
public Issue20419()
7+
{
8+
VerticalStackLayout verticalStackLayout = new VerticalStackLayout();
9+
10+
GraphicsView graphicsView = new GraphicsView()
11+
{
12+
HeightRequest = 300,
13+
WidthRequest = 300,
14+
};
15+
graphicsView.Drawable = new MyDrawable();
16+
17+
Label descriptionLabel = new Label()
18+
{
19+
AutomationId = "descriptionLabel",
20+
Text = "The test passes if the app runs without crashing and fails if the app crashes",
21+
HorizontalTextAlignment = TextAlignment.Center,
22+
FontSize = 20
23+
};
24+
25+
verticalStackLayout.Children.Add(descriptionLabel);
26+
verticalStackLayout.Children.Add(graphicsView);
27+
28+
Content = verticalStackLayout;
29+
}
30+
}
31+
32+
public class MyDrawable : IDrawable
33+
{
34+
public void Draw(ICanvas canvas, RectF dirtyRect)
35+
{
36+
Microsoft.Maui.Graphics.Font font = new Microsoft.Maui.Graphics.Font();
37+
var stringSize = canvas.GetStringSize("MyString", font, 32);
38+
canvas.DrawString($"String Width: { stringSize.Width}, String Height: {stringSize.Height}", dirtyRect.Left + dirtyRect.Width / 2, dirtyRect.Top + dirtyRect.Height / 2, HorizontalAlignment.Center);
39+
}
40+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue20419 : _IssuesUITest
8+
{
9+
public Issue20419(TestDevice device) : base(device)
10+
{
11+
}
12+
13+
public override string Issue => "Argument Exception raised when the GetStringSize method of ICanvas called with default font";
14+
15+
[Test]
16+
[Category(UITestCategories.GraphicsView)]
17+
public void Issue20419ArgumentException()
18+
{
19+
App.WaitForElement("descriptionLabel");
20+
}
21+
}

src/Graphics/src/Graphics/Platforms/Windows/FontExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Graphics.Canvas.Text;
22
using Windows.UI.Text;
3+
using System;
34
#if NETFX_CORE
45
using Windows.UI.Xaml.Media;
56
#else
@@ -28,7 +29,8 @@ public static CanvasTextFormat ToCanvasTextFormat(this IFont font, float size)
2829
{
2930
FontFamily = font?.Name ?? FontFamily.XamlAutoFontFamily.Source,
3031
FontSize = size,
31-
FontWeight = new FontWeight { Weight = (ushort)(font?.Weight ?? FontWeights.Regular) },
32+
// Ensure font weight stays within the valid range (1–999) to avoid runtime errors
33+
FontWeight = new FontWeight { Weight = (ushort)Math.Clamp(font?.Weight ?? FontWeights.Regular, 1, 999) },
3234
FontStyle = (font?.StyleType ?? FontStyleType.Normal).ToFontStyle()
3335
};
3436
}

src/Graphics/src/Graphics/Platforms/Windows/PlatformStringSizeService.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Microsoft.Graphics.Canvas;
22
using Microsoft.Graphics.Canvas.Text;
3-
using Windows.UI.Text;
43

54
#if MAUI_GRAPHICS_WIN2D
65
namespace Microsoft.Maui.Graphics.Win2D
@@ -24,15 +23,8 @@ public SizeF GetStringSize(string value, IFont font, float textSize)
2423

2524
public SizeF GetStringSize(string value, IFont font, float textSize, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment)
2625
{
27-
var format = new CanvasTextFormat
28-
{
29-
FontFamily = font.Name,
30-
FontSize = textSize,
31-
FontWeight = new FontWeight { Weight = (ushort)font.Weight },
32-
FontStyle = font.StyleType.ToFontStyle(),
33-
WordWrapping = CanvasWordWrapping.NoWrap
34-
};
35-
26+
var format = font.ToCanvasTextFormat(textSize);
27+
3628
var device = CanvasDevice.GetSharedDevice();
3729
var textLayout = new CanvasTextLayout(device, value, format, 0.0f, 0.0f);
3830
textLayout.VerticalAlignment = verticalAlignment switch

0 commit comments

Comments
 (0)