From 3112ee10cff10649e3b415d8ab22e2cdc6b45bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Zi=C4=85bek?= Date: Fri, 27 Aug 2021 20:25:20 +0200 Subject: [PATCH] Better new line handling --- QuestPDF.Examples/TextExamples.cs | 4 +++- QuestPDF/Elements/Text/Items/TextBlockSpan.cs | 9 +++++++++ QuestPDF/Fluent/TextExtensions.cs | 7 ++----- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/QuestPDF.Examples/TextExamples.cs b/QuestPDF.Examples/TextExamples.cs index b257ba6..e3f1bea 100644 --- a/QuestPDF.Examples/TextExamples.cs +++ b/QuestPDF.Examples/TextExamples.cs @@ -29,11 +29,13 @@ namespace QuestPDF.Examples { 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 teeny-tiny. ", TextStyle.Default.Size(6)); + 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.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)); diff --git a/QuestPDF/Elements/Text/Items/TextBlockSpan.cs b/QuestPDF/Elements/Text/Items/TextBlockSpan.cs index e3656e5..ab11c35 100644 --- a/QuestPDF/Elements/Text/Items/TextBlockSpan.cs +++ b/QuestPDF/Elements/Text/Items/TextBlockSpan.cs @@ -28,9 +28,18 @@ namespace QuestPDF.Elements.Text.Items { var paint = Style.ToPaint(); var fontMetrics = Style.ToFontMetrics(); + + if (Text.Length == 0) + { + return new TextMeasurementResult + { + Width = request.AvailableWidth + }; + } // start breaking text from requested position var text = Text.Substring(request.StartIndex); + var breakingIndex = (int)paint.BreakText(text, request.AvailableWidth); if (breakingIndex <= 0) diff --git a/QuestPDF/Fluent/TextExtensions.cs b/QuestPDF/Fluent/TextExtensions.cs index 1d6b61d..3c076f9 100644 --- a/QuestPDF/Fluent/TextExtensions.cs +++ b/QuestPDF/Fluent/TextExtensions.cs @@ -51,12 +51,9 @@ namespace QuestPDF.Fluent public void Span(string text, TextStyle? style = null) { style ??= DefaultStyle; - - if (string.IsNullOrWhiteSpace(text)) - return; - + var items = text - .Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) + .Split(new[] { Environment.NewLine }, StringSplitOptions.None) .Select(x => new TextBlockSpan { Text = x,