IStyleExtensions.cs 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // RichTextKit
  2. // Copyright © 2019-2020 Topten Software. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. // not use this product except in compliance with the License. You may obtain
  6. // a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. // License for the specific language governing permissions and limitations
  14. // under the License.
  15. namespace Topten.RichTextKit
  16. {
  17. /// <summary>
  18. /// Extension methods for working with IStyle
  19. /// </summary>
  20. public static class IStyleExtensions
  21. {
  22. /// <summary>
  23. /// Generates a string key that uniquely identifies the formatting characteristics
  24. /// of this style.
  25. /// </summary>
  26. /// <remarks>
  27. /// Two styles with the same Key will rendering identically even if different instances.
  28. /// </remarks>
  29. /// <param name="This">The style instance to generate the key for</param>
  30. /// <returns>A key string</returns>
  31. public static string Key(this IStyle This)
  32. {
  33. return $"{This.FontFamily}.{This.FontSize}.{This.FontWeight}.{This.FontWidth}.{This.FontItalic}.{This.Underline}.{This.StrikeThrough}.{This.LineHeight}.{This.TextColor}.{This.BackgroundColor}.{This.LetterSpacing}.{This.FontVariant}.{This.TextDirection}.{This.ReplacementCharacter}";
  34. }
  35. /// <summary>
  36. /// Compares this style to another and returns true if both will have the same
  37. /// layout, but not necessarily the same appearance (eg: color change, underline etc...)
  38. /// </summary>
  39. /// <param name="This">The style instance</param>
  40. /// <param name="other">The other style instance to compare to</param>
  41. /// <returns>True if both styles will give the same layout</returns>
  42. public static bool HasSameLayout(this IStyle This, IStyle other)
  43. {
  44. if (This.FontFamily != other.FontFamily)
  45. return false;
  46. if (This.FontSize != other.FontSize)
  47. return false;
  48. if (This.FontWeight != other.FontWeight)
  49. return false;
  50. if (This.FontWidth != other.FontWidth)
  51. return false;
  52. if (This.FontItalic != other.FontItalic)
  53. return false;
  54. if (This.LineHeight != other.LineHeight)
  55. return false;
  56. if (This.TextDirection != other.TextDirection)
  57. return false;
  58. if (This.LetterSpacing != other.LetterSpacing)
  59. return false;
  60. if (This.FontVariant != other.FontVariant)
  61. return false;
  62. if (This.ReplacementCharacter != other.ReplacementCharacter)
  63. return false;
  64. return true;
  65. }
  66. /// <summary>
  67. /// Compares this style to another and returns true if both will have the same
  68. /// layout, but not necessarily the same appearance (eg: color change, underline etc...)
  69. /// </summary>
  70. /// <param name="This">The style instance</param>
  71. /// <param name="other">The other style instance to compare to</param>
  72. /// <returns>True if both styles will give the same layout</returns>
  73. public static bool IsSame(this IStyle This, IStyle other)
  74. {
  75. if (This == null && other == null)
  76. return true;
  77. if (This == null || other == null)
  78. return false;
  79. if (!This.HasSameLayout(other))
  80. return false;
  81. if (This.TextColor != other.TextColor)
  82. return false;
  83. if (This.BackgroundColor != other.BackgroundColor)
  84. return false;
  85. if (This.Underline != other.Underline)
  86. return false;
  87. if (This.StrikeThrough != other.StrikeThrough)
  88. return false;
  89. return true;
  90. }
  91. }
  92. }