Browse Source

Pages with layout overflow visualization should be additionally marked for better visibility

Marcin Ziąbek 2 years ago
parent
commit
147ab143fd

+ 17 - 2
Source/QuestPDF/Drawing/DocumentGenerator.cs

@@ -187,11 +187,13 @@ namespace QuestPDF.Drawing
             return content;
             return content;
         }
         }
 
 
-        private static void RenderPass<TCanvas>(PageContext pageContext, TCanvas canvas, Container content, DebuggingState? debuggingState)
+        private static void RenderPass<TCanvas>(PageContext pageContext, TCanvas canvas, ContainerElement content, DebuggingState? debuggingState)
             where TCanvas : ICanvas, IRenderingCanvas
             where TCanvas : ICanvas, IRenderingCanvas
         {
         {
             content.InjectDependencies(pageContext, canvas);
             content.InjectDependencies(pageContext, canvas);
             content.VisitChildren(x => (x as IStateResettable)?.ResetState());
             content.VisitChildren(x => (x as IStateResettable)?.ResetState());
+
+            LayoutOverflowPageMarker? layoutOverflowPageMarker = null;
             
             
             while(true)
             while(true)
             {
             {
@@ -211,9 +213,9 @@ namespace QuestPDF.Drawing
 
 
                 try
                 try
                 {
                 {
-                    pageContext.IncrementPageNumber();
                     canvas.BeginPage(spacePlan);
                     canvas.BeginPage(spacePlan);
                     content.Draw(spacePlan);
                     content.Draw(spacePlan);
+                    pageContext.IncrementPageNumber();
                 }
                 }
                 catch (Exception exception)
                 catch (Exception exception)
                 {
                 {
@@ -241,6 +243,19 @@ namespace QuestPDF.Drawing
 
 
             void ApplyLayoutDebugging()
             void ApplyLayoutDebugging()
             {
             {
+                if (layoutOverflowPageMarker == null)
+                {
+                    layoutOverflowPageMarker = new LayoutOverflowPageMarker();
+                    
+                    content.CreateProxy(child =>
+                    {
+                        layoutOverflowPageMarker.Child = child;
+                        return layoutOverflowPageMarker;
+                    });
+                }
+                
+                layoutOverflowPageMarker.PageNumbersWithLayoutIssues.Add(pageContext.CurrentPage);
+                
                 content.RemoveExistingProxies();
                 content.RemoveExistingProxies();
 
 
                 content.ApplyLayoutOverflowDetection();
                 content.ApplyLayoutOverflowDetection();

+ 42 - 0
Source/QuestPDF/Elements/LayoutOverflowPageMarker.cs

@@ -0,0 +1,42 @@
+using System;
+using System.Collections.Generic;
+using QuestPDF.Drawing;
+using QuestPDF.Helpers;
+using QuestPDF.Infrastructure;
+using SkiaSharp;
+
+namespace QuestPDF.Elements;
+
+internal class LayoutOverflowPageMarker : ContainerElement
+{
+    public HashSet<int> PageNumbersWithLayoutIssues { get; } = new();
+    
+    private const string LineColor = Colors.Red.Medium;
+    private const float BorderThickness = 16f;
+    
+    internal override void Draw(Size availableSpace)
+    {
+        Child?.Draw(availableSpace);
+        
+        if (!PageNumbersWithLayoutIssues.Contains(PageContext.CurrentPage))
+            return;
+
+        DrawPageIndication(availableSpace);
+    }
+
+    private void DrawPageIndication(Size availableSpace)
+    {
+        if (Canvas is not SkiaCanvasBase canvasBase)
+            return;
+
+        using var indicatorPaint = new SKPaint
+        {
+            StrokeWidth = BorderThickness * 2, // half of the width will be outside of the page area
+            Color = SKColor.Parse(LineColor).WithAlpha(128),
+            IsStroke = true
+        };
+        
+        var skiaCanvas = canvasBase.Canvas;
+        skiaCanvas.DrawRect(0, 0, availableSpace.Width, availableSpace.Height, indicatorPaint);
+    }
+}

+ 2 - 1
Source/QuestPDF/Infrastructure/PageContext.cs

@@ -14,11 +14,12 @@ namespace QuestPDF.Infrastructure
         internal void SetDocumentId(int id)
         internal void SetDocumentId(int id)
         {
         {
             CurrentDocumentId = id;
             CurrentDocumentId = id;
+            ResetPageNumber();
         }
         }
         
         
         internal void ResetPageNumber()
         internal void ResetPageNumber()
         {
         {
-            CurrentPage = 0;
+            CurrentPage = 1;
         }
         }
         
         
         internal void IncrementPageNumber()
         internal void IncrementPageNumber()