FormattedTextImplTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Avalonia.Media;
  4. using Avalonia.Platform;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Globalization;
  8. using System.Linq;
  9. using System.Text;
  10. using Xunit;
  11. #if AVALONIA_SKIA
  12. namespace Avalonia.Skia.RenderTests
  13. #else
  14. using Avalonia.Direct2D1.RenderTests;
  15. namespace Avalonia.Direct2D1.RenderTests.Media
  16. #endif
  17. {
  18. public class FormattedTextImplTests : TestBase
  19. {
  20. private const string FontName = "Courier New";
  21. private const double FontSize = 12;
  22. private const double MediumFontSize = 18;
  23. private const double BigFontSize = 32;
  24. private const double FontSizeHeight = 13.594;//real value 13.59375
  25. private const string stringword = "word";
  26. private const string stringmiddle = "The quick brown fox jumps over the lazy dog";
  27. private const string stringmiddle2lines = "The quick brown fox\njumps over the lazy dog";
  28. private const string stringmiddle3lines = "01234567\n\n0123456789";
  29. private const string stringmiddlenewlines = "012345678\r 1234567\r\n 12345678\n0123456789";
  30. private const string stringlong =
  31. "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis " +
  32. "aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero" +
  33. " at sagittis mollis, tellus est malesuada tellus, at luctus turpis elit sit amet quam. Vivamus " +
  34. "pretium ornare est.";
  35. public FormattedTextImplTests()
  36. : base(@"Media\FormattedText")
  37. {
  38. }
  39. private IFormattedTextImpl Create(string text,
  40. string fontFamily,
  41. double fontSize,
  42. FontStyle fontStyle,
  43. TextAlignment textAlignment,
  44. FontWeight fontWeight,
  45. TextWrapping wrapping,
  46. double widthConstraint)
  47. {
  48. var r = AvaloniaLocator.Current.GetService<IPlatformRenderInterface>();
  49. return r.CreateFormattedText(text,
  50. new Typeface(fontFamily, fontSize, fontStyle, fontWeight),
  51. textAlignment,
  52. wrapping,
  53. widthConstraint == -1 ? Size.Infinity : new Size(widthConstraint, double.PositiveInfinity),
  54. null);
  55. }
  56. private IFormattedTextImpl Create(string text, double fontSize)
  57. {
  58. return Create(text, FontName, fontSize,
  59. FontStyle.Normal, TextAlignment.Left,
  60. FontWeight.Normal, TextWrapping.NoWrap,
  61. -1);
  62. }
  63. private IFormattedTextImpl Create(string text, double fontSize, TextAlignment alignment, double widthConstraint)
  64. {
  65. return Create(text, FontName, fontSize,
  66. FontStyle.Normal, alignment,
  67. FontWeight.Normal, TextWrapping.NoWrap,
  68. widthConstraint);
  69. }
  70. private IFormattedTextImpl Create(string text, double fontSize, TextWrapping wrap, double widthConstraint)
  71. {
  72. return Create(text, FontName, fontSize,
  73. FontStyle.Normal, TextAlignment.Left,
  74. FontWeight.Normal, wrap,
  75. widthConstraint);
  76. }
  77. [Theory]
  78. [InlineData("", FontSize, 0, FontSizeHeight)]
  79. [InlineData("x", FontSize, 7.20, FontSizeHeight)]
  80. [InlineData(stringword, FontSize, 28.80, FontSizeHeight)]
  81. [InlineData(stringmiddle, FontSize, 309.65, FontSizeHeight)]
  82. [InlineData(stringmiddle, MediumFontSize, 464.48, 20.391)]
  83. [InlineData(stringmiddle, BigFontSize, 825.73, 36.25)]
  84. [InlineData(stringmiddle2lines, FontSize, 165.63, 2 * FontSizeHeight)]
  85. [InlineData(stringmiddle2lines, MediumFontSize, 248.44, 2 * 20.391)]
  86. [InlineData(stringmiddle2lines, BigFontSize, 441.67, 2 * 36.25)]
  87. [InlineData(stringlong, FontSize, 2160.35, FontSizeHeight)]
  88. [InlineData(stringmiddlenewlines, FontSize, 72.01, 4 * FontSizeHeight)]
  89. public void Should_Measure_String_Correctly(string input, double fontSize, double expWidth, double expHeight)
  90. {
  91. var fmt = Create(input, fontSize);
  92. var size = fmt.Size;
  93. Assert.Equal(expWidth, size.Width, 2);
  94. Assert.Equal(expHeight, size.Height, 2);
  95. var linesHeight = fmt.GetLines().Sum(l => l.Height);
  96. Assert.Equal(expHeight, linesHeight, 2);
  97. }
  98. [Theory]
  99. [InlineData("", 1, -1, TextWrapping.NoWrap)]
  100. [InlineData("x", 1, -1, TextWrapping.NoWrap)]
  101. [InlineData(stringword, 1, -1, TextWrapping.NoWrap)]
  102. [InlineData(stringmiddle, 1, -1, TextWrapping.NoWrap)]
  103. [InlineData(stringmiddle, 3, 150, TextWrapping.Wrap)]
  104. [InlineData(stringmiddle2lines, 2, -1, TextWrapping.NoWrap)]
  105. [InlineData(stringmiddle2lines, 3, 150, TextWrapping.Wrap)]
  106. [InlineData(stringlong, 1, -1, TextWrapping.NoWrap)]
  107. [InlineData(stringlong, 18, 150, TextWrapping.Wrap)]
  108. [InlineData(stringmiddlenewlines, 4, -1, TextWrapping.NoWrap)]
  109. [InlineData(stringmiddlenewlines, 4, 150, TextWrapping.Wrap)]
  110. public void Should_Break_Lines_String_Correctly(string input,
  111. int linesCount,
  112. double widthConstraint,
  113. TextWrapping wrap)
  114. {
  115. var fmt = Create(input, FontSize, wrap, widthConstraint);
  116. var constrained = fmt;
  117. var lines = constrained.GetLines().ToArray();
  118. Assert.Equal(linesCount, lines.Count());
  119. }
  120. [Theory]
  121. [InlineData("x", 0, 0, true, false, 0)]
  122. [InlineData(stringword, -1, -1, false, false, 0)]
  123. [InlineData(stringword, 25, 13, true, false, 3)]
  124. [InlineData(stringword, 28.70, 13.5, true, true, 3)]
  125. [InlineData(stringword, 30, 13, false, true, 3)]
  126. [InlineData(stringword + "\r\n", 30, 13, false, false, 4)]
  127. [InlineData(stringword + "\r\nnext", 30, 13, false, false, 4)]
  128. [InlineData(stringword, 300, 13, false, true, 3)]
  129. [InlineData(stringword + "\r\n", 300, 13, false, false, 4)]
  130. [InlineData(stringword + "\r\nnext", 300, 13, false, false, 4)]
  131. [InlineData(stringword, 300, 300, false, true, 3)]
  132. //TODO: Direct2D implementation return textposition 6
  133. //but the text is 6 length, can't find the logic for me it should be 5
  134. //[InlineData(stringword + "\r\n", 300, 300, false, false, 6)]
  135. [InlineData(stringword + "\r\nnext", 300, 300, false, true, 9)]
  136. [InlineData(stringword + "\r\nnext", 300, 25, false, true, 9)]
  137. [InlineData(stringword, 28, 15, false, true, 3)]
  138. [InlineData(stringword, 30, 15, false, true, 3)]
  139. [InlineData(stringmiddle3lines, 30, 15, false, false, 9)]
  140. [InlineData(stringmiddle3lines, 500, 13, false, false, 8)]
  141. [InlineData(stringmiddle3lines, 30, 25, false, false, 9)]
  142. [InlineData(stringmiddle3lines, -1, 30, false, false, 10)]
  143. public void Should_HitTestPoint_Correctly(string input,
  144. double x, double y,
  145. bool isInside, bool isTrailing, int pos)
  146. {
  147. var fmt = Create(input, FontSize);
  148. var htRes = fmt.HitTestPoint(new Point(x, y));
  149. Assert.Equal(pos, htRes.TextPosition);
  150. Assert.Equal(isInside, htRes.IsInside);
  151. Assert.Equal(isTrailing, htRes.IsTrailing);
  152. }
  153. [Theory]
  154. [InlineData("", 0, 0, 0, 0, FontSizeHeight)]
  155. [InlineData("x", 0, 0, 0, 7.20, FontSizeHeight)]
  156. [InlineData("x", -1, 7.20, 0, 0, FontSizeHeight)]
  157. [InlineData(stringword, 3, 21.60, 0, 7.20, FontSizeHeight)]
  158. [InlineData(stringword, 4, 21.60 + 7.20, 0, 0, FontSizeHeight)]
  159. [InlineData(stringmiddlenewlines, 10, 0, FontSizeHeight, 7.20, FontSizeHeight)]
  160. [InlineData(stringmiddlenewlines, 15, 36.01, FontSizeHeight, 7.20, FontSizeHeight)]
  161. [InlineData(stringmiddlenewlines, 20, 0, 2 * FontSizeHeight, 7.20, FontSizeHeight)]
  162. [InlineData(stringmiddlenewlines, -1, 72.01, 3 * FontSizeHeight, 0, FontSizeHeight)]
  163. public void Should_HitTestPosition_Correctly(string input,
  164. int index, double x, double y, double width, double height)
  165. {
  166. var fmt = Create(input, FontSize);
  167. var r = fmt.HitTestTextPosition(index);
  168. Assert.Equal(x, r.X, 2);
  169. Assert.Equal(y, r.Y, 2);
  170. Assert.Equal(width, r.Width, 2);
  171. Assert.Equal(height, r.Height, 2);
  172. }
  173. [Theory]
  174. [InlineData("x", 0, 200, 200 - 7.20, 0, 7.20, FontSizeHeight)]
  175. [InlineData(stringword, 0, 200, 171.20, 0, 7.20, FontSizeHeight)]
  176. [InlineData(stringword, 3, 200, 200 - 7.20, 0, 7.20, FontSizeHeight)]
  177. public void Should_HitTestPosition_RigthAlign_Correctly(
  178. string input, int index, double widthConstraint,
  179. double x, double y, double width, double height)
  180. {
  181. //parse expected
  182. var fmt = Create(input, FontSize, TextAlignment.Right, widthConstraint);
  183. var constrained = fmt;
  184. var r = constrained.HitTestTextPosition(index);
  185. Assert.Equal(x, r.X, 2);
  186. Assert.Equal(y, r.Y, 2);
  187. Assert.Equal(width, r.Width, 2);
  188. Assert.Equal(height, r.Height, 2);
  189. }
  190. [Theory]
  191. [InlineData("x", 0, 200, 100 - 7.20 / 2, 0, 7.20, FontSizeHeight)]
  192. [InlineData(stringword, 0, 200, 85.6, 0, 7.20, FontSizeHeight)]
  193. [InlineData(stringword, 3, 200, 100 + 7.20, 0, 7.20, FontSizeHeight)]
  194. public void Should_HitTestPosition_CenterAlign_Correctly(
  195. string input, int index, double widthConstraint,
  196. double x, double y, double width, double height)
  197. {
  198. //parse expected
  199. var fmt = Create(input, FontSize, TextAlignment.Center, widthConstraint);
  200. var constrained = fmt;
  201. var r = constrained.HitTestTextPosition(index);
  202. Assert.Equal(x, r.X, 2);
  203. Assert.Equal(y, r.Y, 2);
  204. Assert.Equal(width, r.Width, 2);
  205. Assert.Equal(height, r.Height, 2);
  206. }
  207. [Theory]
  208. [InlineData("x", 0, 1, "0,0,7.20,13.59")]
  209. [InlineData(stringword, 0, 4, "0,0,28.80,13.59")]
  210. [InlineData(stringmiddlenewlines, 10, 10, "0,13.59,57.61,13.59")]
  211. [InlineData(stringmiddlenewlines, 10, 20, "0,13.59,57.61,13.59;0,27.19,64.81,13.59")]
  212. [InlineData(stringmiddlenewlines, 10, 15, "0,13.59,57.61,13.59;0,27.19,36.01,13.59")]
  213. [InlineData(stringmiddlenewlines, 15, 15, "36.01,13.59,21.60,13.59;0,27.19,64.81,13.59")]
  214. public void Should_HitTestRange_Correctly(string input,
  215. int index, int length,
  216. string expectedRects)
  217. {
  218. //parse expected result
  219. var rects = expectedRects.Split(';').Select(s =>
  220. {
  221. double[] v = s.Split(',')
  222. .Select(sd => double.Parse(sd, CultureInfo.InvariantCulture)).ToArray();
  223. return new Rect(v[0], v[1], v[2], v[3]);
  224. }).ToArray();
  225. var fmt = Create(input, FontSize);
  226. var htRes = fmt.HitTestTextRange(index, length).ToArray();
  227. Assert.Equal(rects.Length, htRes.Length);
  228. for (int i = 0; i < rects.Length; i++)
  229. {
  230. var exr = rects[i];
  231. var r = htRes[i];
  232. Assert.Equal(exr.X, r.X, 2);
  233. Assert.Equal(exr.Y, r.Y, 2);
  234. Assert.Equal(exr.Width, r.Width, 2);
  235. Assert.Equal(exr.Height, r.Height, 2);
  236. }
  237. }
  238. }
  239. }