EmbeddedFontCollectionTests.cs 5.7 KB

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