From acdd8164be75c42522043c820c3f4feb3edd58f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Zi=C4=85bek?= Date: Tue, 14 Sep 2021 00:16:16 +0200 Subject: [PATCH] Optimized the TextLine object --- QuestPDF.Examples/TextExamples.cs | 5 +-- .../Elements/Text/Calculation/TextLine.cs | 34 +++++++++++++++---- .../Text/Calculation/TextLineElement.cs | 4 +-- QuestPDF/Elements/Text/TextBlock.cs | 5 +-- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/QuestPDF.Examples/TextExamples.cs b/QuestPDF.Examples/TextExamples.cs index b80a33b..8e80216 100644 --- a/QuestPDF.Examples/TextExamples.cs +++ b/QuestPDF.Examples/TextExamples.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using NUnit.Framework; using QuestPDF.Examples.Engine; using QuestPDF.Fluent; @@ -35,7 +36,7 @@ namespace QuestPDF.Examples text.Span(Placeholders.LoremIpsum()); - text.Line("This is target text that does not show up. > This is a short sentence that will be wrapped into second line hopefully, right? <"); + text.Line($"This is target text that does not show up. {DateTime.UtcNow:T} > This is a short sentence that will be wrapped into second line hopefully, right? <"); }); }); } diff --git a/QuestPDF/Elements/Text/Calculation/TextLine.cs b/QuestPDF/Elements/Text/Calculation/TextLine.cs index 5688099..518a7a6 100644 --- a/QuestPDF/Elements/Text/Calculation/TextLine.cs +++ b/QuestPDF/Elements/Text/Calculation/TextLine.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using QuestPDF.Elements.Text.Items; @@ -6,14 +7,33 @@ namespace QuestPDF.Elements.Text.Calculation { internal class TextLine { - public ICollection Elements { get; set; } + public ICollection Elements { get; private set; } - public float TextHeight => Elements.Max(x => x.Measurement.Height); - public float LineHeight => Elements.Max(x => x.Measurement.LineHeight * x.Measurement.Height); + public float TextHeight { get; private set; } + public float LineHeight { get; private set; } - public float Ascent => Elements.Min(x => x.Measurement.Ascent) - (LineHeight - TextHeight) / 2; - public float Descent => Elements.Max(x => x.Measurement.Descent) + (LineHeight - TextHeight) / 2; + public float Ascent { get; private set; } + public float Descent { get; private set; } - public float Width => Elements.Sum(x => x.Measurement.Width); + public float Width { get; private set; } + + public static TextLine From(ICollection elements) + { + var textHeight = elements.Max(x => x.Measurement.Height); + var lineHeight = elements.Max(x => x.Measurement.LineHeight * x.Measurement.Height); + + return new TextLine + { + Elements = elements, + + TextHeight = textHeight, + LineHeight = lineHeight, + + Ascent = elements.Min(x => x.Measurement.Ascent) - (lineHeight - textHeight) / 2, + Descent = elements.Max(x => x.Measurement.Descent) + (lineHeight - textHeight) / 2, + + Width = elements.Sum(x => x.Measurement.Width) + }; + } } } \ No newline at end of file diff --git a/QuestPDF/Elements/Text/Calculation/TextLineElement.cs b/QuestPDF/Elements/Text/Calculation/TextLineElement.cs index 5f631db..a62df24 100644 --- a/QuestPDF/Elements/Text/Calculation/TextLineElement.cs +++ b/QuestPDF/Elements/Text/Calculation/TextLineElement.cs @@ -1,6 +1,6 @@ -using QuestPDF.Elements.Text.Calculation; +using QuestPDF.Elements.Text.Items; -namespace QuestPDF.Elements.Text.Items +namespace QuestPDF.Elements.Text.Calculation { internal class TextLineElement { diff --git a/QuestPDF/Elements/Text/TextBlock.cs b/QuestPDF/Elements/Text/TextBlock.cs index 18f52b5..106e7e8 100644 --- a/QuestPDF/Elements/Text/TextBlock.cs +++ b/QuestPDF/Elements/Text/TextBlock.cs @@ -192,10 +192,7 @@ namespace QuestPDF.Elements.Text queue.Dequeue(); } - return new TextLine - { - Elements = currentLineElements - }; + return TextLine.From(currentLineElements); } } }