Skip to content

Commit

Permalink
Text element: improved the default sizing behavior for the Text eleme…
Browse files Browse the repository at this point in the history
…nt. Now, when the text is empty, it works more consistently by taking up zero width while still reserving vertical space based on the font size.
  • Loading branch information
MarcinZiabek committed Jun 13, 2024
1 parent 64867b0 commit 32e2b1e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
37 changes: 36 additions & 1 deletion Source/QuestPDF/Elements/Text/TextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using QuestPDF.Drawing;
using QuestPDF.Drawing.Exceptions;
using QuestPDF.Elements.Text.Items;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
using QuestPDF.Skia;
using QuestPDF.Skia.Text;
Expand Down Expand Up @@ -37,6 +36,7 @@ internal sealed class TextBlock : Element, IStateResettable, IContentDirectionAw
private float MaximumWidth { get; set; }

private bool IsRendered { get; set; }
private bool? ContainsOnlyWhiteSpace { get; set; }
private int CurrentLineIndex { get; set; }
private float CurrentTopOffset { get; set; }

Expand All @@ -61,6 +61,13 @@ internal override SpacePlan Measure(Size availableSpace)

if (IsRendered)
return SpacePlan.Empty();

// if the text block does not contain any items, or all items are null, return SpacePlan.Empty
// but if the text block contains only whitespace, return SpacePlan.FullRender with zero width and font-based height
ContainsOnlyWhiteSpace ??= Items.All(x => x is TextBlockSpan textBlockSpan && string.IsNullOrWhiteSpace(textBlockSpan.Text));

if (ContainsOnlyWhiteSpace == true)
return SpacePlan.FullRender(0, MeasureHeightOfParagraphContainingOnlyWhiteSpace());

Initialize();

Expand Down Expand Up @@ -108,6 +115,9 @@ internal override void Draw(Size availableSpace)
if (IsRendered)
return;

if (ContainsOnlyWhiteSpace == true)
return;

CalculateParagraphMetrics(availableSpace);

if (MaximumWidth == 0)
Expand Down Expand Up @@ -501,5 +511,30 @@ private void CheckUnresolvedGlyphs()
$"You can disable this check by setting the 'Settings.CheckIfAllTextGlyphsAreAvailable' option to 'false'. \n" +
$"However, this may result with text glyphs being incorrectly rendered without any warning.");
}

private float MeasureHeightOfParagraphContainingOnlyWhiteSpace()
{
var paragraphStyle = new ParagraphStyleConfiguration
{
Alignment = ParagraphStyleConfiguration.TextAlign.Start,
Direction = ParagraphStyleConfiguration.TextDirection.Ltr
};

var builder = SkParagraphBuilderPoolManager.Get(paragraphStyle);

try
{
foreach (var textBlockSpan in Items.OfType<TextBlockSpan>())
builder.AddText("\u00A0", textBlockSpan.Style.GetSkTextStyle()); // non-breaking space

var paragraph = builder.CreateParagraph();
paragraph.PlanLayout(1000);
return paragraph.GetLineMetrics().First().Height;
}
finally
{
SkParagraphBuilderPoolManager.Return(builder);
}
}
}
}
8 changes: 4 additions & 4 deletions Source/QuestPDF/Fluent/TextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public void Span(string? text, TextStyle style)
/// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.returns.spanDescriptor"]/*' />
public TextSpanDescriptor Span(string? text)
{
if (IsNullOrEmpty(text))
if (text == null)
return new TextSpanDescriptor(new TextBlockSpan());

var textSpan = new TextBlockSpan() { Text = text };
Expand Down Expand Up @@ -352,7 +352,7 @@ public TextSpanDescriptor SectionLink(string? text, string sectionName)
if (IsNullOrEmpty(sectionName))
throw new ArgumentException("Section name cannot be null or empty", nameof(sectionName));

if (IsNullOrEmpty(text))
if (text == null)
return new TextSpanDescriptor(new TextBlockSpan());

var textBlockItem = new TextBlockSectionLink
Expand Down Expand Up @@ -381,7 +381,7 @@ public TextSpanDescriptor Hyperlink(string? text, string url)
if (IsNullOrEmpty(url))
throw new ArgumentException("Url cannot be null or empty", nameof(url));

if (IsNullOrEmpty(text))
if (text == null)
return new TextSpanDescriptor(new TextBlockSpan());

var textBlockItem = new TextBlockHyperlink
Expand Down Expand Up @@ -478,7 +478,7 @@ public static TextSpanDescriptor Text(this IContainer element, object? text)
/// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.returns.spanDescriptor"]/*' />
public static TextBlockDescriptor Text(this IContainer container, string? text)
{
if (IsNullOrEmpty(text))
if (text == null)
return new TextBlockDescriptor(new TextBlock(), new TextBlockSpan());

var textBlock = new TextBlock();
Expand Down
4 changes: 4 additions & 0 deletions Source/QuestPDF/Resources/ReleaseNotes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ Bug Fixes:
- Column Element: Resolved instability issues in nested layouts with spacing and zero-sized elements.
- JPEG Quality: Disabled JPEG image downsampling/downscaling to maintain the highest quality levels.
- Image Compression: Disabled additional image compression performed by Skia.


Version 2024.6.1
- Text element: improved the default sizing behavior for the Text element. Now, when the text is empty, it works more consistently by taking up zero width while still reserving vertical space based on the font size.

0 comments on commit 32e2b1e

Please sign in to comment.