| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043 |
- using System;
- using System.Linq;
- using System.Text;
- using NUnit.Framework;
- using QuestPDF.Elements.Text;
- using QuestPDF.Examples.Engine;
- using QuestPDF.Fluent;
- using QuestPDF.Helpers;
- using QuestPDF.Infrastructure;
- using SkiaSharp;
- namespace QuestPDF.Examples
- {
- public class TextExamples
- {
- [Test]
- public void SimpleText()
- {
- RenderingTest
- .Create()
- .PageSize(500, 100)
-
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(5)
- .MinimalBox()
- .Border(1)
- .Padding(10)
- .Text(Placeholders.Paragraph());
- });
- }
-
- [Test]
- public void SimpleTextBlock()
- {
- RenderingTest
- .Create()
- .PageSize(600, 300)
-
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(5)
- .MinimalBox()
- .Border(1)
- .MaxWidth(300)
- .Padding(10)
- .Text(text =>
- {
- text.DefaultTextStyle(TextStyle.Default.FontSize(20));
- text.Span("This is a normal text, followed by an ");
- text.Span("underlined red text").FontColor(Colors.Red.Medium).Underline();
- text.Span(".");
- });
- });
- }
- [Test]
- public void TextWeight()
- {
- RenderingTest
- .Create()
- .PageSize(500, 500)
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(20)
- .MinimalBox()
- .Border(1)
- .Padding(20)
- .Text(text =>
- {
- text.DefaultTextStyle(x => x.FontFamily(Fonts.Calibri).FontSize(20));
- text.Line("Thin").Thin();
- text.Line("ExtraLight").ExtraLight();
- text.Line("Light").Light();
- text.Line("NormalWeight").NormalWeight();
- text.Line("Medium").Medium();
- text.Line("SemiBold").SemiBold();
- text.Line("Bold").Bold();
- text.Line("ExtraBold").ExtraBold();
- text.Line("Black").Black();
- text.Line("ExtraBlack").ExtraBlack();
- });
- });
- }
-
- [Test]
- public void LineHeight()
- {
- RenderingTest
- .Create()
- .PageSize(500, 700)
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(20)
- .Column(column =>
- {
- var lineHeights = new[] { 0.8f, 1f, 1.5f };
- var paragraph = Placeholders.Paragraph();
- foreach (var lineHeight in lineHeights)
- {
- column
- .Item()
- .Border(1)
- .Padding(10)
- .Text(paragraph)
- .FontSize(16)
- .LineHeight(lineHeight);
- }
- });
- });
- }
- [Test]
- public void LetterSpacing()
- {
- RenderingTest
- .Create()
- .PageSize(500, 700)
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(20)
- .Column(column =>
- {
- var letterSpacing = new[] { -0.1f, 0f, 0.2f };
- var paragraph = Placeholders.Sentence().ToUpper();
- foreach (var spacing in letterSpacing)
- {
- column
- .Item()
- .Border(1)
- .Padding(10)
- .Column(nestedColumn =>
- {
- nestedColumn.Item()
- .Text(paragraph)
- .FontSize(16)
- .LetterSpacing(spacing);
- nestedColumn.Item()
- .Text($"Letter spacing of {spacing} em")
- .FontSize(10)
- .Italic()
- .FontColor(Colors.Blue.Medium);
- });
-
- }
- });
- });
- }
- [Test]
- public void LetterSpacing_Arabic()
- {
- RenderingTest
- .Create()
- .PageSize(500, 700)
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(50)
- .Column(column =>
- {
- var letterSpacing = new[] { -0.1f, 0f, 0.2f };
- var paragraph = "ينا الألم. في بعض الأحيان ونظراً للالتزامات التي يفرضها علينا";
- foreach (var spacing in letterSpacing)
- {
- column
- .Item()
- .Border(1)
- .Padding(10)
- .Column(nestedColumn =>
- {
- nestedColumn.Item()
- .Text(paragraph)
- .FontSize(16)
- .FontFamily(Fonts.Calibri)
- .LetterSpacing(spacing);
- nestedColumn.Item()
- .Text($"Letter spacing of {spacing} em")
- .FontSize(10)
- .Italic()
- .FontColor(Colors.Blue.Medium);
- });
- }
- });
- });
- }
- [Test]
- public void LetterSpacing_Unicode()
- {
- RenderingTest
- .Create()
- .PageSize(500, 700)
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(50)
- .Column(column =>
- {
- var letterSpacing = new[] { 0f, 0.5f };
-
-
-
- var paragraph = "Ţ̴̡̧̤̮̺̤̗͎̱̹͙͎͖͂̿̓́̉̊̀̍͜h̵̞̘͇̾̎̏̅į̵̹̖͔͉̰̎̉̄̐̏͑͂̅̃̃͘͝s̷͓͉̭̭̯̬̥̻̰̩̦̑̀̀͌́̒̍̒̌̇͛̀͛́̎ ̷̡̡̟͕̳̺̝̼͇͔̬̟̖͍̈́̽͜͝͝i̶͔͚̟̊̐͛́͛̄̌ṡ̸̡̤̪͙͍̥͙̟̼̝̰̥͈̿̓̄̿̓͠ ̶̢̦̙͍̯̖̱̰̯͕͔͎̯̝̎͑t̸͖̲̱̼̎͐̎̉̾̎̾̌̅̔̏͘ȩ̶̝̫̙͓̙̣̔̀̌̔̋̂̑̈́̏̀̈͘̕͜͝s̸̫̝̮̻̼͐̅̄̎̎̑͝ț̷̨̢̨̻͈̮̞̆͗̓͊̃̌͂̑̉̕̕͜͝͝";
-
-
- foreach (var spacing in letterSpacing)
- {
- column.Item()
- .Text($"Letter spacing of {spacing} em")
- .FontSize(10)
- .Italic()
- .FontColor(Colors.Blue.Medium);
- column.Item()
- .PaddingVertical(50)
- .Text(paragraph)
- .FontSize(16)
- .FontFamily(Fonts.Calibri)
- .LetterSpacing(spacing);
- }
- });
- });
- }
- [Test]
- public void SuperscriptSubscript_Simple()
- {
- RenderingTest
- .Create()
- .PageSize(500, 500)
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(20)
- .MinimalBox()
- .Border(1)
- .Padding(20)
- .Text(text =>
- {
- text.DefaultTextStyle(x => x.FontSize(20));
- text.ParagraphSpacing(10);
- var highlight = TextStyle.Default.BackgroundColor(Colors.Green.Lighten3);
- text.Span("E=mc").Style(highlight);
- text.Span("2").Superscript().Style(highlight);
- text.Span(" is the equation of mass–energy equivalence.");
- text.EmptyLine();
-
- text.Span("H").Style(highlight);
- text.Span("2").Subscript().Style(highlight);
- text.Span("O").Style(highlight);
- text.Span(" is the chemical formula for water.");
- });
- });
- }
- [Test]
- public void SuperscriptSubscript_Effects()
- {
- RenderingTest
- .Create()
- .PageSize(800, 400)
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(25)
- .DefaultTextStyle(x => x.FontSize(30))
- .Column(column =>
- {
- column.Spacing(25);
-
- column.Item().Text(text =>
- {
- text.DefaultTextStyle(x => x.Underline());
-
- text.Span("Underline of the superscript (E = mc");
- text.Span("2").Superscript();
- text.Span(") should be at the same height as for normal text.");
- });
-
- column.Item().Text(text =>
- {
- text.DefaultTextStyle(x => x.Underline());
-
- text.Span("Underline of the subscript(H");
- text.Span("2").Subscript();
- text.Span("O) should be slightly lower than a normal text.");
- });
-
- column.Item().Text(text =>
- {
- text.DefaultTextStyle(x => x.Strikethrough());
-
- text.Span("Strikethrough of both superscript (E=mc");
- text.Span("2").Superscript();
- text.Span(") and subscript(H");
- text.Span("2").Subscript();
- text.Span("O) should be visible in the middle of the text.");
- });
- });
- });
- }
- [Test]
- public void ParagraphSpacing()
- {
- RenderingTest
- .Create()
- .PageSize(500, 500)
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(5)
- .MinimalBox()
- .Border(1)
- .Padding(10)
- .Text(text =>
- {
- text.Line(Placeholders.Paragraph());
- });
- });
- }
-
- [Test]
- public void CustomElement()
- {
- RenderingTest
- .Create()
- .PageSize(500, 200)
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(5)
- .MinimalBox()
- .Border(1)
- .Padding(10)
- .Text(text =>
- {
- text.DefaultTextStyle(TextStyle.Default.FontSize(20));
- text.Span("This is a random image aligned to the baseline: ");
-
- text.Element()
- .PaddingBottom(-6)
- .Height(24)
- .Width(48)
- .Image(Placeholders.Image);
-
- text.Span(".");
- });
- });
- }
-
- [Test]
- public void TextElements()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProducePdf()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(20)
- .Padding(10)
- .MinimalBox()
- .Border(1)
- .Padding(5)
- .Padding(10)
- .Text(text =>
- {
- text.DefaultTextStyle(TextStyle.Default);
- text.AlignLeft();
- text.ParagraphSpacing(10);
- text.Line(Placeholders.LoremIpsum());
- 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();
- });
- });
- }
-
- [Test]
- public void Textcolumn()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProducePdf()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(20)
- .Padding(10)
- .MinimalBox()
- .Border(1)
- .Padding(5)
- .Padding(10)
- .Text(text =>
- {
- text.DefaultTextStyle(TextStyle.Default);
- text.AlignLeft();
- text.ParagraphSpacing(10);
-
- foreach (var i in Enumerable.Range(1, 100))
- text.Line($"{i}: {Placeholders.Paragraph()}");
- });
- });
- }
- [Test]
- public void SpaceIssue()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProducePdf()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(20)
- .Padding(10)
- .MinimalBox()
- .Border(1)
- .Padding(5)
- .Padding(10)
- .Text(text =>
- {
- text.DefaultTextStyle(x => x.Bold());
-
- text.DefaultTextStyle(TextStyle.Default);
- text.AlignLeft();
- text.ParagraphSpacing(10);
- text.Span(Placeholders.LoremIpsum());
- text.EmptyLine();
- text.Span("This text is a normal text, ");
- text.Span("this is a bold text, ").Bold();
- text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
- text.Span("and this is slightly bigger text.").FontSize(16);
- text.EmptyLine();
- text.Span("The new text element also supports injecting custom content between words: ");
- text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
- text.Span(".");
- text.EmptyLine();
- text.Span("This is page number ");
- text.CurrentPageNumber();
- text.Span(" out of ");
- text.TotalPages();
- text.EmptyLine();
- text.Hyperlink("Please visit QuestPDF website", "https://www.questpdf.com");
- text.EmptyLine();
- text.Span(Placeholders.Paragraphs());
-
-
- text.EmptyLine();
- text.Span(Placeholders.Paragraphs()).Italic();
-
- text.Line("This is target text that does not show up. " + Placeholders.Paragraph());
- });
- });
- }
- [Test]
- public void HugeList()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProducePdf()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(20)
- .Padding(10)
- .MinimalBox()
- .Border(1)
- .Padding(5)
- .Padding(10)
- .Text(text =>
- {
- text.DefaultTextStyle(TextStyle.Default.FontSize(20).BackgroundColor(Colors.Red.Lighten4));
- text.AlignLeft();
- text.ParagraphSpacing(10);
- text.Span("This text is a normal text, ");
- text.Span("this is a bold text, ").Bold();
- text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
- text.Span("and this is slightly bigger text.").FontSize(16);
-
- text.Span("The new text element also supports injecting custom content between words: ");
- text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
- text.Span(".");
-
- text.EmptyLine();
-
- foreach (var i in Enumerable.Range(1, 100))
- {
- text.Line($"{i}: {Placeholders.Paragraph()}");
- text.Hyperlink("Please visit QuestPDF website. ", "https://www.questpdf.com");
-
- text.Span("This is page number ");
- text.CurrentPageNumber();
- text.Span(" out of ");
- text.TotalPages();
-
- text.EmptyLine();
- }
- });
- });
- }
-
- [Test]
- public void MeasureIssueWhenSpaceAtLineEnd()
- {
- // issue 135
-
- RenderingTest
- .Create()
- .ProduceImages()
- .ShowResults()
- .RenderDocument(container =>
- {
- container.Page(page =>
- {
- page.Margin(50);
- page.PageColor(Colors.White);
- page.Size(PageSizes.A4);
- 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);
- });
- });
- }
-
- [Test]
- public void EmptyText()
- {
- // issue 135
-
- RenderingTest
- .Create()
- .ProduceImages()
- .ShowResults()
- .RenderDocument(container =>
- {
- container.Page(page =>
- {
- page.Margin(50);
- page.PageColor(Colors.White);
- page.Size(PageSizes.A4);
- page.Content().Text(" ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
- });
- });
- }
-
- [Test]
- public void Whitespaces()
- {
- // issue 135
-
- RenderingTest
- .Create()
- .ProduceImages()
- .ShowResults()
- .RenderDocument(container =>
- {
- container.Page(page =>
- {
- page.Margin(50);
- page.PageColor(Colors.White);
- page.Size(PageSizes.A4);
- page.Content().Text(" x ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
- });
- });
- }
-
- [Test]
- public void DrawingNullTextShouldNotThrowException()
- {
- RenderingTest
- .Create()
- .ProduceImages()
- .ShowResults()
- .RenderDocument(container =>
- {
- container.Page(page =>
- {
- page.Margin(50);
- page.PageColor(Colors.White);
- page.Size(PageSizes.A4);
- page.Content().Column(column =>
- {
- column.Item().Text((string) null);
- column.Item().Text(text =>
- {
- text.Span(null);
- text.Line(null);
- text.Hyperlink(null, "http://www.questpdf.com");
- text.TotalPages().Format(x => null);
- });
- });
- });
- });
- }
-
- [Test]
- public void BreakingLongWord()
- {
- RenderingTest
- .Create()
- .ProduceImages()
- .ShowResults()
- .RenderDocument(container =>
- {
- container.Page(page =>
- {
- page.Margin(50);
- page.PageColor(Colors.White);
- page.Size(PageSizes.A4);
- page.Content().Column(column =>
- {
- column.Item().Text((string) null);
- column.Item().Text(text =>
- {
- text.DefaultTextStyle(x => x.BackgroundColor(Colors.Red.Lighten3).FontSize(24));
-
- text.Span(" " + Placeholders.LoremIpsum());
- text.Span(" 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 ").WrapAnywhere();
- });
- });
- });
- });
- }
-
- [Test]
- public void TextShaping_Unicode()
- {
- RenderingTest
- .Create()
- .PageSize(600, 100)
-
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(35)
- .MinimalBox()
- .Background(Colors.Grey.Lighten2)
- .Text(text =>
- {
- text.DefaultTextStyle(TextStyle.Default.FontSize(20));
-
- text.Span("Complex Unicode structure: ");
-
-
- text.Span("T̶̖̔͆͆̽̔ḩ̷̼̫̐̈́̀͜͝͝ì̶͇̤͓̱̣͇͓͉̎s̵̡̟̹͍̜͉̗̾͛̈̐́͋͂͝͠ͅ ̴̨͙͍͇̭̒͗̀́͝ì̷̡̺͉̼̏̏̉̌͝s̷͍͙̗̰̖͙̈̑̂̔͑͊̌̓̊̇͜ ̶̛̼͚͊̅͘ṭ̷̨̘̣̙̖͉͌̏̂̅͑̄̽̕͝ȅ̶̲̲̙̭͈̬̣͔̝͔̈́͝s̸̢̯̪̫͓̭̮̓̀͆͜ț̸̢͉̞̥̤̏̌̓͝").FontFamily(Fonts.Calibri).FontColor(Colors.Red.Medium);
-
-
- text.Span(".");
- });
- });
- }
-
- [Test]
- public void TextShaping_Arabic()
- {
- RenderingTest
- .Create()
- .PageSize(250, 100)
-
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(25)
- .MinimalBox()
- .Background(Colors.Grey.Lighten2)
- .Text("خوارزمية ترتيب")
- .FontFamily(Fonts.Calibri)
- .FontSize(30);
- });
- }
-
- [Test]
- public void FontFallback()
- {
- RenderingTest
- .Create()
- .ProduceImages()
- .ShowResults()
- .RenderDocument(container =>
- {
- container.Page(page =>
- {
- page.Margin(50);
- page.PageColor(Colors.White);
- page.DefaultTextStyle(x => x
- .Fallback(y => y.FontFamily("Segoe UI Emoji")
- .Fallback(y => y.FontFamily("Microsoft YaHei"))));
- page.Size(PageSizes.A4);
- page.Content().Text(t =>
- {
- t.Line("This is normal text.");
- t.EmptyLine();
-
- t.Line("Following line should use font fallback:");
- t.Line("中文文本");
- t.EmptyLine();
-
- t.Line("The following line contains a mix of known and unknown characters.");
- t.Line("Mixed line: This 中文 is 文文 a mixed 本 本 line 本 中文文本!");
- t.EmptyLine();
- t.Line("Emojis work out of the box because of font fallback: 😊😅🥳👍❤😍👌");
- });
- });
- });
- }
-
- [Test]
- public void WordWrappingStability()
- {
- // instruction: check if any characters repeat when performing the word-wrapping algorithm
-
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProducePdf()
- .ShowResults()
- .Render(container =>
- {
- var text = "Lorem ipsum dolor sit amet consectetuer";
-
- container
- .Padding(20)
- .Column(column =>
- {
- column.Spacing(10);
- foreach (var width in Enumerable.Range(25, 200))
- {
- column
- .Item()
- .MaxWidth(width)
- .Background(Colors.Grey.Lighten3)
- .Text(text);
- }
- });
- });
- }
-
- [Test]
- public void AdvancedLanguagesSupport()
- {
- RenderingTest
- .Create()
- .PageSize(new PageSize(400, 400))
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- var text = "في المعلوماتية أو الرياضيات، خوارزمية الترتيب هي خوارزمية تمكن من تنظيم مجموعة عناصر حسب ترتيب محدد.";
-
- container
- .Padding(20)
- .ContentFromRightToLeft()
- .Text(text)
- .FontFamily(Fonts.Calibri)
- .FontSize(22);
- });
- }
-
- [Test]
- public void WordWrappingWhenRightToLeft()
- {
- RenderingTest
- .Create()
- .PageSize(new PageSize(1000, 500))
- .ProducePdf()
- .ShowResults()
- .Render(container =>
- {
- var text = "في المعلوماتية أو الرياضيات، خوارزمية الترتيب هي خوارزمية تمكن من تنظيم مجموعة عناصر حسب ترتيب محدد.";
-
- container
- .Padding(25)
- .ContentFromRightToLeft()
- .Column(column =>
- {
- column.Spacing(20);
-
- foreach (var size in new[] { 36, 34, 32, 30, 15 })
- {
- column
- .Item()
- .ShowEntire()
- .MaxWidth(size * 25)
- .Background(Colors.Grey.Lighten3)
- .MinimalBox()
- .Background(Colors.Grey.Lighten2)
- .Text(text)
- .FontSize(20)
- .FontFamily("Segoe UI");
- }
- });
- });
- }
-
- [Test]
- public void ForcingTextDirection()
- {
- RenderingTest
- .Create()
- .PageSize(new PageSize(1000, 500))
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(10)
- .DefaultTextStyle(x => x.FontSize(24).FontFamily("Calibri"))
- .Column(column =>
- {
- column.Spacing(10);
-
- var word = "الجوريتم";
- var definition = "algorithm in Arabic";
- var text = $"{word} - {definition}";
-
- // text direction is automatically detected using the first word
- column.Item().Text(text);
-
- // it is possible to force specific content direction
- column.Item().Text(text).DirectionFromLeftToRight();
- column.Item().Text(text).DirectionFromRightToLeft();
- // to combine text in various content directions, split it into segments
- column.Item().Text(text =>
- {
- text.Span(word);
- text.Span(" - ");
- text.Span(definition);
- });
- });
- });
- }
-
- [Test]
- public void DetectSpanPositionExample()
- {
- RenderingTest
- .Create()
- .PageSize(new PageSize(650, 800))
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- var fontSize = 20;
-
- var paint = new SKPaint
- {
- Color = SKColors.Red,
- TextSize = fontSize
- };
-
- var fontMetrics = paint.FontMetrics;
- var start = 0f;
- var end = 0f;
-
- // corner case: what if text is paged? clamp start and end?
- container
- .Padding(25)
- .DefaultTextStyle(x => x.FontSize(fontSize).FontFamily("Calibri"))
- .Layers(layers =>
- {
- layers.PrimaryLayer().Text(text =>
- {
- text.Span(Placeholders.Paragraph());
- text.Span(" - ");
-
- // record start
- text.Element().Width(0).Height(0)
- .Canvas((canvas, size) => start = canvas.TotalMatrix.TransY / canvas.TotalMatrix.ScaleY);
-
- text.Span(Placeholders.LoremIpsum()).BackgroundColor(Colors.Red.Lighten4);
-
- // record end
- text.Element().Width(0).Height(0)
- .Canvas((canvas, size) => end = canvas.TotalMatrix.TransY / canvas.TotalMatrix.ScaleY);
-
- text.Span(" - ");
- text.Span(Placeholders.Paragraph());
- });
-
- layers.Layer().Canvas((canvas, size) =>
- {
- canvas.Save();
-
- canvas.Translate(-canvas.TotalMatrix.TransX / canvas.TotalMatrix.ScaleX, -canvas.TotalMatrix.TransY / canvas.TotalMatrix.ScaleY);
- canvas.DrawRect(10, start + fontMetrics.Ascent, 5, end - start + (fontMetrics.Bottom - fontMetrics.Ascent), paint);
-
- canvas.Restore();
- });
- });
- });
- }
-
- [Test]
- public void InconsistentLineHeightWhenUsingNewLineTest()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(20)
- .Background(Colors.Grey.Lighten4)
- .Text(text =>
- {
- text.DefaultTextStyle(x => x.FontSize(16));
-
- text.Line(Placeholders.Paragraph());
- text.Line("");
- text.Line(Placeholders.Paragraph());
-
- text.Line(Placeholders.Label()).FontSize(48);
-
- text.Line(Placeholders.Paragraph());
- text.Line("");
- text.Line(Placeholders.Paragraph());
- });
- });
- }
-
- [Test]
- public void FontFallback_Nested()
- {
- RenderingTest
- .Create()
- .ProduceImages()
- .ShowResults()
- .RenderDocument(container =>
- {
- container.Page(page =>
- {
- page.Margin(50);
- page.PageColor(Colors.White);
- page.Size(PageSizes.A5.Landscape());
-
- page.DefaultTextStyle(x => x
- .FontSize(24)
- .Bold()
- .FontFamily("Times New Roman")
- .Fallback(y => y
- .FontFamily("Microsoft YaHei")
- .Underline()
- .BackgroundColor(Colors.Red.Lighten2)));
- page.Content().Text(text =>
- {
- text.Line("Default times new roman 中文文本 text.");
- text.Line("Normal weight and green 中文文本 text.").NormalWeight().BackgroundColor(Colors.Green.Lighten2);
- text.Line("Strikethrough without underline 中文文本 text.").Strikethrough().Underline(false);
- text.Line("Lato italic 中文文本 text.").FontFamily("Lato").Italic();
- });
- });
- });
- }
- }
- }
|