Typeface.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Diagnostics;
  3. namespace Avalonia.Media
  4. {
  5. /// <summary>
  6. /// Represents a typeface.
  7. /// </summary>
  8. [DebuggerDisplay("Name = {FontFamily.Name}, Weight = {Weight}, Style = {Style}")]
  9. public readonly struct Typeface : IEquatable<Typeface>
  10. {
  11. /// <summary>
  12. /// Initializes a new instance of the <see cref="Typeface"/> class.
  13. /// </summary>
  14. /// <param name="fontFamily">The font family.</param>
  15. /// <param name="style">The font style.</param>
  16. /// <param name="weight">The font weight.</param>
  17. /// <param name="stretch">The font stretch.</param>
  18. public Typeface(FontFamily fontFamily,
  19. FontStyle style = FontStyle.Normal,
  20. FontWeight weight = FontWeight.Normal,
  21. FontStretch stretch = FontStretch.Normal)
  22. {
  23. if (weight <= 0)
  24. {
  25. throw new ArgumentException("Font weight must be > 0.");
  26. }
  27. if ((int)stretch < 1)
  28. {
  29. throw new ArgumentException("Font stretch must be > 1.");
  30. }
  31. FontFamily = fontFamily;
  32. Style = style;
  33. Weight = weight;
  34. Stretch = stretch;
  35. }
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="Typeface"/> class.
  38. /// </summary>
  39. /// <param name="fontFamilyName">The name of the font family.</param>
  40. /// <param name="style">The font style.</param>
  41. /// <param name="weight">The font weight.</param>
  42. /// <param name="stretch">The font stretch.</param>
  43. public Typeface(string fontFamilyName,
  44. FontStyle style = FontStyle.Normal,
  45. FontWeight weight = FontWeight.Normal,
  46. FontStretch stretch = FontStretch.Normal)
  47. : this(new FontFamily(fontFamilyName), style, weight, stretch)
  48. {
  49. }
  50. public static Typeface Default { get; } = new Typeface(FontFamily.Default);
  51. /// <summary>
  52. /// Gets the font family.
  53. /// </summary>
  54. public FontFamily FontFamily { get; }
  55. /// <summary>
  56. /// Gets the font style.
  57. /// </summary>
  58. public FontStyle Style { get; }
  59. /// <summary>
  60. /// Gets the font weight.
  61. /// </summary>
  62. public FontWeight Weight { get; }
  63. /// <summary>
  64. /// Gets the font stretch.
  65. /// </summary>
  66. public FontStretch Stretch { get; }
  67. /// <summary>
  68. /// Gets the glyph typeface.
  69. /// </summary>
  70. /// <value>
  71. /// The glyph typeface.
  72. /// </value>
  73. public IGlyphTypeface GlyphTypeface
  74. {
  75. get
  76. {
  77. if(FontManager.Current.TryGetGlyphTypeface(this, out var glyphTypeface))
  78. {
  79. return glyphTypeface;
  80. }
  81. throw new InvalidOperationException("Could not create glyphTypeface.");
  82. }
  83. }
  84. public static bool operator !=(Typeface a, Typeface b)
  85. {
  86. return !(a == b);
  87. }
  88. public static bool operator ==(Typeface a, Typeface b)
  89. {
  90. return a.Equals(b);
  91. }
  92. public override bool Equals(object? obj)
  93. {
  94. return obj is Typeface typeface && Equals(typeface);
  95. }
  96. public bool Equals(Typeface other)
  97. {
  98. return FontFamily == other.FontFamily && Style == other.Style &&
  99. Weight == other.Weight && Stretch == other.Stretch;
  100. }
  101. public override int GetHashCode()
  102. {
  103. unchecked
  104. {
  105. var hashCode = (FontFamily != null ? FontFamily.GetHashCode() : 0);
  106. hashCode = (hashCode * 397) ^ (int)Style;
  107. hashCode = (hashCode * 397) ^ (int)Weight;
  108. hashCode = (hashCode * 397) ^ (int)Stretch;
  109. return hashCode;
  110. }
  111. }
  112. }
  113. }