CustomFontManagerImpl.cs 3.9 KB

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