SimpleTextLine.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Platform;
  4. namespace Avalonia.Media.TextFormatting
  5. {
  6. internal class SimpleTextLine : TextLine
  7. {
  8. private readonly IReadOnlyList<ShapedTextRun> _textRuns;
  9. public SimpleTextLine(TextPointer textPointer, IReadOnlyList<ShapedTextRun> textRuns, TextLineMetrics lineMetrics)
  10. {
  11. Text = textPointer;
  12. _textRuns = textRuns;
  13. LineMetrics = lineMetrics;
  14. }
  15. /// <inheritdoc/>
  16. public override TextPointer Text { get; }
  17. /// <inheritdoc/>
  18. public override IReadOnlyList<TextRun> TextRuns => _textRuns;
  19. /// <inheritdoc/>
  20. public override TextLineMetrics LineMetrics { get; }
  21. /// <inheritdoc/>
  22. public override void Draw(IDrawingContextImpl drawingContext, Point origin)
  23. {
  24. var currentX = origin.X;
  25. foreach (var textRun in _textRuns)
  26. {
  27. var baselineOrigin = new Point(currentX + LineMetrics.BaselineOrigin.X,
  28. origin.Y + LineMetrics.BaselineOrigin.Y);
  29. textRun.Draw(drawingContext, baselineOrigin);
  30. currentX += textRun.Bounds.Width;
  31. }
  32. }
  33. /// <inheritdoc/>
  34. public override CharacterHit GetCharacterHitFromDistance(double distance)
  35. {
  36. if (distance < 0)
  37. {
  38. // hit happens before the line, return the first position
  39. return new CharacterHit(Text.Start);
  40. }
  41. // process hit that happens within the line
  42. var characterHit = new CharacterHit();
  43. foreach (var run in _textRuns)
  44. {
  45. characterHit = run.GlyphRun.GetCharacterHitFromDistance(distance, out _);
  46. if (distance <= run.Bounds.Width)
  47. {
  48. break;
  49. }
  50. distance -= run.Bounds.Width;
  51. }
  52. return characterHit;
  53. }
  54. /// <inheritdoc/>
  55. public override double GetDistanceFromCharacterHit(CharacterHit characterHit)
  56. {
  57. return DistanceFromCodepointIndex(characterHit.FirstCharacterIndex + (characterHit.TrailingLength != 0 ? 1 : 0));
  58. }
  59. /// <inheritdoc/>
  60. public override CharacterHit GetNextCaretCharacterHit(CharacterHit characterHit)
  61. {
  62. int nextVisibleCp;
  63. bool navigableCpFound;
  64. if (characterHit.TrailingLength == 0)
  65. {
  66. navigableCpFound = FindNextCodepointIndex(characterHit.FirstCharacterIndex, out nextVisibleCp);
  67. if (navigableCpFound)
  68. {
  69. // Move from leading to trailing edge
  70. return new CharacterHit(nextVisibleCp, 1);
  71. }
  72. }
  73. navigableCpFound = FindNextCodepointIndex(characterHit.FirstCharacterIndex + 1, out nextVisibleCp);
  74. if (navigableCpFound)
  75. {
  76. // Move from trailing edge of current character to trailing edge of next
  77. return new CharacterHit(nextVisibleCp, 1);
  78. }
  79. // Can't move, we're after the last character
  80. return characterHit;
  81. }
  82. /// <inheritdoc/>
  83. public override CharacterHit GetPreviousCaretCharacterHit(CharacterHit characterHit)
  84. {
  85. int previousCodepointIndex;
  86. bool codepointIndexFound;
  87. var cpHit = characterHit.FirstCharacterIndex;
  88. var trailingHit = characterHit.TrailingLength != 0;
  89. // Input can be right after the end of the current line. Snap it to be at the end of the line.
  90. if (cpHit >= Text.Start + Text.Length)
  91. {
  92. cpHit = Text.Start + Text.Length - 1;
  93. trailingHit = true;
  94. }
  95. if (trailingHit)
  96. {
  97. codepointIndexFound = FindPreviousCodepointIndex(cpHit, out previousCodepointIndex);
  98. if (codepointIndexFound)
  99. {
  100. // Move from trailing to leading edge
  101. return new CharacterHit(previousCodepointIndex, 0);
  102. }
  103. }
  104. codepointIndexFound = FindPreviousCodepointIndex(cpHit - 1, out previousCodepointIndex);
  105. if (codepointIndexFound)
  106. {
  107. // Move from leading edge of current character to leading edge of previous
  108. return new CharacterHit(previousCodepointIndex, 0);
  109. }
  110. // Can't move, we're before the first character
  111. return characterHit;
  112. }
  113. /// <inheritdoc/>
  114. public override CharacterHit GetBackspaceCaretCharacterHit(CharacterHit characterHit)
  115. {
  116. // same operation as move-to-previous
  117. return GetPreviousCaretCharacterHit(characterHit);
  118. }
  119. /// <summary>
  120. /// Get distance from line start to the specified codepoint index
  121. /// </summary>
  122. private double DistanceFromCodepointIndex(int codepointIndex)
  123. {
  124. var currentDistance = 0.0;
  125. foreach (var textRun in _textRuns)
  126. {
  127. if (codepointIndex > textRun.Text.End)
  128. {
  129. currentDistance += textRun.Bounds.Width;
  130. continue;
  131. }
  132. return currentDistance + textRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(codepointIndex));
  133. }
  134. return currentDistance;
  135. }
  136. /// <summary>
  137. /// Search forward from the given codepoint index (inclusive) to find the next navigable codepoint index.
  138. /// Return true if one such codepoint index is found, false otherwise.
  139. /// </summary>
  140. private bool FindNextCodepointIndex(int codepointIndex, out int nextCodepointIndex)
  141. {
  142. nextCodepointIndex = codepointIndex;
  143. if (codepointIndex >= Text.Start + Text.Length)
  144. {
  145. return false; // Cannot go forward anymore
  146. }
  147. GetRunIndexAtCodepointIndex(codepointIndex, out var runIndex, out var cpRunStart);
  148. while (runIndex < TextRuns.Count)
  149. {
  150. // When navigating forward, only the trailing edge of visible content is
  151. // navigable.
  152. if (runIndex < TextRuns.Count)
  153. {
  154. nextCodepointIndex = Math.Max(cpRunStart, codepointIndex);
  155. return true;
  156. }
  157. cpRunStart += TextRuns[runIndex++].Text.Length;
  158. }
  159. return false;
  160. }
  161. /// <summary>
  162. /// Search backward from the given codepoint index (inclusive) to find the previous navigable codepoint index.
  163. /// Return true if one such codepoint is found, false otherwise.
  164. /// </summary>
  165. private bool FindPreviousCodepointIndex(int codepointIndex, out int previousCodepointIndex)
  166. {
  167. previousCodepointIndex = codepointIndex;
  168. if (codepointIndex < Text.Start)
  169. {
  170. return false; // Cannot go backward anymore.
  171. }
  172. // Position the cpRunEnd at the end of the span that contains the given cp
  173. GetRunIndexAtCodepointIndex(codepointIndex, out var runIndex, out var codepointIndexAtRunEnd);
  174. codepointIndexAtRunEnd += TextRuns[runIndex].Text.End;
  175. while (runIndex >= 0)
  176. {
  177. // Visible content has caret stops at its leading edge.
  178. if (runIndex + 1 < TextRuns.Count)
  179. {
  180. previousCodepointIndex = Math.Min(codepointIndexAtRunEnd, codepointIndex);
  181. return true;
  182. }
  183. // Newline sequence has caret stops at its leading edge.
  184. if (runIndex == TextRuns.Count)
  185. {
  186. // Get the cp index at the beginning of the newline sequence.
  187. previousCodepointIndex = codepointIndexAtRunEnd - TextRuns[runIndex].Text.Length + 1;
  188. return true;
  189. }
  190. codepointIndexAtRunEnd -= TextRuns[runIndex--].Text.Length;
  191. }
  192. return false;
  193. }
  194. private void GetRunIndexAtCodepointIndex(int codepointIndex, out int runIndex, out int codepointIndexAtRunStart)
  195. {
  196. codepointIndexAtRunStart = Text.Start;
  197. runIndex = 0;
  198. // Find the span that contains the given cp
  199. while (runIndex < TextRuns.Count &&
  200. codepointIndexAtRunStart + TextRuns[runIndex].Text.Length <= codepointIndex)
  201. {
  202. codepointIndexAtRunStart += TextRuns[runIndex++].Text.Length;
  203. }
  204. }
  205. }
  206. }