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;
}
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)
{

22
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);
}
}

Loading…
Cancel
Save