TextExamples.cs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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.1f, 0f, 0.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[] { -0.1f, 0f, 0.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, 0.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(25));
  340. text.Span("This is a random image aligned to the middle of the baseline: ");
  341. text.Element(TextInjectedElementAlignment.Middle)
  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(24);
  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. .ProducePdf()
  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. [Test]
  883. public void TextAlignment()
  884. {
  885. RenderingTest
  886. .Create()
  887. .ProducePdf()
  888. .ShowResults()
  889. .PageSize(PageSizes.A4)
  890. .Render(container =>
  891. {
  892. container.Padding(25).Column(column =>
  893. {
  894. column.Spacing(25);
  895. foreach (var contentDirection in Enum.GetValues<ContentDirection>())
  896. {
  897. column.Item().Text(contentDirection.ToString()).FontSize(20).Bold();
  898. foreach (var horizontalAlignment in Enum.GetValues<HorizontalAlignment>())
  899. foreach (var textHorizontalAlignment in Enum.GetValues<TextHorizontalAlignment>())
  900. column.Item().Element(GenerateTextCase(contentDirection, horizontalAlignment, textHorizontalAlignment));
  901. }
  902. });
  903. Action<IContainer> GenerateTextCase(ContentDirection contentDirection, HorizontalAlignment horizontalAlignment, TextHorizontalAlignment textAlignment)
  904. {
  905. return container =>
  906. {
  907. container
  908. .Element(element =>
  909. {
  910. return contentDirection switch
  911. {
  912. ContentDirection.LeftToRight => element.ContentFromLeftToRight(),
  913. ContentDirection.RightToLeft => element.ContentFromRightToLeft(),
  914. _ => throw new Exception()
  915. };
  916. })
  917. .Element(element =>
  918. {
  919. return horizontalAlignment switch
  920. {
  921. HorizontalAlignment.Left => element.AlignLeft(),
  922. HorizontalAlignment.Center => element.AlignCenter(),
  923. HorizontalAlignment.Right => element.AlignRight(),
  924. _ => throw new Exception()
  925. };
  926. })
  927. .Background(Colors.Grey.Lighten3)
  928. .Width(200)
  929. .Padding(10)
  930. .Text(text =>
  931. {
  932. if (textAlignment == TextHorizontalAlignment.Left)
  933. text.AlignLeft();
  934. else if (textAlignment == TextHorizontalAlignment.Center)
  935. text.AlignCenter();
  936. else if (textAlignment == TextHorizontalAlignment.Right)
  937. text.AlignRight();
  938. else if (textAlignment == TextHorizontalAlignment.Justify)
  939. text.Justify();
  940. else if (textAlignment == TextHorizontalAlignment.Start)
  941. text.AlignStart();
  942. else if (textAlignment == TextHorizontalAlignment.End)
  943. text.AlignEnd();
  944. text.Span($"{horizontalAlignment.ToString()} - {textAlignment.ToString()}");
  945. });
  946. };
  947. }
  948. });
  949. }
  950. [Test]
  951. public void ClampLines()
  952. {
  953. RenderingTest
  954. .Create()
  955. .PageSize(300, 100)
  956. .ProduceImages()
  957. .ShowResults()
  958. .Render(container =>
  959. {
  960. container
  961. .Padding(5)
  962. .MinimalBox()
  963. .Border(1)
  964. .Padding(10)
  965. .Text(Placeholders.Paragraph())
  966. .ClampLines(2, " [...]");
  967. });
  968. }
  969. }
  970. }