diff --git a/src/Avalonia.Base/Media/Fonts/EmbeddedFontCollection.cs b/src/Avalonia.Base/Media/Fonts/EmbeddedFontCollection.cs index 1d706e9360..9efee47a5c 100644 --- a/src/Avalonia.Base/Media/Fonts/EmbeddedFontCollection.cs +++ b/src/Avalonia.Base/Media/Fonts/EmbeddedFontCollection.cs @@ -71,10 +71,16 @@ namespace Avalonia.Media.Fonts if(matchedKey != key) { + //Create a synthetic glyph typeface. The successfull result will be cached. if (TryCreateSyntheticGlyphTypeface(glyphTypeface, style, weight, stretch, out var syntheticGlyphTypeface)) { glyphTypeface = syntheticGlyphTypeface; } + else + { + //Add the matched glyph typeface to the cache + glyphTypefaces.TryAdd(key, glyphTypeface); + } } return true; diff --git a/tests/Avalonia.RenderTests/Assets/MiSans-Normal.ttf b/tests/Avalonia.RenderTests/Assets/MiSans-Normal.ttf new file mode 100644 index 0000000000..2e1951e853 Binary files /dev/null and b/tests/Avalonia.RenderTests/Assets/MiSans-Normal.ttf differ diff --git a/tests/Avalonia.Skia.UnitTests/Media/EmbeddedFontCollectionTests.cs b/tests/Avalonia.Skia.UnitTests/Media/EmbeddedFontCollectionTests.cs index a1ba9d92f8..75ad5b1a10 100644 --- a/tests/Avalonia.Skia.UnitTests/Media/EmbeddedFontCollectionTests.cs +++ b/tests/Avalonia.Skia.UnitTests/Media/EmbeddedFontCollectionTests.cs @@ -19,6 +19,8 @@ namespace Avalonia.Skia.UnitTests.Media private const string s_manrope = "resm:Avalonia.Skia.UnitTests.Fonts?assembly=Avalonia.Skia.UnitTests#Manrope"; + private const string s_misans = "resm:Avalonia.Skia.UnitTests.Assets?assembly=Avalonia.Skia.UnitTests#MiSans"; + [InlineData(FontWeight.SemiLight, FontStyle.Normal)] [InlineData(FontWeight.Bold, FontStyle.Italic)] @@ -120,6 +122,27 @@ namespace Avalonia.Skia.UnitTests.Media } } + [Fact] + public void Should_Cache_Nearest_Match_For_MiSans() + { + using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface)) + { + var source = new Uri(s_misans, UriKind.Absolute); + + var fontCollection = new TestEmbeddedFontCollection(source, source); + + fontCollection.Initialize(new CustomFontManagerImpl()); + + Assert.True(fontCollection.TryGetGlyphTypeface("MiSans", FontStyle.Normal, FontWeight.Normal, FontStretch.Normal, out var regularGlyphTypeface)); + + Assert.True(fontCollection.TryGetGlyphTypeface("MiSans", FontStyle.Normal, FontWeight.Bold, FontStretch.Normal, out var boldGlyphTypeface)); + + Assert.True(fontCollection.GlyphTypefaceCache.TryGetValue("MiSans", out var glyphTypefaces)); + + Assert.Equal(3, glyphTypefaces.Count); + } + } + private class TestEmbeddedFontCollection : EmbeddedFontCollection { private bool _createSyntheticTypefaces;