Browse Source

Layout testing: drawing grid

Marcin Ziąbek 2 years ago
parent
commit
9748b64e89

+ 58 - 11
Source/QuestPDF.LayoutTests/TestEngine/LayoutTest.cs

@@ -96,26 +96,44 @@ internal class ExpectedLayoutChildPosition
     public Size Size { get; set; }
 }
 
-public static class ElementExtensions
+internal static class ElementExtensions
 {
-    public static void Mock(this IContainer element, string id, float width, float height)
+    public static MockDescriptor Mock(this IContainer element, string id)
     {
-        var mock = new MockChild
+        var mock = new ElementMock
         {
-            Id = id,
-            TotalWidth = width,
-            TotalHeight = height
+            Id = id
         };
         
         element.Element(mock);
+        return new MockDescriptor(mock);
     } 
 }
 
+internal class MockDescriptor
+{
+    private ElementMock Mock { get; }
+
+    public MockDescriptor(ElementMock mock)
+    {
+        Mock = mock;
+    }
+
+    public MockDescriptor Size(float width, float height)
+    {
+        Mock.TotalWidth = width;
+        Mock.TotalHeight = height;
+
+        return this;
+    }
+}
+
 internal class LayoutTest
 {
     private const string DocumentColor = Colors.Grey.Lighten1;
     private const string PageColor = Colors.Grey.Lighten3;
     private const string TargetColor = Colors.White;
+    private const string GridColor = Colors.Grey.Lighten1;
     
     public Size PageSize { get; set; }
     public ICollection<PageDrawingCommand> ActualCommands { get; set; }
@@ -192,7 +210,7 @@ internal class LayoutTest
         }
         
         // extract results
-        var mocks = container.ExtractElementsOfType<MockChild>().Select(x => x.Value); // mock cannot contain another mock, flat structure
+        var mocks = container.ExtractElementsOfType<ElementMock>().Select(x => x.Value); // mock cannot contain another mock, flat structure
 
         return mocks
             .SelectMany(x => x.DrawingCommands)
@@ -203,7 +221,7 @@ internal class LayoutTest
                 Children = x
                     .Select(y => new ChildDrawingCommand
                     {
-                        ChildId = y.ChildId,
+                        ChildId = y.ElementId,
                         Size = y.Size,
                         Position = y.Position
                     })
@@ -349,8 +367,12 @@ internal class LayoutTest
         const int pagePadding = 25;
         var imageInfo = new SKImageInfo((int)pageSize.Width * 2 + pagePadding * 4, (int)(pageSize.Height * matrixHeight + pagePadding * (matrixHeight + 2)));
 
+        const int outputScale = 2;
+        
         using var pdf = SKDocument.CreatePdf(path);
-        using var canvas = pdf.BeginPage(imageInfo.Width, imageInfo.Height);
+        using var canvas = pdf.BeginPage(imageInfo.Width * outputScale, imageInfo.Height * outputScale);
+        
+        canvas.Scale(outputScale, outputScale);
         
         // page background
         canvas.Clear(SKColor.Parse(DocumentColor));
@@ -412,6 +434,27 @@ internal class LayoutTest
             canvas.Restore();
         }
 
