TextExamples.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. }
  903. }