TextLayoutTests.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using Avalonia.Media;
  7. using Avalonia.Media.TextFormatting;
  8. using Avalonia.Media.TextFormatting.Unicode;
  9. using Avalonia.UnitTests;
  10. using Avalonia.Utilities;
  11. using Xunit;
  12. namespace Avalonia.Skia.UnitTests.Media.TextFormatting
  13. {
  14. public class TextLayoutTests
  15. {
  16. private const string SingleLineText = "0123456789";
  17. private const string MultiLineText = "01 23 45 678\r\rabc def gh ij";
  18. private const string RightToLeftText = "זה כיף סתם לשמוע איך תנצח קרפד עץ טוב בגן";
  19. [InlineData("01234\r01234\r", 3)]
  20. [InlineData("01234\r01234", 2)]
  21. [Theory]
  22. public void Should_Break_Lines(string text, int numberOfLines)
  23. {
  24. using (Start())
  25. {
  26. var layout = new TextLayout(
  27. text,
  28. Typeface.Default,
  29. 12.0f,
  30. Brushes.Black);
  31. Assert.Equal(numberOfLines, layout.TextLines.Count);
  32. }
  33. }
  34. [Fact]
  35. public void Should_Apply_TextStyleSpan_To_Text_In_Between()
  36. {
  37. using (Start())
  38. {
  39. var foreground = new SolidColorBrush(Colors.Red).ToImmutable();
  40. var spans = new[]
  41. {
  42. new ValueSpan<TextRunProperties>(1, 2,
  43. new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground))
  44. };
  45. var layout = new TextLayout(
  46. MultiLineText,
  47. Typeface.Default,
  48. 12.0f,
  49. Brushes.Black.ToImmutable(),
  50. textStyleOverrides: spans);
  51. var textLine = layout.TextLines[0];
  52. Assert.Equal(3, textLine.TextRuns.Count);
  53. var textRun = textLine.TextRuns[1];
  54. Assert.Equal(2, textRun.Length);
  55. var actual = textRun.Text.ToString();
  56. Assert.Equal("1 ", actual);
  57. Assert.Equal(foreground, textRun.Properties.ForegroundBrush);
  58. }
  59. }
  60. [InlineData(27)]
  61. [InlineData(22)]
  62. [Theory]
  63. public void Should_Wrap_And_Apply_Style(int length)
  64. {
  65. using (Start())
  66. {
  67. var text = "Multiline TextBox with TextWrapping.";
  68. var foreground = new SolidColorBrush(Colors.Red).ToImmutable();
  69. var expected = new TextLayout(
  70. text,
  71. Typeface.Default,
  72. 12.0f,
  73. Brushes.Black.ToImmutable(),
  74. textWrapping: TextWrapping.Wrap,
  75. maxWidth: 200);
  76. var expectedLines = expected.TextLines.Select(x => text.Substring(x.FirstTextSourceIndex,
  77. x.Length)).ToList();
  78. var spans = new[]
  79. {
  80. new ValueSpan<TextRunProperties>(0, length,
  81. new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground))
  82. };
  83. var actual = new TextLayout(
  84. text,
  85. Typeface.Default,
  86. 12.0f,
  87. Brushes.Black.ToImmutable(),
  88. textWrapping: TextWrapping.Wrap,
  89. maxWidth: 200,
  90. textStyleOverrides: spans);
  91. var actualLines = actual.TextLines.Select(x => text.Substring(x.FirstTextSourceIndex,
  92. x.Length)).ToList();
  93. Assert.Equal(expectedLines.Count, actualLines.Count);
  94. for (var j = 0; j < actual.TextLines.Count; j++)
  95. {
  96. var expectedText = expectedLines[j];
  97. var actualText = actualLines[j];
  98. Assert.Equal(expectedText, actualText);
  99. }
  100. }
  101. }
  102. [Fact]
  103. public void Should_Not_Alter_Lines_After_TextStyleSpan_Was_Applied()
  104. {
  105. using (Start())
  106. {
  107. const string text = "אחד !\ntwo !\nשְׁלוֹשָׁה !";
  108. var red = new SolidColorBrush(Colors.Red).ToImmutable();
  109. var black = Brushes.Black.ToImmutable();
  110. var expected = new TextLayout(
  111. text,
  112. Typeface.Default,
  113. 12.0f,
  114. black,
  115. textWrapping: TextWrapping.Wrap);
  116. var expectedGlyphs = GetGlyphs(expected);
  117. var outer = new GraphemeEnumerator(text);
  118. var inner = new GraphemeEnumerator(text);
  119. var i = 0;
  120. var j = 0;
  121. while (true)
  122. {
  123. Grapheme grapheme;
  124. while (inner.MoveNext(out grapheme))
  125. {
  126. j += grapheme.Length;
  127. if (j + i > text.Length)
  128. {
  129. break;
  130. }
  131. var spans = new[]
  132. {
  133. new ValueSpan<TextRunProperties>(i, j,
  134. new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: red))
  135. };
  136. var actual = new TextLayout(
  137. text,
  138. Typeface.Default,
  139. 12.0f,
  140. black,
  141. textWrapping: TextWrapping.Wrap,
  142. textStyleOverrides: spans);
  143. var actualGlyphs = GetGlyphs(actual);
  144. Assert.Equal(expectedGlyphs.Count, actualGlyphs.Count);
  145. for (var k = 0; k < expectedGlyphs.Count; k++)
  146. {
  147. Assert.Equal(expectedGlyphs[k], actualGlyphs[k]);
  148. }
  149. }
  150. if (!outer.MoveNext(out grapheme))
  151. {
  152. break;
  153. }
  154. inner = new GraphemeEnumerator(text);
  155. i += grapheme.Length;
  156. }
  157. }
  158. static List<string> GetGlyphs(TextLayout textLayout)
  159. => textLayout.TextLines
  160. .Select(line => string.Join('|', line.TextRuns
  161. .Cast<ShapedTextRun>()
  162. .SelectMany(run => run.ShapedBuffer, (_, glyph) => glyph.GlyphIndex)))
  163. .ToList();
  164. }
  165. [Fact]
  166. public void Should_Apply_TextStyleSpan_To_Text_At_Start()
  167. {
  168. using (Start())
  169. {
  170. var foreground = new SolidColorBrush(Colors.Red).ToImmutable();
  171. var spans = new[]
  172. {
  173. new ValueSpan<TextRunProperties>(0, 2,
  174. new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground))
  175. };
  176. var layout = new TextLayout(
  177. SingleLineText,
  178. Typeface.Default,
  179. 12.0f,
  180. Brushes.Black.ToImmutable(),
  181. textStyleOverrides: spans);
  182. var textLine = layout.TextLines[0];
  183. Assert.Equal(2, textLine.TextRuns.Count);
  184. var textRun = textLine.TextRuns[0];
  185. Assert.Equal(2, textRun.Length);
  186. var actual = SingleLineText[..textRun.Length];
  187. Assert.Equal("01", actual);
  188. Assert.Equal(foreground, textRun.Properties.ForegroundBrush);
  189. }
  190. }
  191. [Fact]
  192. public void Should_Apply_TextStyleSpan_To_Text_At_End()
  193. {
  194. using (Start())
  195. {
  196. var foreground = new SolidColorBrush(Colors.Red).ToImmutable();
  197. var spans = new[]
  198. {
  199. new ValueSpan<TextRunProperties>(8, 2,
  200. new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground)),
  201. };
  202. var layout = new TextLayout(
  203. SingleLineText,
  204. Typeface.Default,
  205. 12.0f,
  206. Brushes.Black.ToImmutable(),
  207. textStyleOverrides: spans);
  208. var textLine = layout.TextLines[0];
  209. Assert.Equal(2, textLine.TextRuns.Count);
  210. var textRun = textLine.TextRuns[1];
  211. Assert.Equal(2, textRun.Length);
  212. var actual = textRun.Text.ToString();
  213. Assert.Equal("89", actual);
  214. Assert.Equal(foreground, textRun.Properties.ForegroundBrush);
  215. }
  216. }
  217. [Fact]
  218. public void Should_Apply_TextStyleSpan_To_Single_Character()
  219. {
  220. using (Start())
  221. {
  222. var foreground = new SolidColorBrush(Colors.Red).ToImmutable();
  223. var spans = new[]
  224. {
  225. new ValueSpan<TextRunProperties>(0, 1,
  226. new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground))
  227. };
  228. var layout = new TextLayout(
  229. "0",
  230. Typeface.Default,
  231. 12.0f,
  232. Brushes.Black.ToImmutable(),
  233. textStyleOverrides: spans);
  234. var textLine = layout.TextLines[0];
  235. Assert.Equal(1, textLine.TextRuns.Count);
  236. var textRun = textLine.TextRuns[0];
  237. Assert.Equal(1, textRun.Length);
  238. Assert.Equal(foreground, textRun.Properties.ForegroundBrush);
  239. }
  240. }
  241. [Fact]
  242. public void Should_Apply_TextSpan_To_Unicode_String_In_Between()
  243. {
  244. using (Start())
  245. {
  246. const string text = "😄😄😄😄";
  247. var foreground = new SolidColorBrush(Colors.Red).ToImmutable();
  248. var spans = new[]
  249. {
  250. new ValueSpan<TextRunProperties>(2, 2,
  251. new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground))
  252. };
  253. var layout = new TextLayout(
  254. text,
  255. Typeface.Default,
  256. 12.0f,
  257. Brushes.Black.ToImmutable(),
  258. textStyleOverrides: spans);
  259. var textLine = layout.TextLines[0];
  260. Assert.Equal(3, textLine.TextRuns.Count);
  261. var textRun = textLine.TextRuns[1];
  262. Assert.Equal(2, textRun.Length);
  263. var actual = textRun.Text.ToString();
  264. Assert.Equal("😄", actual);
  265. Assert.Equal(foreground, textRun.Properties.ForegroundBrush);
  266. }
  267. }
  268. [Fact]
  269. public void TextLength_Should_Be_Equal_To_TextLine_Length_Sum()
  270. {
  271. using (Start())
  272. {
  273. var layout = new TextLayout(
  274. MultiLineText,
  275. Typeface.Default,
  276. 12.0f,
  277. Brushes.Black.ToImmutable());
  278. Assert.Equal(MultiLineText.Length, layout.TextLines.Sum(x => x.Length));
  279. }
  280. }
  281. [Fact]
  282. public void TextLength_Should_Be_Equal_To_TextRun_TextLength_Sum()
  283. {
  284. using (Start())
  285. {
  286. var layout = new TextLayout(
  287. MultiLineText,
  288. Typeface.Default,
  289. 12.0f,
  290. Brushes.Black.ToImmutable());
  291. Assert.Equal(
  292. MultiLineText.Length,
  293. layout.TextLines.Select(textLine =>
  294. textLine.TextRuns.Sum(textRun => textRun.Length))
  295. .Sum());
  296. }
  297. }
  298. [Fact]
  299. public void TextLength_Should_Be_Equal_To_TextRun_TextLength_Sum_After_Wrap_With_Style_Applied()
  300. {
  301. using (Start())
  302. {
  303. const string text =
  304. "Multiline TextBox with TextWrapping.\r\rLorem ipsum dolor sit amet";
  305. var foreground = new SolidColorBrush(Colors.Red).ToImmutable();
  306. var spans = new[]
  307. {
  308. new ValueSpan<TextRunProperties>(0, 24,
  309. new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground))
  310. };
  311. var layout = new TextLayout(
  312. text,
  313. Typeface.Default,
  314. 12.0f,
  315. Brushes.Black.ToImmutable(),
  316. textWrapping: TextWrapping.Wrap,
  317. maxWidth: 180,
  318. textStyleOverrides: spans);
  319. Assert.Equal(
  320. text.Length,
  321. layout.TextLines.Select(textLine =>
  322. textLine.TextRuns.Sum(textRun => textRun.Length))
  323. .Sum());
  324. }
  325. }
  326. [Fact]
  327. public void Should_Apply_TextStyleSpan_To_MultiLine()
  328. {
  329. using (Start())
  330. {
  331. var foreground = new SolidColorBrush(Colors.Red).ToImmutable();
  332. var spans = new[]
  333. {
  334. new ValueSpan<TextRunProperties>(5, 20,
  335. new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground))
  336. };
  337. var layout = new TextLayout(
  338. MultiLineText,
  339. Typeface.Default,
  340. 12.0f,
  341. Brushes.Black.ToImmutable(),
  342. maxWidth: 200,
  343. maxHeight: 125,
  344. textStyleOverrides: spans);
  345. Assert.Equal(foreground, layout.TextLines[0].TextRuns[1].Properties.ForegroundBrush);
  346. Assert.Equal(foreground, layout.TextLines[1].TextRuns[0].Properties.ForegroundBrush);
  347. Assert.Equal(foreground, layout.TextLines[2].TextRuns[0].Properties.ForegroundBrush);
  348. }
  349. }
  350. [Fact]
  351. public void Should_Hit_Test_SurrogatePair()
  352. {
  353. using (Start())
  354. {
  355. const string text = "😄😄";
  356. var layout = new TextLayout(
  357. text,
  358. Typeface.Default,
  359. 12.0f,
  360. Brushes.Black.ToImmutable());
  361. var shapedRun = (ShapedTextRun)layout.TextLines[0].TextRuns[0];
  362. var glyphRun = shapedRun.GlyphRun;
  363. var width = glyphRun.Bounds.Width;
  364. var characterHit = glyphRun.GetCharacterHitFromDistance(width, out _);
  365. Assert.Equal(2, characterHit.FirstCharacterIndex);
  366. Assert.Equal(2, characterHit.TrailingLength);
  367. }
  368. }
  369. [Theory]
  370. [InlineData("☝🏿", new int[] { 0 })]
  371. [InlineData("☝🏿 ab", new int[] { 0, 3, 4, 5 })]
  372. [InlineData("ab ☝🏿", new int[] { 0, 1, 2, 3 })]
  373. public void Should_Create_Valid_Clusters_For_Text(string text, int[] clusters)
  374. {
  375. using (Start())
  376. {
  377. var layout = new TextLayout(
  378. text,
  379. Typeface.Default,
  380. 12.0f,
  381. Brushes.Black.ToImmutable());
  382. var textLine = layout.TextLines[0];
  383. var index = 0;
  384. foreach (var textRun in textLine.TextRuns)
  385. {
  386. var shapedRun = (ShapedTextRun)textRun;
  387. var glyphClusters = shapedRun.ShapedBuffer.Select(glyph => glyph.GlyphCluster).ToArray();
  388. var expected = clusters.Skip(index).Take(glyphClusters.Length).ToArray();
  389. Assert.Equal(expected, glyphClusters);
  390. index += glyphClusters.Length;
  391. }
  392. }
  393. }
  394. [Theory]
  395. [InlineData("abcde\r\n", 7)] // Carriage Return + Line Feed
  396. [InlineData("abcde\u000A", 6)] // Line Feed
  397. [InlineData("abcde\u000B", 6)] // Vertical Tab
  398. [InlineData("abcde\u000C", 6)] // Form Feed
  399. [InlineData("abcde\u000D", 6)] // Carriage Return
  400. public void Should_Break_With_BreakChar(string text, int expectedLength)
  401. {
  402. using (Start())
  403. {
  404. var layout = new TextLayout(
  405. text,
  406. Typeface.Default,
  407. 12.0f,
  408. Brushes.Black.ToImmutable());
  409. Assert.Equal(2, layout.TextLines.Count);
  410. Assert.Equal(1, layout.TextLines[0].TextRuns.Count);
  411. Assert.Equal(expectedLength, ((ShapedTextRun)layout.TextLines[0].TextRuns[0]).GlyphRun.GlyphInfos.Count);
  412. Assert.Equal(5, ((ShapedTextRun)layout.TextLines[0].TextRuns[0]).ShapedBuffer[5].GlyphCluster);
  413. if (expectedLength == 7)
  414. {
  415. Assert.Equal(5, ((ShapedTextRun)layout.TextLines[0].TextRuns[0]).ShapedBuffer[6].GlyphCluster);
  416. }
  417. }
  418. }
  419. [Fact]
  420. public void Should_Have_One_Run_With_Common_Script()
  421. {
  422. using (Start())
  423. {
  424. var layout = new TextLayout(
  425. "abcde\r\n",
  426. Typeface.Default,
  427. 12.0f,
  428. Brushes.Black.ToImmutable());
  429. Assert.Equal(1, layout.TextLines[0].TextRuns.Count);
  430. }
  431. }
  432. [Fact]
  433. public void Should_Layout_Corrupted_Text()
  434. {
  435. using (Start())
  436. {
  437. var text = new string(new[] { '\uD802', '\uD802', '\uD802', '\uD802', '\uD802', '\uD802', '\uD802' });
  438. var layout = new TextLayout(
  439. text,
  440. Typeface.Default,
  441. 12,
  442. Brushes.Black.ToImmutable());
  443. var textLine = layout.TextLines[0];
  444. var textRun = (ShapedTextRun)textLine.TextRuns[0];
  445. Assert.Equal(7, textRun.Length);
  446. var replacementGlyph = Typeface.Default.GlyphTypeface.GetGlyph(Codepoint.ReplacementCodepoint);
  447. foreach (var glyphInfo in textRun.GlyphRun.GlyphInfos)
  448. {
  449. Assert.Equal(replacementGlyph, glyphInfo.GlyphIndex);
  450. }
  451. }
  452. }
  453. [InlineData("0123456789\r0123456789")]
  454. [InlineData("0123456789")]
  455. [Theory]
  456. public void Should_Include_First_Line_When_Constraint_Is_Surpassed(string text)
  457. {
  458. using (Start())
  459. {
  460. var glyphTypeface = Typeface.Default.GlyphTypeface;
  461. var emHeight = glyphTypeface.Metrics.DesignEmHeight;
  462. var lineHeight = glyphTypeface.Metrics.LineSpacing * (12.0 / emHeight);
  463. var layout = new TextLayout(
  464. text,
  465. Typeface.Default,
  466. 12,
  467. Brushes.Black.ToImmutable(),
  468. maxHeight: lineHeight - lineHeight * 0.5);
  469. Assert.Equal(1, layout.TextLines.Count);
  470. Assert.Equal(lineHeight, layout.Bounds.Height);
  471. }
  472. }
  473. [InlineData("0123456789\r\n0123456789\r\n0123456789", 0, 3)]
  474. [InlineData("0123456789\r\n0123456789\r\n0123456789", 1, 1)]
  475. [InlineData("0123456789\r\n0123456789\r\n0123456789", 4, 3)]
  476. [Theory]
  477. public void Should_Not_Exceed_MaxLines(string text, int maxLines, int expectedLines)
  478. {
  479. using (Start())
  480. {
  481. var layout = new TextLayout(
  482. text,
  483. Typeface.Default,
  484. 12,
  485. Brushes.Black,
  486. maxWidth: 50,
  487. maxLines: maxLines);
  488. Assert.Equal(expectedLines, layout.TextLines.Count);
  489. }
  490. }
  491. [Fact]
  492. public void Should_Produce_Fixed_Height_Lines()
  493. {
  494. using (Start())
  495. {
  496. var layout = new TextLayout(
  497. MultiLineText,
  498. Typeface.Default,
  499. 12,
  500. Brushes.Black,
  501. lineHeight: 50);
  502. foreach (var line in layout.TextLines)
  503. {
  504. Assert.Equal(50, line.Height);
  505. }
  506. }
  507. }
  508. private const string Text = "日本でTest一番読まれている英字新聞・ジャパンタイムズが発信する国内外ニュースと、様々なジャンルの特集記事。";
  509. [Fact(Skip = "Only used for profiling.")]
  510. public void Should_Wrap()
  511. {
  512. using (Start())
  513. {
  514. for (var i = 0; i < 2000; i++)
  515. {
  516. var layout = new TextLayout(
  517. Text,
  518. Typeface.Default,
  519. 12,
  520. Brushes.Black,
  521. textWrapping: TextWrapping.Wrap,
  522. maxWidth: 50);
  523. }
  524. }
  525. }
  526. [Fact]
  527. public void Should_Process_Multiple_NewLines_Properly()
  528. {
  529. using (Start())
  530. {
  531. var text = "123\r\n\r\n456\r\n\r\n";
  532. var layout = new TextLayout(
  533. text,
  534. Typeface.Default,
  535. 12.0f,
  536. Brushes.Black);
  537. Assert.Equal(5, layout.TextLines.Count);
  538. Assert.Equal("123\r\n", layout.TextLines[0].TextRuns[0].Text.ToString());
  539. Assert.Equal("\r\n", layout.TextLines[1].TextRuns[0].Text.ToString());
  540. Assert.Equal("456\r\n", layout.TextLines[2].TextRuns[0].Text.ToString());
  541. Assert.Equal("\r\n", layout.TextLines[3].TextRuns[0].Text.ToString());
  542. }
  543. }
  544. [Fact]
  545. public void Should_Wrap_Min_OneCharacter_EveryLine()
  546. {
  547. using (Start())
  548. {
  549. var layout = new TextLayout(
  550. SingleLineText,
  551. Typeface.Default,
  552. 12,
  553. Brushes.Black,
  554. textWrapping: TextWrapping.Wrap,
  555. maxWidth: 3);
  556. //every character should be new line as there not enough space for even one character
  557. Assert.Equal(SingleLineText.Length, layout.TextLines.Count);
  558. }
  559. }
  560. [Fact]
  561. public void Should_HitTestTextRange_RightToLeft()
  562. {
  563. using (Start())
  564. {
  565. const int start = 0;
  566. const int length = 10;
  567. var layout = new TextLayout(
  568. RightToLeftText,
  569. Typeface.Default,
  570. 12,
  571. Brushes.Black);
  572. var selectedText = new TextLayout(
  573. RightToLeftText.Substring(start, length),
  574. Typeface.Default,
  575. 12,
  576. Brushes.Black);
  577. var rects = layout.HitTestTextRange(start, length).ToArray();
  578. Assert.Equal(1, rects.Length);
  579. var selectedRect = rects[0];
  580. Assert.Equal(selectedText.Bounds.Width, selectedRect.Width, 2);
  581. }
  582. }
  583. [Fact]
  584. public void Should_HitTestTextRange_BiDi()
  585. {
  586. const string text = "זה כיףabcDEFזה כיף";
  587. using (Start())
  588. {
  589. var layout = new TextLayout(
  590. text,
  591. Typeface.Default,
  592. 12.0f,
  593. Brushes.Black.ToImmutable());
  594. var textLine = layout.TextLines[0];
  595. var start = textLine.GetDistanceFromCharacterHit(new CharacterHit(5, 1));
  596. var end = textLine.GetDistanceFromCharacterHit(new CharacterHit(6, 1));
  597. var rects = layout.HitTestTextRange(0, 7).ToArray();
  598. Assert.Equal(1, rects.Length);
  599. var expected = rects[0];
  600. Assert.Equal(expected.Left, start);
  601. Assert.Equal(expected.Right, end);
  602. }
  603. }
  604. [Fact]
  605. public void Should_HitTestTextRange()
  606. {
  607. using (Start())
  608. {
  609. var layout = new TextLayout(
  610. SingleLineText,
  611. Typeface.Default,
  612. 12.0f,
  613. Brushes.Black.ToImmutable());
  614. var lineRects = layout.HitTestTextRange(0, SingleLineText.Length).ToList();
  615. Assert.Equal(layout.TextLines.Count, lineRects.Count);
  616. for (var i = 0; i < layout.TextLines.Count; i++)
  617. {
  618. var textLine = layout.TextLines[i];
  619. var rect = lineRects[i];
  620. Assert.Equal(textLine.WidthIncludingTrailingWhitespace, rect.Width);
  621. }
  622. var rects = layout.TextLines
  623. .SelectMany(x => x.TextRuns.Cast<ShapedTextRun>())
  624. .SelectMany(x => x.ShapedBuffer, (_, glyph) => glyph.GlyphAdvance)
  625. .ToArray();
  626. for (var i = 0; i < SingleLineText.Length; i++)
  627. {
  628. for (var j = 1; i + j < SingleLineText.Length; j++)
  629. {
  630. var expected = rects.AsSpan(i, j).ToArray().Sum();
  631. var actual = layout.HitTestTextRange(i, j).Sum(x => x.Width);
  632. Assert.Equal(expected, actual);
  633. }
  634. }
  635. }
  636. }
  637. [Fact]
  638. public void Should_Wrap_RightToLeft()
  639. {
  640. const string text =
  641. "يَجِبُ عَلَى الإنْسَانِ أن يَكُونَ أمِيْنَاً وَصَادِقَاً مَعَ نَفْسِهِ وَمَعَ أَهْلِهِ وَجِيْرَانِهِ وَأَنْ يَبْذُلَ كُلَّ جُهْدٍ فِي إِعْلاءِ شَأْنِ الوَطَنِ وَأَنْ يَعْمَلَ عَلَى مَا يَجْلِبُ السَّعَادَةَ لِلنَّاسِ . ولَن يَتِمَّ لَهُ ذلِك إِلا بِأَنْ يُقَدِّمَ المَنْفَعَةَ العَامَّةَ عَلَى المَنْفَعَةِ الخَاصَّةِ وَهذَا مِثَالٌ لِلتَّضْحِيَةِ .";
  642. using (Start())
  643. {
  644. for (var maxWidth = 366; maxWidth < 900; maxWidth += 33)
  645. {
  646. var layout = new TextLayout(
  647. text,
  648. Typeface.Default,
  649. 12.0f,
  650. Brushes.Black.ToImmutable(),
  651. textWrapping: TextWrapping.Wrap,
  652. flowDirection: FlowDirection.RightToLeft,
  653. maxWidth: maxWidth);
  654. foreach (var textLine in layout.TextLines)
  655. {
  656. Assert.True(textLine.Width <= maxWidth);
  657. var actual = new string(textLine.TextRuns.Cast<ShapedTextRun>()
  658. .OrderBy(x => TextTestHelper.GetStartCharIndex(x.Text))
  659. .SelectMany(x => x.Text.ToString())
  660. .ToArray());
  661. var expected = text.Substring(textLine.FirstTextSourceIndex, textLine.Length);
  662. Assert.Equal(expected, actual);
  663. }
  664. }
  665. }
  666. }
  667. [Fact]
  668. public void Should_Layout_Empty_String()
  669. {
  670. using (Start())
  671. {
  672. var layout = new TextLayout(
  673. string.Empty,
  674. Typeface.Default,
  675. 12,
  676. Brushes.Black);
  677. Assert.True(layout.Bounds.Height > 0);
  678. }
  679. }
  680. [Fact]
  681. public void Should_HitTestPoint_RightToLeft()
  682. {
  683. using (Start())
  684. {
  685. var text = "אאא AAA";
  686. var layout = new TextLayout(
  687. text,
  688. Typeface.Default,
  689. 12,
  690. Brushes.Black,
  691. flowDirection: FlowDirection.RightToLeft);
  692. var firstRun = layout.TextLines[0].TextRuns[0] as ShapedTextRun;
  693. var hit = layout.HitTestPoint(new Point());
  694. Assert.Equal(4, hit.TextPosition);
  695. var currentX = 0.0;
  696. for (var i = 0; i < firstRun.GlyphRun.GlyphInfos.Count; i++)
  697. {
  698. var cluster = firstRun.GlyphRun.GlyphInfos[i].GlyphCluster;
  699. var advance = firstRun.GlyphRun.GlyphInfos[i].GlyphAdvance;
  700. hit = layout.HitTestPoint(new Point(currentX, 0));
  701. Assert.Equal(cluster, hit.TextPosition);
  702. var hitRange = layout.HitTestTextRange(hit.TextPosition, 1);
  703. var distance = hitRange.First().Left;
  704. Assert.Equal(currentX, distance, 2);
  705. currentX += advance;
  706. }
  707. var secondRun = layout.TextLines[0].TextRuns[1] as ShapedTextRun;
  708. hit = layout.HitTestPoint(new Point(firstRun.Size.Width, 0));
  709. Assert.Equal(7, hit.TextPosition);
  710. hit = layout.HitTestPoint(new Point(layout.TextLines[0].WidthIncludingTrailingWhitespace, 0));
  711. Assert.Equal(0, hit.TextPosition);
  712. currentX = firstRun.Size.Width + 0.5;
  713. for (var i = 0; i < secondRun.GlyphRun.GlyphInfos.Count; i++)
  714. {
  715. var cluster = secondRun.GlyphRun.GlyphInfos[i].GlyphCluster;
  716. var advance = secondRun.GlyphRun.GlyphInfos[i].GlyphAdvance;
  717. hit = layout.HitTestPoint(new Point(currentX, 0));
  718. Assert.Equal(cluster, hit.CharacterHit.FirstCharacterIndex);
  719. var hitRange = layout.HitTestTextRange(hit.CharacterHit.FirstCharacterIndex, hit.CharacterHit.TrailingLength);
  720. var distance = hitRange.First().Left + 0.5;
  721. Assert.Equal(currentX, distance, 2);
  722. currentX += advance;
  723. }
  724. }
  725. }
  726. [Fact]
  727. public void Should_Get_CharacterHit_From_Distance_RTL()
  728. {
  729. using (Start())
  730. {
  731. var text = "أَبْجَدِيَّة عَرَبِيَّة";
  732. var layout = new TextLayout(
  733. text,
  734. Typeface.Default,
  735. 12,
  736. Brushes.Black);
  737. var textLine = layout.TextLines[0];
  738. var firstRun = (ShapedTextRun)textLine.TextRuns[0];
  739. var firstCluster = firstRun.ShapedBuffer[0].GlyphCluster;
  740. var characterHit = textLine.GetCharacterHitFromDistance(0);
  741. Assert.Equal(firstCluster, characterHit.FirstCharacterIndex);
  742. Assert.Equal(text.Length, characterHit.FirstCharacterIndex + characterHit.TrailingLength);
  743. var distance = textLine.GetDistanceFromCharacterHit(characterHit);
  744. Assert.Equal(0, distance);
  745. distance = textLine.GetDistanceFromCharacterHit(new CharacterHit(characterHit.FirstCharacterIndex));
  746. var firstAdvance = firstRun.ShapedBuffer[0].GlyphAdvance;
  747. Assert.Equal(firstAdvance, distance, 5);
  748. var rect = layout.HitTestTextPosition(22);
  749. Assert.Equal(firstAdvance, rect.Left, 5);
  750. rect = layout.HitTestTextPosition(23);
  751. Assert.Equal(0, rect.Left, 5);
  752. }
  753. }
  754. [Fact]
  755. public void Should_Get_CharacterHit_From_Distance_RTL_With_TextStyles()
  756. {
  757. using (Start())
  758. {
  759. var text = "أَبْجَدِيَّة عَرَبِيَّة";
  760. var i = 0;
  761. var graphemeEnumerator = new GraphemeEnumerator(text);
  762. while (graphemeEnumerator.MoveNext(out var grapheme))
  763. {
  764. var textStyleOverrides = new[] { new ValueSpan<TextRunProperties>(i, grapheme.Length, new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: Brushes.Red)) };
  765. i += grapheme.Length;
  766. var layout = new TextLayout(
  767. text,
  768. Typeface.Default,
  769. 12,
  770. Brushes.Black,
  771. textStyleOverrides: textStyleOverrides);
  772. var textLine = layout.TextLines[0];
  773. var shapedRuns = textLine.TextRuns.Cast<ShapedTextRun>().ToList();
  774. var clusters = shapedRuns.SelectMany(x => x.ShapedBuffer, (_, glyph) => glyph.GlyphCluster).ToList();
  775. var glyphAdvances = shapedRuns.SelectMany(x => x.ShapedBuffer, (_, glyph) => glyph.GlyphAdvance).ToList();
  776. var currentX = 0.0;
  777. var cluster = text.Length;
  778. for (int j = 0; j < clusters.Count - 1; j++)
  779. {
  780. var glyphAdvance = glyphAdvances[j];
  781. var characterHit = textLine.GetCharacterHitFromDistance(currentX);
  782. Assert.Equal(cluster, characterHit.FirstCharacterIndex + characterHit.TrailingLength);
  783. var distance = textLine.GetDistanceFromCharacterHit(new CharacterHit(cluster));
  784. Assert.Equal(currentX, distance, 5);
  785. currentX += glyphAdvance;
  786. if(glyphAdvance > 0)
  787. {
  788. cluster = clusters[j];
  789. }
  790. }
  791. }
  792. }
  793. }
  794. [InlineData("mgfg🧐df f sdf", "g🧐d", 20, 40)]
  795. [InlineData("وه. وقد تعرض لانتقادات", "دات", 5, 30)]
  796. [InlineData("وه. وقد تعرض لانتقادات", "تعرض", 20, 50)]
  797. [InlineData(" علمية 😱ومضللة ،", " علمية 😱ومضللة ،", 40, 100)]
  798. [InlineData("في عام 2018 ، رفعت ل", "في عام 2018 ، رفعت ل", 100, 120)]
  799. [Theory]
  800. public void HitTestTextRange_Range_ValidLength(string text, string textToSelect, double minWidth, double maxWidth)
  801. {
  802. using (Start())
  803. {
  804. var layout = new TextLayout(text, Typeface.Default, 12, Brushes.Black);
  805. var start = text.IndexOf(textToSelect);
  806. var selectionRectangles = layout.HitTestTextRange(start, textToSelect.Length);
  807. Assert.Equal(1, selectionRectangles.Count());
  808. var rect = selectionRectangles.First();
  809. Assert.InRange(rect.Width, minWidth, maxWidth);
  810. }
  811. }
  812. [InlineData("012🧐210", 2, 4, FlowDirection.LeftToRight, "14.40234375,40.8046875")]
  813. [InlineData("210🧐012", 2, 4, FlowDirection.RightToLeft, "0,7.201171875;21.603515625,33.603515625;48.005859375,55.20703125")]
  814. [InlineData("שנב🧐שנב", 2, 4, FlowDirection.LeftToRight, "11.268,38.208")]
  815. [InlineData("שנב🧐שנב", 2, 4, FlowDirection.RightToLeft, "11.268,38.208")]
  816. [Theory]
  817. public void Should_HitTestTextRangeBetweenRuns(string text, int start, int length,
  818. FlowDirection flowDirection, string expected)
  819. {
  820. using (Start())
  821. {
  822. var expectedRects = expected.Split(';').Select(x =>
  823. {
  824. var startEnd = x.Split(',');
  825. var start = double.Parse(startEnd[0], CultureInfo.InvariantCulture);
  826. var end = double.Parse(startEnd[1], CultureInfo.InvariantCulture);
  827. return new Rect(start, 0, end - start, 0);
  828. }).ToArray();
  829. var textLayout = new TextLayout(text, Typeface.Default, 12, Brushes.Black, flowDirection: flowDirection);
  830. var rects = textLayout.HitTestTextRange(start, length).ToArray();
  831. Assert.Equal(expectedRects.Length, rects.Length);
  832. var endX = textLayout.TextLines[0].GetDistanceFromCharacterHit(new CharacterHit(2));
  833. var startX = textLayout.TextLines[0].GetDistanceFromCharacterHit(new CharacterHit(5, 1));
  834. for (int i = 0; i < expectedRects.Length; i++)
  835. {
  836. var expectedRect = expectedRects[i];
  837. Assert.Equal(expectedRect.Left, rects[i].Left, 2);
  838. Assert.Equal(expectedRect.Right, rects[i].Right, 2);
  839. }
  840. }
  841. }
  842. [Fact]
  843. public void Should_HitTestTextRangeWithLineBreaks()
  844. {
  845. using (Start())
  846. {
  847. var beforeLinebreak = "Line before linebreak";
  848. var afterLinebreak = "Line after linebreak";
  849. var text = beforeLinebreak + Environment.NewLine + "" + Environment.NewLine + afterLinebreak;
  850. var textLayout = new TextLayout(text, Typeface.Default, 12, Brushes.Black);
  851. var end = text.Length - afterLinebreak.Length + 1;
  852. var rects = textLayout.HitTestTextRange(0, end).ToArray();
  853. Assert.Equal(3, rects.Length);
  854. var endX = textLayout.TextLines[2].GetDistanceFromCharacterHit(new CharacterHit(end));
  855. //First character should be covered
  856. Assert.Equal(7.201171875, endX, 2);
  857. }
  858. }
  859. private static IDisposable Start()
  860. {
  861. var disposable = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface
  862. .With(renderInterface: new PlatformRenderInterface(null),
  863. textShaperImpl: new TextShaperImpl(),
  864. fontManagerImpl: new CustomFontManagerImpl()));
  865. return disposable;
  866. }
  867. }
  868. }