TextExamples.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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. [Test]
  1020. public void FontFeaturesTest()
  1021. {
  1022. RenderingTest
  1023. .Create()
  1024. .PageSize(500, 150)
  1025. .ProducePdf()
  1026. .ShowResults()
  1027. .Render(container =>
  1028. {
  1029. container.Padding(25).Row(row =>
  1030. {
  1031. row.Spacing(25);
  1032. row.RelativeItem().Column(column =>
  1033. {
  1034. column.Item().Text("Without ligatures").FontSize(16);
  1035. column.Item().Text("fly and fight").FontSize(32).DisableFontFeature(FontFeatures.StandardLigatures);
  1036. });
  1037. row.RelativeItem().Column(column =>
  1038. {
  1039. column.Item().Text("With ligatures").FontSize(16);
  1040. column.Item().Text("fly and fight").FontSize(32).EnableFontFeature(FontFeatures.StandardLigatures);
  1041. });
  1042. });
  1043. });
  1044. }
  1045. }
  1046. }