+        void DrawPageGrid(Size pageSize)
+        {
+            using var paint = new SKPaint
+            {
+                Color = SKColor.Parse(GridColor),
+                StrokeWidth = 1
+            };
+
+            const float GridSize = 10f;
+            
+            foreach (var i in Enumerable.Range(1, (int)Math.Floor(pageSize.Width / GridSize)))
+            {
+                canvas.DrawLine(new SKPoint(i * GridSize, 0), new SKPoint(i * GridSize, pageSize.Height), paint);
+            }
+            
+            foreach (var i in Enumerable.Range(1, (int)Math.Floor(pageSize.Height / GridSize)))
+            {
+                canvas.DrawLine(new SKPoint(0, i * GridSize), new SKPoint(pageSize.Width, i * GridSize), paint);
+            }
+        }
+
         void DrawPage(PageDrawingCommand command)
         {
             // available area
@@ -430,18 +473,22 @@ internal class LayoutTest
             
             canvas.DrawRect(0, 0, command.RequiredArea.Width, command.RequiredArea.Height, takenAreaPaint);
         
+            DrawPageGrid(pageSize);
+            
             // draw children
             foreach (var child in command.Children)
             {
                 canvas.Save();
 
+                const float strokeWidth = 3f;
+
                 var color = colors[child.ChildId];
             
                 using var childBorderPaint = new SKPaint
                 {
                     Color = SKColor.Parse(color),
                     IsStroke = true,
-                    StrokeWidth = 2
+                    StrokeWidth = strokeWidth
                 };
             
                 using var childAreaPaint = new SKPaint
@@ -451,7 +498,7 @@ internal class LayoutTest
             
                 canvas.Translate(child.Position.X, child.Position.Y);
                 canvas.DrawRect(0, 0, child.Size.Width, child.Size.Height, childAreaPaint);
-                canvas.DrawRect(0, 0, child.Size.Width, child.Size.Height, childBorderPaint);
+                canvas.DrawRect(strokeWidth / 2, strokeWidth / 2, child.Size.Width - strokeWidth, child.Size.Height - strokeWidth, childBorderPaint);
             
                 canvas.Restore();
             }

+ 3 - 3
Source/QuestPDF.LayoutTests/TestEngine/MockChild.cs

@@ -6,13 +6,13 @@ namespace QuestPDF.LayoutTests.TestEngine;
 
 internal class MockDrawingCommand
 {
-    public string ChildId { get; set; }
+    public string ElementId { get; set; }
     public int PageNumber { get; set; }
     public Position Position { get; set; }
     public Size Size { get; set; }
 }
 
-internal class MockChild : Element
+internal class ElementMock : Element
 {
     public string Id { get; set; }
     public string Color { get; set; } = Placeholders.Color();
@@ -59,7 +59,7 @@ internal class MockChild : Element
         
         DrawingCommands.Add(new MockDrawingCommand
         {
-            ChildId = Id,
+            ElementId = Id,
             PageNumber = PageContext.CurrentPage,
             Position = new Position(matrix.TransX / matrix.ScaleX, matrix.TransY / matrix.ScaleY),
             Size = size

+ 12 - 12
Source/QuestPDF.LayoutTests/UnitTest1.cs

@@ -24,14 +24,14 @@ public class Tests
                 {
                     column.Spacing(25);
 
-                    column.Item().Mock("a", 150, 200);
-                    column.Item().Mock("b", 150, 150);
-                    column.Item().Mock("c", 150, 100);
-                    column.Item().Mock("d", 150, 150);
-                    column.Item().Mock("e", 150, 300);
-                    column.Item().Mock("f", 150, 150);
-                    column.Item().Mock("g", 150, 100);
-                    column.Item().Mock("h", 150, 500);
+                    column.Item().Mock("a").Size(150, 200);
+                    column.Item().Mock("b").Size(150, 150);
+                    column.Item().Mock("c").Size(150, 100);
+                    column.Item().Mock("d").Size(150, 150);
+                    column.Item().Mock("e").Size(150, 300);
+                    column.Item().Mock("f").Size(150, 150);
+                    column.Item().Mock("g").Size(150, 100);
+                    column.Item().Mock("h").Size(150, 500);
                 });
             })
             .ExpectedDrawResult(document =>
@@ -70,8 +70,8 @@ public class Tests
                 {
                     column.Spacing(25);
 
-                    column.Item().Mock("a", 150, 150);
-                    column.Item().Mock("b", 125, 100);
+                    column.Item().Mock("a").Size(150, 150);
+                    column.Item().Mock("b").Size(125, 100);
                 });
             })
             .ExpectedDrawResult(document =>
@@ -105,8 +105,8 @@ public class Tests
             {
                 content.Layers(layers =>
                 {
-                    layers.Layer().Mock("a", 100, 150);
-                    layers.PrimaryLayer().Mock("b", 150, 100);
+                    layers.Layer().Mock("a").Size(100, 150);
+                    layers.PrimaryLayer().Mock("b").Size(150, 100);
                 });
             })
             .ExpectedDrawResult(document =>