From e684b9ec6d09d5489d70293a1c3a951c6982ede8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Zi=C4=85bek?= Date: Tue, 9 May 2023 01:47:27 +0200 Subject: [PATCH] Table: fixed zindex rendering issue (#553) --- Source/QuestPDF.Examples/TableExamples.cs | 52 +++++++++++++++++++ Source/QuestPDF/Elements/Table/Table.cs | 2 +- Source/QuestPDF/Elements/Table/TableCell.cs | 2 + .../Elements/Table/TableLayoutPlanner.cs | 4 ++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/Source/QuestPDF.Examples/TableExamples.cs b/Source/QuestPDF.Examples/TableExamples.cs index 2690ad5..2bb5ef4 100644 --- a/Source/QuestPDF.Examples/TableExamples.cs +++ b/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] public void TableHeader() { diff --git a/Source/QuestPDF/Elements/Table/Table.cs b/Source/QuestPDF/Elements/Table/Table.cs index b2e5747..951f871 100644 --- a/Source/QuestPDF/Elements/Table/Table.cs +++ b/Source/QuestPDF/Elements/Table/Table.cs @@ -112,7 +112,7 @@ namespace QuestPDF.Elements.Table UpdateColumnsWidth(availableSpace.Width); 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) command.Cell.IsRendered = true; diff --git a/Source/QuestPDF/Elements/Table/TableCell.cs b/Source/QuestPDF/Elements/Table/TableCell.cs index 52d88a1..cee0373 100644 --- a/Source/QuestPDF/Elements/Table/TableCell.cs +++ b/Source/QuestPDF/Elements/Table/TableCell.cs @@ -8,6 +8,8 @@ namespace QuestPDF.Elements.Table public int Column { get; set; } = 0; public int ColumnSpan { get; set; } = 1; + public int ZIndex { get; set; } + public bool IsRendered { get; set; } } } \ No newline at end of file diff --git a/Source/QuestPDF/Elements/Table/TableLayoutPlanner.cs b/Source/QuestPDF/Elements/Table/TableLayoutPlanner.cs index 682fa64..53a60a0 100644 --- a/Source/QuestPDF/Elements/Table/TableLayoutPlanner.cs +++ b/Source/QuestPDF/Elements/Table/TableLayoutPlanner.cs @@ -16,9 +16,13 @@ namespace QuestPDF.Elements.Table { var cellsWindow = new List(); (int x, int y) currentLocation = (1, 1); + var zIndex = 0; foreach (var cell in cells) { + cell.ZIndex = zIndex; + zIndex++; + if (cellsWindow.Count > Math.Max(columnsCount, 16)) { cellsWindow = cellsWindow