diff --git a/QuestPDF.Examples/TextExamples.cs b/QuestPDF.Examples/TextExamples.cs index eb1973a..3381b6f 100644 --- a/QuestPDF.Examples/TextExamples.cs +++ b/QuestPDF.Examples/TextExamples.cs @@ -371,5 +371,35 @@ namespace QuestPDF.Examples }); }); } + + [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(null); + + column.Item().Text(text => + { + text.DefaultTextStyle(x => x.BackgroundColor(Colors.Red.Lighten3)); + text.Span(" " + Placeholders.LoremIpsum() + " 0123456789012345678901234567890123456789012345678901234567890123456789 ").FontSize(24); + }); + }); + }); + }); + } } } \ No newline at end of file diff --git a/QuestPDF/Elements/Text/Calculation/TextMeasurementRequest.cs b/QuestPDF/Elements/Text/Calculation/TextMeasurementRequest.cs index cd578ee..86355be 100644 --- a/QuestPDF/Elements/Text/Calculation/TextMeasurementRequest.cs +++ b/QuestPDF/Elements/Text/Calculation/TextMeasurementRequest.cs @@ -9,6 +9,8 @@ namespace QuestPDF.Elements.Text.Calculation public int StartIndex { get; set; } public float AvailableWidth { get; set; } + + public bool IsFirstElementInBlock { get; set; } public bool IsFirstElementInLine { get; set; } } } \ No newline at end of file diff --git a/QuestPDF/Elements/Text/Calculation/TextMeasurementResult.cs b/QuestPDF/Elements/Text/Calculation/TextMeasurementResult.cs index c501131..a831ae4 100644 --- a/QuestPDF/Elements/Text/Calculation/TextMeasurementResult.cs +++ b/QuestPDF/Elements/Text/Calculation/TextMeasurementResult.cs @@ -14,9 +14,8 @@ namespace QuestPDF.Elements.Text.Calculation public int StartIndex { get; set; } public int EndIndex { get; set; } - public int NextIndex { get; set; } public int TotalIndex { get; set; } - public bool IsLast => NextIndex == EndIndex; + public bool IsLast => EndIndex == TotalIndex; } } \ No newline at end of file diff --git a/QuestPDF/Elements/Text/Items/TextBlockSpan.cs b/QuestPDF/Elements/Text/Items/TextBlockSpan.cs index 904d56a..69d0ce3 100644 --- a/QuestPDF/Elements/Text/Items/TextBlockSpan.cs +++ b/QuestPDF/Elements/Text/Items/TextBlockSpan.cs @@ -35,7 +35,7 @@ namespace QuestPDF.Elements.Text.Items // if the element is the first one within the line, // ignore leading spaces - if (request.IsFirstElementInLine) + if (!request.IsFirstElementInBlock && request.IsFirstElementInLine) { while (startIndex < Text.Length && Text[startIndex] == space) startIndex++; @@ -72,12 +72,7 @@ namespace QuestPDF.Elements.Text.Items text = text.Slice(0, textLength); var endIndex = startIndex + textLength; - var nextIndex = endIndex; - // when breaking text, omit spaces at the end of the line - while (nextIndex < Text.Length && Text[nextIndex] == space) - nextIndex++; - // measure final text var width = paint.MeasureText(text); @@ -92,7 +87,6 @@ namespace QuestPDF.Elements.Text.Items StartIndex = startIndex, EndIndex = endIndex, - NextIndex = nextIndex, TotalIndex = Text.Length }; diff --git a/QuestPDF/Elements/Text/TextBlock.cs b/QuestPDF/Elements/Text/TextBlock.cs index 4c0b184..d355132 100644 --- a/QuestPDF/Elements/Text/TextBlock.cs +++ b/QuestPDF/Elements/Text/TextBlock.cs @@ -108,7 +108,7 @@ namespace QuestPDF.Elements.Text .ForEach(x => RenderingQueue.Dequeue()); var lastElementMeasurement = lines.Last().Elements.Last().Measurement; - CurrentElementIndex = lastElementMeasurement.IsLast ? 0 : lastElementMeasurement.NextIndex; + CurrentElementIndex = lastElementMeasurement.IsLast ? 0 : lastElementMeasurement.EndIndex; if (!RenderingQueue.Any()) ResetState(); @@ -170,6 +170,8 @@ namespace QuestPDF.Elements.Text StartIndex = currentItemIndex, AvailableWidth = availableWidth - currentWidth, + + IsFirstElementInBlock = currentElement == Items.First(), IsFirstElementInLine = !currentLineElements.Any() }; @@ -185,7 +187,7 @@ namespace QuestPDF.Elements.Text }); currentWidth += measurementResponse.Width; - currentItemIndex = measurementResponse.NextIndex; + currentItemIndex = measurementResponse.EndIndex + 1; if (!measurementResponse.IsLast) break;