Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue20419.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace Controls.TestCases.HostApp.Issues;

[Issue(IssueTracker.Github, 20419, "Argument Exception raised when the GetStringSize method of ICanvas called with default font", PlatformAffected.UWP)]
public class Issue20419 : ContentPage
{
public Issue20419()
{
VerticalStackLayout verticalStackLayout = new VerticalStackLayout();

GraphicsView graphicsView = new GraphicsView()
{
HeightRequest = 300,
WidthRequest = 300,
};
graphicsView.Drawable = new MyDrawable();

Label descriptionLabel = new Label()
{
AutomationId = "descriptionLabel",
Text = "The test passes if the app runs without crashing and fails if the app crashes",
HorizontalTextAlignment = TextAlignment.Center,
FontSize = 20
};

verticalStackLayout.Children.Add(descriptionLabel);
verticalStackLayout.Children.Add(graphicsView);

Content = verticalStackLayout;
}
}

public class MyDrawable : IDrawable
{
public void Draw(ICanvas canvas, RectF dirtyRect)
{
Microsoft.Maui.Graphics.Font font = new Microsoft.Maui.Graphics.Font();
var stringSize = canvas.GetStringSize("MyString", font, 32);
canvas.DrawString($"String Width: { stringSize.Width}, String Height: {stringSize.Height}", dirtyRect.Left + dirtyRect.Width / 2, dirtyRect.Top + dirtyRect.Height / 2, HorizontalAlignment.Center);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue20419 : _IssuesUITest
{
public Issue20419(TestDevice device) : base(device)
{
}

public override string Issue => "Argument Exception raised when the GetStringSize method of ICanvas called with default font";

[Test]
[Category(UITestCategories.GraphicsView)]
public void Issue20419ArgumentException()
{
App.WaitForElement("descriptionLabel");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Graphics.Canvas.Text;
using Windows.UI.Text;
using System;
#if NETFX_CORE
using Windows.UI.Xaml.Media;
#else
Expand Down Expand Up @@ -28,7 +29,8 @@ public static CanvasTextFormat ToCanvasTextFormat(this IFont font, float size)
{
FontFamily = font?.Name ?? FontFamily.XamlAutoFontFamily.Source,
FontSize = size,
FontWeight = new FontWeight { Weight = (ushort)(font?.Weight ?? FontWeights.Regular) },
// Ensure font weight stays within the valid range (1–999) to avoid runtime errors
FontWeight = new FontWeight { Weight = (ushort)Math.Clamp(font?.Weight ?? FontWeights.Regular, 1, 999) },
FontStyle = (font?.StyleType ?? FontStyleType.Normal).ToFontStyle()
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Text;
using Windows.UI.Text;

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

public SizeF GetStringSize(string value, IFont font, float textSize, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment)
{
var format = new CanvasTextFormat
{
FontFamily = font.Name,
FontSize = textSize,
FontWeight = new FontWeight { Weight = (ushort)font.Weight },
FontStyle = font.StyleType.ToFontStyle(),
WordWrapping = CanvasWordWrapping.NoWrap
};

var format = font.ToCanvasTextFormat(textSize);

var device = CanvasDevice.GetSharedDevice();
var textLayout = new CanvasTextLayout(device, value, format, 0.0f, 0.0f);
textLayout.VerticalAlignment = verticalAlignment switch
Expand Down
Loading