TextExamples.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using NUnit.Framework;
  5. using QuestPDF.Elements.Text;
  6. using QuestPDF.Examples.Engine;
  7. using QuestPDF.Fluent;
  8. using QuestPDF.Helpers;
  9. using QuestPDF.Infrastructure;
  10. using SkiaSharp;
  11. namespace QuestPDF.Examples
  12. {
  13. public class TextExamples
  14. {
  15. [Test]
  16. public void SimpleText()
  17. {
  18. RenderingTest
  19. .Create()
  20. .PageSize(500, 100)
  21. .ProduceImages()
  22. .ShowResults()
  23. .Render(container =>
  24. {
  25. container
  26. .Padding(5)
  27. .MinimalBox()
  28. .Border(1)
  29. .Padding(10)
  30. .Text(Placeholders.Paragraph());
  31. });
  32. }
  33. [Test]
  34. public void SimpleTextBlock()
  35. {
  36. RenderingTest
  37. .Create()
  38. .PageSize(600, 300)
  39. .ProduceImages()
  40. .ShowResults()
  41. .Render(container =>
  42. {
  43. container
  44. .Padding(5)
  45. .MinimalBox()
  46. .Border(1)
  47. .MaxWidth(300)
  48. .Padding(10)
  49. .Text(text =>
  50. {
  51. text.DefaultTextStyle(TextStyle.Default.FontSize(20));
  52. text.Span("This is a normal text, followed by an ");
  53. text.Span("underlined red text").FontColor(Colors.Red.Medium).Underline();
  54. text.Span(".");
  55. });
  56. });
  57. }
  58. [Test]
  59. public void TextWeight()
  60. {
  61. RenderingTest
  62. .Create()
  63. .PageSize(500, 500)
  64. .ProduceImages()
  65. .ShowResults()
  66. .Render(container =>
  67. {
  68. container
  69. .Padding(20)
  70. .MinimalBox()
  71. .Border(1)
  72. .Padding(20)
  73. .Text(text =>
  74. {
  75. text.DefaultTextStyle(x => x.FontFamily(Fonts.Calibri).FontSize(20));
  76. text.Line("Thin").Thin();
  77. text.Line("ExtraLight").ExtraLight();
  78. text.Line("Light").Light();
  79. text.Line("NormalWeight").NormalWeight();
  80. text.Line("Medium").Medium();
  81. text.Line("SemiBold").SemiBold();
  82. text.Line("Bold").Bold();
  83. text.Line("ExtraBold").ExtraBold();
  84. text.Line("Black").Black();
  85. text.Line("ExtraBlack").ExtraBlack();
  86. });
  87. });
  88. }
  89. [Test]
  90. public void LineHeight()
  91. {
  92. RenderingTest
  93. .Create()
  94. .PageSize(500, 700)
  95. .ProduceImages()
  96. .ShowResults()
  97. .Render(container =>
  98. {
  99. container
  100. .Padding(20)
  101. .Column(column =>
  102. {
  103. var lineHeights = new[] { 0.8f, 1f, 1.5f };
  104. var paragraph = Placeholders.Paragraph();
  105. foreach (var lineHeight in lineHeights)
  106. {
  107. column
  108. .Item()
  109. .Border(1)
  110. .Padding(10)
  111. .Text(paragraph)
  112. .FontSize(16)
  113. .LineHeight(lineHeight);
  114. }
  115. });
  116. });
  117. }
  118. [Test]
  119. public void LetterSpacing()
  120. {
  121. RenderingTest
  122. .Create()
  123. .PageSize(500, 700)
  124. .ProduceImages()
  125. .ShowResults()
  126. .Render(container =>
  127. {
  128. container
  129. .Padding(20)
  130. .Column(column =>
  131. {
  132. var letterSpacing = new[] { -1f, 0f, 2f };
  133. var paragraph = Placeholders.Sentence().ToUpper();
  134. foreach (var spacing in letterSpacing)
  135. {
  136. column
  137. .Item()
  138. .Border(1)
  139. .Padding(10)
  140. .Column(nestedColumn =>
  141. {
  142. nestedColumn.Item()
  143. .Text(paragraph)
  144. .FontSize(16)
  145. .LetterSpacing(spacing);
  146. nestedColumn.Item()
  147. .Text($"Letter spacing of {spacing} em")
  148. .FontSize(10)
  149. .Italic()
  150. .FontColor(Colors.Blue.Medium);
  151. });
  152. }
  153. });
  154. });
  155. }
  156. [Test]
  157. public void LetterSpacing_Arabic()
  158. {
  159. RenderingTest
  160. .Create()
  161. .PageSize(500, 700)
  162. .ProduceImages()
  163. .ShowResults()
  164. .Render(container =>
  165. {
  166. container
  167. .Padding(50)
  168. .Column(column =>
  169. {
  170. var letterSpacing = new[] { -1f, 0f, 2f };
  171. var paragraph = "ينا الألم. في بعض الأحيان ونظراً للالتزامات التي يفرضها علينا";
  172. foreach (var spacing in letterSpacing)
  173. {
  174. column
  175. .Item()
  176. .Border(1)
  177. .Padding(10)
  178. .Column(nestedColumn =>
  179. {
  180. nestedColumn.Item()
  181. .Text(paragraph)
  182. .FontSize(16)
  183. .FontFamily(Fonts.Calibri)
  184. .LetterSpacing(spacing);
  185. nestedColumn.Item()
  186. .Text($"Letter spacing of {spacing} em")
  187. .FontSize(10)
  188. .Italic()
  189. .FontColor(Colors.Blue.Medium);
  190. });
  191. }
  192. });
  193. });
  194. }
  195. [Test]
  196. public void LetterSpacing_Unicode()
  197. {
  198. RenderingTest
  199. .Create()
  200. .PageSize(500, 700)
  201. .ProduceImages()
  202. .ShowResults()
  203. .Render(container =>
  204. {
  205. container
  206. .Padding(50)
  207. .Column(column =>
  208. {
  209. var letterSpacing = new[] { 0f, 5f };
  210. var paragraph = "Ţ̴̡̧̤̮̺̤̗͎̱̹͙͎͖͂̿̓́̉̊̀̍͜h̵̞̘͇̾̎̏̅į̵̹̖͔͉̰̎̉̄̐̏͑͂̅̃̃͘͝s̷͓͉̭̭̯̬̥̻̰̩̦̑̀̀͌́̒̍̒̌̇͛̀͛́̎ ̷̡̡̟͕̳̺̝̼͇͔̬̟̖͍̈́̽͜͝͝i̶͔͚̟̊̐͛́͛̄̌ṡ̸̡̤̪͙͍̥͙̟̼̝̰̥͈̿̓̄̿̓͠ ̶̢̦̙͍̯̖̱̰̯͕͔͎̯̝̎͑t̸͖̲̱̼̎͐̎̉̾̎̾̌̅̔̏͘ȩ̶̝̫̙͓̙̣̔̀̌̔̋̂̑̈́̏̀̈͘̕͜͝s̸̫̝̮̻̼͐̅̄̎̎̑͝ț̷̨̢̨̻͈̮̞̆͗̓͊̃̌͂̑̉̕̕͜͝͝";
  211. foreach (var spacing in letterSpacing)
  212. {
  213. column.Item()
  214. .Text($"Letter spacing of {spacing} em")
  215. .FontSize(10)
  216. .Italic()
  217. .FontColor(Colors.Blue.Medium);
  218. column.Item()
  219. .PaddingVertical(50)
  220. .Text(paragraph)
  221. .FontSize(16)
  222. .FontFamily(Fonts.Calibri)
  223. .LetterSpacing(spacing);
  224. }
  225. });
  226. });
  227. }
  228. [Test]
  229. public void SuperscriptSubscript_Simple()
  230. {
  231. RenderingTest
  232. .Create()
  233. .PageSize(500, 500)
  234. .ProduceImages()
  235. .ShowResults()
  236. .Render(container =>
  237. {
  238. container
  239. .Padding(20)
  240. .MinimalBox()
  241. .Border(1)
  242. .Padding(20)
  243. .Text(text =>
  244. {
  245. text.DefaultTextStyle(x => x.FontSize(20));
  246. text.ParagraphSpacing(10);
  247. var highlight = TextStyle.Default.BackgroundColor(Colors.Green.Lighten3);
  248. text.Span("E=mc").Style(highlight);
  249. text.Span("2").Superscript().Style(highlight);
  250. text.Span(" is the equation of mass–energy equivalence.");
  251. text.EmptyLine();
  252. text.Span("H").Style(highlight);
  253. text.Span("2").Subscript().Style(highlight);
  254. text.Span("O").Style(highlight);
  255. text.Span(" is the chemical formula for water.");
  256. });
  257. });
  258. }
  259. [Test]
  260. public void SuperscriptSubscript_Effects()
  261. {
  262. RenderingTest
  263. .Create()
  264. .PageSize(800, 400)
  265. .ProduceImages()
  266. .ShowResults()
  267. .Render(container =>
  268. {
  269. container
  270. .Padding(25)
  271. .DefaultTextStyle(x => x.FontSize(30))
  272. .Column(column =>
  273. {
  274. column.Spacing(25);
  275. column.Item().Text(text =>
  276. {
  277. text.DefaultTextStyle(x => x.Underline());
  278. text.Span("Underline of the superscript (E = mc");
  279. text.Span("2").Superscript();
  280. text.Span(") should be at the same height as for normal text.");
  281. });
  282. column.Item().Text(text =>
  283. {
  284. text.DefaultTextStyle(x => x.Underline());
  285. text.Span("Underline of the subscript(H");
  286. text.Span("2").Subscript();
  287. text.Span("O) should be slightly lower than a normal text.");
  288. });
  289. column.Item().Text(text =>
  290. {
  291. text.DefaultTextStyle(x => x.Strikethrough());
  292. text.Span("Strikethrough of both superscript (E=mc");
  293. text.Span("2").Superscript();
  294. text.Span(") and subscript(H");
  295. text.Span("2").Subscript();
  296. text.Span("O) should be visible in the middle of the text.");
  297. });
  298. });
  299. });
  300. }
  301. [Test]
  302. public void ParagraphSpacing()
  303. {
  304. RenderingTest
  305. .Create()
  306. .PageSize(500, 500)
  307. .ProduceImages()
  308. .ShowResults()
  309. .Render(container =>
  310. {
  311. container
  312. .Padding(5)
  313. .MinimalBox()
  314. .Border(1)
  315. .Padding(10)
  316. .Text(text =>
  317. {
  318. text.Line(Placeholders.Paragraph());
  319. });
  320. });
  321. }
  322. [Test]
  323. public void CustomElement()
  324. {
  325. RenderingTest
  326. .Create()
  327. .PageSize(500, 200)
  328. .ProduceImages()
  329. .ShowResults()
  330. .Render(container =>
  331. {
  332. container
  333. .Padding(5)
  334. .MinimalBox()
  335. .Border(1)
  336. .Padding(10)
  337. .Text(text =>
  338. {
  339. text.DefaultTextStyle(TextStyle.Default.FontSize(20));
  340. text.Span("This is a random image aligned to the baseline: ");
  341. text.Element()
  342. .Height(24)
  343. .Width(48)
  344. .Image(Placeholders.Image);
  345. text.Span(".");
  346. });
  347. });
  348. }
  349. [Test]
  350. public void TextElements()
  351. {
  352. RenderingTest
  353. .Create()
  354. .PageSize(PageSizes.A4)
  355. .ProducePdf()
  356. .ShowResults()
  357. .Render(container =>
  358. {
  359. container
  360. .Padding(20)
  361. .Padding(10)
  362. .MinimalBox()
  363. .Border(1)
  364. .Padding(5)
  365. .Padding(10)
  366. .Text(text =>
  367. {
  368. text.DefaultTextStyle(TextStyle.Default);
  369. text.AlignLeft();
  370. text.ParagraphSpacing(10);
  371. text.Line(Placeholders.LoremIpsum());
  372. text.Span($"This is target text that should show up. {DateTime.UtcNow:T} > This is a short sentence that will be wrapped into second line hopefully, right? <").Underline();
  373. });
  374. });
  375. }
  376. [Test]
  377. public void Textcolumn()
  378. {
  379. RenderingTest
  380. .Create()
  381. .PageSize(PageSizes.A4)
  382. .ProducePdf()
  383. .ShowResults()
  384. .Render(container =>
  385. {
  386. container
  387. .Padding(20)
  388. .Padding(10)
  389. .MinimalBox()
  390. .Border(1)
  391. .Padding(5)
  392. .Padding(10)
  393. .Text(text =>
  394. {
  395. text.DefaultTextStyle(TextStyle.Default);
  396. text.AlignLeft();
  397. text.ParagraphSpacing(10);
  398. foreach (var i in Enumerable.Range(1, 100))
  399. text.Line($"{i}: {Placeholders.Paragraph()}");
  400. });
  401. });
  402. }
  403. [Test]
  404. public void SpaceIssue()
  405. {
  406. RenderingTest
  407. .Create()
  408. .PageSize(PageSizes.A4)
  409. .ProducePdf()
  410. .ShowResults()
  411. .Render(container =>
  412. {
  413. container
  414. .Padding(20)
  415. .Padding(10)
  416. .MinimalBox()
  417. .Border(1)
  418. .Padding(5)
  419. .Padding(10)
  420. .Text(text =>
  421. {
  422. text.DefaultTextStyle(x => x.Bold());
  423. text.DefaultTextStyle(TextStyle.Default);
  424. text.AlignLeft();
  425. text.ParagraphSpacing(10);
  426. text.Span(Placeholders.LoremIpsum());
  427. text.EmptyLine();
  428. text.Span("This text is a normal text, ");
  429. text.Span("this is a bold text, ").Bold();
  430. text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
  431. text.Span("and this is slightly bigger text.").FontSize(16);
  432. text.EmptyLine();
  433. text.Span("The new text element also supports injecting custom content between words: ");
  434. text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
  435. text.Span(".");
  436. text.EmptyLine();
  437. text.Span("This is page number ");
  438. text.CurrentPageNumber();
  439. text.Span(" out of ");
  440. text.TotalPages();
  441. text.EmptyLine();
  442. text.Hyperlink("Please visit QuestPDF website", "https://www.questpdf.com");
  443. text.EmptyLine();
  444. text.Span(Placeholders.Paragraphs());
  445. text.EmptyLine();
  446. text.Span(Placeholders.Paragraphs()).Italic();
  447. text.Line("This is target text that does not show up. " + Placeholders.Paragraph());
  448. });
  449. });
  450. }
  451. [Test]
  452. public void HugeList()
  453. {
  454. RenderingTest
  455. .Create()
  456. .PageSize(PageSizes.A4)
  457. .ProducePdf()
  458. .ShowResults()
  459. .Render(container =>
  460. {
  461. container
  462. .Padding(20)
  463. .Padding(10)
  464. .MinimalBox()
  465. .Border(1)
  466. .Padding(5)
  467. .Padding(10)
  468. .Text(text =>
  469. {
  470. text.DefaultTextStyle(TextStyle.Default.FontSize(20).BackgroundColor(Colors.Red.Lighten4));
  471. text.AlignLeft();
  472. text.ParagraphSpacing(10);
  473. text.Span("This text is a normal text, ");
  474. text.Span("this is a bold text, ").Bold();
  475. text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
  476. text.Span("and this is slightly bigger text.").FontSize(16);
  477. text.Span("The new text element also supports injecting custom content between words: ");
  478. text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
  479. text.Span(".");
  480. text.EmptyLine();
  481. foreach (var i in Enumerable.Range(1, 100))
  482. {
  483. text.Line($"{i}: {Placeholders.Paragraph()}");
  484. text.Hyperlink("Please visit QuestPDF website. ", "https://www.questpdf.com");
  485. text.Span("This is page number ");
  486. text.CurrentPageNumber();
  487. text.Span(" out of ");
  488. text.TotalPages();
  489. text.EmptyLine();
  490. }
  491. });
  492. });
  493. }
  494. [Test]
  495. public void MeasureIssueWhenSpaceAtLineEnd()
  496. {
  497. // issue 135
  498. RenderingTest
  499. .Create()
  500. .ProduceImages()
  501. .ShowResults()
  502. .RenderDocument(container =>
  503. {
  504. container.Page(page =>
  505. {
  506. page.Margin(50);
  507. page.PageColor(Colors.White);
  508. page.Size(PageSizes.A4);
  509. page.Content().Text("This is a specially crafted sentence with a specially chosen length for demonstration of the bug that occurs ;;;;;. ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
  510. });
  511. });
  512. }
  513. [Test]
  514. public void EmptyText()
  515. {
  516. // issue 135
  517. RenderingTest
  518. .Create()
  519. .ProduceImages()
  520. .ShowResults()
  521. .RenderDocument(container =>
  522. {
  523. container.Page(page =>
  524. {
  525. page.Margin(50);
  526. page.PageColor(Colors.White);
  527. page.Size(PageSizes.A4);
  528. page.Content().Text(" ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
  529. });
  530. });
  531. }
  532. [Test]
  533. public void Whitespaces()
  534. {
  535. // issue 135
  536. RenderingTest
  537. .Create()
  538. .ProduceImages()
  539. .ShowResults()
  540. .RenderDocument(container =>
  541. {
  542. container.Page(page =>
  543. {
  544. page.Margin(50);
  545. page.PageColor(Colors.White);
  546. page.Size(PageSizes.A4);
  547. page.Content().Text(" x ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
  548. });
  549. });
  550. }
  551. [Test]
  552. public void DrawingNullTextShouldNotThrowException()
  553. {
  554. RenderingTest
  555. .Create()
  556. .ProduceImages()
  557. .ShowResults()
  558. .RenderDocument(container =>
  559. {
  560. container.Page(page =>
  561. {
  562. page.Margin(50);
  563. page.PageColor(Colors.White);
  564. page.Size(PageSizes.A4);
  565. page.Content().Column(column =>
  566. {
  567. column.Item().Text((string) null);
  568. column.Item().Text(text =>
  569. {
  570. text.Span(null);
  571. text.Line(null);
  572. text.Hyperlink(null, "http://www.questpdf.com");
  573. text.TotalPages().Format(x => null);
  574. });
  575. });
  576. });
  577. });
  578. }
  579. [Test]
  580. public void BreakingLongWord()
  581. {
  582. RenderingTest
  583. .Create()
  584. .ProduceImages()
  585. .ShowResults()
  586. .RenderDocument(container =>
  587. {
  588. container.Page(page =>
  589. {
  590. page.Margin(50);
  591. page.PageColor(Colors.White);
  592. page.Size(PageSizes.A4);
  593. page.Content().Column(column =>
  594. {
  595. column.Item().Text((string) null);
  596. column.Item().Text(text =>
  597. {
  598. text.DefaultTextStyle(x => x.BackgroundColor(Colors.Red.Lighten3).FontSize(24));
  599. text.Span(" " + Placeholders.LoremIpsum());
  600. text.Span(" 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 ").WrapAnywhere();
  601. });
  602. });
  603. });
  604. });
  605. }
  606. [Test]
  607. public void TextShaping_Unicode()
  608. {
  609. RenderingTest
  610. .Create()
  611. .PageSize(600, 100)
  612. .ProduceImages()
  613. .ShowResults()
  614. .Render(container =>
  615. {
  616. container
  617. .Padding(35)
  618. .MinimalBox()
  619. .Background(Colors.Grey.Lighten2)
  620. .Text(text =>
  621. {
  622. text.DefaultTextStyle(TextStyle.Default.FontSize(20));
  623. text.Span("Complex Unicode structure: ");
  624. text.Span("T̶̖̔͆͆̽̔ḩ̷̼̫̐̈́̀͜͝͝ì̶͇̤͓̱̣͇͓͉̎s̵̡̟̹͍̜͉̗̾͛̈̐́͋͂͝͠ͅ ̴̨͙͍͇̭̒͗̀́͝ì̷̡̺͉̼̏̏̉̌͝s̷͍͙̗̰̖͙̈̑̂̔͑͊̌̓̊̇͜ ̶̛̼͚͊̅͘ṭ̷̨̘̣̙̖͉͌̏̂̅͑̄̽̕͝ȅ̶̲̲̙̭͈̬̣͔̝͔̈́͝s̸̢̯̪̫͓̭̮̓̀͆͜ț̸̢͉̞̥̤̏̌̓͝").FontFamily(Fonts.Calibri).FontColor(Colors.Red.Medium);
  625. text.Span(".");
  626. });
  627. });
  628. }
  629. [Test]
  630. public void TextShaping_Arabic()
  631. {
  632. RenderingTest
  633. .Create()
  634. .PageSize(250, 100)
  635. .ProduceImages()
  636. .ShowResults()
  637. .Render(container =>
  638. {
  639. container
  640. .Padding(25)
  641. .MinimalBox()
  642. .Background(Colors.Grey.Lighten2)
  643. .Text("خوارزمية ترتيب")
  644. .FontFamily(Fonts.Calibri)
  645. .FontSize(30);
  646. });
  647. }
  648. [Test]
  649. public void FontFallback()
  650. {
  651. RenderingTest
  652. .Create()
  653. .ProduceImages()
  654. .ShowResults()
  655. .RenderDocument(container =>
  656. {
  657. container.Page(page =>
  658. {
  659. page.Margin(50);
  660. page.PageColor(Colors.White);
  661. page.DefaultTextStyle(x => x
  662. .Fallback(y => y.FontFamily("Segoe UI Emoji")
  663. .Fallback(y => y.FontFamily("Microsoft YaHei"))));
  664. page.Size(PageSizes.A4);
  665. page.Content().Text(t =>
  666. {
  667. t.Line("This is normal text.");
  668. t.EmptyLine();
  669. t.Line("Following line should use font fallback:");
  670. t.Line("中文文本");
  671. t.EmptyLine();
  672. t.Line("The following line contains a mix of known and unknown characters.");
  673. t.Line("Mixed line: This 中文 is 文文 a mixed 本 本 line 本 中文文本!");
  674. t.EmptyLine();
  675. t.Line("Emojis work out of the box because of font fallback: 😊😅🥳👍❤😍👌");
  676. });
  677. });
  678. });
  679. }
  680. [Test]
  681. public void WordWrappingStability()
  682. {
  683. // instruction: check if any characters repeat when performing the word-wrapping algorithm
  684. RenderingTest
  685. .Create()
  686. .PageSize(PageSizes.A4)
  687. .ProducePdf()
  688. .ShowResults()
  689. .Render(container =>
  690. {
  691. var text = "Lorem ipsum dolor sit amet consectetuer";
  692. container
  693. .Padding(20)
  694. .Column(column =>
  695. {
  696. column.Spacing(10);
  697. foreach (var width in Enumerable.Range(25, 200))
  698. {
  699. column
  700. .Item()
  701. .MaxWidth(width)
  702. .Background(Colors.Grey.Lighten3)
  703. .Text(text);
  704. }
  705. });
  706. });
  707. }
  708. [Test]
  709. public void AdvancedLanguagesSupport()
  710. {
  711. RenderingTest
  712. .Create()
  713. .PageSize(new PageSize(400, 400))
  714. .ProduceImages()
  715. .ShowResults()
  716. .Render(container =>
  717. {
  718. var text = "في المعلوماتية أو الرياضيات، خوارزمية الترتيب هي خوارزمية تمكن من تنظيم مجموعة عناصر حسب ترتيب محدد.";
  719. container
  720. .Padding(20)
  721. .ContentFromRightToLeft()
  722. .Text(text)
  723. .FontFamily(Fonts.Calibri)
  724. .FontSize(22);
  725. });
  726. }
  727. [Test]
  728. public void WordWrappingWhenRightToLeft()
  729. {
  730. RenderingTest
  731. .Create()
  732. .PageSize(new PageSize(1000, 500))
  733. .ProducePdf()
  734. .ShowResults()
  735. .Render(container =>
  736. {
  737. var text = "في المعلوماتية أو الرياضيات، خوارزمية الترتيب هي خوارزمية تمكن من تنظيم مجموعة عناصر حسب ترتيب محدد.";
  738. container
  739. .Padding(25)
  740. .ContentFromRightToLeft()
  741. .Column(column =>
  742. {
  743. column.Spacing(20);
  744. foreach (var size in new[] { 36, 34, 32, 30, 15 })
  745. {
  746. column
  747. .Item()
  748. .ShowEntire()
  749. .MaxWidth(size * 25)
  750. .Background(Colors.Grey.Lighten3)
  751. .MinimalBox()
  752. .Background(Colors.Grey.Lighten2)
  753. .Text(text)
  754. .FontSize(20)
  755. .FontFamily("Segoe UI");
  756. }
  757. });
  758. });
  759. }
  760. [Test]
  761. public void ForcingTextDirection()
  762. {
  763. RenderingTest
  764. .Create()
  765. .PageSize(new PageSize(1000, 500))
  766. .ProduceImages()
  767. .ShowResults()
  768. .Render(container =>
  769. {
  770. container
  771. .Padding(10)
  772. .DefaultTextStyle(x => x.FontSize(24).FontFamily("Calibri"))
  773. .Column(column =>
  774. {
  775. column.Spacing(10);
  776. var word = "الجوريتم";
  777. var definition = "algorithm in Arabic";
  778. var text = $"{word} - {definition}";
  779. // text direction is automatically detected using the first word
  780. column.Item().Text(text);
  781. // it is possible to force specific content direction
  782. column.Item().Text(text).DirectionFromLeftToRight();
  783. column.Item().Text(text).DirectionFromRightToLeft();
  784. // to combine text in various content directions, split it into segments
  785. column.Item().Text(text =>
  786. {
  787. text.Span(word);
  788. text.Span(" - ");
  789. text.Span(definition);
  790. });
  791. });
  792. });
  793. }
  794. [Test]
  795. public void InconsistentLineHeightWhenUsingNewLineTest()
  796. {
  797. RenderingTest
  798. .Create()
  799. .PageSize(PageSizes.A4)
  800. .ProduceImages()
  801. .ShowResults()
  802. .Render(container =>
  803. {
  804. container
  805. .Padding(20)
  806. .Background(Colors.Grey.Lighten4)
  807. .Text(text =>
  808. {
  809. text.DefaultTextStyle(x => x.FontSize(16));
  810. text.Line(Placeholders.Paragraph());
  811. text.Line("");
  812. text.Line(Placeholders.Paragraph());
  813. text.Line(Placeholders.Label()).FontSize(48);
  814. text.Line(Placeholders.Paragraph());
  815. text.Line("");
  816. text.Line(Placeholders.Paragraph());
  817. });
  818. });
  819. }
  820. [Test]
  821. public void FontFallback_Nested()
  822. {
  823. RenderingTest
  824. .Create()
  825. .ProduceImages()
  826. .ShowResults()
  827. .RenderDocument(container =>
  828. {
  829. container.Page(page =>
  830. {
  831. page.Margin(50);
  832. page.PageColor(Colors.White);
  833. page.Size(PageSizes.A5.Landscape());
  834. page.DefaultTextStyle(x => x
  835. .FontSize(24)
  836. .Bold()
  837. .FontFamily("Times New Roman"));
  838. page.Content().Text(text =>
  839. {
  840. text.Line("Default times new roman 中文文本 text.");
  841. text.Line("Normal weight and green 中文文本 text.").NormalWeight().BackgroundColor(Colors.Green.Lighten2);
  842. text.Line("Strikethrough without underline 中文文本 text.").Strikethrough().Underline(false);
  843. text.Line("Lato italic 中文文本 text.").FontFamily("Lato").Italic();
  844. });
  845. });
  846. });
  847. }
  848. [Test]
  849. public void TextShouldInheritAlignment()
  850. {
  851. RenderingTest
  852. .Create()
  853. .ProduceImages()
  854. .ShowResults()
  855. .PageSize(PageSizes.A4)
  856. .Render(container =>
  857. {
  858. var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
  859. container.Padding(20).Width(400).Column(column =>
  860. {
  861. column.Spacing(20);
  862. column.Item().Background(Colors.Grey.Lighten3).AlignLeft().Text(text);
  863. column.Item().Background(Colors.Grey.Lighten3).AlignCenter().Text(text);
  864. column.Item().Background(Colors.Grey.Lighten3).AlignRight().Text(text);
  865. });
  866. });
  867. }
  868. [Test]
  869. public void LongPageableText()
  870. {
  871. var longText = string.Join("\n", Enumerable.Range(0, 100).Select(_ => Placeholders.Paragraph()));
  872. RenderingTest
  873. .Create()
  874. .ProducePdf()
  875. .ShowResults()
  876. .PageSize(PageSizes.A4)
  877. .Render(container =>
  878. {
  879. container.Padding(25).Background(Colors.Grey.Lighten3).AlignLeft().Text(longText);
  880. });
  881. }
  882. }
  883. }