From 891fd8dec4208bf856386a47b1ad5cf01c4cf089 Mon Sep 17 00:00:00 2001 From: Benedikt Stebner Date: Tue, 28 Sep 2021 08:44:59 +0200 Subject: [PATCH] Try to load a custom font with all possible weights and styles --- .../Avalonia.Skia/SKTypefaceCollection.cs | 7 +++--- .../Media/SKTypefaceCollectionCacheTests.cs | 22 ++++++++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Skia/Avalonia.Skia/SKTypefaceCollection.cs b/src/Skia/Avalonia.Skia/SKTypefaceCollection.cs index 7c4ff4edc0..21b2959089 100644 --- a/src/Skia/Avalonia.Skia/SKTypefaceCollection.cs +++ b/src/Skia/Avalonia.Skia/SKTypefaceCollection.cs @@ -27,15 +27,16 @@ namespace Avalonia.Skia { return typeface; } + + var initialWeight = (int)key.Weight; var weight = (int)key.Weight; - weight -= weight % 100; // make sure we start at a full weight + weight -= weight % 50; // make sure we start at a full weight for (var i = 0; i < 2; i++) { - // only try 2 font weights in each direction - for (var j = 0; j < 200; j += 100) + for (var j = 0; j < initialWeight; j += 50) { if (weight - j >= 100) { diff --git a/tests/Avalonia.Skia.UnitTests/Media/SKTypefaceCollectionCacheTests.cs b/tests/Avalonia.Skia.UnitTests/Media/SKTypefaceCollectionCacheTests.cs index 68813f28ab..ddf4a36dcd 100644 --- a/tests/Avalonia.Skia.UnitTests/Media/SKTypefaceCollectionCacheTests.cs +++ b/tests/Avalonia.Skia.UnitTests/Media/SKTypefaceCollectionCacheTests.cs @@ -6,18 +6,24 @@ namespace Avalonia.Skia.UnitTests.Media { public class SKTypefaceCollectionCacheTests { - [Fact] - public void Should_Get_Near_Matching_Typeface() + private const string s_notoMono = + "resm:Avalonia.Skia.UnitTests.Assets?assembly=Avalonia.Skia.UnitTests#Noto Mono"; + + [InlineData(s_notoMono, FontWeight.SemiLight, FontStyle.Normal)] + [InlineData(s_notoMono, FontWeight.Bold, FontStyle.Italic)] + [InlineData(s_notoMono, FontWeight.Heavy, FontStyle.Oblique)] + [Theory] + public void Should_Get_Near_Matching_Typeface(string familyName, FontWeight fontWeight, FontStyle fontStyle) { using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface)) { - var notoMono = - new FontFamily("resm:Avalonia.Skia.UnitTests.Assets?assembly=Avalonia.Skia.UnitTests#Noto Mono"); - - var notoMonoCollection = SKTypefaceCollectionCache.GetOrAddTypefaceCollection(notoMono); + var fontFamily = new FontFamily(familyName); + + var typefaceCollection = SKTypefaceCollectionCache.GetOrAddTypefaceCollection(fontFamily); - Assert.Equal("Noto Mono", - notoMonoCollection.Get(new Typeface(notoMono, weight: FontWeight.Bold)).FamilyName); + var actual = typefaceCollection.Get(new Typeface(fontFamily, fontStyle, fontWeight))?.FamilyName; + + Assert.Equal("Noto Mono", actual); } }