CustomFontManagerImpl.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections.Generic;
  2. using System.Globalization;
  3. using System.Linq;
  4. using Avalonia.Media;
  5. using Avalonia.Platform;
  6. using Avalonia.Skia;
  7. using SkiaSharp;
  8. namespace Avalonia.Web.Blazor
  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("avares://Avalonia.Web.Blazor/Assets#Noto Mono");
  16. private readonly Typeface _italicTypeface =
  17. new Typeface("avares://Avalonia.Web.Blazor/Assets#Noto Sans");
  18. private readonly Typeface _emojiTypeface =
  19. new Typeface("avares://Avalonia.Web.Blazor/Assets#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, fontStyle, fontWeight);
  44. return true;
  45. }
  46. typeface = _defaultTypeface;
  47. return true;
  48. }
  49. public IGlyphTypefaceImpl CreateGlyphTypeface(Typeface typeface)
  50. {
  51. SKTypeface skTypeface;
  52. switch (typeface.FontFamily.Name)
  53. {
  54. case "Twitter Color Emoji":
  55. {
  56. var typefaceCollection = SKTypefaceCollectionCache.GetOrAddTypefaceCollection(_emojiTypeface.FontFamily);
  57. skTypeface = typefaceCollection.Get(typeface);
  58. break;
  59. }
  60. case "Noto Sans":
  61. {
  62. var typefaceCollection = SKTypefaceCollectionCache.GetOrAddTypefaceCollection(_italicTypeface.FontFamily);
  63. skTypeface = typefaceCollection.Get(typeface);
  64. break;
  65. }
  66. default:
  67. {
  68. var typefaceCollection = SKTypefaceCollectionCache.GetOrAddTypefaceCollection(_defaultTypeface.FontFamily);
  69. skTypeface = typefaceCollection.Get(_defaultTypeface);
  70. break;
  71. }
  72. }
  73. return new GlyphTypefaceImpl(skTypeface);
  74. }
  75. }
  76. }