TextFormatter.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using Avalonia.Media.TextFormatting.Unicode;
  2. using Avalonia.Utility;
  3. namespace Avalonia.Media.TextFormatting
  4. {
  5. /// <summary>
  6. /// Represents a base class for text formatting.
  7. /// </summary>
  8. public abstract class TextFormatter
  9. {
  10. /// <summary>
  11. /// Gets the current <see cref="TextFormatter"/> that is used for non complex text formatting.
  12. /// </summary>
  13. public static TextFormatter Current
  14. {
  15. get
  16. {
  17. var current = AvaloniaLocator.Current.GetService<TextFormatter>();
  18. if (current != null)
  19. {
  20. return current;
  21. }
  22. current = new SimpleTextFormatter();
  23. AvaloniaLocator.CurrentMutable.Bind<TextFormatter>().ToConstant(current);
  24. return current;
  25. }
  26. }
  27. /// <summary>
  28. /// Formats a text line.
  29. /// </summary>
  30. /// <param name="textSource">The text source.</param>
  31. /// <param name="firstTextSourceIndex">The first character index to start the text line from.</param>
  32. /// <param name="paragraphWidth">A <see cref="double"/> value that specifies the width of the paragraph that the line fills.</param>
  33. /// <param name="paragraphProperties">A <see cref="TextParagraphProperties"/> value that represents paragraph properties,
  34. /// such as TextWrapping, TextAlignment, or TextStyle.</param>
  35. /// <returns>The formatted line.</returns>
  36. public abstract TextLine FormatLine(ITextSource textSource, int firstTextSourceIndex, double paragraphWidth,
  37. TextParagraphProperties paragraphProperties);
  38. /// <summary>
  39. /// Creates a text style run with unique properties.
  40. /// </summary>
  41. /// <param name="text">The text to create text runs from.</param>
  42. /// <param name="defaultStyle"></param>
  43. /// <returns>A list of text runs.</returns>
  44. protected TextStyleRun CreateShapableTextStyleRun(ReadOnlySlice<char> text, TextStyle defaultStyle)
  45. {
  46. var defaultTypeface = defaultStyle.TextFormat.Typeface;
  47. var currentTypeface = defaultTypeface;
  48. if (TryGetRunProperties(text, currentTypeface, defaultTypeface, out var count))
  49. {
  50. return new TextStyleRun(new TextPointer(text.Start, count), new TextStyle(currentTypeface,
  51. defaultStyle.TextFormat.FontRenderingEmSize,
  52. defaultStyle.Foreground, defaultStyle.TextDecorations));
  53. }
  54. var codepoint = Codepoint.ReadAt(text, count, out _);
  55. //ToDo: Fix FontFamily fallback
  56. currentTypeface =
  57. FontManager.Current.MatchCharacter(codepoint, defaultTypeface.Weight, defaultTypeface.Style);
  58. if (currentTypeface != null && TryGetRunProperties(text, currentTypeface, defaultTypeface, out count))
  59. {
  60. //Fallback found
  61. return new TextStyleRun(new TextPointer(text.Start, count), new TextStyle(currentTypeface,
  62. defaultStyle.TextFormat.FontRenderingEmSize,
  63. defaultStyle.Foreground, defaultStyle.TextDecorations));
  64. }
  65. // no fallback found
  66. currentTypeface = defaultTypeface;
  67. var glyphTypeface = currentTypeface.GlyphTypeface;
  68. var enumerator = new GraphemeEnumerator(text);
  69. while (enumerator.MoveNext())
  70. {
  71. var grapheme = enumerator.Current;
  72. if (!grapheme.FirstCodepoint.IsWhiteSpace && glyphTypeface.TryGetGlyph(grapheme.FirstCodepoint, out _))
  73. {
  74. break;
  75. }
  76. count += grapheme.Text.Length;
  77. }
  78. return new TextStyleRun(new TextPointer(text.Start, count),
  79. new TextStyle(currentTypeface, defaultStyle.TextFormat.FontRenderingEmSize,
  80. defaultStyle.Foreground, defaultStyle.TextDecorations));
  81. }
  82. /// <summary>
  83. /// Tries to get run properties.
  84. /// </summary>
  85. /// <param name="defaultTypeface"></param>
  86. /// <param name="text"></param>
  87. /// <param name="typeface">The typeface that is used to find matching characters.</param>
  88. /// <param name="count"></param>
  89. /// <returns></returns>
  90. protected bool TryGetRunProperties(ReadOnlySlice<char> text, Typeface typeface, Typeface defaultTypeface,
  91. out int count)
  92. {
  93. if (text.Length == 0)
  94. {
  95. count = 0;
  96. return false;
  97. }
  98. var isFallback = typeface != defaultTypeface;
  99. count = 0;
  100. var script = Script.Common;
  101. //var direction = BiDiClass.LeftToRight;
  102. var font = typeface.GlyphTypeface;
  103. var defaultFont = defaultTypeface.GlyphTypeface;
  104. var enumerator = new GraphemeEnumerator(text);
  105. while (enumerator.MoveNext())
  106. {
  107. var grapheme = enumerator.Current;
  108. var currentScript = grapheme.FirstCodepoint.Script;
  109. //var currentDirection = grapheme.FirstCodepoint.BiDiClass;
  110. //// ToDo: Implement BiDi algorithm
  111. //if (currentScript.HorizontalDirection != direction)
  112. //{
  113. // if (!UnicodeUtility.IsWhiteSpace(grapheme.FirstCodepoint))
  114. // {
  115. // break;
  116. // }
  117. //}
  118. if (currentScript != script)
  119. {
  120. if (currentScript != Script.Inherited && currentScript != Script.Common)
  121. {
  122. if (script == Script.Inherited || script == Script.Common)
  123. {
  124. script = currentScript;
  125. }
  126. else
  127. {
  128. break;
  129. }
  130. }
  131. }
  132. if (isFallback)
  133. {
  134. if (defaultFont.TryGetGlyph(grapheme.FirstCodepoint, out _))
  135. {
  136. break;
  137. }
  138. }
  139. if (!font.TryGetGlyph(grapheme.FirstCodepoint, out _))
  140. {
  141. if (!grapheme.FirstCodepoint.IsWhiteSpace)
  142. {
  143. break;
  144. }
  145. }
  146. count += grapheme.Text.Length;
  147. }
  148. return count > 0;
  149. }
  150. }
  151. }