HarfBuzzFontManagerImpl.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using Avalonia.Media;
  7. using Avalonia.Platform;
  8. namespace Avalonia.UnitTests
  9. {
  10. public class HarfBuzzFontManagerImpl : IFontManagerImpl
  11. {
  12. private readonly Typeface[] _customTypefaces;
  13. private readonly string _defaultFamilyName;
  14. private static readonly Typeface _defaultTypeface =
  15. new Typeface(new FontFamily("resm:Avalonia.UnitTests.Assets?assembly=Avalonia.UnitTests#Noto Mono"));
  16. private static readonly Typeface _italicTypeface =
  17. new Typeface(new FontFamily("resm:Avalonia.UnitTests.Assets?assembly=Avalonia.UnitTests#Noto Sans"));
  18. private static readonly Typeface _emojiTypeface =
  19. new Typeface(new FontFamily("resm:Avalonia.UnitTests.Assets?assembly=Avalonia.UnitTests#Twitter Color Emoji"));
  20. public HarfBuzzFontManagerImpl(string defaultFamilyName = "Noto Mono")
  21. {
  22. _customTypefaces = new[] { _emojiTypeface, _italicTypeface, _defaultTypeface };
  23. _defaultFamilyName = defaultFamilyName;
  24. }
  25. public string GetDefaultFontFamilyName()
  26. {
  27. return _defaultFamilyName;
  28. }
  29. string[] IFontManagerImpl.GetInstalledFontFamilyNames(bool checkForUpdates)
  30. {
  31. return _customTypefaces.Select(x => x.FontFamily!.Name).ToArray();
  32. }
  33. public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fontWeight,
  34. FontStretch fontStretch, CultureInfo culture, out Typeface fontKey)
  35. {
  36. foreach (var customTypeface in _customTypefaces)
  37. {
  38. var glyphTypeface = customTypeface.GlyphTypeface;
  39. if (!glyphTypeface.TryGetGlyph((uint)codepoint, out _))
  40. {
  41. continue;
  42. }
  43. fontKey = customTypeface;
  44. return true;
  45. }
  46. fontKey = default;
  47. return false;
  48. }
  49. public bool TryCreateGlyphTypeface(Stream stream, FontSimulations fontSimulations, out IGlyphTypeface glyphTypeface)
  50. {
  51. glyphTypeface = new HarfBuzzGlyphTypefaceImpl(stream);
  52. return true;
  53. }
  54. public bool TryCreateGlyphTypeface(string familyName, FontStyle style, FontWeight weight,
  55. FontStretch stretch, [NotNullWhen(true)] out IGlyphTypeface glyphTypeface)
  56. {
  57. glyphTypeface = null;
  58. return false;
  59. }
  60. }
  61. }