TextLine.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections.Generic;
  2. namespace Avalonia.Media.TextFormatting
  3. {
  4. /// <summary>
  5. /// Represents a line of text that is used for text rendering.
  6. /// </summary>
  7. public abstract class TextLine
  8. {
  9. /// <summary>
  10. /// Gets the text range that is covered by the line.
  11. /// </summary>
  12. /// <value>
  13. /// The text range that is covered by the line.
  14. /// </value>
  15. public abstract TextRange TextRange { get; }
  16. /// <summary>
  17. /// Gets the text runs.
  18. /// </summary>
  19. /// <value>
  20. /// The text runs.
  21. /// </value>
  22. public abstract IReadOnlyList<TextRun> TextRuns { get; }
  23. /// <summary>
  24. /// Gets the line metrics.
  25. /// </summary>
  26. /// <value>
  27. /// The line metrics.
  28. /// </value>
  29. public abstract TextLineMetrics LineMetrics { get; }
  30. /// <summary>
  31. /// Gets the state of the line when broken by line breaking process.
  32. /// </summary>
  33. /// <returns>
  34. /// A <see cref="LineBreak"/> value that represents the line break.
  35. /// </returns>
  36. public abstract TextLineBreak LineBreak { get; }
  37. /// <summary>
  38. /// Gets a value that indicates whether the line is collapsed.
  39. /// </summary>
  40. /// <returns>
  41. /// <c>true</c>, if the line is collapsed; otherwise, <c>false</c>.
  42. /// </returns>
  43. public abstract bool HasCollapsed { get; }
  44. /// <summary>
  45. /// Draws the <see cref="TextLine"/> at the given origin.
  46. /// </summary>
  47. /// <param name="drawingContext">The drawing context.</param>
  48. /// <param name="origin">The origin.</param>
  49. public abstract void Draw(DrawingContext drawingContext, Point origin);
  50. /// <summary>
  51. /// Create a collapsed line based on collapsed text properties.
  52. /// </summary>
  53. /// <param name="collapsingPropertiesList">A list of <see cref="TextCollapsingProperties"/>
  54. /// objects that represent the collapsed text properties.</param>
  55. /// <returns>
  56. /// A <see cref="TextLine"/> value that represents a collapsed line that can be displayed.
  57. /// </returns>
  58. public abstract TextLine Collapse(params TextCollapsingProperties[] collapsingPropertiesList);
  59. /// <summary>
  60. /// Gets the character hit corresponding to the specified distance from the beginning of the line.
  61. /// </summary>
  62. /// <param name="distance">A <see cref="double"/> value that represents the distance from the beginning of the line.</param>
  63. /// <returns>The <see cref="CharacterHit"/> object at the specified distance from the beginning of the line.</returns>
  64. public abstract CharacterHit GetCharacterHitFromDistance(double distance);
  65. /// <summary>
  66. /// Gets the distance from the beginning of the line to the specified character hit.
  67. /// <see cref="CharacterHit"/>.
  68. /// </summary>
  69. /// <param name="characterHit">The <see cref="CharacterHit"/> object whose distance you want to query.</param>
  70. /// <returns>A <see cref="double"/> that represents the distance from the beginning of the line.</returns>
  71. public abstract double GetDistanceFromCharacterHit(CharacterHit characterHit);
  72. /// <summary>
  73. /// Gets the next character hit for caret navigation.
  74. /// </summary>
  75. /// <param name="characterHit">The current <see cref="CharacterHit"/>.</param>
  76. /// <returns>The next <see cref="CharacterHit"/>.</returns>
  77. public abstract CharacterHit GetNextCaretCharacterHit(CharacterHit characterHit);
  78. /// <summary>
  79. /// Gets the previous character hit for caret navigation.
  80. /// </summary>
  81. /// <param name="characterHit">The current <see cref="CharacterHit"/>.</param>
  82. /// <returns>The previous <see cref="CharacterHit"/>.</returns>
  83. public abstract CharacterHit GetPreviousCaretCharacterHit(CharacterHit characterHit);
  84. /// <summary>
  85. /// Gets the previous character hit after backspacing.
  86. /// </summary>
  87. /// <param name="characterHit">The current <see cref="CharacterHit"/>.</param>
  88. /// <returns>The <see cref="CharacterHit"/> after backspacing.</returns>
  89. public abstract CharacterHit GetBackspaceCaretCharacterHit(CharacterHit characterHit);
  90. /// <summary>
  91. /// Gets the text line offset x.
  92. /// </summary>
  93. /// <param name="lineWidth">The line width.</param>
  94. /// <param name="paragraphWidth">The paragraph width.</param>
  95. /// <param name="textAlignment">The text alignment.</param>
  96. /// <returns>The paragraph offset.</returns>
  97. internal static double GetParagraphOffsetX(double lineWidth, double paragraphWidth, TextAlignment textAlignment)
  98. {
  99. if (double.IsPositiveInfinity(paragraphWidth))
  100. {
  101. return 0;
  102. }
  103. switch (textAlignment)
  104. {
  105. case TextAlignment.Center:
  106. return (paragraphWidth - lineWidth) / 2;
  107. case TextAlignment.Right:
  108. return paragraphWidth - lineWidth;
  109. default:
  110. return 0.0f;
  111. }
  112. }
  113. }
  114. }