Skip to content

Commit

Permalink
Improved layout stability for dynamic components that use the TotalPa…
Browse files Browse the repository at this point in the history
…ges property to generate content
  • Loading branch information
MarcinZiabek committed Jan 1, 2024
1 parent 7358e70 commit 890ec24
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static LayoutTestResult.DocumentLayout Execute(Size pageSize, Container c
{
// inject dependencies
var pageContext = new PageContext();
pageContext.ResetPageNumber();
pageContext.ProceedToNextRenderingPhase();

var canvas = new PreviewerCanvas();

Expand Down
6 changes: 3 additions & 3 deletions Source/QuestPDF/Drawing/DocumentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private static void RenderSingleDocument<TCanvas>(TCanvas canvas, IDocument docu

var pageContext = new PageContext();
RenderPass(pageContext, new FreeCanvas(), content);
pageContext.ResetPageNumber();
pageContext.ProceedToNextRenderingPhase();
RenderPass(pageContext, canvas, content);
}

Expand Down Expand Up @@ -147,7 +147,7 @@ private static void RenderMergedDocument<TCanvas>(TCanvas canvas, MergedDocument
RenderPass(documentPageContext, new FreeCanvas(), documentPart.Content);
}

documentPageContext.ResetPageNumber();
documentPageContext.ProceedToNextRenderingPhase();

foreach (var documentPart in documentParts)
{
Expand All @@ -163,7 +163,7 @@ private static void RenderMergedDocument<TCanvas>(TCanvas canvas, MergedDocument
pageContext.SetDocumentId(documentPart.DocumentId);

RenderPass(pageContext, new FreeCanvas(), documentPart.Content);
pageContext.ResetPageNumber();
pageContext.ProceedToNextRenderingPhase();
RenderPass(pageContext, canvas, documentPart.Content);
}
}
Expand Down
9 changes: 6 additions & 3 deletions Source/QuestPDF/Elements/Dynamic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private DynamicComponentComposeResult GetContent(Size availableSize, bool accept
UseOriginalImage = UseOriginalImage,

PageNumber = PageContext.CurrentPage,
TotalPages = PageContext.DocumentLength,
TotalPages = PageContext.IsInitialRenderingPhase ? int.MaxValue : PageContext.DocumentLength,
AvailableSize = availableSize
};

Expand Down Expand Up @@ -104,8 +104,11 @@ public class DynamicContext
/// Returns the total count of pages in the document.
/// </summary>
/// <remarks>
/// Document rendering occurs in two phases.
/// The value of this property might be imprecise during the initial rendering phase.
/// <para>
/// Document rendering process is performed in two phases.
/// During the first phase, the value of this property is equal to <c>int.MaxValue</c> to indicate its unavailability.
/// </para>
/// <para>Please note that using this property may result with unstable layouts and unpredicted behaviors, especially when generating conditional content of various sizes.</para>
/// </remarks>
public int TotalPages { get; internal set; }

Expand Down
1 change: 1 addition & 0 deletions Source/QuestPDF/Infrastructure/IPageContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal sealed class DocumentLocation

internal interface IPageContext
{
bool IsInitialRenderingPhase { get; }
int DocumentLength { get; }
int CurrentPage { get; }
void SetSectionPage(string name);
Expand Down
4 changes: 3 additions & 1 deletion Source/QuestPDF/Infrastructure/PageContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace QuestPDF.Infrastructure
{
internal sealed class PageContext : IPageContext
{
public bool IsInitialRenderingPhase { get; private set; } = true;
public int DocumentLength { get; private set; }
private List<DocumentLocation> Locations { get; } = new();

Expand All @@ -16,8 +17,9 @@ internal void SetDocumentId(int id)
CurrentDocumentId = id;
}

internal void ResetPageNumber()
internal void ProceedToNextRenderingPhase()
{
IsInitialRenderingPhase = false;
CurrentPage = 0;
}

Expand Down

0 comments on commit 890ec24

Please sign in to comment.