diff --git a/src/Compatibility/ControlGallery/src/UITests.Shared/Tests/ButtonUITests.cs b/src/Compatibility/ControlGallery/src/UITests.Shared/Tests/ButtonUITests.cs index 8c82b5c7d85a..a8fcdab27c73 100644 --- a/src/Compatibility/ControlGallery/src/UITests.Shared/Tests/ButtonUITests.cs +++ b/src/Compatibility/ControlGallery/src/UITests.Shared/Tests/ButtonUITests.cs @@ -126,7 +126,7 @@ public void Font() Assert.Inconclusive("needs testing"); #else var font = remote.GetProperty(Button.FontProperty); - Assert.True(font.Weight.HasFlag(FontWeight.Bold)); + Assert.AreEqual (FontWeight.Bold, font.Weight); #endif } diff --git a/src/Compatibility/Core/src/Compatibility-net6.csproj b/src/Compatibility/Core/src/Compatibility-net6.csproj index bfdd50f35460..0df5eed659c8 100644 --- a/src/Compatibility/Core/src/Compatibility-net6.csproj +++ b/src/Compatibility/Core/src/Compatibility-net6.csproj @@ -78,8 +78,8 @@ - + diff --git a/src/Compatibility/Core/src/WinUI/Compatibility.UAP.csproj b/src/Compatibility/Core/src/WinUI/Compatibility.UAP.csproj index 01abbdc16e02..4c8d29098993 100644 --- a/src/Compatibility/Core/src/WinUI/Compatibility.UAP.csproj +++ b/src/Compatibility/Core/src/WinUI/Compatibility.UAP.csproj @@ -26,4 +26,9 @@ + + + + + diff --git a/src/Compatibility/Core/src/iOS/Compatibility.iOS.csproj b/src/Compatibility/Core/src/iOS/Compatibility.iOS.csproj index b9f18a7fe18f..4a710cdc669f 100644 --- a/src/Compatibility/Core/src/iOS/Compatibility.iOS.csproj +++ b/src/Compatibility/Core/src/iOS/Compatibility.iOS.csproj @@ -52,6 +52,10 @@ + + + + @@ -112,12 +116,6 @@ - - AppHostBuilderExtensions.cs - - - MauiHandlersCollectionExtensions.cs - diff --git a/src/Controls/samples/Controls.Sample/Resources/Fonts/LobsterTwo-Bold.ttf b/src/Controls/samples/Controls.Sample/Resources/Fonts/LobsterTwo-Bold.ttf new file mode 100644 index 000000000000..2e979fbff2ca Binary files /dev/null and b/src/Controls/samples/Controls.Sample/Resources/Fonts/LobsterTwo-Bold.ttf differ diff --git a/src/Controls/samples/Controls.Sample/Resources/Fonts/LobsterTwo-BoldItalic.ttf b/src/Controls/samples/Controls.Sample/Resources/Fonts/LobsterTwo-BoldItalic.ttf new file mode 100644 index 000000000000..8bbf8d827893 Binary files /dev/null and b/src/Controls/samples/Controls.Sample/Resources/Fonts/LobsterTwo-BoldItalic.ttf differ diff --git a/src/Controls/samples/Controls.Sample/Resources/Fonts/LobsterTwo-Italic.ttf b/src/Controls/samples/Controls.Sample/Resources/Fonts/LobsterTwo-Italic.ttf new file mode 100644 index 000000000000..b88ec1723c6c Binary files /dev/null and b/src/Controls/samples/Controls.Sample/Resources/Fonts/LobsterTwo-Italic.ttf differ diff --git a/src/Controls/samples/Controls.Sample/Resources/Fonts/LobsterTwo-Regular.ttf b/src/Controls/samples/Controls.Sample/Resources/Fonts/LobsterTwo-Regular.ttf new file mode 100644 index 000000000000..556c45e7d1d6 Binary files /dev/null and b/src/Controls/samples/Controls.Sample/Resources/Fonts/LobsterTwo-Regular.ttf differ diff --git a/src/Controls/samples/Controls.Sample/Startup.cs b/src/Controls/samples/Controls.Sample/Startup.cs index 8c7b86971ad7..4a1e4e47247b 100644 --- a/src/Controls/samples/Controls.Sample/Startup.cs +++ b/src/Controls/samples/Controls.Sample/Startup.cs @@ -103,6 +103,10 @@ public void Configure(IAppHostBuilder appBuilder) .ConfigureFonts(fonts => { fonts.AddFont("Dokdo-Regular.ttf", "Dokdo"); + fonts.AddFont("LobsterTwo-Regular.ttf", "Lobster Two"); + fonts.AddFont("LobsterTwo-Bold.ttf", "Lobster Two Bold"); + fonts.AddFont("LobsterTwo-Italic.ttf", "Lobster Two Italic"); + fonts.AddFont("LobsterTwo-BoldItalic.ttf", "Lobster Two BoldItalic"); fonts.AddFont("ionicons.ttf", "Ionicons"); }) //.ConfigureEssentials(essentials => diff --git a/src/Core/src/Fonts/EmbeddedFontLoader.Standard.cs b/src/Core/src/Fonts/EmbeddedFontLoader.Standard.cs index 09e3f9b0844c..caa09637cf99 100644 --- a/src/Core/src/Fonts/EmbeddedFontLoader.Standard.cs +++ b/src/Core/src/Fonts/EmbeddedFontLoader.Standard.cs @@ -4,6 +4,6 @@ namespace Microsoft.Maui { public partial class EmbeddedFontLoader { - public (bool success, string? filePath) LoadFont(EmbeddedFont font) => (false, null); + public string? LoadFont(EmbeddedFont font) => null; } } \ No newline at end of file diff --git a/src/Core/src/Fonts/EmbeddedFontLoader.Windows.cs b/src/Core/src/Fonts/EmbeddedFontLoader.Windows.cs index 8204f3dfc58f..8bf30c6816e3 100644 --- a/src/Core/src/Fonts/EmbeddedFontLoader.Windows.cs +++ b/src/Core/src/Fonts/EmbeddedFontLoader.Windows.cs @@ -10,13 +10,13 @@ public partial class EmbeddedFontLoader { const string FontCacheFolderName = "fonts"; - public (bool success, string? filePath) LoadFont(EmbeddedFont font) + public string? LoadFont(EmbeddedFont font) { var tmpdir = ApplicationData.Current.LocalFolder.CreateFolderAsync(FontCacheFolderName, CreationCollisionOption.OpenIfExists).AsTask().Result; var file = tmpdir.TryGetItemAsync(font.FontName).AsTask().Result; if (file != null) - return (true, CleanseFilePath(file.Path)); + return CleanseFilePath(file.Path); StorageFile? newFile = null; try @@ -30,7 +30,7 @@ public partial class EmbeddedFontLoader font.ResourceStream.CopyTo(fileStream); } - return (true, CleanseFilePath(newFile.Path)); + return CleanseFilePath(newFile.Path); } catch (Exception ex) { @@ -40,7 +40,7 @@ public partial class EmbeddedFontLoader newFile.DeleteAsync().AsTask().Wait(); } - return (false, null); + return null; } static string CleanseFilePath(string filePath) diff --git a/src/Core/src/Fonts/EmbeddedFontLoader.iOS.cs b/src/Core/src/Fonts/EmbeddedFontLoader.iOS.cs index cd9438650b08..d9de19008df7 100644 --- a/src/Core/src/Fonts/EmbeddedFontLoader.iOS.cs +++ b/src/Core/src/Fonts/EmbeddedFontLoader.iOS.cs @@ -10,7 +10,7 @@ namespace Microsoft.Maui { public partial class EmbeddedFontLoader { - public (bool success, string? filePath) LoadFont(EmbeddedFont font) + public string? LoadFont(EmbeddedFont font) { try { @@ -19,15 +19,15 @@ public partial class EmbeddedFontLoader var data = NSData.FromStream(font.ResourceStream); var provider = new CGDataProvider(data); - var cGFont = CGFont.CreateFromProvider(provider); - var name = cGFont.PostScriptName; + var cgFont = CGFont.CreateFromProvider(provider); + var name = cgFont.PostScriptName; - if (CTFontManager.RegisterGraphicsFont(cGFont, out var error)) - return (true, name); + if (CTFontManager.RegisterGraphicsFont(cgFont, out var error)) + return name; var uiFont = UIFont.FromName(name, 10); if (uiFont != null) - return (true, name); + return name; throw new NSErrorException(error); } @@ -36,7 +36,7 @@ public partial class EmbeddedFontLoader _logger?.LogWarning(ex, "Unable register font {Font} with the system.", font.FontName); } - return (false, null); + return null; } } } \ No newline at end of file diff --git a/src/Core/src/Fonts/FileSystemEmbeddedFontLoader.cs b/src/Core/src/Fonts/FileSystemEmbeddedFontLoader.cs index 7de033b062a3..ac8a11b770d7 100644 --- a/src/Core/src/Fonts/FileSystemEmbeddedFontLoader.cs +++ b/src/Core/src/Fonts/FileSystemEmbeddedFontLoader.cs @@ -16,11 +16,11 @@ public FileSystemEmbeddedFontLoader(string rootPath, ILogger _logger = logger; } - public (bool success, string? filePath) LoadFont(EmbeddedFont font) + public string? LoadFont(EmbeddedFont font) { var filePath = Path.Combine(_rootPath, font.FontName!); if (File.Exists(filePath)) - return (true, filePath); + return filePath; try { @@ -35,7 +35,7 @@ public FileSystemEmbeddedFontLoader(string rootPath, ILogger font.ResourceStream.CopyTo(fileStream); } - return (true, filePath); + return filePath; } catch (Exception ex) { @@ -44,7 +44,7 @@ public FileSystemEmbeddedFontLoader(string rootPath, ILogger File.Delete(filePath); } - return (false, null); + return null; } } } \ No newline at end of file diff --git a/src/Core/src/Fonts/FontManager.Android.cs b/src/Core/src/Fonts/FontManager.Android.cs index a1a81be325e6..c7fb00972aaf 100644 --- a/src/Core/src/Fonts/FontManager.Android.cs +++ b/src/Core/src/Fonts/FontManager.Android.cs @@ -8,6 +8,13 @@ namespace Microsoft.Maui { public class FontManager : IFontManager { + static readonly string[] FontFolders = new[] + { + "", + "Fonts/", + "fonts/", + }; + readonly ConcurrentDictionary<(string fontFamilyName, FontWeight weight, bool italic), Typeface?> _typefaces = new(); readonly IFontRegistrar _fontRegistrar; readonly ILogger? _logger; @@ -31,77 +38,63 @@ public FontManager(IFontRegistrar fontRegistrar, ILogger? logger = } public float GetFontSize(Font font, float defaultFontSize = 0) => - font.FontSize > 0 ? (float)font.FontSize : (defaultFontSize > 0 ? defaultFontSize : 14f); + font.FontSize <= 0 + ? (defaultFontSize > 0 ? defaultFontSize : 14f) + : (float)font.FontSize; - (bool success, Typeface? typeface) TryGetFromAssets(string fontName) + Typeface? GetFromAssets(string fontName) { - //First check Alias - var (hasFontAlias, fontPostScriptName) = _fontRegistrar.HasFont(fontName); - if (hasFontAlias) - return (true, Typeface.CreateFromFile(fontPostScriptName)); + // First check Alias + if (_fontRegistrar.GetFont(fontName) is string fontPostScriptName) + return Typeface.CreateFromFile(fontPostScriptName); var isAssetFont = IsAssetFontFamily(fontName); if (isAssetFont) - { return LoadTypefaceFromAsset(fontName); - } - var folders = new[] - { - "", - "Fonts/", - "fonts/", - }; - - //copied text + // copied text var fontFile = FontFile.FromString(fontName); if (!string.IsNullOrWhiteSpace(fontFile.Extension)) { - var (hasFont, fontPath) = _fontRegistrar.HasFont(fontFile.FileNameWithExtension()); - if (hasFont) - { - return (true, Typeface.CreateFromFile(fontPath)); - } + if (_fontRegistrar.GetFont(fontFile.FileNameWithExtension()) is string fontPath) + return Typeface.CreateFromFile(fontPath); } else { foreach (var ext in FontFile.Extensions) { - var formated = fontFile.FileNameWithExtension(ext); - var (hasFont, fontPath) = _fontRegistrar.HasFont(formated); - if (hasFont) - { - return (true, Typeface.CreateFromFile(fontPath)); - } + var formatted = fontFile.FileNameWithExtension(ext); + if (_fontRegistrar.GetFont(formatted) is string fontPath) + return Typeface.CreateFromFile(fontPath); - foreach (var folder in folders) + foreach (var folder in FontFolders) { - formated = $"{folder}{fontFile.FileNameWithExtension()}#{fontFile.PostScriptName}"; - var result = LoadTypefaceFromAsset(formated); - if (result.success) + formatted = $"{folder}{fontFile.FileNameWithExtension()}#{fontFile.PostScriptName}"; + var result = LoadTypefaceFromAsset(formatted, false); + if (result != null) return result; } - } } - return (false, null); + return null; } - (bool success, Typeface? typeface) LoadTypefaceFromAsset(string fontfamily) + Typeface? LoadTypefaceFromAsset(string fontfamily, bool warning = true) { try { - var result = Typeface.CreateFromAsset(AApplication.Context.Assets, FontNameToFontFile(fontfamily)); - return (true, result); + return Typeface.CreateFromAsset(AApplication.Context.Assets, FontNameToFontFile(fontfamily)); } catch (Exception ex) { - _logger?.LogWarning(ex, "Unable to load font '{Font}' from assets.", fontfamily); - return (false, null); + if (warning) + _logger?.LogWarning(ex, "Unable to load font '{Font}' from assets.", fontfamily); } + + return null; } bool IsAssetFontFamily(string name) @@ -113,54 +106,37 @@ bool IsAssetFontFamily(string name) { var (fontFamily, weight, italic) = fontData; fontFamily ??= string.Empty; + var style = ToTypefaceStyle(weight, italic); - Typeface? result; + var result = Typeface.Default; - if (string.IsNullOrWhiteSpace(fontFamily)) + if (!string.IsNullOrWhiteSpace(fontFamily)) { - if (NativeVersion.IsAtLeast(28)) + if (IsAssetFontFamily(fontFamily)) { - result = Typeface.Create(Typeface.Default, (int)weight, italic); + result = Typeface.CreateFromAsset(AApplication.Context.Assets, FontNameToFontFile(fontFamily)); } else { - var style = ToTypefaceStyle(weight, italic); - result = Typeface.Create(Typeface.Default, style); - } - } - else if (IsAssetFontFamily(fontFamily)) - { - result = Typeface.CreateFromAsset(AApplication.Context.Assets, FontNameToFontFile(fontFamily)); - } - else - { - fontFamily ??= string.Empty; - var (success, typeface) = TryGetFromAssets(fontFamily); - if (success) - { - return typeface; - } - else - { - if (NativeVersion.IsAtLeast(28)) - { - return Typeface.Create(Typeface.Default, (int)weight, italic); - } + if (GetFromAssets(fontFamily) is Typeface typeface) + result = typeface; else - { - var style = ToTypefaceStyle(weight, italic); - return Typeface.Create(Typeface.Default, style); - } + result = Typeface.Create(fontFamily, style); } } + if (NativeVersion.IsAtLeast(28)) + result = Typeface.Create(result, (int)weight, italic); + else + result = Typeface.Create(result, style); + return result; } TypefaceStyle ToTypefaceStyle(FontWeight weight, bool italic) { var style = TypefaceStyle.Normal; - var bold = weight > FontWeight.Bold; + var bold = weight >= FontWeight.Bold; if (bold && italic) style = TypefaceStyle.BoldItalic; else if (bold) @@ -169,6 +145,7 @@ TypefaceStyle ToTypefaceStyle(FontWeight weight, bool italic) style = TypefaceStyle.Italic; return style; } + string FontNameToFontFile(string fontFamily) { fontFamily ??= string.Empty; diff --git a/src/Core/src/Fonts/FontManager.Windows.cs b/src/Core/src/Fonts/FontManager.Windows.cs index 6e8d6fd8dcb0..451cbb53065e 100644 --- a/src/Core/src/Fonts/FontManager.Windows.cs +++ b/src/Core/src/Fonts/FontManager.Windows.cs @@ -46,7 +46,10 @@ public FontFamily GetFontFamily(Font font) return _fonts.GetOrAdd(font.FontFamily, CreateFontFamily); } - public double GetFontSize(Font font, double defaultFontSize = 0) => font.FontSize > 0 ? font.FontSize : (defaultFontSize > 0 ? defaultFontSize : DefaultFontSize); + public double GetFontSize(Font font, double defaultFontSize = 0) => + font.FontSize <= 0 + ? (defaultFontSize > 0 ? defaultFontSize : DefaultFontSize) + : font.FontSize; FontFamily CreateFontFamily(string fontFamily) { @@ -60,7 +63,7 @@ FontFamily CreateFontFamily(string fontFamily) IEnumerable GetAllFontPossibilities(string fontFamily) { // First check Alias - if (_fontRegistrar.TryGetFont(fontFamily, out var fontPostScriptName)) + if (_fontRegistrar.GetFont(fontFamily) is string fontPostScriptName) { if (fontPostScriptName!.Contains("://") && fontPostScriptName.Contains("#")) { @@ -84,8 +87,7 @@ IEnumerable GetAllFontPossibilities(string fontFamily) var hasExtension = !string.IsNullOrWhiteSpace(fontFile.Extension); if (hasExtension) { - var (hasFont, filePath) = _fontRegistrar.HasFont(fontFile.FileNameWithExtension()); - if (hasFont) + if (_fontRegistrar.GetFont(fontFile.FileNameWithExtension()) is string filePath) { var familyName = FindFontFamilyName(filePath); var formatted = $"{filePath}#{familyName ?? fontFile.GetPostScriptNameWithSpaces()}"; @@ -102,8 +104,7 @@ IEnumerable GetAllFontPossibilities(string fontFamily) // There was no extension so let's just try a few things foreach (var ext in TypicalFontFileExtensions) { - var (hasFont, filePath) = _fontRegistrar.HasFont(fontFile.FileNameWithExtension(ext)); - if (hasFont) + if (_fontRegistrar.GetFont(fontFile.FileNameWithExtension(ext)) is string filePath) { var familyName = FindFontFamilyName(filePath); var formatted = $"{filePath}#{familyName ?? fontFile.GetPostScriptNameWithSpaces()}"; diff --git a/src/Core/src/Fonts/FontManager.iOS.cs b/src/Core/src/Fonts/FontManager.iOS.cs index ea2871438634..d685b00ca615 100644 --- a/src/Core/src/Fonts/FontManager.iOS.cs +++ b/src/Core/src/Fonts/FontManager.iOS.cs @@ -8,6 +8,22 @@ namespace Microsoft.Maui { public class FontManager : IFontManager { + // UIFontWeight[Constant] is internal in Xamarin.iOS but the convertion from + // the public (int-based) enum is not helpful in this case. + // -1.0 (Thin / 100) to 1.0 (Black / 900) with 0 being Regular (400) + // which is not quite the center, not are the constant values linear + static readonly (float value, FontWeight weight)[] FontWeightMap = new (float, FontWeight)[] { + (-0.80f, FontWeight.Ultralight), + (-0.60f, FontWeight.Thin), + (-0.40f, FontWeight.Light), + (0.0f, FontWeight.Regular), + (0.23f, FontWeight.Medium), + (0.30f, FontWeight.Semibold), + (0.40f, FontWeight.Bold), + (0.56f, FontWeight.Heavy), + (0.62f, FontWeight.Black) + }; + readonly ConcurrentDictionary _fonts = new(); readonly IFontRegistrar _fontRegistrar; readonly ILogger? _logger; @@ -23,25 +39,13 @@ public FontManager(IFontRegistrar fontRegistrar, ILogger? logger = public UIFont DefaultFont => _defaultFont ??= UIFont.SystemFontOfSize(UIFont.SystemFontSize); - public UIFont GetFont(Font font, double defaultFontSize = 0) => GetFont(font, defaultFontSize, CreateFont); - - public double GetFontSize(Font font, double defaultFontSize = 0) => font.FontSize <= 0 ? (defaultFontSize > 0 ? (float)defaultFontSize : DefaultFont.PointSize) : (nfloat)font.FontSize; + public UIFont GetFont(Font font, double defaultFontSize = 0) => + GetFont(font, defaultFontSize, CreateFont); - // UIFontWeight[Constant] is internal in Xamarin.iOS but the convertion from - // the public (int-based) enum is not helpful in this case. - // -1.0 (Thin / 100) to 1.0 (Black / 900) with 0 being Regular (400) - // which is not quite the center, not are the constant values linear - static readonly (float value, FontWeight weight)[] FontWeightMap = new (float, FontWeight)[] { - (-0.80f, FontWeight.Ultralight), - (-0.60f, FontWeight.Thin), - (-0.40f, FontWeight.Light), - (0.0f, FontWeight.Regular), - (0.23f, FontWeight.Medium), - (0.30f, FontWeight.Semibold), - (0.40f, FontWeight.Bold), - (0.56f, FontWeight.Heavy), - (0.62f, FontWeight.Black) - }; + public double GetFontSize(Font font, double defaultFontSize = 0) => + font.FontSize <= 0 + ? (defaultFontSize > 0 ? (float)defaultFontSize : DefaultFont.PointSize) + : (nfloat)font.FontSize; static float GetWeightConstant(FontWeight self) { @@ -60,6 +64,7 @@ UIFont GetFont(Font font, double defaultFont, Func factory) font = font.WithSize(size); return _fonts.GetOrAdd(font, factory); } + static UIFontAttributes GetFontAttributes(Font font) { var a = new UIFontAttributes @@ -92,21 +97,21 @@ UIFont CreateFont(Font font) var family = font.FontFamily; var size = (nfloat)font.FontSize; - bool hasAttributes = font.Weight != FontWeight.Regular || font.FontSlant != FontSlant.Default; - + var hasAttributes = + font.Weight != FontWeight.Regular || + font.FontSlant != FontSlant.Default; if (family != null && family != DefaultFont.FamilyName) { try { UIFont? result = null; + if (UIFont.FamilyNames.Contains(family)) { var descriptor = new UIFontDescriptor().CreateWithFamily(family); if (hasAttributes) - { descriptor = descriptor.CreateWithAttributes(GetFontAttributes(font)); - } result = UIFont.FromDescriptor(descriptor, size); if (result != null) @@ -115,6 +120,9 @@ UIFont CreateFont(Font font) var cleansedFont = CleanseFontName(family); result = UIFont.FromName(cleansedFont, size); + if (result != null) + return result; + if (family.StartsWith(".SFUI", StringComparison.InvariantCultureIgnoreCase)) { var fontWeight = family.Split('-').LastOrDefault(); @@ -122,14 +130,16 @@ UIFont CreateFont(Font font) if (!string.IsNullOrWhiteSpace(fontWeight) && Enum.TryParse(fontWeight, true, out var uIFontWeight)) { result = UIFont.SystemFontOfSize(size, uIFontWeight); - return result; + if (result != null) + return result; } result = UIFont.SystemFontOfSize(size, UIFontWeight.Regular); - return result; + if (result != null) + return result; } - if (result == null) - result = UIFont.FromName(family, size); + + result = UIFont.FromName(family, size); if (result != null) return result; } @@ -152,26 +162,22 @@ UIFont CreateFont(Font font) string? CleanseFontName(string fontName) { // First check Alias - var (hasFontAlias, fontPostScriptName) = _fontRegistrar.HasFont(fontName); - if (hasFontAlias) + if (_fontRegistrar.GetFont(fontName) is string fontPostScriptName) return fontPostScriptName; var fontFile = FontFile.FromString(fontName); if (!string.IsNullOrWhiteSpace(fontFile.Extension)) { - var (hasFont, filePath) = _fontRegistrar.HasFont(fontFile.FileNameWithExtension()); - if (hasFont) + if (_fontRegistrar.GetFont(fontFile.FileNameWithExtension()) is string filePath) return filePath ?? fontFile.PostScriptName; } else { foreach (var ext in FontFile.Extensions) { - - var formated = fontFile.FileNameWithExtension(ext); - var (hasFont, filePath) = _fontRegistrar.HasFont(formated); - if (hasFont) + var formatted = fontFile.FileNameWithExtension(ext); + if (_fontRegistrar.GetFont(formatted) is string filePath) return filePath; } } diff --git a/src/Core/src/Fonts/FontRegistrar.Android.cs b/src/Core/src/Fonts/FontRegistrar.Android.cs new file mode 100644 index 000000000000..7d4a0d7ce3ce --- /dev/null +++ b/src/Core/src/Fonts/FontRegistrar.Android.cs @@ -0,0 +1,27 @@ +#nullable enable +using System.IO; + +namespace Microsoft.Maui +{ + public partial class FontRegistrar : IFontRegistrar + { + string? LoadNativeAppFont(string font, string filename, string? alias) + { + using var stream = GetNativeFontStream(filename, alias); + + return LoadEmbeddedFont(font, filename, alias, stream); + } + + Stream GetNativeFontStream(string filename, string? alias) + { + var assets = Android.App.Application.Context.Assets; + + if (assets != null && assets.Open(filename) is Stream assetStream) + return assetStream; + + // TODO: check other folders as well + + throw new FileNotFoundException($"Native font with the name {filename} was not found."); + } + } +} \ No newline at end of file diff --git a/src/Core/src/Fonts/FontRegistrar.Standard.cs b/src/Core/src/Fonts/FontRegistrar.Standard.cs new file mode 100644 index 000000000000..34135a35ebbb --- /dev/null +++ b/src/Core/src/Fonts/FontRegistrar.Standard.cs @@ -0,0 +1,9 @@ +#nullable enable + +namespace Microsoft.Maui +{ + public partial class FontRegistrar : IFontRegistrar + { + string? LoadNativeAppFont(string font, string filename, string? alias) => null; + } +} \ No newline at end of file diff --git a/src/Core/src/Fonts/FontRegistrar.Windows.cs b/src/Core/src/Fonts/FontRegistrar.Windows.cs new file mode 100644 index 000000000000..d6f764174793 --- /dev/null +++ b/src/Core/src/Fonts/FontRegistrar.Windows.cs @@ -0,0 +1,29 @@ +#nullable enable +using System.IO; + +namespace Microsoft.Maui +{ + public partial class FontRegistrar : IFontRegistrar + { + string? LoadNativeAppFont(string font, string filename, string? alias) + { + var root = Windows.ApplicationModel.Package.Current.InstalledLocation.Path; + + var packagePath = Path.Combine(root, "Assets", filename); + if (File.Exists(packagePath)) + return $"ms-appx:///Assets/{filename}"; + + packagePath = Path.Combine(root, "Fonts", filename); + if (File.Exists(packagePath)) + return $"ms-appx:///Fonts/{filename}"; + + packagePath = Path.Combine(root, "Assets", "Fonts", filename); + if (File.Exists(packagePath)) + return $"ms-appx:///Assets/Fonts/{filename}"; + + // TODO: check other folders as well + + throw new FileNotFoundException($"Native font with the name {filename} was not found."); + } + } +} \ No newline at end of file diff --git a/src/Core/src/Fonts/FontRegistrar.cs b/src/Core/src/Fonts/FontRegistrar.cs index 77958ae57468..04fad8a240ca 100644 --- a/src/Core/src/Fonts/FontRegistrar.cs +++ b/src/Core/src/Fonts/FontRegistrar.cs @@ -8,11 +8,11 @@ namespace Microsoft.Maui { - public class FontRegistrar : IFontRegistrar + public partial class FontRegistrar : IFontRegistrar { readonly Dictionary _embeddedFonts = new(); readonly Dictionary _nativeFonts = new(); - readonly Dictionary _fontLookupCache = new(); + readonly Dictionary _fontLookupCache = new(); readonly ILogger? _logger; IEmbeddedFontLoader? _fontLoader; @@ -44,7 +44,7 @@ public void Register(string filename, string? alias) _nativeFonts[alias!] = (filename, alias); } - public (bool hasFont, string? fontPath) HasFont(string font) + public string? GetFont(string font) { if (_fontLookupCache.TryGetValue(font, out var foundResult)) return foundResult; @@ -55,22 +55,11 @@ public void Register(string filename, string? alias) { using var stream = GetEmbeddedResourceStream(foundFont); - return TryLoadFont(font, foundFont.Filename, foundFont.Alias, stream); + return LoadEmbeddedFont(font, foundFont.Filename, foundFont.Alias, stream); } else if (_nativeFonts.TryGetValue(font, out var foundNativeFont)) { - if (CanUsePackagedNativeFonts) - { - var match = (true, GetNativeFontUri(foundNativeFont)); - _fontLookupCache[font] = match; - return match; - } - else - { - using var stream = GetNativeFontStream(foundNativeFont); - - return TryLoadFont(font, foundNativeFont.Filename, foundNativeFont.Alias, stream); - } + return LoadNativeAppFont(font, foundNativeFont.Filename, foundNativeFont.Alias); } } catch (Exception ex) @@ -78,10 +67,10 @@ public void Register(string filename, string? alias) _logger?.LogWarning(ex, "Unable to load font '{Font}'.", font); } - return _fontLookupCache[font] = (false, null); + return _fontLookupCache[font] = null; } - (bool hasFont, string? fontPath) TryLoadFont(string cacheKey, string filename, string? alias, Stream stream) + string? LoadEmbeddedFont(string cacheKey, string filename, string? alias, Stream stream) { var font = new EmbeddedFont { FontName = filename, ResourceStream = stream }; @@ -117,71 +106,5 @@ bool IsFile(string path, string file) return path.Replace(file, "").EndsWith(".", StringComparison.Ordinal); } - - bool CanUsePackagedNativeFonts => -#if WINDOWS - true; -#else - false; -#endif - - string GetNativeFontUri((string Filename, string? Alias) nativeFont) - { -#if WINDOWS - var alias = nativeFont.Alias; - if (!string.IsNullOrEmpty(alias)) - alias = "#" + alias; - - var root = Windows.ApplicationModel.Package.Current.InstalledLocation.Path; - - var packagePath = Path.Combine(root, "Assets", nativeFont.Filename); - if (File.Exists(packagePath)) - return $"ms-appx:///Assets/{nativeFont.Filename}{alias}"; - - packagePath = Path.Combine(root, "Fonts", nativeFont.Filename); - if (File.Exists(packagePath)) - return $"ms-appx:///Fonts/{nativeFont.Filename}{alias}"; - - packagePath = Path.Combine(root, "Assets", "Fonts", nativeFont.Filename); - if (File.Exists(packagePath)) - return $"ms-appx:///Assets/Fonts/{nativeFont.Filename}{alias}"; - - // TODO: check other folders as well -#endif - - throw new FileNotFoundException($"Native font with the name {nativeFont.Filename} was not found."); - } - - Stream GetNativeFontStream((string Filename, string? Alias) nativeFont) - { -#if __IOS__ || IOS - var mainBundlePath = Foundation.NSBundle.MainBundle.BundlePath; - - var fontBundlePath = Path.Combine(mainBundlePath, nativeFont.Filename); - if (File.Exists(fontBundlePath)) - return File.OpenRead(fontBundlePath); - - fontBundlePath = Path.Combine(mainBundlePath, "Resources", nativeFont.Filename); - if (File.Exists(fontBundlePath)) - return File.OpenRead(fontBundlePath); - - fontBundlePath = Path.Combine(mainBundlePath, "Fonts", nativeFont.Filename); - if (File.Exists(fontBundlePath)) - return File.OpenRead(fontBundlePath); - - fontBundlePath = Path.Combine(mainBundlePath, "Resources", "Fonts", nativeFont.Filename); - if (File.Exists(fontBundlePath)) - return File.OpenRead(fontBundlePath); -#elif __ANDROID__ || ANDROID - var assets = Android.App.Application.Context.Assets; - - if (assets != null && assets.Open(nativeFont.Filename) is Stream assetStream) - return assetStream; - - // TODO: check other folders as well -#endif - - throw new FileNotFoundException($"Native font with the name {nativeFont.Filename} was not found."); - } } } \ No newline at end of file diff --git a/src/Core/src/Fonts/FontRegistrar.iOS.cs b/src/Core/src/Fonts/FontRegistrar.iOS.cs new file mode 100644 index 000000000000..40d6ab48ab5f --- /dev/null +++ b/src/Core/src/Fonts/FontRegistrar.iOS.cs @@ -0,0 +1,40 @@ +#nullable enable +using System.IO; + +namespace Microsoft.Maui +{ + public partial class FontRegistrar : IFontRegistrar + { + string? LoadNativeAppFont(string font, string filename, string? alias) + { + using var stream = GetNativeFontStream(filename, alias); + + return LoadEmbeddedFont(font, filename, alias, stream); + } + + Stream GetNativeFontStream(string filename, string? alias) + { + var mainBundlePath = Foundation.NSBundle.MainBundle.BundlePath; + + var fontBundlePath = Path.Combine(mainBundlePath, filename); + if (File.Exists(fontBundlePath)) + return File.OpenRead(fontBundlePath); + + fontBundlePath = Path.Combine(mainBundlePath, "Resources", filename); + if (File.Exists(fontBundlePath)) + return File.OpenRead(fontBundlePath); + + fontBundlePath = Path.Combine(mainBundlePath, "Fonts", filename); + if (File.Exists(fontBundlePath)) + return File.OpenRead(fontBundlePath); + + fontBundlePath = Path.Combine(mainBundlePath, "Resources", "Fonts", filename); + if (File.Exists(fontBundlePath)) + return File.OpenRead(fontBundlePath); + + // TODO: check other folders as well + + throw new FileNotFoundException($"Native font with the name {filename} was not found."); + } + } +} \ No newline at end of file diff --git a/src/Core/src/Fonts/FontRegistrarExtensions.cs b/src/Core/src/Fonts/FontRegistrarExtensions.cs deleted file mode 100644 index 45f59e7a0f71..000000000000 --- a/src/Core/src/Fonts/FontRegistrarExtensions.cs +++ /dev/null @@ -1,18 +0,0 @@ -#nullable enable -using System.Diagnostics.CodeAnalysis; - -namespace Microsoft.Maui -{ - public static class FontRegistrarExtensions - { - public static bool TryGetFont(this IFontRegistrar fontRegistrar, string font, out string? fontPath) - { - var (hasFont, foundFontPath) = fontRegistrar.HasFont(font); - if (hasFont) - fontPath = foundFontPath!; - else - fontPath = null; - return hasFont; - } - } -} \ No newline at end of file diff --git a/src/Core/src/Fonts/IEmbeddedFontLoader.cs b/src/Core/src/Fonts/IEmbeddedFontLoader.cs index b6892b902359..aa4a5be2c935 100644 --- a/src/Core/src/Fonts/IEmbeddedFontLoader.cs +++ b/src/Core/src/Fonts/IEmbeddedFontLoader.cs @@ -3,6 +3,7 @@ namespace Microsoft.Maui { public interface IEmbeddedFontLoader { - (bool success, string? filePath) LoadFont(EmbeddedFont font); + // TODO: this should be async as it involves copying files + string? LoadFont(EmbeddedFont font); } } \ No newline at end of file diff --git a/src/Core/src/Fonts/IFontRegistrar.cs b/src/Core/src/Fonts/IFontRegistrar.cs index 66bb43c28194..81fd54c285e5 100644 --- a/src/Core/src/Fonts/IFontRegistrar.cs +++ b/src/Core/src/Fonts/IFontRegistrar.cs @@ -9,7 +9,7 @@ public interface IFontRegistrar void Register(string filename, string? alias); - //TODO: Investigate making this Async - (bool hasFont, string? fontPath) HasFont(string font); + // TODO: this should be async as it involves copying files + string? GetFont(string font); } } \ No newline at end of file diff --git a/src/Core/tests/DeviceTests/AssertionExtensions.Android.cs b/src/Core/tests/DeviceTests/AssertionExtensions.Android.cs index 441f11c53565..328133f875b1 100644 --- a/src/Core/tests/DeviceTests/AssertionExtensions.Android.cs +++ b/src/Core/tests/DeviceTests/AssertionExtensions.Android.cs @@ -205,5 +205,10 @@ public static TextUtils.TruncateAt ToNative(this LineBreakMode mode) => LineBreakMode.MiddleTruncation => TextUtils.TruncateAt.Middle, _ => throw new ArgumentOutOfRangeException(nameof(mode)) }; + + public static FontWeight GetFontWeight(this Typeface typeface) => + NativeVersion.IsAtLeast(28) + ? (FontWeight)typeface.Weight + : typeface.IsBold ? FontWeight.Bold : FontWeight.Regular; } } \ No newline at end of file diff --git a/src/Core/tests/DeviceTests/Fonts/LobsterTwo-Bold.ttf b/src/Core/tests/DeviceTests/Fonts/LobsterTwo-Bold.ttf new file mode 100644 index 000000000000..2e979fbff2ca Binary files /dev/null and b/src/Core/tests/DeviceTests/Fonts/LobsterTwo-Bold.ttf differ diff --git a/src/Core/tests/DeviceTests/Fonts/LobsterTwo-BoldItalic.ttf b/src/Core/tests/DeviceTests/Fonts/LobsterTwo-BoldItalic.ttf new file mode 100644 index 000000000000..8bbf8d827893 Binary files /dev/null and b/src/Core/tests/DeviceTests/Fonts/LobsterTwo-BoldItalic.ttf differ diff --git a/src/Core/tests/DeviceTests/Fonts/LobsterTwo-Italic.ttf b/src/Core/tests/DeviceTests/Fonts/LobsterTwo-Italic.ttf new file mode 100644 index 000000000000..b88ec1723c6c Binary files /dev/null and b/src/Core/tests/DeviceTests/Fonts/LobsterTwo-Italic.ttf differ diff --git a/src/Core/tests/DeviceTests/Fonts/LobsterTwo-Regular.ttf b/src/Core/tests/DeviceTests/Fonts/LobsterTwo-Regular.ttf new file mode 100644 index 000000000000..556c45e7d1d6 Binary files /dev/null and b/src/Core/tests/DeviceTests/Fonts/LobsterTwo-Regular.ttf differ diff --git a/src/Core/tests/DeviceTests/Handlers/Button/ButtonHandlerTests.Android.cs b/src/Core/tests/DeviceTests/Handlers/Button/ButtonHandlerTests.Android.cs index c02f6b492d4c..1b443eb82e57 100644 --- a/src/Core/tests/DeviceTests/Handlers/Button/ButtonHandlerTests.Android.cs +++ b/src/Core/tests/DeviceTests/Handlers/Button/ButtonHandlerTests.Android.cs @@ -1,6 +1,5 @@ using System.Threading.Tasks; using AndroidX.AppCompat.Widget; -using Java.IO; using Microsoft.Extensions.DependencyInjection; using Microsoft.Maui.DeviceTests.Stubs; using Microsoft.Maui.Graphics; @@ -135,7 +134,7 @@ double GetNativeUnscaledFontSize(ButtonHandler buttonHandler) } bool GetNativeIsBold(ButtonHandler buttonHandler) => - GetNativeButton(buttonHandler).Typeface.Weight == (int)FontWeight.Bold || GetNativeButton(buttonHandler).Typeface.IsBold; + GetNativeButton(buttonHandler).Typeface.GetFontWeight() == FontWeight.Bold; bool GetNativeIsItalic(ButtonHandler buttonHandler) => GetNativeButton(buttonHandler).Typeface.IsItalic; diff --git a/src/Core/tests/DeviceTests/Handlers/Button/ButtonHandlerTests.cs b/src/Core/tests/DeviceTests/Handlers/Button/ButtonHandlerTests.cs index f4c5f0104429..23a0c617b20e 100644 --- a/src/Core/tests/DeviceTests/Handlers/Button/ButtonHandlerTests.cs +++ b/src/Core/tests/DeviceTests/Handlers/Button/ButtonHandlerTests.cs @@ -78,8 +78,8 @@ public async Task FontAttributesInitializeCorrectly(FontWeight weight, bool isBo Font = Font.OfSize("Arial", 10, weight, isItalic ? FontSlant.Italic : FontSlant.Default) }; - await ValidatePropertyInitValue(button, () => button.Font.Weight.HasFlag(FontWeight.Bold), GetNativeIsBold, isBold); - await ValidatePropertyInitValue(button, () => button.Font.FontSlant.HasFlag(FontSlant.Italic), GetNativeIsItalic, isItalic); + await ValidatePropertyInitValue(button, () => button.Font.Weight == FontWeight.Bold, GetNativeIsBold, isBold); + await ValidatePropertyInitValue(button, () => button.Font.FontSlant == FontSlant.Italic, GetNativeIsItalic, isItalic); } } } \ No newline at end of file diff --git a/src/Core/tests/DeviceTests/Handlers/Entry/EntryHandlerTests.Android.cs b/src/Core/tests/DeviceTests/Handlers/Entry/EntryHandlerTests.Android.cs index 3c17226a5174..771ad04079fd 100644 --- a/src/Core/tests/DeviceTests/Handlers/Entry/EntryHandlerTests.Android.cs +++ b/src/Core/tests/DeviceTests/Handlers/Entry/EntryHandlerTests.Android.cs @@ -184,7 +184,7 @@ double GetNativeUnscaledFontSize(EntryHandler entryHandler) } bool GetNativeIsBold(EntryHandler entryHandler) => - GetNativeEntry(entryHandler).Typeface.IsBold; + GetNativeEntry(entryHandler).Typeface.GetFontWeight() == FontWeight.Bold; bool GetNativeIsItalic(EntryHandler entryHandler) => GetNativeEntry(entryHandler).Typeface.IsItalic; diff --git a/src/Core/tests/DeviceTests/Handlers/Entry/EntryHandlerTests.cs b/src/Core/tests/DeviceTests/Handlers/Entry/EntryHandlerTests.cs index d06f512c2385..de02becdf941 100644 --- a/src/Core/tests/DeviceTests/Handlers/Entry/EntryHandlerTests.cs +++ b/src/Core/tests/DeviceTests/Handlers/Entry/EntryHandlerTests.cs @@ -190,8 +190,8 @@ public async Task FontAttributesInitializeCorrectly(FontWeight weight, bool isBo Font = Font.OfSize("Arial", 10, weight, isItalic ? FontSlant.Italic : FontSlant.Default) }; - await ValidatePropertyInitValue(entry, () => entry.Font.Weight.HasFlag(FontWeight.Bold), GetNativeIsBold, isBold); - await ValidatePropertyInitValue(entry, () => entry.Font.FontSlant.HasFlag(FontSlant.Italic), GetNativeIsItalic, isItalic); + await ValidatePropertyInitValue(entry, () => entry.Font.Weight == FontWeight.Bold, GetNativeIsBold, isBold); + await ValidatePropertyInitValue(entry, () => entry.Font.FontSlant == FontSlant.Italic, GetNativeIsItalic, isItalic); } [Theory(DisplayName = "Validates clear button visibility.")] diff --git a/src/Core/tests/DeviceTests/Handlers/HandlerTestBase.cs b/src/Core/tests/DeviceTests/Handlers/HandlerTestBase.cs index 329b5c3c2249..480689f549a7 100644 --- a/src/Core/tests/DeviceTests/Handlers/HandlerTestBase.cs +++ b/src/Core/tests/DeviceTests/Handlers/HandlerTestBase.cs @@ -25,6 +25,10 @@ public HandlerTestBase() .ConfigureFonts((ctx, fonts) => { fonts.AddFont("dokdo_regular.ttf", "Dokdo"); + fonts.AddFont("LobsterTwo-Regular.ttf", "Lobster Two"); + fonts.AddFont("LobsterTwo-Bold.ttf", "Lobster Two Bold"); + fonts.AddFont("LobsterTwo-Italic.ttf", "Lobster Two Italic"); + fonts.AddFont("LobsterTwo-BoldItalic.ttf", "Lobster Two BoldItalic"); }); _host = appBuilder.Build(); diff --git a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Android.cs b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Android.cs index df898498e93a..5188537c36b1 100644 --- a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Android.cs +++ b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Android.cs @@ -139,7 +139,7 @@ double GetNativeUnscaledFontSize(LabelHandler labelHandler) } bool GetNativeIsBold(LabelHandler labelHandler) => - GetNativeLabel(labelHandler).Typeface.IsBold; + GetNativeLabel(labelHandler).Typeface.GetFontWeight() == FontWeight.Bold; bool GetNativeIsItalic(LabelHandler labelHandler) => GetNativeLabel(labelHandler).Typeface.IsItalic; diff --git a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.cs b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.cs index f011f3f2e97b..fd2250c027a1 100644 --- a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.cs +++ b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.cs @@ -1,4 +1,3 @@ -using System; using System.Threading.Tasks; using Microsoft.Maui.DeviceTests.Stubs; using Microsoft.Maui.Graphics; @@ -74,8 +73,8 @@ public async Task FontAttributesInitializeCorrectly(FontWeight weight, bool isBo Font = Font.OfSize("Arial", 10, weight, isItalic ? FontSlant.Italic : FontSlant.Default) }; - await ValidatePropertyInitValue(label, () => label.Font.Weight.HasFlag(FontWeight.Bold), GetNativeIsBold, isBold); - await ValidatePropertyInitValue(label, () => label.Font.FontSlant.HasFlag(FontSlant.Italic), GetNativeIsItalic, isItalic); + await ValidatePropertyInitValue(label, () => label.Font.Weight == FontWeight.Bold, GetNativeIsBold, isBold); + await ValidatePropertyInitValue(label, () => label.Font.FontSlant == FontSlant.Italic, GetNativeIsItalic, isItalic); } [Fact(DisplayName = "CharacterSpacing Initializes Correctly")] @@ -168,6 +167,49 @@ await ValidateUnrelatedPropertyUnaffected( () => label.Font = Font.SystemFontOfSize(newSize)); } + [Theory(DisplayName = "Font Family and Weight Initializes Correctly")] + [InlineData(null, FontWeight.Regular, FontSlant.Default)] + [InlineData(null, FontWeight.Regular, FontSlant.Italic)] + [InlineData(null, FontWeight.Bold, FontSlant.Default)] + [InlineData(null, FontWeight.Bold, FontSlant.Italic)] + [InlineData("Lobster Two", FontWeight.Regular, FontSlant.Default)] + [InlineData("Lobster Two", FontWeight.Regular, FontSlant.Italic)] + [InlineData("Lobster Two", FontWeight.Bold, FontSlant.Default)] + [InlineData("Lobster Two", FontWeight.Bold, FontSlant.Italic)] +#if !__IOS__ + // iOS cannot force a font to be bold like all other OS + [InlineData("Dokdo", FontWeight.Regular, FontSlant.Default)] + [InlineData("Dokdo", FontWeight.Regular, FontSlant.Italic)] + [InlineData("Dokdo", FontWeight.Bold, FontSlant.Default)] + [InlineData("Dokdo", FontWeight.Bold, FontSlant.Italic)] +#endif +#if __ANDROID__ + // "monospace" is a special font name on Android + [InlineData("monospace", FontWeight.Regular, FontSlant.Default)] + [InlineData("monospace", FontWeight.Regular, FontSlant.Italic)] + [InlineData("monospace", FontWeight.Bold, FontSlant.Default)] + [InlineData("monospace", FontWeight.Bold, FontSlant.Italic)] +#endif + public async Task FontFamilyAndAttributesInitializesCorrectly(string family, FontWeight weight, FontSlant slant) + { + var label = new LabelStub + { + Text = "Test", + Font = Font.OfSize(family, 30, weight, slant) + }; + + var (isBold, isItalic) = await GetValueAsync(label, (handler) => + { + var isBold = GetNativeIsBold(handler); + var isItalic = GetNativeIsItalic(handler); + + return (isBold, isItalic); + }); + + Assert.Equal(weight == FontWeight.Bold, isBold); + Assert.Equal(slant == FontSlant.Italic, isItalic); + } + [Theory(DisplayName = "Updating Text Does Not Affect HorizontalTextAlignment")] [InlineData("Short", "Longer Text")] [InlineData("Long thext here", "Short")] diff --git a/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.Android.cs b/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.Android.cs index ac2bfd046837..e02800ec2e5a 100644 --- a/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.Android.cs +++ b/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.Android.cs @@ -81,7 +81,7 @@ double GetNativeUnscaledFontSize(PickerHandler pickerHandler) } bool GetNativeIsBold(PickerHandler pickerHandler) => - GetNativePicker(pickerHandler).Typeface.IsBold; + GetNativePicker(pickerHandler).Typeface.GetFontWeight() == FontWeight.Bold; bool GetNativeIsItalic(PickerHandler pickerHandler) => GetNativePicker(pickerHandler).Typeface.IsItalic; diff --git a/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.cs b/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.cs index 08d1534ec354..319834b8d8ed 100644 --- a/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.cs +++ b/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.cs @@ -58,8 +58,8 @@ public async Task FontAttributesInitializeCorrectly(FontWeight weight, bool isBo picker.ItemsSource = items; picker.SelectedIndex = 0; - await ValidatePropertyInitValue(picker, () => picker.Font.Weight.HasFlag(FontWeight.Bold), GetNativeIsBold, isBold); - await ValidatePropertyInitValue(picker, () => picker.Font.FontSlant.HasFlag(FontSlant.Italic), GetNativeIsItalic, isItalic); + await ValidatePropertyInitValue(picker, () => picker.Font.Weight == FontWeight.Bold, GetNativeIsBold, isBold); + await ValidatePropertyInitValue(picker, () => picker.Font.FontSlant == FontSlant.Italic, GetNativeIsItalic, isItalic); } [Theory(DisplayName = "Updating Font Does Not Affect HorizontalTextAlignment")] diff --git a/src/Core/tests/DeviceTests/Handlers/SearchBar/SearchBarHandlerTests.Android.cs b/src/Core/tests/DeviceTests/Handlers/SearchBar/SearchBarHandlerTests.Android.cs index 495de4309a4f..4246040f0865 100644 --- a/src/Core/tests/DeviceTests/Handlers/SearchBar/SearchBarHandlerTests.Android.cs +++ b/src/Core/tests/DeviceTests/Handlers/SearchBar/SearchBarHandlerTests.Android.cs @@ -110,7 +110,7 @@ bool GetNativeIsBold(SearchBarHandler searchBarHandler) if (editText == null) return false; - return editText.Typeface.IsBold; + return editText.Typeface.GetFontWeight() == FontWeight.Bold; } bool GetNativeIsItalic(SearchBarHandler searchBarHandler) diff --git a/src/Core/tests/DeviceTests/Handlers/SearchBar/SearchBarHandlerTests.cs b/src/Core/tests/DeviceTests/Handlers/SearchBar/SearchBarHandlerTests.cs index 74aa72507e73..b5a19037b396 100644 --- a/src/Core/tests/DeviceTests/Handlers/SearchBar/SearchBarHandlerTests.cs +++ b/src/Core/tests/DeviceTests/Handlers/SearchBar/SearchBarHandlerTests.cs @@ -82,8 +82,8 @@ public async Task FontAttributesInitializeCorrectly(FontWeight weight, bool isBo Font = Font.OfSize("Arial", 10, weight, isItalic ? FontSlant.Italic : FontSlant.Default), }; - await ValidatePropertyInitValue(searchBar, () => searchBar.Font.Weight.HasFlag(FontWeight.Bold), GetNativeIsBold, isBold); - await ValidatePropertyInitValue(searchBar, () => searchBar.Font.FontSlant.HasFlag(FontSlant.Italic), GetNativeIsItalic, isItalic); + await ValidatePropertyInitValue(searchBar, () => searchBar.Font.Weight == FontWeight.Bold, GetNativeIsBold, isBold); + await ValidatePropertyInitValue(searchBar, () => searchBar.Font.FontSlant == FontSlant.Italic, GetNativeIsItalic, isItalic); } } } \ No newline at end of file diff --git a/src/Core/tests/DeviceTests/Handlers/TimePicker/TimePickerHandlerTests.Android.cs b/src/Core/tests/DeviceTests/Handlers/TimePicker/TimePickerHandlerTests.Android.cs index 803f9f36e778..e20a5b43fedc 100644 --- a/src/Core/tests/DeviceTests/Handlers/TimePicker/TimePickerHandlerTests.Android.cs +++ b/src/Core/tests/DeviceTests/Handlers/TimePicker/TimePickerHandlerTests.Android.cs @@ -98,7 +98,7 @@ double GetNativeUnscaledFontSize(TimePickerHandler timePickerHandler) } bool GetNativeIsBold(TimePickerHandler timePickerHandler) => - GetNativeTimePicker(timePickerHandler).Typeface.IsBold; + GetNativeTimePicker(timePickerHandler).Typeface.GetFontWeight() == FontWeight.Bold; bool GetNativeIsItalic(TimePickerHandler timePickerHandler) => GetNativeTimePicker(timePickerHandler).Typeface.IsItalic; diff --git a/src/Core/tests/DeviceTests/Handlers/TimePicker/TimePickerHandlerTests.cs b/src/Core/tests/DeviceTests/Handlers/TimePicker/TimePickerHandlerTests.cs index 462c4d97cb52..a8c5545de9f9 100644 --- a/src/Core/tests/DeviceTests/Handlers/TimePicker/TimePickerHandlerTests.cs +++ b/src/Core/tests/DeviceTests/Handlers/TimePicker/TimePickerHandlerTests.cs @@ -48,8 +48,8 @@ public async Task FontAttributesInitializeCorrectly(FontWeight weight, bool isBo Font = Font.OfSize("Arial", 10, weight, isItalic ? FontSlant.Italic : FontSlant.Default) }; - await ValidatePropertyInitValue(timePicker, () => timePicker.Font.Weight.HasFlag(FontWeight.Bold), GetNativeIsBold, isBold); - await ValidatePropertyInitValue(timePicker, () => timePicker.Font.FontSlant.HasFlag(FontSlant.Italic), GetNativeIsItalic, isItalic); + await ValidatePropertyInitValue(timePicker, () => timePicker.Font.Weight == FontWeight.Bold, GetNativeIsBold, isBold); + await ValidatePropertyInitValue(timePicker, () => timePicker.Font.FontSlant == FontSlant.Italic, GetNativeIsItalic, isItalic); } } } \ No newline at end of file diff --git a/src/Core/tests/UnitTests/Hosting/HostBuilderFontsTests.cs b/src/Core/tests/UnitTests/Hosting/HostBuilderFontsTests.cs index 4dc53711924a..1ab01e5f13c5 100644 --- a/src/Core/tests/UnitTests/Hosting/HostBuilderFontsTests.cs +++ b/src/Core/tests/UnitTests/Hosting/HostBuilderFontsTests.cs @@ -40,12 +40,14 @@ public void ConfigureFontsRegistersFonts(string filename, string alias) var registrar = host.Services.GetRequiredService(); - Assert.True(registrar.TryGetFont(filename, out var path)); + var path = registrar.GetFont(filename); + Assert.NotNull(path); Assert.StartsWith(root, path); if (alias != null) { - Assert.True(registrar.TryGetFont(alias, out path)); + path = registrar.GetFont(alias); + Assert.NotNull(path); Assert.StartsWith(root, path); }