Improved wrapping support
This commit is contained in:
committed by
Marcin Ziąbek
parent
940616cd3e
commit
c7008ad940
@@ -13,7 +13,8 @@ namespace QuestPDF.Examples
|
||||
return container
|
||||
.Border(1)
|
||||
.Background(dark ? Colors.Grey.Lighten2 : Colors.White)
|
||||
.Padding(5);
|
||||
.Padding(5)
|
||||
.EnsureSpace(10);
|
||||
}
|
||||
|
||||
public static void LabelCell(this IContainer container, string text) => container.Cell(true).Text(text, TextStyle.Default.SemiBold());
|
||||
|
||||
@@ -103,7 +103,7 @@ namespace QuestPDF.Examples
|
||||
.ProducePdf()
|
||||
.PageSize(PageSizes.A4)
|
||||
.ShowResults()
|
||||
.Render(container => GeneratePerformanceStructure(container, 2));
|
||||
.Render(container => GeneratePerformanceStructure(container, 10));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -117,7 +117,7 @@ namespace QuestPDF.Examples
|
||||
.EnableCaching()
|
||||
.EnableDebugging(false)
|
||||
.ShowResults()
|
||||
.Render(container => GeneratePerformanceStructure(container, 100));
|
||||
.Render(container => GeneratePerformanceStructure(container, 250));
|
||||
}
|
||||
|
||||
public static void GeneratePerformanceStructure(IContainer container, int repeats)
|
||||
|
||||
@@ -67,6 +67,7 @@ namespace QuestPDF.Elements.Table
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
Children.ForEach(x => x.IsRendered = false);
|
||||
CurrentRow = 1;
|
||||
}
|
||||
|
||||
@@ -89,6 +90,11 @@ namespace QuestPDF.Elements.Table
|
||||
|
||||
foreach (var command in layout.CellRenderingCommands)
|
||||
{
|
||||
var measurementResult = command.Cell.Measure(command.Size);
|
||||
|
||||
if (measurementResult.Type == SpacePlanType.FullRender)
|
||||
command.Cell.IsRendered = true;
|
||||
|
||||
Canvas.Translate(command.Offset);
|
||||
command.Cell.Draw(command.Size);
|
||||
Canvas.Translate(command.Offset.Reverse());
|
||||
@@ -123,11 +129,15 @@ namespace QuestPDF.Elements.Table
|
||||
// update row heights
|
||||
var rowBottomOffsets = new DynamicDictionary<int, float>();
|
||||
var childrenToTry = Enumerable.Range(CurrentRow, RowsCount - CurrentRow + 1).SelectMany(x => OrderedChildren[x]);
|
||||
|
||||
|
||||
var maxRenderingRow = RowsCount;
|
||||
var currentRow = CurrentRow;
|
||||
|
||||
foreach (var child in childrenToTry)
|
||||
{
|
||||
if (child.Row > maxRenderingRow)
|
||||
break;
|
||||
|
||||
if (child.Row > currentRow)
|
||||
{
|
||||
rowBottomOffsets[currentRow] = Math.Max(rowBottomOffsets[currentRow], rowBottomOffsets[currentRow - 1]);
|
||||
@@ -139,9 +149,21 @@ namespace QuestPDF.Elements.Table
|
||||
}
|
||||
|
||||
var topOffset = rowBottomOffsets[child.Row - 1];
|
||||
var availableHeight = availableSpace.Height - topOffset;
|
||||
|
||||
var height = GetCellSize(child).Height;
|
||||
var cellBottomOffset = height + topOffset;
|
||||
var cellSize = GetCellSize(child, availableHeight);
|
||||
|
||||
if (cellSize.Type == SpacePlanType.PartialRender)
|
||||
{
|
||||
maxRenderingRow = Math.Min(maxRenderingRow, child.Row + child.RowSpan - 1);
|
||||
}
|
||||
|
||||
if (cellSize.Type == SpacePlanType.Wrap)
|
||||
{
|
||||
maxRenderingRow = Math.Min(maxRenderingRow, child.Row - 1);
|
||||
}
|
||||
|
||||
var cellBottomOffset = cellSize.Height + topOffset;
|
||||
|
||||
var targetRowId = child.Row + child.RowSpan - 1; // -1 because rowSpan starts at 1
|
||||
rowBottomOffsets[targetRowId] = Math.Max(rowBottomOffsets[targetRowId], cellBottomOffset);
|
||||
@@ -155,13 +177,11 @@ namespace QuestPDF.Elements.Table
|
||||
.ForEach(x => rowHeights[x] = rowBottomOffsets[x] - rowBottomOffsets[x-1]);
|
||||
|
||||
// find rows count to render in this pass
|
||||
var maxRowToDisplay = rowBottomOffsets.Items.Where(x => x.Value <= availableSpace.Height + Size.Epsilon).Max(x => x.Key);
|
||||
|
||||
var totalHeight = rowHeights.Items.Where(x => x.Key <= maxRowToDisplay).Sum(x => x.Value);
|
||||
var totalHeight = rowHeights.Items.Where(x => x.Key <= maxRenderingRow).Sum(x => x.Value);
|
||||
var totalWidth = Columns.Sum(x => x.Width);
|
||||
|
||||
var childrenToDraw = Enumerable
|
||||
.Range(CurrentRow, maxRowToDisplay - CurrentRow + 1)
|
||||
.Range(CurrentRow, maxRenderingRow - CurrentRow + 1)
|
||||
.SelectMany(x => OrderedChildren[x]);
|
||||
|
||||
foreach (var cell in childrenToDraw)
|
||||
@@ -184,7 +204,7 @@ namespace QuestPDF.Elements.Table
|
||||
{
|
||||
Size = new Size(totalWidth, totalHeight),
|
||||
CellRenderingCommands = cellRenderingCommands,
|
||||
MaxRowRendered = maxRowToDisplay
|
||||
MaxRowRendered = maxRenderingRow
|
||||
};
|
||||
|
||||
float GetCellWidth(TableCell cell)
|
||||
@@ -192,17 +212,12 @@ namespace QuestPDF.Elements.Table
|
||||
return cellOffsets[cell.Column + cell.ColumnSpan - 1] - cellOffsets[cell.Column - 1];
|
||||
}
|
||||
|
||||
Size GetCellSize(TableCell cell)
|
||||
SpacePlan GetCellSize(TableCell cell, float availableHeight)
|
||||
{
|
||||
var width = GetCellWidth(cell);
|
||||
var cellSize = new Size(width, Size.Max.Height);
|
||||
var cellSize = new Size(width, availableHeight);
|
||||
|
||||
var measurement = cell.Measure(cellSize);
|
||||
|
||||
if (measurement.Type == SpacePlanType.Wrap)
|
||||
return new Size(width, Size.Infinity);
|
||||
|
||||
return measurement;
|
||||
return cell.Measure(cellSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,5 +7,7 @@ namespace QuestPDF.Elements.Table
|
||||
|
||||
public int Column { get; set; } = 1;
|
||||
public int ColumnSpan { get; set; } = 1;
|
||||
|
||||
public bool IsRendered { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user