From 6a78a4ebdb25db39d497408d8ef9e6edeb5eb713 Mon Sep 17 00:00:00 2001 From: MarcinZiabek Date: Tue, 6 Sep 2022 13:45:48 +0200 Subject: [PATCH] Moved document rendering flags to QuestPDF.Settings class --- QuestPDF.ReportSample/PerformanceTests.cs | 4 ++-- QuestPDF.ReportSample/Tests.cs | 5 ++--- QuestPDF/Drawing/DocumentGenerator.cs | 20 ++++++++--------- QuestPDF/Drawing/DocumentMetadata.cs | 27 +++++++++++++++++------ QuestPDF/Infrastructure/Settings.cs | 10 +++++++++ 5 files changed, 43 insertions(+), 23 deletions(-) create mode 100644 QuestPDF/Infrastructure/Settings.cs diff --git a/QuestPDF.ReportSample/PerformanceTests.cs b/QuestPDF.ReportSample/PerformanceTests.cs index 90f314b..2d2a27e 100644 --- a/QuestPDF.ReportSample/PerformanceTests.cs +++ b/QuestPDF.ReportSample/PerformanceTests.cs @@ -51,7 +51,7 @@ namespace QuestPDF.ReportSample Content = documentContainer.Compose(); PageContext = new PageContext(); - DocumentGenerator.RenderPass(PageContext, new FreeCanvas(), Content, Metadata, null); + DocumentGenerator.RenderPass(PageContext, new FreeCanvas(), Content, null); var sw = new Stopwatch(); sw.Start(); @@ -69,7 +69,7 @@ namespace QuestPDF.ReportSample [Benchmark] public void GenerationTest() { - DocumentGenerator.RenderPass(PageContext, new FreeCanvas(), Content, Metadata, null); + DocumentGenerator.RenderPass(PageContext, new FreeCanvas(), Content, null); } } } \ No newline at end of file diff --git a/QuestPDF.ReportSample/Tests.cs b/QuestPDF.ReportSample/Tests.cs index 111df78..b460e9e 100644 --- a/QuestPDF.ReportSample/Tests.cs +++ b/QuestPDF.ReportSample/Tests.cs @@ -48,11 +48,10 @@ namespace QuestPDF.ReportSample Report.Compose(container); var content = container.Compose(); - var metadata = Report.GetMetadata(); var pageContext = new PageContext(); - DocumentGenerator.RenderPass(pageContext, new FreeCanvas(), content, metadata, null); - DocumentGenerator.RenderPass(pageContext, new FreeCanvas(), content, metadata, null); + DocumentGenerator.RenderPass(pageContext, new FreeCanvas(), content, null); + DocumentGenerator.RenderPass(pageContext, new FreeCanvas(), content, null); } } } \ No newline at end of file diff --git a/QuestPDF/Drawing/DocumentGenerator.cs b/QuestPDF/Drawing/DocumentGenerator.cs index 3fe373c..c24e285 100644 --- a/QuestPDF/Drawing/DocumentGenerator.cs +++ b/QuestPDF/Drawing/DocumentGenerator.cs @@ -66,19 +66,17 @@ namespace QuestPDF.Drawing var content = container.Compose(); ApplyDefaultTextStyle(content, TextStyle.LibraryDefault); - var metadata = document.GetMetadata(); - var pageContext = new PageContext(); - - var debuggingState = metadata.ApplyDebugging ? ApplyDebugging(content) : null; + var debuggingState = Settings.EnableDebugging ? ApplyDebugging(content) : null; - if (metadata.ApplyCaching) + if (Settings.EnableCaching) ApplyCaching(content); - RenderPass(pageContext, new FreeCanvas(), content, metadata, debuggingState); - RenderPass(pageContext, canvas, content, metadata, debuggingState); + var pageContext = new PageContext(); + RenderPass(pageContext, new FreeCanvas(), content, debuggingState); + RenderPass(pageContext, canvas, content, debuggingState); } - internal static void RenderPass(PageContext pageContext, TCanvas canvas, Container content, DocumentMetadata documentMetadata, DebuggingState? debuggingState) + internal static void RenderPass(PageContext pageContext, TCanvas canvas, Container content, DebuggingState? debuggingState) where TCanvas : ICanvas, IRenderingCanvas { content.VisitChildren(x => x?.Initialize(pageContext, canvas)); @@ -114,7 +112,7 @@ namespace QuestPDF.Drawing canvas.EndPage(); - if (currentPage >= documentMetadata.DocumentLayoutExceptionThreshold) + if (currentPage >= Settings.DocumentLayoutExceptionThreshold) { canvas.EndDocument(); ThrowLayoutException(); @@ -131,8 +129,8 @@ namespace QuestPDF.Drawing void ThrowLayoutException() { var message = $"Composed layout generates infinite document. This may happen in two cases. " + - $"1) Your document and its layout configuration is correct but the content takes more than {documentMetadata.DocumentLayoutExceptionThreshold} pages. " + - $"In this case, please increase the value {nameof(DocumentMetadata)}.{nameof(DocumentMetadata.DocumentLayoutExceptionThreshold)} property configured in the {nameof(IDocument.GetMetadata)} method. " + + $"1) Your document and its layout configuration is correct but the content takes more than {Settings.DocumentLayoutExceptionThreshold} pages. " + + $"In this case, please increase the value {nameof(QuestPDF)}.{nameof(Settings)}.{nameof(Settings.DocumentLayoutExceptionThreshold)} static property. " + $"2) The layout configuration of your document is invalid. Some of the elements require more space than is provided." + $"Please analyze your documents structure to detect this element and fix its size constraints."; diff --git a/QuestPDF/Drawing/DocumentMetadata.cs b/QuestPDF/Drawing/DocumentMetadata.cs index 5db70ff..43420f6 100644 --- a/QuestPDF/Drawing/DocumentMetadata.cs +++ b/QuestPDF/Drawing/DocumentMetadata.cs @@ -1,4 +1,5 @@ using System; +using QuestPDF.Infrastructure; namespace QuestPDF.Drawing { @@ -18,14 +19,26 @@ namespace QuestPDF.Drawing public DateTime CreationDate { get; set; } = DateTime.Now; public DateTime ModifiedDate { get; set; } = DateTime.Now; - /// - /// If the number of generated pages exceeds this threshold - /// (likely due to infinite layout), the exception is thrown. - /// - public int DocumentLayoutExceptionThreshold { get; set; } = 250; + [Obsolete("This API has been moved since version 2022.9. Please use the QuestPDF.Settings.DocumentLayoutExceptionThreshold static property.")] + public int DocumentLayoutExceptionThreshold + { + get => Settings.DocumentLayoutExceptionThreshold; + set => Settings.DocumentLayoutExceptionThreshold = value; + } - public bool ApplyCaching { get; set; } = !System.Diagnostics.Debugger.IsAttached; - public bool ApplyDebugging { get; set; } = System.Diagnostics.Debugger.IsAttached; + [Obsolete("This API has been moved since version 2022.9. Please use the QuestPDF.Settings.EnableCaching static property.")] + public bool ApplyCaching + { + get => Settings.EnableCaching; + set => Settings.EnableCaching = value; + } + + [Obsolete("This API has been moved since version 2022.9. Please use the QuestPDF.Settings.EnableDebugging static property.")] + public bool ApplyDebugging + { + get => Settings.EnableDebugging; + set => Settings.EnableDebugging = value; + } public static DocumentMetadata Default => new DocumentMetadata(); } diff --git a/QuestPDF/Infrastructure/Settings.cs b/QuestPDF/Infrastructure/Settings.cs new file mode 100644 index 0000000..7787f97 --- /dev/null +++ b/QuestPDF/Infrastructure/Settings.cs @@ -0,0 +1,10 @@ +namespace QuestPDF.Infrastructure +{ + public static class Settings + { + public static int DocumentLayoutExceptionThreshold { get; set; } = 250; + + public static bool EnableCaching { get; set; } = !System.Diagnostics.Debugger.IsAttached; + public static bool EnableDebugging { get; set; } = System.Diagnostics.Debugger.IsAttached; + } +} \ No newline at end of file