EmbeddedFontCollectionTests.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #nullable enable
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using Avalonia.Media;
  7. using Avalonia.Media.Fonts;
  8. using Avalonia.UnitTests;
  9. using Xunit;
  10. namespace Avalonia.Skia.UnitTests.Media
  11. {
  12. public class EmbeddedFontCollectionTests
  13. {
  14. private const string s_notoMono =
  15. "resm:Avalonia.Skia.UnitTests.Assets?assembly=Avalonia.Skia.UnitTests#Noto Mono";
  16. private const string s_manrope = "resm:Avalonia.Skia.UnitTests.Fonts?assembly=Avalonia.Skia.UnitTests#Manrope";
  17. [InlineData(FontWeight.SemiLight, FontStyle.Normal)]
  18. [InlineData(FontWeight.Bold, FontStyle.Italic)]
  19. [InlineData(FontWeight.Heavy, FontStyle.Oblique)]
  20. [Theory]
  21. public void Should_Get_Near_Matching_Typeface(FontWeight fontWeight, FontStyle fontStyle)
  22. {
  23. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  24. {
  25. var source = new Uri(s_notoMono, UriKind.Absolute);
  26. var fontCollection = new EmbeddedFontCollection(source, source);
  27. fontCollection.Initialize(new CustomFontManagerImpl());
  28. Assert.True(fontCollection.TryGetGlyphTypeface("Noto Mono", fontStyle, fontWeight, FontStretch.Normal, out var glyphTypeface));
  29. var actual = glyphTypeface.FamilyName;
  30. Assert.Equal("Noto Mono", actual);
  31. }
  32. }
  33. [Fact]
  34. public void Should_Not_Get_Typeface_For_Invalid_FamilyName()
  35. {
  36. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  37. {
  38. var source = new Uri(s_notoMono, UriKind.Absolute);
  39. var fontCollection = new EmbeddedFontCollection(source, source);
  40. fontCollection.Initialize(new CustomFontManagerImpl());
  41. Assert.False(fontCollection.TryGetGlyphTypeface("ABC", FontStyle.Normal, FontWeight.Normal, FontStretch.Normal, out _));
  42. }
  43. }
  44. [Fact]
  45. public void Should_Get_Typeface_For_Partial_FamilyName()
  46. {
  47. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  48. {
  49. var source = new Uri("resm:Avalonia.Skia.UnitTests.Assets?assembly=Avalonia.Skia.UnitTests#T", UriKind.Absolute);
  50. var fontCollection = new EmbeddedFontCollection(source, source);
  51. fontCollection.Initialize(new CustomFontManagerImpl());
  52. Assert.True(fontCollection.TryGetGlyphTypeface("T", FontStyle.Normal, FontWeight.Normal, FontStretch.Normal, out var glyphTypeface));
  53. Assert.Equal("Twitter Color Emoji", glyphTypeface.FamilyName);
  54. }
  55. }
  56. [Fact]
  57. public void Should_Get_Typeface_For_TypographicFamilyName()
  58. {
  59. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  60. {
  61. var source = new Uri(s_manrope, UriKind.Absolute);
  62. var fontCollection = new EmbeddedFontCollection(source, source);
  63. fontCollection.Initialize(new CustomFontManagerImpl());
  64. Assert.True(fontCollection.TryGetGlyphTypeface("Manrope", FontStyle.Normal, FontWeight.Light, FontStretch.Normal, out var glyphTypeface));
  65. Assert.Equal("Manrope Light", glyphTypeface.FamilyName);
  66. Assert.True(glyphTypeface is IGlyphTypeface2);
  67. var glyphTypeface2 = (IGlyphTypeface2)glyphTypeface;
  68. Assert.Equal("Manrope", glyphTypeface2.TypographicFamilyName);
  69. }
  70. }
  71. [Fact]
  72. public void Should_Cache_Synthetic_GlyphTypeface()
  73. {
  74. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  75. {
  76. var source = new Uri(s_manrope, UriKind.Absolute);
  77. var fontCollection = new TestEmbeddedFontCollection(source, source);
  78. fontCollection.Initialize(new CustomFontManagerImpl());
  79. Assert.True(fontCollection.TryGetGlyphTypeface("Manrope", FontStyle.Normal, FontWeight.ExtraBlack, FontStretch.Normal, out var glyphTypeface));
  80. Assert.True(fontCollection.GlyphTypefaceCache.TryGetValue("Manrope", out var glyphTypefaces));
  81. Assert.Equal(2, glyphTypefaces.Count);
  82. fontCollection.TryGetGlyphTypeface("Manrope", FontStyle.Normal, FontWeight.ExtraBlack, FontStretch.Normal, out var otherGlyphTypeface);
  83. Assert.Equal(glyphTypeface, otherGlyphTypeface);
  84. }
  85. }
  86. private class TestEmbeddedFontCollection : EmbeddedFontCollection
  87. {
  88. public TestEmbeddedFontCollection(Uri key, Uri source) : base(key, source)
  89. {
  90. }
  91. public IDictionary<string, ConcurrentDictionary<FontCollectionKey, IGlyphTypeface?>> GlyphTypefaceCache => _glyphTypefaceCache;
  92. }
  93. }
  94. }