CaretInfo.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. using SkiaSharp;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Text;
  19. namespace Topten.RichTextKit
  20. {
  21. /// <summary>
  22. /// Used to return caret positioning information from the
  23. /// <see cref="TextBlock.GetCaretInfo(CaretPosition)"/> method.
  24. /// </summary>
  25. public struct CaretInfo
  26. {
  27. /// <summary>
  28. /// Returns the index of the code point that this caret info refers to.
  29. /// </summary>
  30. public int CodePointIndex;
  31. /// <summary>
  32. /// Returns the line number that contains the caret
  33. /// </summary>
  34. public int LineIndex;
  35. /// <summary>
  36. /// The X-coordinate where the caret should be displayed for this code point.
  37. /// </summary>
  38. public float CaretXCoord;
  39. /// <summary>
  40. /// A rectangle describing where the caret should be drawn, relative to the top-left
  41. /// corner of the text block. The caret should be drawn from the returned rectangle's
  42. /// top-right to bottom-left.
  43. /// </summary>
  44. /// <remarks>
  45. /// This will be based on the *previous* character on this line (or the same character
  46. /// if this is first character in the line).
  47. ///
  48. /// Usually this will be a zero-width rectangle describing the x, top and bottom
  49. /// coordinates of where the caret should be drawn. The width of the drawn caret
  50. /// isn't provided and should be determined by the client.
  51. ///
  52. /// When the caret is immediately following an italic character, the returned
  53. /// rectangle will be sloped to the right and should be drawn from the top-right
  54. /// coordinate to the bottom-left coordinate.
  55. ///
  56. /// If you don't want to draw a sloped caret for italics, use the top and bottom
  57. /// coordinates of the returned rectangle and get the x-coordinate from the
  58. /// <see cref="CaretXCoord"/> property.
  59. /// </remarks>
  60. public SKRect CaretRectangle;
  61. /// <summary>
  62. /// Checks if this caret info represents a caret position of none, or not found
  63. /// </summary>
  64. public bool IsNone => CodePointIndex < 0;
  65. /// <summary>
  66. /// Place holder caret info structure for no caret
  67. /// </summary>
  68. public static CaretInfo None = new CaretInfo()
  69. {
  70. CodePointIndex = -1,
  71. };
  72. /*
  73. * Commented out as untested.
  74. *
  75. *
  76. /// <summary>
  77. /// The base line of this cluster, relative to the top of the text block
  78. /// </summary>
  79. public float ClusterBaseLine => FontRun.Line.YPosition + FontRun.Line.BaseLine;
  80. /// <summary>
  81. /// The cluster's ascent
  82. /// </summary>
  83. public float ClusterAscent => FontRun.Ascent;
  84. /// <summary>
  85. /// The cluster's descent
  86. /// </summary>
  87. public float ClusterDescent => FontRun.Descent;
  88. /// <summary>
  89. /// Get the left x-coord of this cluster
  90. /// </summary>
  91. public float ClusterLeftXCoord
  92. {
  93. get
  94. {
  95. return FontRun.GetCodePointXCoord(Direction == TextDirection.LTR ? CodePointIndex : NextCodePointIndex);
  96. }
  97. }
  98. /// <summary>
  99. /// Get the right x-coord of this cluster
  100. /// </summary>
  101. public float ClusterRightXCoord
  102. {
  103. get
  104. {
  105. return FontRun.GetCodePointXCoord(Direction == TextDirection.RTL ? CodePointIndex : NextCodePointIndex);
  106. }
  107. }
  108. /// <summary>
  109. /// The code point index of the next cluster.
  110. /// </summary>
  111. /// <remarks>
  112. /// If the code point index refers to the last code point in the
  113. /// text block then this property returns the current code point index.
  114. /// </remarks>
  115. public int NextCodePointIndex;
  116. /// <summary>
  117. /// The code point index of the previous cluster.
  118. /// </summary>
  119. /// <remarks>
  120. /// If the code point index refers to the first code point in the
  121. /// text block then this property returns 0.
  122. /// </remarks>
  123. public int PreviousCodePointIndex;
  124. /// <summary>
  125. /// The number of code points in this cluster.
  126. /// </summary>
  127. public int CodePointCount => NextCodePointIndex - CodePointIndex;
  128. /// <summary>
  129. /// The font run that contains the code point.
  130. /// </summary>
  131. public FontRun FontRun;
  132. /// <summary>
  133. /// The style run that contains the code point.
  134. /// </summary>
  135. public StyleRun StyleRun;
  136. */
  137. }
  138. }