TextExamples.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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(20));
  340. text.Span("This is a random image aligned to the baseline: ");
  341. text.Element()
  342. .PaddingBottom(-6)
  343. .Height(24)
  344. .Width(48)
  345. .Image(Placeholders.Image);
  346. text.Span(".");
  347. });
  348. });
  349. }
  350. [Test]
  351. public void TextElements()
  352. {
  353. RenderingTest
  354. .Create()
  355. .PageSize(PageSizes.A4)
  356. .ProducePdf()
  357. .ShowResults()
  358. .Render(container =>
  359. {
  360. container
  361. .Padding(20)
  362. .Padding(10)
  363. .MinimalBox()
  364. .Border(1)
  365. .Padding(5)
  366. .Padding(10)
  367. .Text(text =>
  368. {
  369. text.DefaultTextStyle(TextStyle.Default);
  370. text.AlignLeft();
  371. text.ParagraphSpacing(10);
  372. text.Line(Placeholders.LoremIpsum());
  373. 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();
  374. });
  375. });
  376. }
  377. [Test]
  378. public void Textcolumn()
  379. {
  380. RenderingTest
  381. .Create()
  382. .PageSize(PageSizes.A4)
  383. .ProducePdf()
  384. .ShowResults()
  385. .Render(container =>
  386. {
  387. container
  388. .Padding(20)
  389. .Padding(10)
  390. .MinimalBox()
  391. .Border(1)
  392. .Padding(5)
  393. .Padding(10)
  394. .Text(text =>
  395. {
  396. text.DefaultTextStyle(TextStyle.Default);
  397. text.AlignLeft();
  398. text.ParagraphSpacing(10);
  399. foreach (var i in Enumerable.Range(1, 100))
  400. text.Line($"{i}: {Placeholders.Paragraph()}");
  401. });
  402. });
  403. }
  404. [Test]
  405. public void SpaceIssue()
  406. {
  407. RenderingTest
  408. .Create()
  409. .PageSize(PageSizes.A4)
  410. .ProducePdf()
  411. .ShowResults()
  412. .Render(container =>
  413. {
  414. container
  415. .Padding(20)
  416. .Padding(10)
  417. .MinimalBox()
  418. .Border(1)
  419. .Padding(5)
  420. .Padding(10)
  421. .Text(text =>
  422. {
  423. text.DefaultTextStyle(x => x.Bold());
  424. text.DefaultTextStyle(TextStyle.Default);
  425. text.AlignLeft();
  426. text.ParagraphSpacing(10);
  427. text.Span(Placeholders.LoremIpsum());
  428. text.EmptyLine();
  429. text.Span("This text is a normal text, ");
  430. text.Span("this is a bold text, ").Bold();
  431. text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
  432. text.Span("and this is slightly bigger text.").FontSize(16);
  433. text.EmptyLine();
  434. text.Span("The new text element also supports injecting custom content between words: ");
  435. text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
  436. text.Span(".");
  437. text.EmptyLine();
  438. text.Span("This is page number ");
  439. text.CurrentPageNumber();
  440. text.Span(" out of ");
  441. text.TotalPages();
  442. text.EmptyLine();
  443. text.Hyperlink("Please visit QuestPDF website", "https://www.questpdf.com");
  444. text.EmptyLine();
  445. text.Span(Placeholders.Paragraphs());
  446. text.EmptyLine();
  447. text.Span(Placeholders.Paragraphs()).Italic();
  448. text.Line("This is target text that does not show up. " + Placeholders.Paragraph());
  449. });
  450. });
  451. }
  452. [Test]
  453. public void HugeList()
  454. {
  455. RenderingTest
  456. .Create()
  457. .PageSize(PageSizes.A4)
  458. .ProducePdf()
  459. .ShowResults()
  460. .Render(container =>
  461. {
  462. container
  463. .Padding(20)
  464. .Padding(10)
  465. .MinimalBox()
  466. .Border(1)
  467. .Padding(5)
  468. .Padding(10)
  469. .Text(text =>
  470. {
  471. text.DefaultTextStyle(TextStyle.Default.FontSize(20).BackgroundColor(Colors.Red.Lighten4));
  472. text.AlignLeft();
  473. text.ParagraphSpacing(10);
  474. text.Span("This text is a normal text, ");
  475. text.Span("this is a bold text, ").Bold();
  476. text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
  477. text.Span("and this is slightly bigger text.").FontSize(16);
  478. text.Span("The new text element also supports injecting custom content between words: ");
  479. text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
  480. text.Span(".");
  481. text.EmptyLine();
  482. foreach (var i in Enumerable.Range(1, 100))
  483. {
  484. text.Line($"{i}: {Placeholders.Paragraph()}");
  485. text.Hyperlink("Please visit QuestPDF website. ", "https://www.questpdf.com");
  486. text.Span("This is page number ");
  487. text.CurrentPageNumber();
  488. text.Span(" out of ");
  489. text.TotalPages();
  490. text.EmptyLine();
  491. }
  492. });
  493. });
  494. }
  495. [Test]
  496. public void MeasureIssueWhenSpaceAtLineEnd()
  497. {
  498. // issue 135
  499. RenderingTest
  500. .Create()
  501. .ProduceImages()
  502. .ShowResults()
  503. .RenderDocument(container =>
  504. {
  505. container.Page(page =>
  506. {
  507. page.Margin(50);
  508. page.PageColor(Colors.White);
  509. page.Size(PageSizes.A4);
  510. 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);
  511. });
  512. });
  513. }
  514. [Test]
  515. public void EmptyText()
  516. {
  517. // issue 135
  518. RenderingTest
  519. .Create()
  520. .ProduceImages()
  521. .ShowResults()
  522. .RenderDocument(container =>
  523. {
  524. container.Page(page =>
  525. {
  526. page.Margin(50);
  527. page.PageColor(Colors.White);
  528. page.Size(PageSizes.A4);
  529. page.Content().Text(" ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
  530. });
  531. });
  532. }
  533. [Test]
  534. public void Whitespaces()
  535. {
  536. // issue 135
  537. RenderingTest
  538. .Create()
  539. .ProduceImages()
  540. .ShowResults()
  541. .RenderDocument(container =>
  542. {
  543. container.Page(page =>
  544. {
  545. page.Margin(50);
  546. page.PageColor(Colors.White);
  547. page.Size(PageSizes.A4);
  548. page.Content().Text(" x ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
  549. });
  550. });
  551. }
  552. [Test]
  553. public void DrawingNullTextShouldNotThrowException()
  554. {
  555. RenderingTest
  556. .Create()
  557. .ProduceImages()
  558. .ShowResults()
  559. .RenderDocument(container =>
  560. {
  561. container.Page(page =>
  562. {
  563. page.Margin(50);
  564. page.PageColor(Colors.White);
  565. page.Size(PageSizes.A4);
  566. page.Content().Column(column =>
  567. {
  568. column.Item().Text((string) null);
  569. column.Item().Text(text =>
  570. {
  571. text.Span(null);
  572. text.Line(null);
  573. text.Hyperlink(null, "http://www.questpdf.com");
  574. text.TotalPages().Format(x => null);
  575. });
  576. });
  577. });
  578. });
  579. }
  580. [Test]
  581. public void BreakingLongWord()
  582. {
  583. RenderingTest
  584. .Create()
  585. .ProduceImages()
  586. .ShowResults()
  587. .RenderDocument(container =>
  588. {
  589. container.Page(page =>
  590. {
  591. page.Margin(50);
  592. page.PageColor(Colors.White);
  593. page.Size(PageSizes.A4);
  594. page.Content().Column(column =>
  595. {
  596. column.Item().Text((string) null);
  597. column.Item().Text(text =>
  598. {
  599. text.DefaultTextStyle(x => x.BackgroundColor(Colors.Red.Lighten3).FontSize(24));
  600. text.Span(" " + Placeholders.LoremIpsum());
  601. text.Span(" 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 ").WrapAnywhere();
  602. });
  603. });
  604. });
  605. });
  606. }
  607. [Test]
  608. public void TextShaping_Unicode()
  609. {
  610. RenderingTest
  611. .Create()
  612. .PageSize(600, 100)
  613. .ProduceImages()
  614. .ShowResults()
  615. .Render(container =>
  616. {
  617. container
  618. .Padding(35)
  619. .MinimalBox()
  620. .Background(Colors.Grey.Lighten2)
  621. .Text(text =>
  622. {
  623. text.DefaultTextStyle(TextStyle.Default.FontSize(20));
  624. text.Span("Complex Unicode structure: ");
  625. text.Span("T̶̖̔͆͆̽̔ḩ̷̼̫̐̈́̀͜͝͝ì̶͇̤͓̱̣͇͓͉̎s̵̡̟̹͍̜͉̗̾͛̈̐́͋͂͝͠ͅ ̴̨͙͍͇̭̒͗̀́͝ì̷̡̺͉̼̏̏̉̌͝s̷͍͙̗̰̖͙̈̑̂̔͑͊̌̓̊̇͜ ̶̛̼͚͊̅͘ṭ̷̨̘̣̙̖͉͌̏̂̅͑̄̽̕͝ȅ̶̲̲̙̭͈̬̣͔̝͔̈́͝s̸̢̯̪̫͓̭̮̓̀͆͜ț̸̢͉̞̥̤̏̌̓͝").FontFamily(Fonts.Calibri).FontColor(Colors.Red.Medium);
  626. text.Span(".");
  627. });
  628. });
  629. }
  630. [Test]
  631. public void TextShaping_Arabic()
  632. {
  633. RenderingTest
  634. .Create()
  635. .PageSize(250, 100)
  636. .ProduceImages()
  637. .ShowResults()
  638. .Render(container =>
  639. {
  640. container
  641. .Padding(25)
  642. .MinimalBox()
  643. .Background(Colors.Grey.Lighten2)
  644. .Text("خوارزمية ترتيب")
  645. .FontFamily(Fonts.Calibri)
  646. .FontSize(30);
  647. });
  648. }
  649. [Test]
  650. public void FontFallback()
  651. {
  652. RenderingTest
  653. .Create()
  654. .ProduceImages()
  655. .ShowResults()
  656. .RenderDocument(container =>
  657. {
  658. container.Page(page =>
  659. {
  660. page.Margin(50);
  661. page.PageColor(Colors.White);
  662. page.DefaultTextStyle(x => x
  663. .Fallback(y => y.FontFamily("Segoe UI Emoji")
  664. .Fallback(y => y.FontFamily("Microsoft YaHei"))));
  665. page.Size(PageSizes.A4);
  666. page.Content().Text(t =>
  667. {
  668. t.Line("This is normal text.");
  669. t.EmptyLine();
  670. t.Line("Following line should use font fallback:");
  671. t.Line("中文文本");
  672. t.EmptyLine();
  673. t.Line("The following line contains a mix of known and unknown characters.");
  674. t.Line("Mixed line: This 中文 is 文文 a mixed 本 本 line 本 中文文本!");
  675. t.EmptyLine();
  676. t.Line("Emojis work out of the box because of font fallback: 😊😅🥳👍❤😍👌");
  677. });
  678. });
  679. });
  680. }
  681. [Test]
  682. public void WordWrappingStability()
  683. {
  684. // instruction: check if any characters repeat when performing the word-wrapping algorithm
  685. RenderingTest
  686. .Create()
  687. .PageSize(PageSizes.A4)
  688. .ProducePdf()
  689. .ShowResults()
  690. .Render(container =>
  691. {
  692. var text = "Lorem ipsum dolor sit amet consectetuer";
  693. container
  694. .Padding(20)
  695. .Column(column =>
  696. {
  697. column.Spacing(10);
  698. foreach (var width in Enumerable.Range(25, 200))
  699. {
  700. column
  701. .Item()
  702. .MaxWidth(width)
  703. .Background(Colors.Grey.Lighten3)
  704. .Text(text);
  705. }
  706. });
  707. });
  708. }
  709. [Test]
  710. public void AdvancedLanguagesSupport()
  711. {
  712. RenderingTest
  713. .Create()
  714. .PageSize(new PageSize(400, 400))
  715. .ProduceImages()
  716. .ShowResults()
  717. .Render(container =>
  718. {
  719. var text = "في المعلوماتية أو الرياضيات، خوارزمية الترتيب هي خوارزمية تمكن من تنظيم مجموعة عناصر حسب ترتيب محدد.";
  720. container
  721. .Padding(20)
  722. .ContentFromRightToLeft()
  723. .Text(text)
  724. .FontFamily(Fonts.Calibri)
  725. .FontSize(22);
  726. });
  727. }
  728. [Test]
  729. public void WordWrappingWhenRightToLeft()
  730. {
  731. RenderingTest
  732. .Create()
  733. .PageSize(new PageSize(1000, 500))
  734. .ProducePdf()
  735. .ShowResults()
  736. .Render(container =>
  737. {
  738. var text = "في المعلوماتية أو الرياضيات، خوارزمية الترتيب هي خوارزمية تمكن من تنظيم مجموعة عناصر حسب ترتيب محدد.";
  739. container
  740. .Padding(25)
  741. .ContentFromRightToLeft()
  742. .Column(column =>
  743. {
  744. column.Spacing(20);
  745. foreach (var size in new[] { 36, 34, 32, 30, 15 })
  746. {
  747. column
  748. .Item()
  749. .ShowEntire()
  750. .MaxWidth(size * 25)
  751. .Background(Colors.Grey.Lighten3)
  752. .MinimalBox()
  753. .Background(Colors.Grey.Lighten2)
  754. .Text(text)
  755. .FontSize(20)
  756. .FontFamily("Segoe UI");
  757. }
  758. });
  759. });
  760. }
  761. [Test]
  762. public void ForcingTextDirection()
  763. {
  764. RenderingTest
  765. .Create()
  766. .PageSize(new PageSize(1000, 500))
  767. .ProduceImages()
  768. .ShowResults()
  769. .Render(container =>
  770. {
  771. container
  772. .Padding(10)
  773. .DefaultTextStyle(x => x.FontSize(24).FontFamily("Calibri"))
  774. .Column(column =>
  775. {
  776. column.Spacing(10);
  777. var word = "الجوريتم";
  778. var definition = "algorithm in Arabic";
  779. var text = $"{word} - {definition}";
  780. // text direction is automatically detected using the first word
  781. column.Item().Text(text);
  782. // it is possible to force specific content direction
  783. column.Item().Text(text).DirectionFromLeftToRight();
  784. column.Item().Text(text).DirectionFromRightToLeft();
  785. // to combine text in various content directions, split it into segments
  786. column.Item().Text(text =>
  787. {
  788. text.Span(word);
  789. text.Span(" - ");
  790. text.Span(definition);
  791. });
  792. });
  793. });
  794. }
  795. [Test]
  796. public void DetectSpanPositionExample()
  797. {
  798. RenderingTest
  799. .Create()
  800. .PageSize(new PageSize(650, 800))
  801. .ProduceImages()
  802. .ShowResults()
  803. .Render(container =>
  804. {
  805. var fontSize = 20;
  806. var paint = new SKPaint
  807. {
  808. Color = SKColors.Red,
  809. TextSize = fontSize
  810. };
  811. var fontMetrics = paint.FontMetrics;
  812. var start = 0f;
  813. var end = 0f;
  814. // corner case: what if text is paged? clamp start and end?
  815. container
  816. .Padding(25)
  817. .DefaultTextStyle(x => x.FontSize(fontSize).FontFamily("Calibri"))
  818. .Layers(layers =>
  819. {
  820. layers.PrimaryLayer().Text(text =>
  821. {
  822. text.Span(Placeholders.Paragraph());
  823. text.Span(" - ");
  824. // record start
  825. text.Element().Width(0).Height(0)
  826. .Canvas((canvas, size) => start = canvas.TotalMatrix.TransY / canvas.TotalMatrix.ScaleY);
  827. text.Span(Placeholders.LoremIpsum()).BackgroundColor(Colors.Red.Lighten4);
  828. // record end
  829. text.Element().Width(0).Height(0)
  830. .Canvas((canvas, size) => end = canvas.TotalMatrix.TransY / canvas.TotalMatrix.ScaleY);
  831. text.Span(" - ");
  832. text.Span(Placeholders.Paragraph());
  833. });
  834. layers.Layer().Canvas((canvas, size) =>
  835. {
  836. canvas.Save();
  837. canvas.Translate(-canvas.TotalMatrix.TransX / canvas.TotalMatrix.ScaleX, -canvas.TotalMatrix.TransY / canvas.TotalMatrix.ScaleY);
  838. canvas.DrawRect(10, start + fontMetrics.Ascent, 5, end - start + (fontMetrics.Bottom - fontMetrics.Ascent), paint);
  839. canvas.Restore();
  840. });
  841. });
  842. });
  843. }
  844. [Test]
  845. public void InconsistentLineHeightWhenUsingNewLineTest()
  846. {
  847. RenderingTest
  848. .Create()
  849. .PageSize(PageSizes.A4)
  850. .ProduceImages()
  851. .ShowResults()
  852. .Render(container =>
  853. {
  854. container
  855. .Padding(20)
  856. .Background(Colors.Grey.Lighten4)
  857. .Text(text =>
  858. {
  859. text.DefaultTextStyle(x => x.FontSize(16));
  860. text.Line(Placeholders.Paragraph());
  861. text.Line("");
  862. text.Line(Placeholders.Paragraph());
  863. text.Line(Placeholders.Label()).FontSize(48);
  864. text.Line(Placeholders.Paragraph());
  865. text.Line("");
  866. text.Line(Placeholders.Paragraph());
  867. });
  868. });
  869. }
  870. [Test]
  871. public void FontFallback_Nested()
  872. {
  873. RenderingTest
  874. .Create()
  875. .ProduceImages()
  876. .ShowResults()
  877. .RenderDocument(container =>
  878. {
  879. container.Page(page =>
  880. {
  881. page.Margin(50);
  882. page.PageColor(Colors.White);
  883. page.Size(PageSizes.A5.Landscape());
  884. page.DefaultTextStyle(x => x
  885. .FontSize(24)
  886. .Bold()
  887. .FontFamily("Times New Roman")
  888. .Fallback(y => y
  889. .FontFamily("Microsoft YaHei")
  890. .Underline()
  891. .BackgroundColor(Colors.Red.Lighten2)));
  892. page.Content().Text(text =>
  893. {
  894. text.Line("Default times new roman 中文文本 text.");
  895. text.Line("Normal weight and green 中文文本 text.").NormalWeight().BackgroundColor(Colors.Green.Lighten2);
  896. text.Line("Strikethrough without underline 中文文本 text.").Strikethrough().Underline(false);
  897. text.Line("Lato italic 中文文本 text.").FontFamily("Lato").Italic();
  898. });
  899. });
  900. });
  901. }
  902. [Test]
  903. public void TextShouldInheritAlignment()
  904. {
  905. RenderingTest
  906. .Create()
  907. .ProduceImages()
  908. .ShowResults()
  909. .PageSize(PageSizes.A4)
  910. .Render(container =>
  911. {
  912. var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
  913. container.Padding(20).Width(400).Column(column =>
  914. {
  915. column.Spacing(20);
  916. column.Item().Background(Colors.Grey.Lighten3).AlignLeft().Text(text);
  917. column.Item().Background(Colors.Grey.Lighten3).AlignCenter().Text(text);
  918. column.Item().Background(Colors.Grey.Lighten3).AlignRight().Text(text);
  919. });
  920. });
  921. }
  922. }
  923. }