From 49ce97a6baf9735e4308f21a5921b5a6de08180d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Tue, 9 May 2023 16:53:31 +0200 Subject: [PATCH 1/4] Allow to use Android system fonts directly --- src/Core/src/Fonts/FontManager.Android.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Core/src/Fonts/FontManager.Android.cs b/src/Core/src/Fonts/FontManager.Android.cs index e74f82a25ca4..295e9341d037 100644 --- a/src/Core/src/Fonts/FontManager.Android.cs +++ b/src/Core/src/Fonts/FontManager.Android.cs @@ -119,6 +119,11 @@ public FontSize GetFontSize(Font font, float defaultFontSize = 0) { try { + var defaultTypeface = LoadDefaultTypeface(fontfamily); + + if (defaultTypeface is not null) + return defaultTypeface; + return Typeface.CreateFromAsset(AApplication.Context.Assets, FontNameToFontFile(fontfamily)); } catch (Exception ex) @@ -130,6 +135,21 @@ public FontSize GetFontSize(Font font, float defaultFontSize = 0) return null; } + Typeface? LoadDefaultTypeface(string fontfamily) + { + switch (fontfamily.ToLowerInvariant()) + { + case "monospace": + return Typeface.Monospace; + case "sansserif": + return Typeface.SansSerif; + case "serif": + return Typeface.Serif; + default: + return null; + } + } + Typeface? CreateTypeface((string? fontFamilyName, FontWeight weight, bool italic) fontData) { var (fontFamily, weight, italic) = fontData; From 7213a1294ecb1f16c81b5f095b80497e78861329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Wed, 10 May 2023 14:27:13 +0200 Subject: [PATCH 2/4] Added some tests --- .../Services/FontManagerTests.Android.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Core/tests/DeviceTests/Services/FontManagerTests.Android.cs b/src/Core/tests/DeviceTests/Services/FontManagerTests.Android.cs index 44fd5986c794..96cf149620e3 100644 --- a/src/Core/tests/DeviceTests/Services/FontManagerTests.Android.cs +++ b/src/Core/tests/DeviceTests/Services/FontManagerTests.Android.cs @@ -79,4 +79,20 @@ public void CanLoadEmbeddedFont() Assert.False(expected.Equals(actual)); } + + [Theory] + [InlineData("monospace")] + [InlineData("sansserif")] + [InlineData("serif")] + public void CanLoadSystemFonts(string fontName) + { + if (!OperatingSystem.IsAndroidVersionAtLeast(28)) + return; + + var registrar = new FontRegistrar(fontLoader: null); + var manager = new FontManager(registrar); + var actual = manager.GetTypeface(Font.OfSize(fontName, 12)); + + Assert.NotEqual(Typeface.Default, actual); + } } From f8e3ecec1dcb41bf2741d5b26638c97805e6de1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Sua=CC=81rez?= Date: Thu, 11 May 2023 09:49:27 +0200 Subject: [PATCH 3/4] Updated impl --- src/Core/src/Fonts/FontManager.Android.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Core/src/Fonts/FontManager.Android.cs b/src/Core/src/Fonts/FontManager.Android.cs index 295e9341d037..7c563c4a495d 100644 --- a/src/Core/src/Fonts/FontManager.Android.cs +++ b/src/Core/src/Fonts/FontManager.Android.cs @@ -2,6 +2,7 @@ using System.Collections.Concurrent; using System.IO; using Android.Graphics; +using Android.Graphics.Fonts; using Android.Util; using Microsoft.Extensions.Logging; using AApplication = Android.App.Application; @@ -100,7 +101,7 @@ public FontSize GetFontSize(Font font, float defaultFontSize = 0) } Typeface? FindFont(string fileWithExtension) - { + { var result = LoadTypefaceFromAsset(fileWithExtension, warning: false); if (result != null) return result; @@ -119,11 +120,6 @@ public FontSize GetFontSize(Font font, float defaultFontSize = 0) { try { - var defaultTypeface = LoadDefaultTypeface(fontfamily); - - if (defaultTypeface is not null) - return defaultTypeface; - return Typeface.CreateFromAsset(AApplication.Context.Assets, FontNameToFontFile(fontfamily)); } catch (Exception ex) @@ -142,6 +138,7 @@ public FontSize GetFontSize(Font font, float defaultFontSize = 0) case "monospace": return Typeface.Monospace; case "sansserif": + case "sans-serif": return Typeface.SansSerif; case "serif": return Typeface.Serif; @@ -160,7 +157,9 @@ public FontSize GetFontSize(Font font, float defaultFontSize = 0) if (!string.IsNullOrWhiteSpace(fontFamily)) { - if (GetFromAssets(fontFamily) is Typeface typeface) + if (LoadDefaultTypeface(fontFamily) is Typeface systemTypeface) + result = systemTypeface; + else if (GetFromAssets(fontFamily) is Typeface typeface) result = typeface; else result = Typeface.Create(fontFamily, style); From 11db79fe86ea85572a09dd2159dc1adbc9abd1e4 Mon Sep 17 00:00:00 2001 From: GitHub Actions Autoformatter Date: Thu, 11 May 2023 07:54:47 +0000 Subject: [PATCH 4/4] Auto-format source code --- src/Core/src/Fonts/FontManager.Android.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/src/Fonts/FontManager.Android.cs b/src/Core/src/Fonts/FontManager.Android.cs index 7c563c4a495d..531a9fffad07 100644 --- a/src/Core/src/Fonts/FontManager.Android.cs +++ b/src/Core/src/Fonts/FontManager.Android.cs @@ -101,7 +101,7 @@ public FontSize GetFontSize(Font font, float defaultFontSize = 0) } Typeface? FindFont(string fileWithExtension) - { + { var result = LoadTypefaceFromAsset(fileWithExtension, warning: false); if (result != null) return result;