TextExamples.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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[] { -0.05f, 0f, 0.2f };
  133. var paragraph = Placeholders.Sentence();
  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 WordSpacing()
  158. {
  159. RenderingTest
  160. .Create()
  161. .PageSize(500, 700)
  162. .ProduceImages()
  163. .ShowResults()
  164. .Render(container =>
  165. {
  166. container
  167. .Padding(20)
  168. .Column(column =>
  169. {
  170. var wordSpacing = new[] { -0.2f, 0f, 0.2f };
  171. var paragraph = Placeholders.Sentence();
  172. foreach (var spacing in wordSpacing)
  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. .WordSpacing(spacing);
  184. nestedColumn.Item()
  185. .Text($"Word spacing of {spacing} em")
  186. .FontSize(10)
  187. .Italic()
  188. .FontColor(Colors.Blue.Medium);
  189. });
  190. }
  191. });
  192. });
  193. }
  194. [Test]
  195. public void LetterSpacing_Unicode()
  196. {
  197. RenderingTest
  198. .Create()
  199. .PageSize(500, 700)
  200. .ProduceImages()
  201. .ShowResults()
  202. .Render(container =>
  203. {
  204. container
  205. .Padding(50)
  206. .Column(column =>
  207. {
  208. var letterSpacing = new[] { 0f, 0.5f };
  209. var paragraph = "Ţ̴̡̧̤̮̺̤̗͎̱̹͙͎͖͂̿̓́̉̊̀̍͜h̵̞̘͇̾̎̏̅į̵̹̖͔͉̰̎̉̄̐̏͑͂̅̃̃͘͝s̷͓͉̭̭̯̬̥̻̰̩̦̑̀̀͌́̒̍̒̌̇͛̀͛́̎ ̷̡̡̟͕̳̺̝̼͇͔̬̟̖͍̈́̽͜͝͝i̶͔͚̟̊̐͛́͛̄̌ṡ̸̡̤̪͙͍̥͙̟̼̝̰̥͈̿̓̄̿̓͠ ̶̢̦̙͍̯̖̱̰̯͕͔͎̯̝̎͑t̸͖̲̱̼̎͐̎̉̾̎̾̌̅̔̏͘ȩ̶̝̫̙͓̙̣̔̀̌̔̋̂̑̈́̏̀̈͘̕͜͝s̸̫̝̮̻̼͐̅̄̎̎̑͝ț̷̨̢̨̻͈̮̞̆͗̓͊̃̌͂̑̉̕̕͜͝͝";
  210. foreach (var spacing in letterSpacing)
  211. {
  212. column.Item()
  213. .Text($"Letter spacing of {spacing} em")
  214. .FontSize(10)
  215. .Italic()
  216. .FontColor(Colors.Blue.Medium);
  217. column.Item()
  218. .PaddingVertical(50)
  219. .Text(paragraph)
  220. .FontSize(16)
  221. .FontFamily(Fonts.Calibri)
  222. .LetterSpacing(spacing);
  223. }
  224. });
  225. });
  226. }
  227. [Test]
  228. public void SuperscriptSubscript_Simple()
  229. {
  230. RenderingTest
  231. .Create()
  232. .PageSize(500, 500)
  233. .ProduceImages()
  234. .ShowResults()
  235. .Render(container =>
  236. {
  237. container
  238. .Padding(20)
  239. .MinimalBox()
  240. .Border(1)
  241. .Padding(20)
  242. .Text(text =>
  243. {
  244. text.DefaultTextStyle(x => x.FontSize(20));
  245. text.ParagraphSpacing(10);
  246. var highlight = TextStyle.Default.BackgroundColor(Colors.Green.Lighten3);
  247. text.Span("E=mc").Style(highlight);
  248. text.Span("2").Superscript().Style(highlight);
  249. text.Span(" is the equation of mass–energy equivalence.");
  250. text.EmptyLine();
  251. text.Span("H").Style(highlight);
  252. text.Span("2").Subscript().Style(highlight);
  253. text.Span("O").Style(highlight);
  254. text.Span(" is the chemical formula for water.");
  255. });
  256. });
  257. }
  258. [Test]
  259. public void SuperscriptSubscript_Effects()
  260. {
  261. RenderingTest
  262. .Create()
  263. .PageSize(800, 400)
  264. .ProduceImages()
  265. .ShowResults()
  266. .Render(container =>
  267. {
  268. container
  269. .Padding(25)
  270. .DefaultTextStyle(x => x.FontSize(30))
  271. .Column(column =>
  272. {
  273. column.Spacing(25);
  274. column.Item().Text(text =>
  275. {
  276. text.DefaultTextStyle(x => x.Underline());
  277. text.Span("Underline of the superscript (E = mc");
  278. text.Span("2").Superscript();
  279. text.Span(") should be at the same height as for normal text.");
  280. });
  281. column.Item().Text(text =>
  282. {
  283. text.DefaultTextStyle(x => x.Underline());
  284. text.Span("Underline of the subscript(H");
  285. text.Span("2").Subscript();
  286. text.Span("O) should be slightly lower than a normal text.");
  287. });
  288. column.Item().Text(text =>
  289. {
  290. text.DefaultTextStyle(x => x.Strikethrough());
  291. text.Span("Strikethrough of both superscript (E=mc");
  292. text.Span("2").Superscript();
  293. text.Span(") and subscript(H");
  294. text.Span("2").Subscript();
  295. text.Span("O) should be visible in the middle of the text.");
  296. });
  297. });
  298. });
  299. }
  300. [Test]
  301. public void ParagraphSpacing()
  302. {
  303. RenderingTest
  304. .Create()
  305. .PageSize(400, 500)
  306. .ProduceImages()
  307. .ShowResults()
  308. .Render(container =>
  309. {
  310. container
  311. .Padding(5)
  312. .MinimalBox()
  313. .Padding(10)
  314. .Text(Placeholders.Paragraphs())
  315. .ParagraphSpacing(10)
  316. .FontSize(16);
  317. });
  318. }
  319. [Test]
  320. public void ParagraphFirstLineIndentation()
  321. {
  322. RenderingTest
  323. .Create()
  324. .PageSize(400, 500)
  325. .ProduceImages()
  326. .ShowResults()
  327. .Render(container =>
  328. {
  329. container
  330. .Padding(5)
  331. .MinimalBox()
  332. .Padding(10)
  333. .Text(Placeholders.Paragraphs())
  334. .ParagraphFirstLineIndentation(20)
  335. .FontSize(16);
  336. });
  337. }
  338. [Test]
  339. public void CustomElement()
  340. {
  341. RenderingTest
  342. .Create()
  343. .PageSize(500, 200)
  344. .ProduceImages()
  345. .ShowResults()
  346. .Render(container =>
  347. {
  348. container
  349. .Padding(5)
  350. .MinimalBox()
  351. .Border(1)
  352. .Padding(10)
  353. .Text(text =>
  354. {
  355. text.DefaultTextStyle(TextStyle.Default.FontSize(25));
  356. text.Span("This is a random image aligned to the middle of the baseline: ");
  357. text.Element(TextInjectedElementAlignment.Middle)
  358. .Height(24)
  359. .Width(48)
  360. .Image(Placeholders.Image);
  361. text.Span(".");
  362. });
  363. });
  364. }
  365. [Test]
  366. public void TextElements()
  367. {
  368. RenderingTest
  369. .Create()
  370. .PageSize(PageSizes.A4)
  371. .ProducePdf()
  372. .ShowResults()
  373. .Render(container =>
  374. {
  375. container
  376. .Padding(20)
  377. .Padding(10)
  378. .MinimalBox()
  379. .Border(1)
  380. .Padding(5)
  381. .Padding(10)
  382. .Text(text =>
  383. {
  384. text.DefaultTextStyle(TextStyle.Default);
  385. text.AlignLeft();
  386. text.ParagraphSpacing(10);
  387. text.Line(Placeholders.LoremIpsum());
  388. 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();
  389. });
  390. });
  391. }
  392. [Test]
  393. public void Textcolumn()
  394. {
  395. RenderingTest
  396. .Create()
  397. .PageSize(PageSizes.A4)
  398. .ProducePdf()
  399. .ShowResults()
  400. .Render(container =>
  401. {
  402. container
  403. .Padding(20)
  404. .Padding(10)
  405. .MinimalBox()
  406. .Border(1)
  407. .Padding(5)
  408. .Padding(10)
  409. .Text(text =>
  410. {
  411. text.DefaultTextStyle(TextStyle.Default);
  412. text.AlignLeft();
  413. text.ParagraphSpacing(10);
  414. foreach (var i in Enumerable.Range(1, 100))
  415. text.Line($"{i}: {Placeholders.Paragraph()}");
  416. });
  417. });
  418. }
  419. [Test]
  420. public void SpaceIssue()
  421. {
  422. RenderingTest
  423. .Create()
  424. .PageSize(PageSizes.A4)
  425. .ProducePdf()
  426. .ShowResults()
  427. .Render(container =>
  428. {
  429. container
  430. .Padding(20)
  431. .Padding(10)
  432. .MinimalBox()
  433. .Border(1)
  434. .Padding(5)
  435. .Padding(10)
  436. .Text(text =>
  437. {
  438. text.DefaultTextStyle(x => x.Bold());
  439. text.DefaultTextStyle(TextStyle.Default);
  440. text.AlignLeft();
  441. text.ParagraphSpacing(10);
  442. text.Span(Placeholders.LoremIpsum());
  443. text.EmptyLine();
  444. text.Span("This text is a normal text, ");
  445. text.Span("this is a bold text, ").Bold();
  446. text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
  447. text.Span("and this is slightly bigger text.").FontSize(16);
  448. text.EmptyLine();
  449. text.Span("The new text element also supports injecting custom content between words: ");
  450. text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
  451. text.Span(".");
  452. text.EmptyLine();
  453. text.Span("This is page number ");
  454. text.CurrentPageNumber();
  455. text.Span(" out of ");
  456. text.TotalPages();
  457. text.EmptyLine();
  458. text.Hyperlink("Please visit QuestPDF website", "https://www.questpdf.com");
  459. text.EmptyLine();
  460. text.Span(Placeholders.Paragraphs());
  461. text.EmptyLine();
  462. text.Span(Placeholders.Paragraphs()).Italic();
  463. text.Line("This is target text that does not show up. " + Placeholders.Paragraph());
  464. });
  465. });
  466. }
  467. [Test]
  468. public void HugeList()
  469. {
  470. RenderingTest
  471. .Create()
  472. .PageSize(PageSizes.A4)
  473. .ProducePdf()
  474. .ShowResults()
  475. .Render(container =>
  476. {
  477. container
  478. .Padding(20)
  479. .Padding(10)
  480. .MinimalBox()
  481. .Border(1)
  482. .Padding(5)
  483. .Padding(10)
  484. .Text(text =>
  485. {
  486. text.DefaultTextStyle(TextStyle.Default.FontSize(20).BackgroundColor(Colors.Red.Lighten4));
  487. text.AlignLeft();
  488. text.ParagraphSpacing(10);
  489. text.Span("This text is a normal text, ");
  490. text.Span("this is a bold text, ").Bold();
  491. text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
  492. text.Span("and this is slightly bigger text.").FontSize(24);
  493. text.Span("The new text element also supports injecting custom content between words: ");
  494. text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
  495. text.Span(".");
  496. text.EmptyLine();
  497. foreach (var i in Enumerable.Range(1, 100))
  498. {
  499. text.Line($"{i}: {Placeholders.Paragraph()}");
  500. text.Hyperlink("Please visit QuestPDF website. ", "https://www.questpdf.com");
  501. text.Span("This is page number ");
  502. text.CurrentPageNumber();
  503. text.Span(" out of ");
  504. text.TotalPages();
  505. text.EmptyLine();
  506. }
  507. });
  508. });
  509. }
  510. [Test]
  511. public void MeasureIssueWhenSpaceAtLineEnd()
  512. {
  513. // issue 135
  514. RenderingTest
  515. .Create()
  516. .ProduceImages()
  517. .ShowResults()
  518. .RenderDocument(container =>
  519. {
  520. container.Page(page =>
  521. {
  522. page.Margin(50);
  523. page.PageColor(Colors.White);
  524. page.Size(PageSizes.A4);
  525. 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);
  526. });
  527. });
  528. }
  529. [Test]
  530. public void EmptyText()
  531. {
  532. // issue 135
  533. RenderingTest
  534. .Create()
  535. .ProduceImages()
  536. .ShowResults()
  537. .RenderDocument(container =>
  538. {
  539. container.Page(page =>
  540. {
  541. page.Margin(50);
  542. page.PageColor(Colors.White);
  543. page.Size(PageSizes.A4);
  544. page.Content().Text(" ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
  545. });
  546. });
  547. }
  548. [Test]
  549. public void Whitespaces()
  550. {
  551. // issue 135
  552. RenderingTest
  553. .Create()
  554. .ProduceImages()
  555. .ShowResults()
  556. .RenderDocument(container =>
  557. {
  558. container.Page(page =>
  559. {
  560. page.Margin(50);
  561. page.PageColor(Colors.White);
  562. page.Size(PageSizes.A4);
  563. page.Content().Text(" x ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
  564. });
  565. });
  566. }
  567. [Test]
  568. public void DrawingNullTextShouldNotThrowException()
  569. {
  570. RenderingTest
  571. .Create()
  572. .ProduceImages()
  573. .ShowResults()
  574. .RenderDocument(container =>
  575. {
  576. container.Page(page =>
  577. {
  578. page.Margin(50);
  579. page.PageColor(Colors.White);
  580. page.Size(PageSizes.A4);
  581. page.Content().Column(column =>
  582. {
  583. column.Item().Text((string) null);
  584. column.Item().Text(text =>
  585. {
  586. text.Span(null);
  587. text.Line(null);
  588. text.Hyperlink(null, "http://www.questpdf.com");
  589. text.TotalPages().Format(x => null);
  590. });
  591. });
  592. });
  593. });
  594. }
  595. [Test]
  596. public void BreakingLongWord()
  597. {
  598. RenderingTest
  599. .Create()
  600. .ProduceImages()
  601. .ShowResults()
  602. .RenderDocument(container =>
  603. {
  604. container.Page(page =>
  605. {
  606. page.Margin(50);
  607. page.PageColor(Colors.White);
  608. page.Size(PageSizes.A4);
  609. page.Content().Column(column =>
  610. {
  611. column.Item().Text((string) null);
  612. column.Item().Text(text =>
  613. {
  614. text.DefaultTextStyle(x => x.BackgroundColor(Colors.Red.Lighten3).FontSize(24));
  615. text.Span(" " + Placeholders.LoremIpsum());
  616. text.Span(" 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 ").WrapAnywhere();
  617. });
  618. });
  619. });
  620. });
  621. }
  622. [Test]
  623. public void TextShaping_Unicode()
  624. {
  625. RenderingTest
  626. .Create()
  627. .PageSize(600, 100)
  628. .ProduceImages()
  629. .ShowResults()
  630. .Render(container =>
  631. {
  632. container
  633. .Padding(35)
  634. .MinimalBox()
  635. .Background(Colors.Grey.Lighten2)
  636. .Text(text =>
  637. {
  638. text.DefaultTextStyle(TextStyle.Default.FontSize(20));
  639. text.Span("Complex Unicode structure: ");
  640. text.Span("T̶̖̔͆͆̽̔ḩ̷̼̫̐̈́̀͜͝͝ì̶͇̤͓̱̣͇͓͉̎s̵̡̟̹͍̜͉̗̾͛̈̐́͋͂͝͠ͅ ̴̨͙͍͇̭̒͗̀́͝ì̷̡̺͉̼̏̏̉̌͝s̷͍͙̗̰̖͙̈̑̂̔͑͊̌̓̊̇͜ ̶̛̼͚͊̅͘ṭ̷̨̘̣̙̖͉͌̏̂̅͑̄̽̕͝ȅ̶̲̲̙̭͈̬̣͔̝͔̈́͝s̸̢̯̪̫͓̭̮̓̀͆͜ț̸̢͉̞̥̤̏̌̓͝").FontFamily(Fonts.Calibri).FontColor(Colors.Red.Medium);
  641. text.Span(".");
  642. });
  643. });
  644. }
  645. [Test]
  646. public void TextShaping_Arabic()
  647. {
  648. RenderingTest
  649. .Create()
  650. .PageSize(250, 100)
  651. .ProduceImages()
  652. .ShowResults()
  653. .Render(container =>
  654. {
  655. container
  656. .Padding(25)
  657. .MinimalBox()
  658. .Background(Colors.Grey.Lighten2)
  659. .Text("خوارزمية ترتيب")
  660. .FontFamily(Fonts.Calibri)
  661. .FontSize(30);
  662. });
  663. }
  664. [Test]
  665. public void FontFallback()
  666. {
  667. RenderingTest
  668. .Create()
  669. .ProducePdf()
  670. .ShowResults()
  671. .RenderDocument(container =>
  672. {
  673. container.Page(page =>
  674. {
  675. page.Margin(50);
  676. page.PageColor(Colors.White);
  677. page.DefaultTextStyle(x => x
  678. .Fallback(y => y.FontFamily("Segoe UI Emoji")
  679. .Fallback(y => y.FontFamily("Microsoft YaHei"))));
  680. page.Size(PageSizes.A4);
  681. page.Content().Text(t =>
  682. {
  683. t.Line("This is normal text.");
  684. t.EmptyLine();
  685. t.Line("Following line should use font fallback:");
  686. t.Line("中文文本");
  687. t.EmptyLine();
  688. t.Line("The following line contains a mix of known and unknown characters.");
  689. t.Line("Mixed line: This 中文 is 文文 a mixed 本 本 line 本 中文文本!");
  690. t.EmptyLine();
  691. t.Line("Emojis work out of the box because of font fallback: 😊😅🥳👍❤😍👌");
  692. });
  693. });
  694. });
  695. }
  696. [Test]
  697. public void WordWrappingStability()
  698. {
  699. // instruction: check if any characters repeat when performing the word-wrapping algorithm
  700. RenderingTest
  701. .Create()
  702. .PageSize(PageSizes.A4)
  703. .ProducePdf()
  704. .ShowResults()
  705. .Render(container =>
  706. {
  707. var text = "Lorem ipsum dolor sit amet consectetuer";
  708. container
  709. .Padding(20)
  710. .Column(column =>
  711. {
  712. column.Spacing(10);
  713. foreach (var width in Enumerable.Range(25, 200))
  714. {
  715. column
  716. .Item()
  717. .MaxWidth(width)
  718. .Background(Colors.Grey.Lighten3)
  719. .Text(text);
  720. }
  721. });
  722. });
  723. }
  724. [Test]
  725. public void AdvancedLanguagesSupport()
  726. {
  727. RenderingTest
  728. .Create()
  729. .PageSize(new PageSize(400, 400))
  730. .ProduceImages()
  731. .ShowResults()
  732. .Render(container =>
  733. {
  734. var text = "في المعلوماتية أو الرياضيات، خوارزمية الترتيب هي خوارزمية تمكن من تنظيم مجموعة عناصر حسب ترتيب محدد.";
  735. container
  736. .Padding(20)
  737. .ContentFromRightToLeft()
  738. .Text(text)
  739. .FontFamily(Fonts.Calibri)
  740. .FontSize(22);
  741. });
  742. }
  743. [Test]
  744. public void WordWrappingWhenRightToLeft()
  745. {
  746. RenderingTest
  747. .Create()
  748. .PageSize(new PageSize(1000, 500))
  749. .ProducePdf()
  750. .ShowResults()
  751. .Render(container =>
  752. {
  753. var text = "في المعلوماتية أو الرياضيات، خوارزمية الترتيب هي خوارزمية تمكن من تنظيم مجموعة عناصر حسب ترتيب محدد.";
  754. container
  755. .Padding(25)
  756. .ContentFromRightToLeft()
  757. .Column(column =>
  758. {
  759. column.Spacing(20);
  760. foreach (var size in new[] { 36, 34, 32, 30, 15 })
  761. {
  762. column
  763. .Item()
  764. .ShowEntire()
  765. .MaxWidth(size * 25)
  766. .Background(Colors.Grey.Lighten3)
  767. .MinimalBox()
  768. .Background(Colors.Grey.Lighten2)
  769. .Text(text)
  770. .FontSize(20)
  771. .FontFamily("Segoe UI");
  772. }
  773. });
  774. });
  775. }
  776. [Test]
  777. public void ForcingTextDirection()
  778. {
  779. RenderingTest
  780. .Create()
  781. .PageSize(new PageSize(1000, 500))
  782. .ProduceImages()
  783. .ShowResults()
  784. .Render(container =>
  785. {
  786. container
  787. .Padding(10)
  788. .DefaultTextStyle(x => x.FontSize(24).FontFamily("Calibri"))
  789. .Column(column =>
  790. {
  791. column.Spacing(10);
  792. var word = "الجوريتم";
  793. var definition = "algorithm in Arabic";
  794. var text = $"{word} - {definition}";
  795. // text direction is automatically detected using the first word
  796. column.Item().Text(text);
  797. // it is possible to force specific content direction
  798. column.Item().Text(text).DirectionFromLeftToRight();
  799. column.Item().Text(text).DirectionFromRightToLeft();
  800. // to combine text in various content directions, split it into segments
  801. column.Item().Text(text =>
  802. {
  803. text.Span(word);
  804. text.Span(" - ");
  805. text.Span(definition);
  806. });
  807. });
  808. });
  809. }
  810. [Test]
  811. public void InconsistentLineHeight()
  812. {
  813. RenderingTest
  814. .Create()
  815. .PageSize(new PageSize(1000, 100))
  816. .ProducePdf()
  817. .ShowResults()
  818. .Render(container =>
  819. {
  820. container
  821. .Padding(20)
  822. .Scale(1 / 5f)
  823. .DefaultTextStyle(TextStyle.Default.FontFamily("Lato").FontSize(7f).Bold())
  824. .Row(row =>
  825. {
  826. var text = Placeholders.Paragraph();
  827. var i = 0;
  828. while(true)
  829. {
  830. var lineHeight = 0.7f + i * 0.01f;
  831. i++;
  832. if (lineHeight > 1.5f)
  833. break;
  834. row.RelativeItem()
  835. .ShrinkVertical()
  836. .Background(Placeholders.BackgroundColor())
  837. .Text($"{i}\n{text}")
  838. .LineHeight(lineHeight);
  839. }
  840. });
  841. });
  842. }
  843. [Test]
  844. public void FontFallback_Nested()
  845. {
  846. RenderingTest
  847. .Create()
  848. .ProduceImages()
  849. .ShowResults()
  850. .RenderDocument(container =>
  851. {
  852. container.Page(page =>
  853. {
  854. page.Margin(50);
  855. page.PageColor(Colors.White);
  856. page.Size(PageSizes.A5.Landscape());
  857. page.DefaultTextStyle(x => x
  858. .FontSize(24)
  859. .Bold()
  860. .FontFamily("Times New Roman"));
  861. page.Content().Text(text =>
  862. {
  863. text.Line("Default times new roman 中文文本 text.");
  864. text.Line("Normal weight and green 中文文本 text.").NormalWeight().BackgroundColor(Colors.Green.Lighten2);
  865. text.Line("Strikethrough without underline 中文文本 text.").Strikethrough().Underline(false);
  866. text.Line("Lato italic 中文文本 text.").FontFamily("Lato").Italic();
  867. });
  868. });
  869. });
  870. }
  871. [Test]
  872. public void TextShouldInheritAlignment()
  873. {
  874. RenderingTest
  875. .Create()
  876. .ProduceImages()
  877. .ShowResults()
  878. .PageSize(PageSizes.A4)
  879. .Render(container =>
  880. {
  881. var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
  882. container.Padding(20).Width(400).Column(column =>
  883. {
  884. column.Spacing(20);
  885. column.Item().Background(Colors.Grey.Lighten3).AlignLeft().Text(text);
  886. column.Item().Background(Colors.Grey.Lighten3).AlignCenter().Text(text);
  887. column.Item().Background(Colors.Grey.Lighten3).AlignRight().Text(text);
  888. });
  889. });
  890. }
  891. [Test]
  892. public void LongPageableText()
  893. {
  894. var longText = string.Join("\n", Enumerable.Range(0, 100).Select(_ => Placeholders.Paragraph()));
  895. RenderingTest
  896. .Create()
  897. .ProducePdf()
  898. .ShowResults()
  899. .PageSize(PageSizes.A4)
  900. .Render(container =>
  901. {
  902. container.Padding(25).Background(Colors.Grey.Lighten3).AlignLeft().Text(longText);
  903. });
  904. }
  905. [Test]
  906. public void TextAlignment()
  907. {
  908. RenderingTest
  909. .Create()
  910. .ProduceImages()
  911. .ShowResults()
  912. .PageSize(500, 800)
  913. .Render(content =>
  914. {
  915. var text = Placeholders.Paragraph();
  916. content.Padding(20).Column(column =>
  917. {
  918. column.Spacing(20);
  919. IContainer BlockStyle(IContainer container) => container.Background(Colors.Grey.Lighten3).Padding(10);
  920. column.Item().Text("Left alignment").Bold();
  921. column.Item().Element(BlockStyle).Text(text).AlignLeft();
  922. column.Item().Text("Center alignment").Bold();
  923. column.Item().Element(BlockStyle).Text(text).AlignCenter();
  924. column.Item().Text("Right alignment").Bold();
  925. column.Item().Element(BlockStyle).Text(text).AlignRight();
  926. column.Item().Text("Start alignment").Bold();
  927. column.Item().Element(BlockStyle).Text(text).AlignStart();
  928. column.Item().Text("End alignment").Bold();
  929. column.Item().Element(BlockStyle).Text(text).AlignEnd();
  930. column.Item().Text("Justify alignment").Bold();
  931. column.Item().Element(BlockStyle).Text(text).Justify();
  932. });
  933. });
  934. }
  935. [Test]
  936. public void TextAlignmentComplex()
  937. {
  938. RenderingTest
  939. .Create()
  940. .ProducePdf()
  941. .ShowResults()
  942. .PageSize(PageSizes.A4)
  943. .Render(container =>
  944. {
  945. container.Padding(25).Column(column =>
  946. {
  947. column.Spacing(25);
  948. foreach (var contentDirection in Enum.GetValues<ContentDirection>())
  949. {
  950. column.Item().Text(contentDirection.ToString()).FontSize(20).Bold();
  951. foreach (var horizontalAlignment in Enum.GetValues<HorizontalAlignment>())
  952. foreach (var textHorizontalAlignment in Enum.GetValues<TextHorizontalAlignment>())
  953. column.Item().Element(GenerateTextCase(contentDirection, horizontalAlignment, textHorizontalAlignment));
  954. }
  955. });
  956. Action<IContainer> GenerateTextCase(ContentDirection contentDirection, HorizontalAlignment horizontalAlignment, TextHorizontalAlignment textAlignment)
  957. {
  958. return container =>
  959. {
  960. container
  961. .Element(element =>
  962. {
  963. return contentDirection switch
  964. {
  965. ContentDirection.LeftToRight => element.ContentFromLeftToRight(),
  966. ContentDirection.RightToLeft => element.ContentFromRightToLeft(),
  967. _ => throw new Exception()
  968. };
  969. })
  970. .Element(element =>
  971. {
  972. return horizontalAlignment switch
  973. {
  974. HorizontalAlignment.Left => element.AlignLeft(),
  975. HorizontalAlignment.Center => element.AlignCenter(),
  976. HorizontalAlignment.Right => element.AlignRight(),
  977. _ => throw new Exception()
  978. };
  979. })
  980. .Background(Colors.Grey.Lighten3)
  981. .Width(200)
  982. .Padding(10)
  983. .Text(text =>
  984. {
  985. if (textAlignment == TextHorizontalAlignment.Left)
  986. text.AlignLeft();
  987. else if (textAlignment == TextHorizontalAlignment.Center)
  988. text.AlignCenter();
  989. else if (textAlignment == TextHorizontalAlignment.Right)
  990. text.AlignRight();
  991. else if (textAlignment == TextHorizontalAlignment.Justify)
  992. text.Justify();
  993. else if (textAlignment == TextHorizontalAlignment.Start)
  994. text.AlignStart();
  995. else if (textAlignment == TextHorizontalAlignment.End)
  996. text.AlignEnd();
  997. text.Span($"{horizontalAlignment.ToString()} - {textAlignment.ToString()}");
  998. });
  999. };
  1000. }
  1001. });
  1002. }
  1003. [Test]
  1004. public void ClampLines()
  1005. {
  1006. RenderingTest
  1007. .Create()
  1008. .PageSize(300, 80)
  1009. .ProduceImages()
  1010. .ShowResults()
  1011. .Render(container =>
  1012. {
  1013. container
  1014. .Padding(10)
  1015. .Text(Placeholders.Paragraph())
  1016. .ClampLines(3, " [...]");
  1017. });
  1018. }
  1019. }
  1020. }