Browse Source

fix(#704) + code refactoring of the layout issue page marker

Marcin Ziąbek 2 years ago
parent
commit
a4e7b58d9e

+ 1 - 36
Source/QuestPDF/Drawing/DocumentGenerator.cs

@@ -210,9 +210,9 @@ namespace QuestPDF.Drawing
 
                 try
                 {
+                    pageContext.IncrementPageNumber();
                     canvas.BeginPage(spacePlan);
                     content.Draw(spacePlan);
-                    pageContext.IncrementPageNumber();
                 }
                 catch (Exception exception)
                 {
@@ -226,12 +226,6 @@ namespace QuestPDF.Drawing
                     break;
             }
 
-            if (Settings.EnableDebugging)
-            {
-                ConfigureLayoutOverflowMarker();
-                CheckIfDocumentHasLayoutOverflowIssues();
-            }
-
             void ApplyLayoutDebugging()
             {
                 content.RemoveExistingProxies();
@@ -247,35 +241,6 @@ namespace QuestPDF.Drawing
 
                 content.RemoveExistingProxies();
             }
-
-            void ConfigureLayoutOverflowMarker()
-            {
-                var layoutOverflowPageMarker = new LayoutOverflowPageMarker();
-                    
-                content.CreateProxy(child =>
-                {
-                    layoutOverflowPageMarker.Child = child;
-                    return layoutOverflowPageMarker;
-                });
-
-                var pageNumbersWithLayoutIssues = content
-                    .ExtractElementsOfType<LayoutOverflowVisualization>()
-                    .SelectMany(x => x.Flatten())
-                    .SelectMany(x => x.Value.VisibleOnPageNumbers)
-                    .Distinct();
-
-                layoutOverflowPageMarker.PageNumbersWithLayoutIssues = new HashSet<int>(pageNumbersWithLayoutIssues);
-            }
-
-            void CheckIfDocumentHasLayoutOverflowIssues()
-            {
-                var hasLayoutOverflowVisualizationElements = content
-                    .ExtractElementsOfType<LayoutOverflowVisualization>()
-                    .SelectMany(x => x.Flatten())
-                    .Any();
-
-                canvas.DocumentContentHasLayoutOverflowIssues |= hasLayoutOverflowVisualizationElements;
-            }
             
             void ThrowLayoutException()
             {

+ 5 - 0
Source/QuestPDF/Drawing/FreeCanvas.cs

@@ -29,6 +29,11 @@ namespace QuestPDF.Drawing
             
         }
 
+        public void MarkCurrentPageAsHavingLayoutIssues()
+        {
+            
+        }
+
         #endregion
 
         #region ICanvas

+ 47 - 3
Source/QuestPDF/Drawing/SkiaCanvasBase.cs

@@ -1,6 +1,6 @@
+using QuestPDF.Helpers;
 using QuestPDF.Infrastructure;
 using SkiaSharp;
-using SkiaSharp.HarfBuzz;
 
 namespace QuestPDF.Drawing
 {
@@ -8,13 +8,55 @@ namespace QuestPDF.Drawing
     {
         internal SKCanvas Canvas { get; set; }
 
+        #region IRenderingCanvas
+        
         public bool DocumentContentHasLayoutOverflowIssues { get; set; }
         
+        private Size CurrentPageSize { get; set; } = Size.Zero;
+        private bool CurrentPageHasLayoutIssues { get; set; }
+        
         public abstract void BeginDocument();
         public abstract void EndDocument();
+
+        public virtual void BeginPage(Size size)
+        {
+            CurrentPageSize = size;
+            CurrentPageHasLayoutIssues = false;
+        }
+
+        public virtual void EndPage()
+        {
+            if (CurrentPageHasLayoutIssues)
+                DrawLayoutIssuesIndicatorOnCurrentPage();
+        }
+
+        public void MarkCurrentPageAsHavingLayoutIssues()
+        {
+            CurrentPageHasLayoutIssues = true;
+            DocumentContentHasLayoutOverflowIssues = true;
+        }
         
-        public abstract void BeginPage(Size size);
-        public abstract void EndPage();
+        private void DrawLayoutIssuesIndicatorOnCurrentPage()
+        {
+            // visual configuration
+            const string lineColor = Colors.Red.Medium;
+            const byte lineOpacity = 64;
+            const float borderThickness = 24f;
+        
+            // implementation
+            using var indicatorPaint = new SKPaint
+            {
+                StrokeWidth = borderThickness * 2, // half of the width will be outside of the page area
+                Color = SKColor.Parse(lineColor).WithAlpha(lineOpacity),
+                IsStroke = true
+            };
+        
+            Canvas.DrawRect(0, 0, CurrentPageSize.Width, CurrentPageSize.Height, indicatorPaint);
+        }
+        
+        #endregion
+        
+        #region ICanvas
         
         public void Translate(Position vector)
         {
@@ -64,5 +106,7 @@ namespace QuestPDF.Drawing
         {
             Canvas.Scale(scaleX, scaleY);
         }
+        
+        #endregion
     }
 }

+ 4 - 0
Source/QuestPDF/Drawing/SkiaDocumentCanvasBase.cs

@@ -33,10 +33,14 @@ namespace QuestPDF.Drawing
         public override void BeginPage(Size size)
         {
             Canvas = Document.BeginPage(size.Width, size.Height);
+            
+            base.BeginPage(size);
         }
 
         public override void EndPage()
         {
+            base.EndPage();
+            
             Document.EndPage();
             Canvas.Dispose();
         }

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

@@ -1,44 +0,0 @@
-using System;
-using System.Collections.Generic;
-using QuestPDF.Drawing;
-using QuestPDF.Fluent;
-using QuestPDF.Helpers;
-using QuestPDF.Infrastructure;
-using SkiaSharp;
-
-namespace QuestPDF.Elements;
-
-internal class LayoutOverflowPageMarker : ContainerElement
-{
-    public HashSet<int> PageNumbersWithLayoutIssues { get; set; } = new();
-    
-    private const string LineColor = Colors.Red.Medium;
-    private const byte LineOpacity = 64;
-    private const float BorderThickness = 24f;
-
-    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(LineOpacity),
-            IsStroke = true
-        };
-        
-        var skiaCanvas = canvasBase.Canvas;
-        skiaCanvas.DrawRect(0, 0, availableSpace.Width, availableSpace.Height, indicatorPaint);
-    }
-}

