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
84 changes: 80 additions & 4 deletions maui/src/Core/Extensions/CanvasExtensions.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,38 @@
using System.Runtime.Versioning;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Font = Microsoft.Maui.Font;
using Windows.UI.ViewManagement;

namespace Syncfusion.Maui.Toolkit.Graphics.Internals
{
/// <summary>
/// Provides helper methods for determining if the current application is running as a packaged app.
/// </summary>
/// <exclude/>
public static class PackagedAppHelper
{
/// <summary>
/// Gets or sets a value indicating whether the application is running as a packaged or unpackaged.
/// Returns `true` if the application is packaged, otherwise `false`.
/// </summary>
/// <returns>A boolean indicating if the application is packaged.</returns>
/// <exclude/>
public static bool IsPackaged()
{
try
{
var _ = Package.Current;
return true;
}
catch (InvalidOperationException)
{
return false;
}
}
}

/// <summary>
/// Provides extension methods for the <see cref="ICanvas"/> interface on the windows platfrom.
/// </summary>
Expand All @@ -40,8 +68,7 @@ public static void DrawText(this ICanvas canvas, string value, float x, float y,
var font = textElement.Font;
if (fontManager != null)
{
var fontFamily = fontManager.GetFontFamily(font);
format.FontFamily = fontFamily.Source;
format.FontFamily = GetFontFamily(textElement, fontManager, font);
UpdateFontSize(textElement, format);
format.FontStyle = font.ToFontStyle();
format.FontWeight = font.ToFontWeight();
Expand Down Expand Up @@ -70,8 +97,7 @@ public static void DrawText(this ICanvas canvas, string value, Rect rect, Horizo
var font = textElement.Font;
if (fontManager != null)
{
var fontFamily = fontManager.GetFontFamily(font);
format.FontFamily = fontFamily.Source;
format.FontFamily = GetFontFamily(textElement, fontManager, font);
if (!double.IsNaN(textElement.FontSize))
{
UpdateFontSize(textElement, format);
Expand Down Expand Up @@ -108,6 +134,56 @@ public static void DrawText(this ICanvas canvas, string value, Rect rect, Horizo
}
}

/// <summary>
/// Get the font family of the font
/// </summary>
/// <param name="textElement">The text element</param>
/// <param name="fontManager">The font manager</param>
/// <param name="font">The font</param>
/// <returns>A string representing the local path or URI source of the font family. Defaults to "Segoe UI" if retrieval fails.</returns>
private static string GetFontFamily(ITextElement textElement, IFontManager fontManager, Font font)
{
string fontPath = string.Empty;

if (fontManager is null)
return "Segoe UI";

var fontFamily = fontManager.GetFontFamily(font);

if (fontFamily is null)
return "Segoe UI";

string path = fontFamily.Source;
string prefix = "ms-appx:///";
string fontName = path.StartsWith(prefix) ? path.Substring(prefix.Length) : path;

if (!string.IsNullOrEmpty(textElement.Font.Family))
{
try
{
fontPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{fontName}");
}
catch (Exception)
{
fontPath = string.Empty;
}
}

if (!string.IsNullOrEmpty(fontPath))
{
if (!PackagedAppHelper.IsPackaged())
{
return $@"{fontPath}";
}
else
{
return fontFamily.Source;
}
}

return "Segoe UI";
}

private static void UpdateFontSize(ITextElement textElement, CanvasTextFormat format)
{
var uiSettings = new UISettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3252,7 +3252,7 @@ public void TestVelocityValueCheck()
var value1 = GetPrivateField(horizontal, "_velocityX");
Assert.NotNull(value1);
double val = (double)value1;
Assert.Equal(0.15658, Math.Round(val, 5));
Assert.Equal(0.15657, Math.Round(val, 5));
}

[Fact]
Expand Down Expand Up @@ -3850,4 +3850,4 @@ public string? SubName
}
}
}
}
}