TextLayoutTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using Avalonia.Media;
  2. using System;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Threading.Tasks;
  7. using Avalonia.Controls;
  8. using Avalonia.Media.TextFormatting;
  9. using Avalonia.Utilities;
  10. using Xunit;
  11. #if AVALONIA_SKIA
  12. namespace Avalonia.Skia.RenderTests
  13. #else
  14. namespace Avalonia.Direct2D1.RenderTests.Media
  15. #endif
  16. {
  17. public class TextLayoutTests : TestBase
  18. {
  19. private const string FontName = "Courier New";
  20. private const double FontSize = 12;
  21. private const double MediumFontSize = 18;
  22. private const double BigFontSize = 32;
  23. private const double FontSizeHeight = 13.594;//real value 13.59375
  24. private const string stringword = "word";
  25. private const string stringmiddle = "The quick brown fox jumps over the lazy dog";
  26. private const string stringmiddle2lines = "The quick brown fox\njumps over the lazy dog";
  27. private const string stringmiddle3lines = "01234567\n\n0123456789";
  28. private const string stringmiddlenewlines = "012345678\r 1234567\r\n 12345678\n0123456789";
  29. private const string stringlong =
  30. "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis " +
  31. "aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero" +
  32. " at sagittis mollis, tellus est malesuada tellus, at luctus turpis elit sit amet quam. Vivamus " +
  33. "pretium ornare est.";
  34. public TextLayoutTests()
  35. : base(@"Media\TextFormatting\TextLayout")
  36. {
  37. }
  38. private static TextLayout Create(string text,
  39. string fontFamily,
  40. double fontSize,
  41. FontStyle fontStyle,
  42. TextAlignment textAlignment,
  43. FontWeight fontWeight,
  44. TextWrapping wrapping,
  45. double widthConstraint)
  46. {
  47. var typeface = new Typeface(fontFamily, fontStyle, fontWeight);
  48. var formattedText = new TextLayout(text, typeface, fontSize, null, textAlignment, wrapping,
  49. maxWidth: widthConstraint == -1 ? double.PositiveInfinity : widthConstraint);
  50. return formattedText;
  51. }
  52. private static TextLayout Create(string text, double fontSize)
  53. {
  54. return Create(text, FontName, fontSize,
  55. FontStyle.Normal, TextAlignment.Left,
  56. FontWeight.Normal, TextWrapping.NoWrap,
  57. -1);
  58. }
  59. private static TextLayout Create(string text, double fontSize, TextAlignment alignment, double widthConstraint)
  60. {
  61. return Create(text, FontName, fontSize,
  62. FontStyle.Normal, alignment,
  63. FontWeight.Normal, TextWrapping.NoWrap,
  64. widthConstraint);
  65. }
  66. private static TextLayout Create(string text, double fontSize, TextWrapping wrap, double widthConstraint)
  67. {
  68. return Create(text, FontName, fontSize,
  69. FontStyle.Normal, TextAlignment.Left,
  70. FontWeight.Normal, wrap,
  71. widthConstraint);
  72. }
  73. [Theory]
  74. [InlineData("", FontSize, 0, FontSizeHeight)]
  75. [InlineData("x", FontSize, 7.20, FontSizeHeight)]
  76. [InlineData(stringword, FontSize, 28.80, FontSizeHeight)]
  77. [InlineData(stringmiddle, FontSize, 309.65, FontSizeHeight)]
  78. [InlineData(stringmiddle, MediumFontSize, 464.48, 20.391)]
  79. [InlineData(stringmiddle, BigFontSize, 825.73, 36.25)]
  80. [InlineData(stringmiddle2lines, FontSize, 165.63, 2 * FontSizeHeight)]
  81. [InlineData(stringmiddle2lines, MediumFontSize, 248.44, 2 * 20.391)]
  82. [InlineData(stringmiddle2lines, BigFontSize, 441.67, 2 * 36.25)]
  83. [InlineData(stringlong, FontSize, 2160.35, FontSizeHeight)]
  84. [InlineData(stringmiddlenewlines, FontSize, 72.01, 4 * FontSizeHeight)]
  85. public void Should_Measure_String_Correctly(string input, double fontSize, double expWidth, double expHeight)
  86. {
  87. var fmt = Create(input, fontSize);
  88. Assert.Equal(expWidth, fmt.Bounds.Width, 2);
  89. Assert.Equal(expHeight, fmt.Bounds.Height, 2);
  90. }
  91. [Theory]
  92. [InlineData("", 1, -1, TextWrapping.NoWrap)]
  93. [InlineData("x", 1, -1, TextWrapping.NoWrap)]
  94. [InlineData(stringword, 1, -1, TextWrapping.NoWrap)]
  95. [InlineData(stringmiddle, 1, -1, TextWrapping.NoWrap)]
  96. [InlineData(stringmiddle, 3, 150, TextWrapping.Wrap)]
  97. [InlineData(stringmiddle2lines, 2, -1, TextWrapping.NoWrap)]
  98. [InlineData(stringmiddle2lines, 3, 150, TextWrapping.Wrap)]
  99. [InlineData(stringlong, 1, -1, TextWrapping.NoWrap)]
  100. [InlineData(stringlong, 18, 150, TextWrapping.Wrap)]
  101. [InlineData(stringmiddlenewlines, 4, -1, TextWrapping.NoWrap)]
  102. [InlineData(stringmiddlenewlines, 4, 150, TextWrapping.Wrap)]
  103. public void Should_Break_Lines_String_Correctly(string input,
  104. int linesCount,
  105. double widthConstraint,
  106. TextWrapping wrap)
  107. {
  108. var fmt = Create(input, FontSize, wrap, widthConstraint);
  109. var constrained = fmt;
  110. var lines = constrained.TextLines.ToArray();
  111. Assert.Equal(linesCount, lines.Count());
  112. }
  113. [Theory]
  114. [InlineData("x", 0, 0, true, false, 0)]
  115. [InlineData(stringword, -1, -1, false, false, 0)]
  116. [InlineData(stringword, 25, 13, true, false, 3)]
  117. [InlineData(stringword, 28.70, 13.5, true, true, 4)]
  118. [InlineData(stringword, 30, 13, false, true, 4)]
  119. [InlineData(stringword + "\r\n", 30, 13, false, false, 4)]
  120. [InlineData(stringword + "\r\nnext", 30, 13, false, false, 4)]
  121. [InlineData(stringword, 300, 13, false, true, 4)]
  122. [InlineData(stringword + "\r\n", 300, 13, false, false, 4)]
  123. [InlineData(stringword + "\r\nnext", 300, 13, false, false, 4)]
  124. [InlineData(stringword, 300, 300, false, true, 4)]
  125. //TODO: Direct2D implementation return textposition 6
  126. //but the text is 6 length, can't find the logic for me it should be 5
  127. //[InlineData(stringword + "\r\n", 300, 300, false, false, 6)]
  128. [InlineData(stringword + "\r\nnext", 300, 300, false, true, 10)]
  129. [InlineData(stringword + "\r\nnext", 300, 25, false, true, 10)]
  130. [InlineData(stringword, 28, 15, false, true, 4)]
  131. [InlineData(stringword, 30, 15, false, true, 4)]
  132. [InlineData(stringmiddle3lines, 30, 15, false, false, 9)]
  133. [InlineData(stringmiddle3lines, 500, 13, false, false, 8)]
  134. [InlineData(stringmiddle3lines, 30, 25, false, false, 9)]
  135. [InlineData(stringmiddle3lines, -1, 30, false, false, 10)]
  136. public void Should_HitTestPoint_Correctly(string input,
  137. double x, double y,
  138. bool isInside, bool isTrailing, int pos)
  139. {
  140. var fmt = Create(input, FontSize);
  141. var htRes = fmt.HitTestPoint(new Point(x, y));
  142. Assert.Equal(pos, htRes.TextPosition);
  143. Assert.Equal(isInside, htRes.IsInside);
  144. Assert.Equal(isTrailing, htRes.IsTrailing);
  145. }
  146. [Theory]
  147. [InlineData("", 0, 0, 0, 0, FontSizeHeight)]
  148. [InlineData("x", 0, 0, 0, 7.20, FontSizeHeight)]
  149. [InlineData("x", -1, 7.20, 0, 0, FontSizeHeight)]
  150. [InlineData(stringword, 3, 21.60, 0, 7.20, FontSizeHeight)]
  151. [InlineData(stringword, 4, 21.60 + 7.20, 0, 0, FontSizeHeight)]
  152. [InlineData(stringmiddlenewlines, 10, 0, FontSizeHeight, 7.20, FontSizeHeight)]
  153. [InlineData(stringmiddlenewlines, 15, 36.01, FontSizeHeight, 7.20, FontSizeHeight)]
  154. [InlineData(stringmiddlenewlines, 20, 0, 2 * FontSizeHeight, 7.20, FontSizeHeight)]
  155. [InlineData(stringmiddlenewlines, -1, 72.01, 3 * FontSizeHeight, 0, FontSizeHeight)]
  156. public void Should_HitTestPosition_Correctly(string input,
  157. int index, double x, double y, double width, double height)
  158. {
  159. var fmt = Create(input, FontSize);
  160. var r = fmt.HitTestTextPosition(index);
  161. Assert.Equal(x, r.X, 2);
  162. Assert.Equal(y, r.Y, 2);
  163. Assert.Equal(width, r.Width, 2);
  164. Assert.Equal(height, r.Height, 2);
  165. }
  166. [Theory]
  167. [InlineData("x", 0, 200, 200 - 7.20, 0, 7.20, FontSizeHeight)]
  168. [InlineData(stringword, 0, 200, 171.20, 0, 7.20, FontSizeHeight)]
  169. [InlineData(stringword, 3, 200, 200 - 7.20, 0, 7.20, FontSizeHeight)]
  170. public void Should_HitTestPosition_RigthAlign_Correctly(
  171. string input, int index, double widthConstraint,
  172. double x, double y, double width, double height)
  173. {
  174. //parse expected
  175. var fmt = Create(input, FontSize, TextAlignment.Right, widthConstraint);
  176. var constrained = fmt;
  177. var r = constrained.HitTestTextPosition(index);
  178. Assert.Equal(x, r.X, 2);
  179. Assert.Equal(y, r.Y, 2);
  180. Assert.Equal(width, r.Width, 2);
  181. Assert.Equal(height, r.Height, 2);
  182. }
  183. [Theory]
  184. [InlineData("x", 0, 200, 100 - 7.20 / 2, 0, 7.20, FontSizeHeight)]
  185. [InlineData(stringword, 0, 200, 85.6, 0, 7.20, FontSizeHeight)]
  186. [InlineData(stringword, 3, 200, 100 + 7.20, 0, 7.20, FontSizeHeight)]
  187. public void Should_HitTestPosition_CenterAlign_Correctly(
  188. string input, int index, double widthConstraint,
  189. double x, double y, double width, double height)
  190. {
  191. //parse expected
  192. var fmt = Create(input, FontSize, TextAlignment.Center, widthConstraint);
  193. var constrained = fmt;
  194. var r = constrained.HitTestTextPosition(index);
  195. Assert.Equal(x, r.X, 2);
  196. Assert.Equal(y, r.Y, 2);
  197. Assert.Equal(width, r.Width, 2);
  198. Assert.Equal(height, r.Height, 2);
  199. }
  200. [Theory]
  201. [InlineData("x", 0, 1, "0,0,7.20,13.59")]
  202. [InlineData(stringword, 0, 4, "0,0,28.80,13.59")]
  203. [InlineData(stringmiddlenewlines, 10, 10, "0,13.59,57.61,13.59")]
  204. [InlineData(stringmiddlenewlines, 10, 20, "0,13.59,57.61,13.59;0,27.19,64.81,13.59")]
  205. [InlineData(stringmiddlenewlines, 10, 15, "0,13.59,57.61,13.59;0,27.19,36.01,13.59")]
  206. [InlineData(stringmiddlenewlines, 15, 15, "36.01,13.59,21.60,13.59;0,27.19,64.81,13.59")]
  207. public void Should_HitTestRange_Correctly(string input,
  208. int index, int length,
  209. string expectedRects)
  210. {
  211. //parse expected result
  212. var rects = expectedRects.Split(';').Select(s =>
  213. {
  214. double[] v = s.Split(',')
  215. .Select(sd => double.Parse(sd, CultureInfo.InvariantCulture)).ToArray();
  216. return new Rect(v[0], v[1], v[2], v[3]);
  217. }).ToArray();
  218. var fmt = Create(input, FontSize);
  219. var htRes = fmt.HitTestTextRange(index, length).ToArray();
  220. Assert.Equal(rects.Length, htRes.Length);
  221. for (int i = 0; i < rects.Length; i++)
  222. {
  223. var exr = rects[i];
  224. var r = htRes[i];
  225. Assert.Equal(exr.X, r.X, 2);
  226. Assert.Equal(exr.Y, r.Y, 2);
  227. Assert.Equal(exr.Width, r.Width, 2);
  228. Assert.Equal(exr.Height, r.Height, 2);
  229. }
  230. }
  231. [Fact]
  232. public async Task TextLayout_Basic()
  233. {
  234. // Skip test on OSX: text rendering is subtly different.
  235. if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  236. return;
  237. var t = new TextLayout(
  238. "Avalonia!",
  239. new Typeface(TestFontFamily),
  240. 24,
  241. Brushes.Black);
  242. var target = new Border
  243. {
  244. Width = 200,
  245. Height = 200,
  246. Background = Brushes.White,
  247. Child = new DrawnControl(c =>
  248. {
  249. var textRect = t.Bounds;
  250. var bounds = new Rect(0, 0, 200, 200);
  251. var rect = bounds.CenterRect(textRect);
  252. c.DrawRectangle(Brushes.Yellow, null, rect);
  253. t.Draw(c, rect.Position);
  254. }),
  255. };
  256. await RenderToFile(target);
  257. CompareImages();
  258. }
  259. [Fact]
  260. public async Task TextLayout_Rotated()
  261. {
  262. // Skip test on OSX: text rendering is subtly different.
  263. if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  264. return;
  265. var t = new TextLayout(
  266. "Avalonia!",
  267. new Typeface(TestFontFamily),
  268. 24,
  269. Brushes.Black);
  270. var target = new Border
  271. {
  272. Width = 200,
  273. Height = 200,
  274. Background = Brushes.White,
  275. Child = new DrawnControl(c =>
  276. {
  277. var textRect = t.Bounds;
  278. var bounds = new Rect(0, 0, 200, 200);
  279. var rect = bounds.CenterRect(textRect);
  280. var rotate = Matrix.CreateTranslation(-100, -100) *
  281. Matrix.CreateRotation(MathUtilities.Deg2Rad(90)) *
  282. Matrix.CreateTranslation(100, 100);
  283. using var transform = c.PushPreTransform(rotate);
  284. c.DrawRectangle(Brushes.Yellow, null, rect);
  285. t.Draw(c, rect.Position);
  286. }),
  287. };
  288. await RenderToFile(target);
  289. CompareImages();
  290. }
  291. private class DrawnControl : Control
  292. {
  293. private readonly Action<DrawingContext> _render;
  294. public DrawnControl(Action<DrawingContext> render) => _render = render;
  295. public override void Render(DrawingContext context) => _render(context);
  296. }
  297. }
  298. }