Browse Source

Table: fixed zindex rendering issue (#554)

Marcin Ziąbek 2 years ago
parent
commit
d1a7018748

+ 52 - 0
Source/QuestPDF.Examples/TableExamples.cs

@@ -316,6 +316,58 @@ namespace QuestPDF.Examples
                 });
                 });
         }
         }
         
         
+        [Test]
+        public void Bug_RowSpanWorksIncorrectly()
+        {
+            // https://github.com/QuestPDF/QuestPDF/issues/552
+            
+            RenderingTest
+                .Create()
+                .ProduceImages()
+                .PageSize(PageSizes.A5.Landscape())
+                .ShowResults()
+                .Render(container =>
+                {
+                    container.Padding(20).Table(table => 
+                    {
+                        table.ColumnsDefinition(columns => 
+                        {
+                            columns.RelativeColumn(1);
+                            columns.RelativeColumn(2);
+                            columns.RelativeColumn(2);
+                            columns.RelativeColumn(2);
+                        });
+
+                        foreach (var i in Enumerable.Range(6, 9))
+                        {
+                            table.Cell().Element(CellStyleMainTable).Text($"{i:00}:00");
+                            
+                            foreach (var j in Enumerable.Range(1, 3))
+                                table.Cell().Element(CellStyleMainTable).Text("");
+                        }
+
+                        static IContainer CellStyleMainTable(IContainer container) 
+                        {
+                            return container
+                                .Border(0.5f).BorderColor(Colors.Blue.Lighten4)
+                                .Background(Colors.Blue.Lighten5)
+                                .PaddingVertical(5);
+                        }
+
+                        table.Cell().Row(1).RowSpan(3).Column(2).Element(BlockAccepted).Text("3 rows");
+                        table.Cell().Row(1).RowSpan(6).Column(3).Element(BlockAccepted).Text("6 rows");
+                        table.Cell().Row(3).RowSpan(5).Column(4).Element(BlockAccepted).Text("5 rows");
+                        
+                        static IContainer BlockAccepted(IContainer container)
+                        {
+                            return container
+                                .Border(1.5f).BorderColor(Colors.Green.Lighten2)
+                                .Background(Colors.Green.Lighten4);
+                        }
+                    });
+                });
+        }
+        
         [Test]
         [Test]
         public void TableHeader()
         public void TableHeader()
         {
         {

+ 1 - 1
Source/QuestPDF/Elements/Table/Table.cs

@@ -112,7 +112,7 @@ namespace QuestPDF.Elements.Table
             UpdateColumnsWidth(availableSpace.Width);
             UpdateColumnsWidth(availableSpace.Width);
             var renderingCommands = PlanLayout(availableSpace);
             var renderingCommands = PlanLayout(availableSpace);
 
 
-            foreach (var command in renderingCommands)
+            foreach (var command in renderingCommands.OrderBy(x => x.Cell.ZIndex))
             {
             {
                 if (command.Measurement.Type == SpacePlanType.FullRender)
                 if (command.Measurement.Type == SpacePlanType.FullRender)
                     command.Cell.IsRendered = true;
                     command.Cell.IsRendered = true;

+ 2 - 0
Source/QuestPDF/Elements/Table/TableCell.cs

@@ -8,6 +8,8 @@ namespace QuestPDF.Elements.Table
         public int Column { get; set; } = 0;
         public int Column { get; set; } = 0;
         public int ColumnSpan { get; set; } = 1;
         public int ColumnSpan { get; set; } = 1;
         
         
+        public int ZIndex { get; set; }
+        
         public bool IsRendered { get; set; }
         public bool IsRendered { get; set; }
     }
     }
 }
 }

+ 4 - 0
Source/QuestPDF/Elements/Table/TableLayoutPlanner.cs

@@ -16,9 +16,13 @@ namespace QuestPDF.Elements.Table
         {
         {
             var cellsWindow = new List<TableCell>();
             var cellsWindow = new List<TableCell>();
             (int x, int y) currentLocation = (1, 1);
             (int x, int y) currentLocation = (1, 1);
+            var zIndex = 0;
             
             
             foreach (var cell in cells)
             foreach (var cell in cells)
             {
             {
+                cell.ZIndex = zIndex;
+                zIndex++;
+                
                 if (cellsWindow.Count > Math.Max(columnsCount, 16))
                 if (cellsWindow.Count > Math.Max(columnsCount, 16))
                 {
                 {
                     cellsWindow = cellsWindow
                     cellsWindow = cellsWindow