Skip to content

Commit

Permalink
[Android] System Fonts fix (#20961)
Browse files Browse the repository at this point in the history
* Added a UI test (#19556)

* [Android] Systemfonts fix (#19556)

* Code review comments

* [android] Add screenshot for UI test

---------

Co-authored-by: Rui Marinho <me@ruimarinho.net>
  • Loading branch information
kubaflo and rmarinho authored Mar 13, 2024
1 parent fb8747e commit a648f08
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue19556"
xmlns:ios="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;assembly=Microsoft.Maui.Controls"
ios:NavigationPage.PrefersLargeTitles="false"
Title="Issue19556">
<StackLayout Padding="30,0" Spacing="0">
<Label Text="NET Multi-platform App UI monospace"
FontFamily="monospace"
AutomationId="label"
SemanticProperties.HeadingLevel="Level1" />
<Label Text=".NET Multi-platform App UI sans-serif-condensed"
FontFamily="sans-serif-condensed"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I sans-serif-condensed" />
<Label Text=".NET Multi-platform App UI sans-serif-condensed-light"
FontFamily="sans-serif-condensed-light"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I sans-serif-condensed-light" />
<Label Text=".NET Multi-platform App UI serif"
FontFamily="serif"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I serif " />
<Label Text=".NET Multi-platform App UI sans-serif"
FontFamily="sans-serif"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I sans-serif" />
<Label Text=".NET Multi-platform App UI sans-serif-black"
FontFamily="sans-serif-black"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I sans-serif-black" />
<Label Text=".NET Multi-platform App UI sans-serif-light"
FontFamily="sans-serif-light"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I sans-serif-light" />
<Label Text=".NET Multi-platform App UI sans-serif-medium"
FontFamily="sans-serif-medium"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I sans-serif-medium" />
</StackLayout>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;
namespace Maui.Controls.Sample.Issues;

[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 19556, "[Android] Systemfonts (light/black etc.) not working", PlatformAffected.Android)]

public partial class Issue19556 : ContentPage
{
public Issue19556()
{
InitializeComponent();
}
}
25 changes: 25 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue19556.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests.Issues;

public class Issue19556 : _IssuesUITest
{
public override string Issue => "[Android] Systemfonts (light/black etc.) not working";

public Issue19556(TestDevice device)
: base(device)
{ }

[Test]
public void SystemFontsShouldRenderCorrectly()
{
this.IgnoreIfPlatforms(new TestDevice[] { TestDevice.iOS, TestDevice.Mac, TestDevice.Windows });

_ = App.WaitForElement("label");

// The test passes if fonts are correctly rendered
VerifyScreenshot();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions src/Core/src/Fonts/FontManager.Android.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using Android.Graphics;
using Android.Graphics.Fonts;
Expand All @@ -18,6 +19,14 @@ public class FontManager : IFontManager
"fonts/",
};

static readonly Dictionary<string, FontWeight> FontWeightMap = new(StringComparer.OrdinalIgnoreCase)
{
{ "-light", FontWeight.Light },
{ "-medium", FontWeight.Medium },
{ "-black", FontWeight.Black }
// Add more styles as needed
};

readonly ConcurrentDictionary<(string? fontFamilyName, FontWeight weight, bool italic), Typeface?> _typefaces = new();
readonly IFontRegistrar _fontRegistrar;
readonly IServiceProvider? _serviceProvider;
Expand Down Expand Up @@ -171,7 +180,20 @@ public FontSize GetFontSize(Font font, float defaultFontSize = 0)
}

if (OperatingSystem.IsAndroidVersionAtLeast(28))
{
if (!string.IsNullOrWhiteSpace(fontFamily))
{
foreach (var fontWeight in FontWeightMap)
{
if (fontFamily.EndsWith(fontWeight.Key, StringComparison.OrdinalIgnoreCase))
{
return Typeface.Create(result, (int)fontWeight.Value, italic);
}
}
}

result = Typeface.Create(result, (int)weight, italic);
}
else
result = Typeface.Create(result, style);

Expand Down

0 comments on commit a648f08

Please sign in to comment.