TextExamples.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. using System;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using QuestPDF.Examples.Engine;
  5. using QuestPDF.Fluent;
  6. using QuestPDF.Helpers;
  7. using QuestPDF.Infrastructure;
  8. using SkiaSharp;
  9. namespace QuestPDF.Examples
  10. {
  11. public class TextExamples
  12. {
  13. [Test]
  14. public void SimpleText()
  15. {
  16. RenderingTest
  17. .Create()
  18. .PageSize(500, 100)
  19. .ProduceImages()
  20. .ShowResults()
  21. .Render(container =>
  22. {
  23. container
  24. .Padding(5)
  25. .MinimalBox()
  26. .Border(1)
  27. .Padding(10)
  28. .Text(Placeholders.Paragraph());
  29. });
  30. }
  31. [Test]
  32. public void SimpleTextBlock()
  33. {
  34. RenderingTest
  35. .Create()
  36. .PageSize(600, 300)
  37. .ProduceImages()
  38. .ShowResults()
  39. .Render(container =>
  40. {
  41. container
  42. .Padding(5)
  43. .MinimalBox()
  44. .Border(1)
  45. .MaxWidth(300)
  46. .Padding(10)
  47. .Text(text =>
  48. {
  49. text.DefaultTextStyle(TextStyle.Default.FontSize(20));
  50. text.Span("This is a normal text, followed by an ");
  51. text.Span("underlined red text").FontColor(Colors.Red.Medium).Underline();
  52. text.Span(".");
  53. });
  54. });
  55. }
  56. [Test]
  57. public void TextWeight()
  58. {
  59. RenderingTest
  60. .Create()
  61. .PageSize(500, 500)
  62. .ProduceImages()
  63. .ShowResults()
  64. .Render(container =>
  65. {
  66. container
  67. .Padding(20)
  68. .MinimalBox()
  69. .Border(1)
  70. .Padding(20)
  71. .Text(text =>
  72. {
  73. text.DefaultTextStyle(x => x.FontFamily(Fonts.Calibri).FontSize(20));
  74. text.Line("Thin").Thin();
  75. text.Line("ExtraLight").ExtraLight();
  76. text.Line("Light").Light();
  77. text.Line("NormalWeight").NormalWeight();
  78. text.Line("Medium").Medium();
  79. text.Line("SemiBold").SemiBold();
  80. text.Line("Bold").Bold();
  81. text.Line("ExtraBold").ExtraBold();
  82. text.Line("Black").Black();
  83. text.Line("ExtraBlack").ExtraBlack();
  84. });
  85. });
  86. }
  87. [Test]
  88. public void LineHeight()
  89. {
  90. RenderingTest
  91. .Create()
  92. .PageSize(500, 700)
  93. .ProduceImages()
  94. .ShowResults()
  95. .Render(container =>
  96. {
  97. container
  98. .Padding(20)
  99. .Column(column =>
  100. {
  101. var lineHeights = new[] { 0.8f, 1f, 1.5f };
  102. var paragraph = Placeholders.Paragraph();
  103. foreach (var lineHeight in lineHeights)
  104. {
  105. column
  106. .Item()
  107. .Border(1)
  108. .Padding(10)
  109. .Text(paragraph)
  110. .FontSize(16)
  111. .LineHeight(lineHeight);
  112. }
  113. });
  114. });
  115. }
  116. [Test]
  117. public void LetterSpacing()
  118. {
  119. RenderingTest
  120. .Create()
  121. .PageSize(500, 700)
  122. .ProduceImages()
  123. .ShowResults()
  124. .Render(container =>
  125. {
  126. container
  127. .Padding(20)
  128. .Column(column =>
  129. {
  130. var letterSpacing = new[] { -0.1f, 0f, 0.2f };
  131. var paragraph = Placeholders.Sentence().ToUpper();
  132. foreach (var spacing in letterSpacing)
  133. {
  134. column
  135. .Item()
  136. .Border(1)
  137. .Padding(10)
  138. .Column(nestedColumn =>
  139. {
  140. nestedColumn.Item()
  141. .Text(paragraph)
  142. .FontSize(16)
  143. .LetterSpacing(spacing);
  144. nestedColumn.Item()
  145. .Text($"Letter spacing of {spacing} em")
  146. .FontSize(10)
  147. .Italic()
  148. .FontColor(Colors.Blue.Medium);
  149. });
  150. }
  151. });
  152. });
  153. }
  154. [Test]
  155. public void LetterSpacing_Arabic()
  156. {
  157. RenderingTest
  158. .Create()
  159. .PageSize(500, 700)
  160. .ProduceImages()
  161. .ShowResults()
  162. .Render(container =>
  163. {
  164. container
  165. .Padding(50)
  166. .Column(column =>
  167. {
  168. var letterSpacing = new[] { -0.1f, 0f, 0.2f };
  169. var paragraph = "ينا الألم. في بعض الأحيان ونظراً للالتزامات التي يفرضها علينا";
  170. foreach (var spacing in letterSpacing)
  171. {
  172. column
  173. .Item()
  174. .Border(1)
  175. .Padding(10)
  176. .Column(nestedColumn =>
  177. {
  178. nestedColumn.Item()
  179. .Text(paragraph)
  180. .FontSize(16)
  181. .FontFamily(Fonts.Calibri)
  182. .LetterSpacing(spacing);
  183. nestedColumn.Item()
  184. .Text($"Letter spacing of {spacing} em")
  185. .FontSize(10)
  186. .Italic()
  187. .FontColor(Colors.Blue.Medium);
  188. });
  189. }
  190. });
  191. });
  192. }
  193. [Test]
  194. public void LetterSpacing_Unicode()
  195. {
  196. RenderingTest
  197. .Create()
  198. .PageSize(500, 700)
  199. .ProduceImages()
  200. .ShowResults()
  201. .Render(container =>
  202. {
  203. container
  204. .Padding(50)
  205. .Column(column =>
  206. {
  207. var letterSpacing = new[] { 0f, 0.5f };
  208. var paragraph = "Ţ̴̡̧̤̮̺̤̗͎̱̹͙͎͖͂̿̓́̉̊̀̍͜h̵̞̘͇̾̎̏̅į̵̹̖͔͉̰̎̉̄̐̏͑͂̅̃̃͘͝s̷͓͉̭̭̯̬̥̻̰̩̦̑̀̀͌́̒̍̒̌̇͛̀͛́̎ ̷̡̡̟͕̳̺̝̼͇͔̬̟̖͍̈́̽͜͝͝i̶͔͚̟̊̐͛́͛̄̌ṡ̸̡̤̪͙͍̥͙̟̼̝̰̥͈̿̓̄̿̓͠ ̶̢̦̙͍̯̖̱̰̯͕͔͎̯̝̎͑t̸͖̲̱̼̎͐̎̉̾̎̾̌̅̔̏͘ȩ̶̝̫̙͓̙̣̔̀̌̔̋̂̑̈́̏̀̈͘̕͜͝s̸̫̝̮̻̼͐̅̄̎̎̑͝ț̷̨̢̨̻͈̮̞̆͗̓͊̃̌͂̑̉̕̕͜͝͝";
  209. foreach (var spacing in letterSpacing)
  210. {
  211. column.Item()
  212. .Text($"Letter spacing of {spacing} em")
  213. .FontSize(10)
  214. .Italic()
  215. .FontColor(Colors.Blue.Medium);
  216. column.Item()
  217. .PaddingVertical(50)
  218. .Text(paragraph)
  219. .FontSize(16)
  220. .FontFamily(Fonts.Calibri)
  221. .LetterSpacing(spacing);
  222. }
  223. });
  224. });
  225. }
  226. [Test]
  227. public void SuperscriptSubscript_Simple()
  228. {
  229. RenderingTest
  230. .Create()
  231. .PageSize(500, 500)
  232. .ProduceImages()
  233. .ShowResults()
  234. .Render(container =>
  235. {
  236. container
  237. .Padding(20)
  238. .MinimalBox()
  239. .Border(1)
  240. .Padding(20)
  241. .Text(text =>
  242. {
  243. text.DefaultTextStyle(x => x.FontSize(20));
  244. text.ParagraphSpacing(10);
  245. var highlight = TextStyle.Default.BackgroundColor(Colors.Green.Lighten3);
  246. text.Span("E=mc").Style(highlight);
  247. text.Span("2").Superscript().Style(highlight);
  248. text.Span(" is the equation of mass–energy equivalence.");
  249. text.EmptyLine();
  250. text.Span("H").Style(highlight);
  251. text.Span("2").Subscript().Style(highlight);
  252. text.Span("O").Style(highlight);
  253. text.Span(" is the chemical formula for water.");
  254. });
  255. });
  256. }
  257. [Test]
  258. public void SuperscriptSubscript_Effects()
  259. {
  260. RenderingTest
  261. .Create()
  262. .PageSize(800, 400)
  263. .ProduceImages()
  264. .ShowResults()
  265. .Render(container =>
  266. {
  267. container
  268. .Padding(25)
  269. .DefaultTextStyle(x => x.FontSize(30))
  270. .Column(column =>
  271. {
  272. column.Spacing(25);
  273. column.Item().Text(text =>
  274. {
  275. text.DefaultTextStyle(x => x.Underline());
  276. text.Span("Underline of the superscript (E = mc");
  277. text.Span("2").Superscript();
  278. text.Span(") should be at the same height as for normal text.");
  279. });
  280. column.Item().Text(text =>
  281. {
  282. text.DefaultTextStyle(x => x.Underline());
  283. text.Span("Underline of the subscript(H");
  284. text.Span("2").Subscript();
  285. text.Span("O) should be slightly lower than a normal text.");
  286. });
  287. column.Item().Text(text =>
  288. {
  289. text.DefaultTextStyle(x => x.Strikethrough());
  290. text.Span("Strikethrough of both superscript (E=mc");
  291. text.Span("2").Superscript();
  292. text.Span(") and subscript(H");
  293. text.Span("2").Subscript();
  294. text.Span("O) should be visible in the middle of the text.");
  295. });
  296. });
  297. });
  298. }
  299. [Test]
  300. public void ParagraphSpacing()
  301. {
  302. RenderingTest
  303. .Create()
  304. .PageSize(500, 500)
  305. .ProduceImages()
  306. .ShowResults()
  307. .Render(container =>
  308. {
  309. container
  310. .Padding(5)
  311. .MinimalBox()
  312. .Border(1)
  313. .Padding(10)
  314. .Text(text =>
  315. {
  316. text.Line(Placeholders.Paragraph());
  317. });
  318. });
  319. }
  320. [Test]
  321. public void CustomElement()
  322. {
  323. RenderingTest
  324. .Create()
  325. .PageSize(500, 200)
  326. .ProduceImages()
  327. .ShowResults()
  328. .Render(container =>
  329. {
  330. container
  331. .Padding(5)
  332. .MinimalBox()
  333. .Border(1)
  334. .Padding(10)
  335. .Text(text =>
  336. {
  337. text.DefaultTextStyle(TextStyle.Default.FontSize(20));
  338. text.Span("This is a random image aligned to the baseline: ");
  339. text.Element()
  340. .PaddingBottom(-6)
  341. .Height(24)
  342. .Width(48)
  343. .Image(Placeholders.Image);
  344. text.Span(".");
  345. });
  346. });
  347. }
  348. [Test]
  349. public void TextElements()
  350. {
  351. RenderingTest
  352. .Create()
  353. .PageSize(PageSizes.A4)
  354. .ProducePdf()
  355. .ShowResults()
  356. .Render(container =>
  357. {
  358. container
  359. .Padding(20)
  360. .Padding(10)
  361. .MinimalBox()
  362. .Border(1)
  363. .Padding(5)
  364. .Padding(10)
  365. .Text(text =>
  366. {
  367. text.DefaultTextStyle(TextStyle.Default);
  368. text.AlignLeft();
  369. text.ParagraphSpacing(10);
  370. text.Line(Placeholders.LoremIpsum());
  371. 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();
  372. });
  373. });
  374. }
  375. [Test]
  376. public void Textcolumn()
  377. {
  378. RenderingTest
  379. .Create()
  380. .PageSize(PageSizes.A4)
  381. .ProducePdf()
  382. .ShowResults()
  383. .Render(container =>
  384. {
  385. container
  386. .Padding(20)
  387. .Padding(10)
  388. .MinimalBox()
  389. .Border(1)
  390. .Padding(5)
  391. .Padding(10)
  392. .Text(text =>
  393. {
  394. text.DefaultTextStyle(TextStyle.Default);
  395. text.AlignLeft();
  396. text.ParagraphSpacing(10);
  397. foreach (var i in Enumerable.Range(1, 100))
  398. text.Line($"{i}: {Placeholders.Paragraph()}");
  399. });
  400. });
  401. }
  402. [Test]
  403. public void SpaceIssue()
  404. {
  405. RenderingTest
  406. .Create()
  407. .PageSize(PageSizes.A4)
  408. .ProducePdf()
  409. .ShowResults()
  410. .Render(container =>
  411. {
  412. container
  413. .Padding(20)
  414. .Padding(10)
  415. .MinimalBox()
  416. .Border(1)
  417. .Padding(5)
  418. .Padding(10)
  419. .Text(text =>
  420. {
  421. text.DefaultTextStyle(x => x.Bold());
  422. text.DefaultTextStyle(TextStyle.Default);
  423. text.AlignLeft();
  424. text.ParagraphSpacing(10);
  425. text.Span(Placeholders.LoremIpsum());
  426. text.EmptyLine();
  427. text.Span("This text is a normal text, ");
  428. text.Span("this is a bold text, ").Bold();
  429. text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
  430. text.Span("and this is slightly bigger text.").FontSize(16);
  431. text.EmptyLine();
  432. text.Span("The new text element also supports injecting custom content between words: ");
  433. text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
  434. text.Span(".");
  435. text.EmptyLine();
  436. text.Span("This is page number ");
  437. text.CurrentPageNumber();
  438. text.Span(" out of ");
  439. text.TotalPages();
  440. text.EmptyLine();
  441. text.Hyperlink("Please visit QuestPDF website", "https://www.questpdf.com");
  442. text.EmptyLine();
  443. text.Span(Placeholders.Paragraphs());
  444. text.EmptyLine();
  445. text.Span(Placeholders.Paragraphs()).Italic();
  446. text.Line("This is target text that does not show up. " + Placeholders.Paragraph());
  447. });
  448. });
  449. }
  450. [Test]
  451. public void HugeList()
  452. {
  453. RenderingTest
  454. .Create()
  455. .PageSize(PageSizes.A4)
  456. .ProducePdf()
  457. .ShowResults()
  458. .Render(container =>
  459. {
  460. container
  461. .Padding(20)
  462. .Padding(10)
  463. .MinimalBox()
  464. .Border(1)
  465. .Padding(5)
  466. .Padding(10)
  467. .Text(text =>
  468. {
  469. text.DefaultTextStyle(TextStyle.Default.FontSize(20).BackgroundColor(Colors.Red.Lighten4));
  470. text.AlignLeft();
  471. text.ParagraphSpacing(10);
  472. text.Span("This text is a normal text, ");
  473. text.Span("this is a bold text, ").Bold();
  474. text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
  475. text.Span("and this is slightly bigger text.").FontSize(16);
  476. text.Span("The new text element also supports injecting custom content between words: ");
  477. text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
  478. text.Span(".");
  479. text.EmptyLine();
  480. foreach (var i in Enumerable.Range(1, 100))
  481. {
  482. text.Line($"{i}: {Placeholders.Paragraph()}");
  483. text.Hyperlink("Please visit QuestPDF website. ", "https://www.questpdf.com");
  484. text.Span("This is page number ");
  485. text.CurrentPageNumber();
  486. text.Span(" out of ");
  487. text.TotalPages();
  488. text.EmptyLine();
  489. }
  490. });
  491. });
  492. }
  493. [Test]
  494. public void MeasureIssueWhenSpaceAtLineEnd()
  495. {
  496. // issue 135
  497. RenderingTest
  498. .Create()
  499. .ProduceImages()
  500. .ShowResults()
  501. .RenderDocument(container =>
  502. {
  503. container.Page(page =>
  504. {
  505. page.Margin(50);
  506. page.PageColor(Colors.White);
  507. page.Size(PageSizes.A4);
  508. 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);
  509. });
  510. });
  511. }
  512. [Test]
  513. public void EmptyText()
  514. {
  515. // issue 135
  516. RenderingTest
  517. .Create()
  518. .ProduceImages()
  519. .ShowResults()
  520. .RenderDocument(container =>
  521. {
  522. container.Page(page =>
  523. {
  524. page.Margin(50);
  525. page.PageColor(Colors.White);
  526. page.Size(PageSizes.A4);
  527. page.Content().Text(" ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
  528. });
  529. });
  530. }
  531. [Test]
  532. public void Whitespaces()
  533. {
  534. // issue 135
  535. RenderingTest
  536. .Create()
  537. .ProduceImages()
  538. .ShowResults()
  539. .RenderDocument(container =>
  540. {
  541. container.Page(page =>
  542. {
  543. page.Margin(50);
  544. page.PageColor(Colors.White);
  545. page.Size(PageSizes.A4);
  546. page.Content().Text(" x ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
  547. });
  548. });
  549. }
  550. [Test]
  551. public void DrawingNullTextShouldNotThrowException()
  552. {
  553. RenderingTest
  554. .Create()
  555. .ProduceImages()
  556. .ShowResults()
  557. .RenderDocument(container =>
  558. {
  559. container.Page(page =>
  560. {
  561. page.Margin(50);
  562. page.PageColor(Colors.White);
  563. page.Size(PageSizes.A4);
  564. page.Content().Column(column =>
  565. {
  566. column.Item().Text((string) null);
  567. column.Item().Text(text =>
  568. {
  569. text.Span(null);
  570. text.Line(null);
  571. text.Hyperlink(null, "http://www.questpdf.com");
  572. text.TotalPages().Format(x => null);
  573. });
  574. });
  575. });
  576. });
  577. }
  578. [Test]
  579. public void BreakingLongWord()
  580. {
  581. RenderingTest
  582. .Create()
  583. .ProduceImages()
  584. .ShowResults()
  585. .RenderDocument(container =>
  586. {
  587. container.Page(page =>
  588. {
  589. page.Margin(50);
  590. page.PageColor(Colors.White);
  591. page.Size(PageSizes.A4);
  592. page.Content().Column(column =>
  593. {
  594. column.Item().Text((string) null);
  595. column.Item().Text(text =>
  596. {
  597. text.DefaultTextStyle(x => x.BackgroundColor(Colors.Red.Lighten3).FontSize(24));
  598. text.Span(" " + Placeholders.LoremIpsum());
  599. text.Span(" 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 ").WrapAnywhere();
  600. });
  601. });
  602. });
  603. });
  604. }
  605. [Test]
  606. public void TextShaping_Unicode()
  607. {
  608. RenderingTest
  609. .Create()
  610. .PageSize(600, 100)
  611. .ProduceImages()
  612. .ShowResults()
  613. .Render(container =>
  614. {
  615. container
  616. .Padding(35)
  617. .MinimalBox()
  618. .Background(Colors.Grey.Lighten2)
  619. .Text(text =>
  620. {
  621. text.DefaultTextStyle(TextStyle.Default.FontSize(20));
  622. text.Span("Complex Unicode structure: ");
  623. text.Span("T̶̖̔͆͆̽̔ḩ̷̼̫̐̈́̀͜͝͝ì̶͇̤͓̱̣͇͓͉̎s̵̡̟̹͍̜͉̗̾͛̈̐́͋͂͝͠ͅ ̴̨͙͍͇̭̒͗̀́͝ì̷̡̺͉̼̏̏̉̌͝s̷͍͙̗̰̖͙̈̑̂̔͑͊̌̓̊̇͜ ̶̛̼͚͊̅͘ṭ̷̨̘̣̙̖͉͌̏̂̅͑̄̽̕͝ȅ̶̲̲̙̭͈̬̣͔̝͔̈́͝s̸̢̯̪̫͓̭̮̓̀͆͜ț̸̢͉̞̥̤̏̌̓͝").FontFamily(Fonts.Calibri).FontColor(Colors.Red.Medium);
  624. text.Span(".");
  625. });
  626. });
  627. }
  628. [Test]
  629. public void TextShaping_Arabic()
  630. {
  631. RenderingTest
  632. .Create()
  633. .PageSize(250, 100)
  634. .ProduceImages()
  635. .ShowResults()
  636. .Render(container =>
  637. {
  638. container
  639. .Padding(25)
  640. .MinimalBox()
  641. .Background(Colors.Grey.Lighten2)
  642. .Text("خوارزمية ترتيب")
  643. .FontFamily(Fonts.Calibri)
  644. .FontSize(30);
  645. });
  646. }
  647. [Test]
  648. public void FontFallback()
  649. {
  650. RenderingTest
  651. .Create()
  652. .ProduceImages()
  653. .ShowResults()
  654. .RenderDocument(container =>
  655. {
  656. container.Page(page =>
  657. {
  658. page.Margin(50);
  659. page.PageColor(Colors.White);
  660. page.DefaultTextStyle(x => x
  661. .Fallback(y => y.FontFamily("Segoe UI Emoji")
  662. .Fallback(y => y.FontFamily("Microsoft YaHei"))));
  663. page.Size(PageSizes.A4);
  664. page.Content().Text(t =>
  665. {
  666. t.Line("This is normal text.");
  667. t.EmptyLine();
  668. t.Line("Following line should use font fallback:");
  669. t.Line("中文文本");
  670. t.EmptyLine();
  671. t.Line("The following line contains a mix of known and unknown characters.");
  672. t.Line("Mixed line: This 中文 is 文文 a mixed 本 本 line 本 中文文本!");
  673. t.EmptyLine();
  674. t.Line("Emojis work out of the box because of font fallback: 😊😅🥳👍❤😍👌");
  675. });
  676. });
  677. });
  678. }
  679. [Test]
  680. public void WordWrappingStability()
  681. {
  682. // instruction: check if any characters repeat when performing the word-wrapping algorithm
  683. RenderingTest
  684. .Create()
  685. .PageSize(PageSizes.A4)
  686. .ProducePdf()
  687. .ShowResults()
  688. .Render(container =>
  689. {
  690. var text = "Lorem ipsum dolor sit amet consectetuer";
  691. container
  692. .Padding(20)
  693. .Column(column =>
  694. {
  695. column.Spacing(10);
  696. foreach (var width in Enumerable.Range(25, 200))
  697. {
  698. column
  699. .Item()
  700. .MaxWidth(width)
  701. .Background(Colors.Grey.Lighten3)
  702. .Text(text);
  703. }
  704. });
  705. });
  706. }
  707. [Test]
  708. public void AdvancedLanguagesSupport()
  709. {
  710. RenderingTest
  711. .Create()
  712. .PageSize(new PageSize(400, 400))
  713. .ProduceImages()
  714. .ShowResults()
  715. .Render(container =>
  716. {
  717. var text = "في المعلوماتية أو الرياضيات، خوارزمية الترتيب هي خوارزمية تمكن من تنظيم مجموعة عناصر حسب ترتيب محدد.";
  718. container
  719. .Padding(20)
  720. .ContentFromRightToLeft()
  721. .Text(text)
  722. .FontFamily(Fonts.Calibri)
  723. .FontSize(22);
  724. });
  725. }
  726. [Test]
  727. public void WordWrappingWhenRightToLeft()
  728. {
  729. RenderingTest
  730. .Create()
  731. .PageSize(new PageSize(1000, 500))
  732. .ProducePdf()
  733. .ShowResults()
  734. .Render(container =>
  735. {
  736. var text = "في المعلوماتية أو الرياضيات، خوارزمية الترتيب هي خوارزمية تمكن من تنظيم مجموعة عناصر حسب ترتيب محدد.";
  737. container
  738. .Padding(25)
  739. .ContentFromRightToLeft()
  740. .Column(column =>
  741. {
  742. column.Spacing(20);
  743. foreach (var size in new[] { 36, 34, 32, 30, 15 })
  744. {
  745. column
  746. .Item()
  747. .ShowEntire()
  748. .MaxWidth(size * 25)
  749. .Background(Colors.Grey.Lighten3)
  750. .MinimalBox()
  751. .Background(Colors.Grey.Lighten2)
  752. .Text(text)
  753. .FontSize(20)
  754. .FontFamily("Segoe UI");
  755. }
  756. });
  757. });
  758. }
  759. [Test]
  760. public void ForcingTextDirection()
  761. {
  762. RenderingTest
  763. .Create()
  764. .PageSize(new PageSize(1000, 500))
  765. .ProduceImages()
  766. .ShowResults()
  767. .Render(container =>
  768. {
  769. container
  770. .Padding(10)
  771. .DefaultTextStyle(x => x.FontSize(24).FontFamily("Calibri"))
  772. .Column(column =>
  773. {
  774. column.Spacing(10);
  775. var word = "الجوريتم";
  776. var definition = "algorithm in Arabic";
  777. var text = $"{word} - {definition}";
  778. // text direction is automatically detected using the first word
  779. column.Item().Text(text);
  780. // it is possible to force specific content direction
  781. column.Item().Text(text).DirectionFromLeftToRight();
  782. column.Item().Text(text).DirectionFromRightToLeft();
  783. // to combine text in various content directions, split it into segments
  784. column.Item().Text(text =>
  785. {
  786. text.Span(word);
  787. text.Span(" - ");
  788. text.Span(definition);
  789. });
  790. });
  791. });
  792. }
  793. [Test]
  794. public void DetectSpanPositionExample()
  795. {
  796. RenderingTest
  797. .Create()
  798. .PageSize(new PageSize(650, 800))
  799. .ProduceImages()
  800. .ShowResults()
  801. .Render(container =>
  802. {
  803. var fontSize = 20;
  804. var paint = new SKPaint
  805. {
  806. Color = SKColors.Red,
  807. TextSize = fontSize
  808. };
  809. var fontMetrics = paint.FontMetrics;
  810. var start = 0f;
  811. var end = 0f;
  812. // corner case: what if text is paged? clamp start and end?
  813. container
  814. .Padding(25)
  815. .DefaultTextStyle(x => x.FontSize(fontSize).FontFamily("Calibri"))
  816. .Layers(layers =>
  817. {
  818. layers.PrimaryLayer().Text(text =>
  819. {
  820. text.Span(Placeholders.Paragraph());
  821. text.Span(" - ");
  822. // record start
  823. text.Element().Width(1).Height(1)
  824. .Canvas((canvas, size) => start = canvas.TotalMatrix.TransY / canvas.TotalMatrix.ScaleY);
  825. text.Span(Placeholders.LoremIpsum()).BackgroundColor(Colors.Red.Lighten4);
  826. // record end
  827. text.Element().Width(1).Height(1)
  828. .Canvas((canvas, size) => end = canvas.TotalMatrix.TransY / canvas.TotalMatrix.ScaleY);
  829. text.Span(" - ");
  830. text.Span(Placeholders.Paragraph());
  831. });
  832. layers.Layer().Canvas((canvas, size) =>
  833. {
  834. canvas.Save();
  835. canvas.Translate(-canvas.TotalMatrix.TransX / canvas.TotalMatrix.ScaleX, -canvas.TotalMatrix.TransY / canvas.TotalMatrix.ScaleY);
  836. canvas.DrawRect(10, start + fontMetrics.Ascent, 5, end - start + (fontMetrics.Bottom - fontMetrics.Ascent), paint);
  837. canvas.Restore();
  838. });
  839. });
  840. });
  841. }
  842. [Test]
  843. public void InconsistentLineHeightWhenUsingNewLineTest()
  844. {
  845. RenderingTest
  846. .Create()
  847. .PageSize(PageSizes.A4)
  848. .ProduceImages()
  849. .ShowResults()
  850. .Render(container =>
  851. {
  852. container
  853. .Padding(20)
  854. .Background(Colors.Grey.Lighten4)
  855. .Text(text =>
  856. {
  857. text.DefaultTextStyle(x => x.FontSize(16));
  858. text.Line(Placeholders.Paragraph());
  859. text.Line("");
  860. text.Line(Placeholders.Paragraph());
  861. text.Line(Placeholders.Label()).FontSize(48);
  862. text.Line(Placeholders.Paragraph());
  863. text.Line("");
  864. text.Line(Placeholders.Paragraph());
  865. });
  866. });
  867. }
  868. [Test]
  869. public void FontFallback_Nested()
  870. {
  871. RenderingTest
  872. .Create()
  873. .ProduceImages()
  874. .ShowResults()
  875. .RenderDocument(container =>
  876. {
  877. container.Page(page =>
  878. {
  879. page.Margin(50);
  880. page.PageColor(Colors.White);
  881. page.Size(PageSizes.A5.Landscape());
  882. page.DefaultTextStyle(x => x
  883. .FontSize(24)
  884. .Bold()
  885. .FontFamily("Times New Roman")
  886. .Fallback(y => y
  887. .FontFamily("Microsoft YaHei")
  888. .Underline()
  889. .BackgroundColor(Colors.Red.Lighten2)));
  890. page.Content().Text(text =>
  891. {
  892. text.Line("Default times new roman 中文文本 text.");
  893. text.Line("Normal weight and green 中文文本 text.").NormalWeight().BackgroundColor(Colors.Green.Lighten2);
  894. text.Line("Strikethrough without underline 中文文本 text.").Strikethrough().Underline(false);
  895. text.Line("Lato italic 中文文本 text.").FontFamily("Lato").Italic();
  896. });
  897. });
  898. });
  899. }
  900. [Test]
  901. public void TextShouldInheritAlignment()
  902. {
  903. RenderingTest
  904. .Create()
  905. .ProduceImages()
  906. .ShowResults()
  907. .PageSize(PageSizes.A4)
  908. .Render(container =>
  909. {
  910. var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
  911. container.Padding(20).Width(400).Column(column =>
  912. {
  913. column.Spacing(20);
  914. column.Item().Background(Colors.Grey.Lighten3).AlignLeft().Text(text);
  915. column.Item().Background(Colors.Grey.Lighten3).AlignCenter().Text(text);
  916. column.Item().Background(Colors.Grey.Lighten3).AlignRight().Text(text);
  917. });
  918. });
  919. }
  920. }
  921. }