Browse Source

Try to load a custom font with all possible weights and styles

pull/6651/head
Benedikt Stebner 5 years ago
parent
commit
891fd8dec4
  1. 7
      src/Skia/Avalonia.Skia/SKTypefaceCollection.cs
  2. 22
      tests/Avalonia.Skia.UnitTests/Media/SKTypefaceCollectionCacheTests.cs

7
src/Skia/Avalonia.Skia/SKTypefaceCollection.cs

@ -27,15 +27,16 @@ namespace Avalonia.Skia
{ {
return typeface; return typeface;
} }
var initialWeight = (int)key.Weight;
var weight = (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++) for (var i = 0; i < 2; i++)
{ {
// only try 2 font weights in each direction for (var j = 0; j < initialWeight; j += 50)
for (var j = 0; j < 200; j += 100)
{ {
if (weight - j >= 100) if (weight - j >= 100)
{ {

22
tests/Avalonia.Skia.UnitTests/Media/SKTypefaceCollectionCacheTests.cs

@ -6,18 +6,24 @@ namespace Avalonia.Skia.UnitTests.Media
{ {
public class SKTypefaceCollectionCacheTests public class SKTypefaceCollectionCacheTests
{ {
[Fact] private const string s_notoMono =
public void Should_Get_Near_Matching_Typeface() "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)) using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
{ {
var notoMono = var fontFamily = new FontFamily(familyName);
new FontFamily("resm:Avalonia.Skia.UnitTests.Assets?assembly=Avalonia.Skia.UnitTests#Noto Mono");
var typefaceCollection = SKTypefaceCollectionCache.GetOrAddTypefaceCollection(fontFamily);
var notoMonoCollection = SKTypefaceCollectionCache.GetOrAddTypefaceCollection(notoMono);
Assert.Equal("Noto Mono", var actual = typefaceCollection.Get(new Typeface(fontFamily, fontStyle, fontWeight))?.FamilyName;
notoMonoCollection.Get(new Typeface(notoMono, weight: FontWeight.Bold)).FamilyName);
Assert.Equal("Noto Mono", actual);
} }
} }

Loading…
Cancel
Save