RuneExtensions.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System.Globalization;
  2. using Wcwidth;
  3. namespace Terminal.Gui;
  4. /// <summary>Extends <see cref="System.Text.Rune"/> to support TUI text manipulation.</summary>
  5. public static class RuneExtensions
  6. {
  7. /// <summary>Maximum Unicode code point.</summary>
  8. public static int MaxUnicodeCodePoint = 0x10FFFF;
  9. /// <summary>Reports if the provided array of bytes can be encoded as UTF-8.</summary>
  10. /// <param name="buffer">The byte array to probe.</param>
  11. /// <value><c>true</c> if is valid; otherwise, <c>false</c>.</value>
  12. public static bool CanBeEncodedAsRune (byte [] buffer)
  13. {
  14. string str = Encoding.Unicode.GetString (buffer);
  15. foreach (Rune rune in str.EnumerateRunes ())
  16. {
  17. if (rune == Rune.ReplacementChar)
  18. {
  19. return false;
  20. }
  21. }
  22. return true;
  23. }
  24. /// <summary>Attempts to decode the rune as a surrogate pair to UTF-16.</summary>
  25. /// <remarks>This is a Terminal.Gui extension method to <see cref="System.Text.Rune"/> to support TUI text manipulation.</remarks>
  26. /// <param name="rune">The rune to decode.</param>
  27. /// <param name="chars">The chars if the rune is a surrogate pair. Null otherwise.</param>
  28. /// <returns><see langword="true"/> if the rune is a valid surrogate pair; <see langword="false"/> otherwise.</returns>
  29. public static bool DecodeSurrogatePair (this Rune rune, out char [] chars)
  30. {
  31. if (rune.IsSurrogatePair ())
  32. {
  33. chars = rune.ToString ().ToCharArray ();
  34. return true;
  35. }
  36. chars = null;
  37. return false;
  38. }
  39. /// <summary>Writes into the destination buffer starting at offset the UTF8 encoded version of the rune.</summary>
  40. /// <remarks>This is a Terminal.Gui extension method to <see cref="System.Text.Rune"/> to support TUI text manipulation.</remarks>
  41. /// <param name="rune">The rune to encode.</param>
  42. /// <param name="dest">The destination buffer.</param>
  43. /// <param name="start">Starting offset to look into.</param>
  44. /// <param name="count">Number of bytes valid in the buffer, or -1 to make it the length of the buffer.</param>
  45. /// <returns>he number of bytes written into the destination buffer.</returns>
  46. public static int Encode (this Rune rune, byte [] dest, int start = 0, int count = -1)
  47. {
  48. byte [] bytes = Encoding.UTF8.GetBytes (rune.ToString ());
  49. var length = 0;
  50. for (var i = 0; i < (count == -1 ? bytes.Length : count); i++)
  51. {
  52. if (bytes [i] == 0)
  53. {
  54. break;
  55. }
  56. dest [start + i] = bytes [i];
  57. length++;
  58. }
  59. return length;
  60. }
  61. /// <summary>Attempts to encode (as UTF-16) a surrogate pair.</summary>
  62. /// <param name="highSurrogate">The high surrogate code point.</param>
  63. /// <param name="lowSurrogate">The low surrogate code point.</param>
  64. /// <param name="result">The encoded rune.</param>
  65. /// <returns><see langword="true"/> if the encoding succeeded; <see langword="false"/> otherwise.</returns>
  66. public static bool EncodeSurrogatePair (char highSurrogate, char lowSurrogate, out Rune result)
  67. {
  68. result = default (Rune);
  69. if (char.IsSurrogatePair (highSurrogate, lowSurrogate))
  70. {
  71. result = (Rune)char.ConvertToUtf32 (highSurrogate, lowSurrogate);
  72. return true;
  73. }
  74. return false;
  75. }
  76. /// <summary>Gets the number of columns the rune occupies in the terminal.</summary>
  77. /// <remarks>This is a Terminal.Gui extension method to <see cref="System.Text.Rune"/> to support TUI text manipulation.</remarks>
  78. /// <param name="rune">The rune to measure.</param>
  79. /// <returns>
  80. /// The number of columns required to fit the rune, 0 if the argument is the null character, or -1 if the value is
  81. /// not printable, otherwise the number of columns that the rune occupies.
  82. /// </returns>
  83. public static int GetColumns (this Rune rune) { return UnicodeCalculator.GetWidth (rune); }
  84. /// <summary>Get number of bytes required to encode the rune, based on the provided encoding.</summary>
  85. /// <remarks>This is a Terminal.Gui extension method to <see cref="System.Text.Rune"/> to support TUI text manipulation.</remarks>
  86. /// <param name="rune">The rune to probe.</param>
  87. /// <param name="encoding">The encoding used; the default is UTF8.</param>
  88. /// <returns>The number of bytes required.</returns>
  89. public static int GetEncodingLength (this Rune rune, Encoding encoding = null)
  90. {
  91. encoding ??= Encoding.UTF8;
  92. byte [] bytes = encoding.GetBytes (rune.ToString ().ToCharArray ());
  93. var offset = 0;
  94. if (bytes [^1] == 0)
  95. {
  96. offset++;
  97. }
  98. return bytes.Length - offset;
  99. }
  100. /// <summary>Returns <see langword="true"/> if the rune is a combining character.</summary>
  101. /// <remarks>This is a Terminal.Gui extension method to <see cref="System.Text.Rune"/> to support TUI text manipulation.</remarks>
  102. /// <param name="rune"></param>
  103. /// <returns></returns>
  104. public static bool IsCombiningMark (this Rune rune)
  105. {
  106. UnicodeCategory category = Rune.GetUnicodeCategory (rune);
  107. return Rune.GetUnicodeCategory (rune) == UnicodeCategory.NonSpacingMark
  108. || category == UnicodeCategory.SpacingCombiningMark
  109. || category == UnicodeCategory.EnclosingMark;
  110. }
  111. /// <summary>Reports whether a rune is a surrogate code point.</summary>
  112. /// <remarks>This is a Terminal.Gui extension method to <see cref="System.Text.Rune"/> to support TUI text manipulation.</remarks>
  113. /// <param name="rune">The rune to probe.</param>
  114. /// <returns><see langword="true"/> if the rune is a surrogate code point; <see langword="false"/> otherwise.</returns>
  115. public static bool IsSurrogatePair (this Rune rune) { return char.IsSurrogatePair (rune.ToString (), 0); }
  116. /// <summary>
  117. /// Ensures the rune is not a control character and can be displayed by translating characters below 0x20 to
  118. /// equivalent, printable, Unicode chars.
  119. /// </summary>
  120. /// <remarks>This is a Terminal.Gui extension method to <see cref="System.Text.Rune"/> to support TUI text manipulation.</remarks>
  121. /// <param name="rune"></param>
  122. /// <returns></returns>
  123. public static Rune MakePrintable (this Rune rune) { return Rune.IsControl (rune) ? new Rune (rune.Value + 0x2400) : rune; }
  124. }