Browse Source

Moved document rendering flags to QuestPDF.Settings class

MarcinZiabek 3 years ago
parent
commit
6a78a4ebdb

+ 2 - 2
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);
         }
     }
 }

+ 2 - 3
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);
         }
     }
 }

+ 9 - 11
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<TCanvas>(PageContext pageContext, TCanvas canvas, Container content, DocumentMetadata documentMetadata, DebuggingState? debuggingState)
+        internal static void RenderPass<TCanvas>(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.";
 

+ 20 - 7
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;
 
-        /// <summary>
-        /// If the number of generated pages exceeds this threshold
-        /// (likely due to infinite layout), the exception is thrown.
-        /// </summary>
-        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();
     }

+ 10 - 0
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;
+    }
+}