From 6ad465b9fad160b992ce4cd44a494fdf3d946ea9 Mon Sep 17 00:00:00 2001 From: MarcinZiabek Date: Sun, 16 Apr 2023 11:42:38 +0200 Subject: [PATCH] Fix(#499): inconsistent text height when using multiple lines --- Source/QuestPDF.Examples/TextExamples.cs | 30 ++++++++++++++++++++++++ Source/QuestPDF/Fluent/TextExtensions.cs | 14 ++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Source/QuestPDF.Examples/TextExamples.cs b/Source/QuestPDF.Examples/TextExamples.cs index 4b42021..3aa3df3 100644 --- a/Source/QuestPDF.Examples/TextExamples.cs +++ b/Source/QuestPDF.Examples/TextExamples.cs @@ -974,5 +974,35 @@ namespace QuestPDF.Examples }); }); } + + [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()); + }); + }); + } } } \ No newline at end of file diff --git a/Source/QuestPDF/Fluent/TextExtensions.cs b/Source/QuestPDF/Fluent/TextExtensions.cs index 61fd331..c39bba8 100644 --- a/Source/QuestPDF/Fluent/TextExtensions.cs +++ b/Source/QuestPDF/Fluent/TextExtensions.cs @@ -88,7 +88,19 @@ namespace QuestPDF.Fluent if (!TextBlocks.Any()) TextBlocks.Add(new TextBlock()); - TextBlocks.Last().Items.Add(item); + var lastTextBlock = TextBlocks.Last(); + + // TextBlock with only one Span with empty text is a special case. + // It represents an empty line with a given text style (e.g. text height). + // When more content is put to text block, the first items should be ignored (removed in this case). + // This change fixes inconsistent line height problem. + if (lastTextBlock.Items.Count == 1 && lastTextBlock.Items[0] is TextBlockSpan { Text: "" }) + { + lastTextBlock.Items[0] = item; + return; + } + + lastTextBlock.Items.Add(item); } [Obsolete("This element has been renamed since version 2022.3. Please use the overload that returns a TextSpanDescriptor object which allows to specify text style.")]