CustomFontManagerImpl.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.Collections.Generic;
  2. using System.Globalization;
  3. using System.Linq;
  4. using Avalonia.Media;
  5. using Avalonia.Media.Fonts;
  6. using Avalonia.Platform;
  7. using SkiaSharp;
  8. namespace Avalonia.Skia.UnitTests.Media
  9. {
  10. public class CustomFontManagerImpl : IFontManagerImpl
  11. {
  12. private readonly Typeface[] _customTypefaces;
  13. private readonly string _defaultFamilyName;
  14. private readonly Typeface _defaultTypeface =
  15. new Typeface("resm:Avalonia.Skia.UnitTests.Assets?assembly=Avalonia.Skia.UnitTests#Noto Mono");
  16. private readonly Typeface _arabicTypeface =
  17. new Typeface("resm:Avalonia.Skia.UnitTests.Assets?assembly=Avalonia.Skia.UnitTests#Noto Kufi Arabic");
  18. private readonly Typeface _italicTypeface =
  19. new Typeface("resm:Avalonia.Skia.UnitTests.Assets?assembly=Avalonia.Skia.UnitTests#Noto Sans", FontStyle.Italic);
  20. private readonly Typeface _emojiTypeface =
  21. new Typeface("resm:Avalonia.Skia.UnitTests.Assets?assembly=Avalonia.Skia.UnitTests#Twitter Color Emoji");
  22. public CustomFontManagerImpl()
  23. {
  24. _customTypefaces = new[] { _emojiTypeface, _italicTypeface, _arabicTypeface, _defaultTypeface };
  25. _defaultFamilyName = _defaultTypeface.FontFamily.FamilyNames.PrimaryFamilyName;
  26. }
  27. public string GetDefaultFontFamilyName()
  28. {
  29. return _defaultFamilyName;
  30. }
  31. public IEnumerable<string> GetInstalledFontFamilyNames(bool checkForUpdates = false)
  32. {
  33. return _customTypefaces.Select(x => x.FontFamily.Name);
  34. }
  35. private readonly string[] _bcp47 = { CultureInfo.CurrentCulture.ThreeLetterISOLanguageName, CultureInfo.CurrentCulture.TwoLetterISOLanguageName };
  36. public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch,
  37. FontFamily fontFamily,
  38. CultureInfo culture, out Typeface typeface)
  39. {
  40. foreach (var customTypeface in _customTypefaces)
  41. {
  42. if (customTypeface.GlyphTypeface.GetGlyph((uint)codepoint) == 0)
  43. {
  44. continue;
  45. }
  46. typeface = new Typeface(customTypeface.FontFamily, fontStyle, fontWeight);
  47. return true;
  48. }
  49. var fallback = SKFontManager.Default.MatchCharacter(fontFamily?.Name, (SKFontStyleWeight)fontWeight,
  50. (SKFontStyleWidth)fontStretch, (SKFontStyleSlant)fontStyle, _bcp47, codepoint);
  51. typeface = new Typeface(fallback?.FamilyName ?? _defaultFamilyName, fontStyle, fontWeight);
  52. return true;
  53. }
  54. public IGlyphTypefaceImpl CreateGlyphTypeface(Typeface typeface)
  55. {
  56. SKTypeface skTypeface;
  57. switch (typeface.FontFamily.Name)
  58. {
  59. case "Twitter Color Emoji":
  60. {
  61. var typefaceCollection = SKTypefaceCollectionCache.GetOrAddTypefaceCollection(_emojiTypeface.FontFamily);
  62. skTypeface = typefaceCollection.Get(typeface);
  63. break;
  64. }
  65. case "Noto Sans":
  66. {
  67. var typefaceCollection = SKTypefaceCollectionCache.GetOrAddTypefaceCollection(_italicTypeface.FontFamily);
  68. skTypeface = typefaceCollection.Get(typeface);
  69. break;
  70. }
  71. case FontFamily.DefaultFontFamilyName:
  72. case "Noto Mono":
  73. {
  74. var typefaceCollection = SKTypefaceCollectionCache.GetOrAddTypefaceCollection(_defaultTypeface.FontFamily);
  75. skTypeface = typefaceCollection.Get(_defaultTypeface);
  76. break;
  77. }
  78. default:
  79. {
  80. skTypeface = SKTypeface.FromFamilyName(typeface.FontFamily.Name,
  81. (SKFontStyleWeight)typeface.Weight, SKFontStyleWidth.Normal, (SKFontStyleSlant)typeface.Style);
  82. break;
  83. }
  84. }
  85. return new GlyphTypefaceImpl(skTypeface);
  86. }
  87. }
  88. }