+ 3 - 3
Source/QuestPDF/Elements/LayoutOverflowVisualization.cs

@@ -18,7 +18,6 @@ internal class LayoutOverflowVisualization : ContainerElement, IContentDirection
     private const byte AreaOpacity = 64;
 
     public ContentDirection ContentDirection { get; set; }
-    public ICollection<int> VisibleOnPageNumbers { get; set; } = new List<int>();
 
     internal override SpacePlan Measure(Size availableSpace)
     {
@@ -32,8 +31,6 @@ internal class LayoutOverflowVisualization : ContainerElement, IContentDirection
         
     internal override void Draw(Size availableSpace)
     {
-        VisibleOnPageNumbers.Add(PageContext.CurrentPage);
-        
         // measure content area
         var childSize = base.Measure(availableSpace);
         
@@ -43,6 +40,9 @@ internal class LayoutOverflowVisualization : ContainerElement, IContentDirection
             return;
         }
         
+        if (Canvas is SkiaCanvasBase skiaCanvasBase)
+            skiaCanvasBase.MarkCurrentPageAsHavingLayoutIssues();
+        
         // check overflow area
         var contentSize = 
             TryVerticalOverflow(availableSpace) 

+ 1 - 0
Source/QuestPDF/Infrastructure/IRenderingCanvas.cs

@@ -3,6 +3,7 @@
     internal interface IRenderingCanvas
     {
         bool DocumentContentHasLayoutOverflowIssues { get; set; }
+        void MarkCurrentPageAsHavingLayoutIssues();
         
         void BeginDocument();
         void EndDocument();

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

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