Skip to content

Commit

Permalink
Fix performance for rowspan height distribution (#1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
fellmann authored Jan 8, 2024
1 parent bea0488 commit 077991b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
10 changes: 6 additions & 4 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfPTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,11 @@ public float calculateHeights(boolean firsttime) {
for (int k = 0; k < rows.size(); ++k) {
totalHeight += getRowHeight(k, firsttime);
}
if(firsttime) {
// Redistribute row height for row span once
this.redistributeRowspanHeight();
calculateHeights(false);
}
return totalHeight;
}

Expand Down Expand Up @@ -850,9 +855,6 @@ public float getRowHeight(int idx) {

private void redistributeRowspanHeight() {
float delta = 0.001f;
for (PdfPRow pdfPRow : rows) {
pdfPRow.calculateHeights();
}
for (int rowIdx = 0; rowIdx < rows.size(); rowIdx++) {
PdfPRow row = rows.get(rowIdx);
PdfPCell[] cells = row.getCells();
Expand Down Expand Up @@ -909,7 +911,7 @@ public float getRowHeight(int idx, boolean firsttime) {
return 0;
if (firsttime) {
row.setWidths(absoluteWidths);
this.redistributeRowspanHeight();
row.calculateHeights();
}
return row.getMaxHeights();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public void threeRowSpanTest() {
float heightRow2 = table.getRows().get(1).getMaxHeights();
float heightRow3 = table.getRows().get(2).getMaxHeights();
document.close();
Assertions.assertEquals(0, heightRow1 - heightRow2);
Assertions.assertEquals(0, heightRow3 - heightRow2);
Assertions.assertEquals(heightRow1, heightRow2, 0.01);
Assertions.assertEquals(heightRow3, heightRow2, 0.01);
}

@Test
Expand All @@ -52,8 +52,40 @@ public void threeWithOneUnevenTest() {
float heightRow2 = table.getRows().get(1).getMaxHeights();
float heightRow3 = table.getRows().get(2).getMaxHeights();
document.close();
Assertions.assertEquals(0, heightRow2 - heightRow3);
Assertions.assertNotEquals(0, heightRow1 - heightRow2);
Assertions.assertEquals(heightRow2, heightRow3, 0.01);
Assertions.assertNotEquals(heightRow1, heightRow2, 0.01);
}

@Test
public void threeWithLargeRowspanCellHugeTableTest() {
Document document = new Document(PageSize.A4);
ByteArrayOutputStream pdfOut = new ByteArrayOutputStream();
PdfWriter.getInstance(document, pdfOut);
PdfPTable table = new PdfPTable(2);

int rows = 9_000;

for (int i = 0; i < rows; i += 3) {
PdfPCell cell = new PdfPCell();
cell.setRowspan(3);
cell.addElement(new Chunk("rowspan1\nrowspan2\nrowspan3\nrowspan4\nrowspan5\nrowspan6\nrowspan7"));
table.addCell(cell);

table.addCell("row1");
table.addCell("row2");
table.addCell("row3");
}

document.open();
document.add(table);
for (int i = 0; i < rows; i += 3) {
float heightRow1 = table.getRows().get(i).getMaxHeights();
float heightRow2 = table.getRows().get(i + 1).getMaxHeights();
float heightRow3 = table.getRows().get(i + 2).getMaxHeights();
Assertions.assertEquals(heightRow2, heightRow3, 0.01);
Assertions.assertEquals(heightRow1, heightRow2, 0.01);
}
document.close();
}

@Test
Expand All @@ -76,8 +108,9 @@ public void threeWithLargeRowspanCellTest() {
float heightRow2 = table.getRows().get(1).getMaxHeights();
float heightRow3 = table.getRows().get(2).getMaxHeights();
document.close();
Assertions.assertEquals(0, heightRow2 - heightRow3);
Assertions.assertEquals(0, heightRow1 - heightRow2);
Assertions.assertEquals(heightRow2, heightRow3, 0.01);
Assertions.assertEquals(heightRow1, heightRow2, 0.01);

}

@Test
Expand All @@ -100,8 +133,8 @@ public void threeWithLargeRowspanCellTestUnevenDistribution() {
float heightRow2 = table.getRows().get(1).getMaxHeights();
float heightRow3 = table.getRows().get(2).getMaxHeights();
document.close();
Assertions.assertEquals(0, heightRow2 - heightRow3);
Assertions.assertNotEquals(0, heightRow1 - heightRow2);
Assertions.assertEquals(heightRow2, heightRow3, 0.01);
Assertions.assertNotEquals(heightRow1, heightRow2, 0.01);
Assertions.assertTrue(heightRow1 > heightRow2);
}
}

0 comments on commit 077991b

Please sign in to comment.