From 22401be79111fef21ea3bd4519cfa3de092f3cf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Zi=C4=85bek?= Date: Sun, 12 Sep 2021 21:01:10 +0200 Subject: [PATCH] Fixed rendering spaces, fixed text alignment --- QuestPDF.Examples/TextBenchmark.cs | 4 +- QuestPDF.Examples/TextExamples.cs | 85 ++++++++++++++----- QuestPDF/Elements/Text/Items/TextBlockSpan.cs | 8 +- QuestPDF/Elements/Text/TextBlock.cs | 38 ++++++--- 4 files changed, 94 insertions(+), 41 deletions(-) diff --git a/QuestPDF.Examples/TextBenchmark.cs b/QuestPDF.Examples/TextBenchmark.cs index a39d6db..7082ddd 100644 --- a/QuestPDF.Examples/TextBenchmark.cs +++ b/QuestPDF.Examples/TextBenchmark.cs @@ -92,8 +92,8 @@ namespace QuestPDF.Examples var lineFrom = chapterPointers[index]; var lineTo = chapterPointers[index + 1] - 1; - - var lines = book.Skip(lineFrom + 1).Take(lineTo - lineFrom).Where(x => !string.IsNullOrWhiteSpace(x)) + + var lines = book.Skip(lineFrom + 1).Take(lineTo - lineFrom).Where(x => !string.IsNullOrWhiteSpace(x)); var content = string.Join(Environment.NewLine, lines); yield return new BookChapter diff --git a/QuestPDF.Examples/TextExamples.cs b/QuestPDF.Examples/TextExamples.cs index e3f1bea..8384bd0 100644 --- a/QuestPDF.Examples/TextExamples.cs +++ b/QuestPDF.Examples/TextExamples.cs @@ -22,35 +22,74 @@ namespace QuestPDF.Examples { container .Padding(20) + .Padding(10) .Box() .Border(1) .Padding(5) + .Padding(10) .Text(text => { - text.Span("Let's start with bold text. ", TextStyle.Default.Bold().BackgroundColor(Colors.Grey.Lighten3).Size(16)); - text.Span("Then something bigger. ", TextStyle.Default.Size(28).Color(Colors.DeepOrange.Darken2).BackgroundColor(Colors.Yellow.Lighten3).Underlined()); - text.Span("And tiny \r\n teeny-tiny. ", TextStyle.Default.Size(6)); - text.Span("Stroked text also works fine. ", TextStyle.Default.Size(14).Stroked().BackgroundColor(Colors.Grey.Lighten4)); - - text.Span("0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789", TextStyle.Default.Size(18)); + text.DefaultTextStyle(TextStyle.Default); + text.AlignRight(); + text.ParagraphSpacing(10); + + text.Span(Placeholders.LoremIpsum()); + + text.NewLine(); + + text.Span("This text is a normal text, "); + text.Span("this is a bold text, ", TextStyle.Default.Bold()); + text.Span("this is a red and underlined text, ", TextStyle.Default.Color(Colors.Red.Medium).Underlined()); + text.Span("and this is slightly bigger text.", TextStyle.Default.Size(16)); + + text.NewLine(); + + text.Span("The new text element also supports injecting custom content between words: "); + text.Element().PaddingBottom(-10).Height(16).Width(32).Image(Placeholders.Image); + text.Span("."); + + text.NewLine(); + + text.Span("This is page number "); + text.CurrentPageNumber(); + text.Span(" out of "); + text.TotalPages(); + + text.NewLine(); + + text.ExternalLocation("Please visit QuestPDF website", "https://www.questpdf.com"); - text.NewLine(); - text.NewLine(); - text.NewLine(); - text.Span("Is it time for lorem ipsum? ", TextStyle.Default.Size(12).Underlined().BackgroundColor(Colors.Grey.Lighten3)); - text.Span(Placeholders.LoremIpsum(), TextStyle.Default.Size(12)); - - text.Span("Before element - "); - text.Element().PaddingBottom(-10).Background(Colors.Red.Lighten4).Height(20).PaddingHorizontal(5).AlignMiddle().Text("Text inside text", TextStyle.Default.Size(8)); - text.Span(" - end of element."); - - text.NewLine(); - // text.Span("And now some colors: ", TextStyle.Default.Size(16).Color(Colors.Green.Medium)); - // - // foreach (var i in Enumerable.Range(1, 100)) - // { - // text.Span($"{i}: {Placeholders.Sentence()} ", TextStyle.Default.Size(12 + i / 5).LineHeight(2.75f - i / 50f).Color(Placeholders.Color()).BackgroundColor(Placeholders.BackgroundColor())); - // } + text.Span("\nThis is \npage number "); + }); + }); + } + + [Test] + public void PageNumber() + { + RenderingTest + .Create() + .PageSize(500, 400) + .FileName() + .ProduceImages() + .ShowResults() + .Render(container => + { + container + .Padding(10) + .Box() + .Border(1) + .Padding(10) + .Text(text => + { + text.DefaultTextStyle(TextStyle.Default); + text.AlignLeft(); + text.ParagraphSpacing(10); + + text.Span("This is page number "); + text.CurrentPageNumber(); + text.Span(" out of "); + text.TotalPages(); }); }); } diff --git a/QuestPDF/Elements/Text/Items/TextBlockSpan.cs b/QuestPDF/Elements/Text/Items/TextBlockSpan.cs index 7396ea2..905fab5 100644 --- a/QuestPDF/Elements/Text/Items/TextBlockSpan.cs +++ b/QuestPDF/Elements/Text/Items/TextBlockSpan.cs @@ -33,8 +33,12 @@ namespace QuestPDF.Elements.Text.Items var startIndex = request.StartIndex; - while (startIndex + 1 < Text.Length && Text[startIndex] == space) - startIndex++; + if (request.IsFirstLineElement) + { + while (startIndex + 1 < Text.Length && Text[startIndex] == space) + startIndex++; + } + if (Text.Length == 0) { diff --git a/QuestPDF/Elements/Text/TextBlock.cs b/QuestPDF/Elements/Text/TextBlock.cs index 847e9c2..9edfc0f 100644 --- a/QuestPDF/Elements/Text/TextBlock.cs +++ b/QuestPDF/Elements/Text/TextBlock.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using QuestPDF.Drawing.SpacePlan; using QuestPDF.Elements.Text.Calculation; @@ -50,7 +51,7 @@ namespace QuestPDF.Elements.Text internal override void Draw(Size availableSpace) { - var lines = DivideTextItemsIntoLines(availableSpace.Width, availableSpace.Height).ToList(); + var lines = DivideTextItemsIntoLines(availableSpace.Width, availableSpace.Height + Size.Epsilon).ToList(); if (!lines.Any()) return; @@ -62,14 +63,9 @@ namespace QuestPDF.Elements.Text { widthOffset = 0f; - var emptySpace = availableSpace.Width - line.Width; - - if (Alignment == HorizontalAlignment.Center) - emptySpace /= 2f; - - if (Alignment != HorizontalAlignment.Left) - Canvas.Translate(new Position(emptySpace, 0)); + var alignmentOffset = GetAlignmentOffset(line.Width); + Canvas.Translate(new Position(alignmentOffset, 0)); Canvas.Translate(new Position(0, -line.Ascent)); foreach (var item in line.Elements) @@ -92,12 +88,10 @@ namespace QuestPDF.Elements.Text widthOffset += item.Measurement.Width; } - if (Alignment != HorizontalAlignment.Right) - Canvas.Translate(new Position(emptySpace, 0)); - - Canvas.Translate(new Position(-line.Width - emptySpace, line.Ascent)); - + Canvas.Translate(new Position(-alignmentOffset, 0)); + Canvas.Translate(new Position(-line.Width, line.Ascent)); Canvas.Translate(new Position(0, line.LineHeight)); + heightOffset += line.LineHeight; } @@ -116,6 +110,22 @@ namespace QuestPDF.Elements.Text if (!RenderingQueue.Any()) ResetState(); + + float GetAlignmentOffset(float lineWidth) + { + if (Alignment == HorizontalAlignment.Left) + return 0; + + var emptySpace = availableSpace.Width - lineWidth; + + if (Alignment == HorizontalAlignment.Right) + return emptySpace; + + if (Alignment == HorizontalAlignment.Center) + return emptySpace / 2; + + throw new ArgumentException(); + } } public IEnumerable DivideTextItemsIntoLines(float availableWidth, float availableHeight)