Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 4427 - System.InvalidOperationException: Default font family name can't be null or empty #12817

Merged
merged 6 commits into from
Sep 14, 2023
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: 26 additions & 14 deletions src/Avalonia.Base/Media/FontManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,13 @@ public FontManager(IFontManagerImpl platformImpl)
{
PlatformImpl = platformImpl;

var options = AvaloniaLocator.Current.GetService<FontManagerOptions>();
AddFontCollection(new SystemFontCollection(this));

var options = AvaloniaLocator.Current.GetService<FontManagerOptions>();
_fontFallbacks = options?.FontFallbacks;

var defaultFontFamilyName = options?.DefaultFamilyName ?? PlatformImpl.GetDefaultFontFamilyName();

if (string.IsNullOrEmpty(defaultFontFamilyName))
{
throw new InvalidOperationException("Default font family name can't be null or empty.");
}

var defaultFontFamilyName = GetDefaultFontFamilyName(options);
DefaultFontFamily = new FontFamily(defaultFontFamilyName);

AddFontCollection(new SystemFontCollection(this));
}

/// <summary>
Expand Down Expand Up @@ -111,8 +104,8 @@ public bool TryGetGlyphTypeface(Typeface typeface, [NotNullWhen(true)] out IGlyp
var key = compositeKey.Keys[i];

var familyName = fontFamily.FamilyNames[i];
if (TryGetGlyphTypefaceByKeyAndName(typeface, key, familyName, out glyphTypeface) &&

if (TryGetGlyphTypefaceByKeyAndName(typeface, key, familyName, out glyphTypeface) &&
glyphTypeface.FamilyName.Contains(familyName))
{
return true;
Expand Down Expand Up @@ -165,7 +158,7 @@ private bool TryGetGlyphTypefaceByKeyAndName(Typeface typeface, FontFamilyKey ke
source = new Uri(key.BaseUri, source);
}

if (TryGetFontCollection(source, out var fontCollection) &&
if (TryGetFontCollection(source, out var fontCollection) &&
fontCollection.TryGetGlyphTypeface(familyName, typeface.Style, typeface.Weight, typeface.Stretch, out glyphTypeface))
{
if (glyphTypeface.FamilyName.Contains(familyName))
Expand Down Expand Up @@ -270,7 +263,7 @@ public void RemoveFontCollection(Uri key)

private bool TryGetFontCollection(Uri source, [NotNullWhen(true)] out IFontCollection? fontCollection)
{
if(source.Scheme == SystemFontScheme)
if (source.Scheme == SystemFontScheme)
{
source = SystemFontsKey;
}
Expand All @@ -289,5 +282,24 @@ private bool TryGetFontCollection(Uri source, [NotNullWhen(true)] out IFontColle

return fontCollection != null;
}

private string GetDefaultFontFamilyName(FontManagerOptions? options)
{
var defaultFontFamilyName = options?.DefaultFamilyName
?? PlatformImpl.GetDefaultFontFamilyName();

if (string.IsNullOrEmpty(defaultFontFamilyName) && SystemFonts.Count > 0)
{
defaultFontFamilyName = SystemFonts[0].Name;
}

if (string.IsNullOrEmpty(defaultFontFamilyName))
{
throw new InvalidOperationException(
"Default font family name can't be null or empty.");
}

return defaultFontFamilyName;
}
}
}
59 changes: 59 additions & 0 deletions src/Headless/Avalonia.Headless/HeadlessPlatformStubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,65 @@ public virtual bool TryCreateGlyphTypeface(Stream stream, out IGlyphTypeface gly
}
}

internal class HeadlessFontManagerWithMultipleSystemFontsStub : IFontManagerImpl
{
private readonly string[] _installedFontFamilyNames;
private readonly string _defaultFamilyName;

public HeadlessFontManagerWithMultipleSystemFontsStub(
string[] installedFontFamilyNames,
string defaultFamilyName = "Default")
{
_installedFontFamilyNames = installedFontFamilyNames;
_defaultFamilyName = defaultFamilyName;
}

public int TryCreateGlyphTypefaceCount { get; private set; }

public string GetDefaultFontFamilyName()
{
return _defaultFamilyName;
}

string[] IFontManagerImpl.GetInstalledFontFamilyNames(bool checkForUpdates)
{
return _installedFontFamilyNames;
}

public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fontWeight,
FontStretch fontStretch,
CultureInfo? culture, out Typeface fontKey)
{
fontKey = new Typeface(_defaultFamilyName);

return false;
}

public virtual bool TryCreateGlyphTypeface(string familyName, FontStyle style, FontWeight weight,
FontStretch stretch, [NotNullWhen(true)] out IGlyphTypeface? glyphTypeface)
{
glyphTypeface = null;

TryCreateGlyphTypefaceCount++;

if (familyName == "Unknown")
{
return false;
}

glyphTypeface = new HeadlessGlyphTypefaceImpl();

return true;
}

public virtual bool TryCreateGlyphTypeface(Stream stream, out IGlyphTypeface glyphTypeface)
{
glyphTypeface = new HeadlessGlyphTypefaceImpl();

return true;
}
}

internal class HeadlessIconLoaderStub : IPlatformIconLoader
{
private class IconStub : IWindowIconImpl
Expand Down
19 changes: 17 additions & 2 deletions tests/Avalonia.Base.UnitTests/Media/FontManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ public void Should_Create_Single_Instance_Typeface()
}

[Fact]
public void Should_Throw_When_Default_FamilyName_Is_Null()
public void Should_Throw_When_Default_FamilyName_Is_Null_And_Installed_Font_Family_Names_Is_Empty()
{
using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface.With(fontManagerImpl: new HeadlessFontManagerStub(null!))))
using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface
.With(fontManagerImpl: new HeadlessFontManagerWithMultipleSystemFontsStub(
installedFontFamilyNames: new string[] { },
defaultFamilyName: null))))
{
Assert.Throws<InvalidOperationException>(() => FontManager.Current);
}
Expand Down Expand Up @@ -73,5 +76,17 @@ public void Should_Use_FontManagerOptions_FontFallback()
Assert.Equal("MyFont", typeface.FontFamily.Name);
}
}

[Fact]
public void Should_Return_First_Installed_Font_Family_Name_When_Default_Family_Name_Is_Null()
{
using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface
.With(fontManagerImpl: new HeadlessFontManagerWithMultipleSystemFontsStub(
installedFontFamilyNames: new[] { "DejaVu", "Verdana" },
defaultFamilyName: null))))
{
Assert.Equal("DejaVu", FontManager.Current.DefaultFontFamily.Name);
}
}
}
}