TextFormat.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. namespace Avalonia.Media.TextFormatting
  5. {
  6. /// <summary>
  7. /// Unique text formatting properties that are used by the <see cref="TextFormatter"/>.
  8. /// </summary>
  9. public readonly struct TextFormat : IEquatable<TextFormat>
  10. {
  11. public TextFormat(Typeface typeface, double fontRenderingEmSize)
  12. {
  13. Typeface = typeface;
  14. FontRenderingEmSize = fontRenderingEmSize;
  15. FontMetrics = new FontMetrics(typeface, fontRenderingEmSize);
  16. }
  17. /// <summary>
  18. /// Gets the typeface.
  19. /// </summary>
  20. /// <value>
  21. /// The typeface.
  22. /// </value>
  23. public Typeface Typeface { get; }
  24. /// <summary>
  25. /// Gets the font rendering em size.
  26. /// </summary>
  27. /// <value>
  28. /// The em rendering size of the font.
  29. /// </value>
  30. public double FontRenderingEmSize { get; }
  31. /// <summary>
  32. /// Gets the font metrics.
  33. /// </summary>
  34. /// <value>
  35. /// The metrics of the font.
  36. /// </value>
  37. public FontMetrics FontMetrics { get; }
  38. public static bool operator ==(TextFormat self, TextFormat other)
  39. {
  40. return self.Equals(other);
  41. }
  42. public static bool operator !=(TextFormat self, TextFormat other)
  43. {
  44. return !(self == other);
  45. }
  46. public bool Equals(TextFormat other)
  47. {
  48. return Typeface.Equals(other.Typeface) && FontRenderingEmSize.Equals(other.FontRenderingEmSize);
  49. }
  50. public override bool Equals(object obj)
  51. {
  52. return obj is TextFormat other && Equals(other);
  53. }
  54. public override int GetHashCode()
  55. {
  56. unchecked
  57. {
  58. var hashCode = (Typeface != null ? Typeface.GetHashCode() : 0);
  59. hashCode = (hashCode * 397) ^ FontRenderingEmSize.GetHashCode();
  60. return hashCode;
  61. }
  62. }
  63. }
  64. }