Skip to content

Commit 077991b

Browse files
authored
Fix performance for rowspan height distribution (#1019)
1 parent bea0488 commit 077991b

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

openpdf/src/main/java/com/lowagie/text/pdf/PdfPTable.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,11 @@ public float calculateHeights(boolean firsttime) {
418418
for (int k = 0; k < rows.size(); ++k) {
419419
totalHeight += getRowHeight(k, firsttime);
420420
}
421+
if(firsttime) {
422+
// Redistribute row height for row span once
423+
this.redistributeRowspanHeight();
424+
calculateHeights(false);
425+
}
421426
return totalHeight;
422427
}
423428

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

851856
private void redistributeRowspanHeight() {
852857
float delta = 0.001f;
853-
for (PdfPRow pdfPRow : rows) {
854-
pdfPRow.calculateHeights();
855-
}
856858
for (int rowIdx = 0; rowIdx < rows.size(); rowIdx++) {
857859
PdfPRow row = rows.get(rowIdx);
858860
PdfPCell[] cells = row.getCells();
@@ -909,7 +911,7 @@ public float getRowHeight(int idx, boolean firsttime) {
909911
return 0;
910912
if (firsttime) {
911913
row.setWidths(absoluteWidths);
912-
this.redistributeRowspanHeight();
914+
row.calculateHeights();
913915
}
914916
return row.getMaxHeights();
915917
}

openpdf/src/test/java/com/lowagie/text/pdf/TableRowSpanEvenSplitTest.java

+41-8
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public void threeRowSpanTest() {
2828
float heightRow2 = table.getRows().get(1).getMaxHeights();
2929
float heightRow3 = table.getRows().get(2).getMaxHeights();
3030
document.close();
31-
Assertions.assertEquals(0, heightRow1 - heightRow2);
32-
Assertions.assertEquals(0, heightRow3 - heightRow2);
31+
Assertions.assertEquals(heightRow1, heightRow2, 0.01);
32+
Assertions.assertEquals(heightRow3, heightRow2, 0.01);
3333
}
3434

3535
@Test
@@ -52,8 +52,40 @@ public void threeWithOneUnevenTest() {
5252
float heightRow2 = table.getRows().get(1).getMaxHeights();
5353
float heightRow3 = table.getRows().get(2).getMaxHeights();
5454
document.close();
55-
Assertions.assertEquals(0, heightRow2 - heightRow3);
56-
Assertions.assertNotEquals(0, heightRow1 - heightRow2);
55+
Assertions.assertEquals(heightRow2, heightRow3, 0.01);
56+
Assertions.assertNotEquals(heightRow1, heightRow2, 0.01);
57+
}
58+
59+
@Test
60+
public void threeWithLargeRowspanCellHugeTableTest() {
61+
Document document = new Document(PageSize.A4);
62+
ByteArrayOutputStream pdfOut = new ByteArrayOutputStream();
63+
PdfWriter.getInstance(document, pdfOut);
64+
PdfPTable table = new PdfPTable(2);
65+
66+
int rows = 9_000;
67+
68+
for (int i = 0; i < rows; i += 3) {
69+
PdfPCell cell = new PdfPCell();
70+
cell.setRowspan(3);
71+
cell.addElement(new Chunk("rowspan1\nrowspan2\nrowspan3\nrowspan4\nrowspan5\nrowspan6\nrowspan7"));
72+
table.addCell(cell);
73+
74+
table.addCell("row1");
75+
table.addCell("row2");
76+
table.addCell("row3");
77+
}
78+
79+
document.open();
80+
document.add(table);
81+
for (int i = 0; i < rows; i += 3) {
82+
float heightRow1 = table.getRows().get(i).getMaxHeights();
83+
float heightRow2 = table.getRows().get(i + 1).getMaxHeights();
84+
float heightRow3 = table.getRows().get(i + 2).getMaxHeights();
85+
Assertions.assertEquals(heightRow2, heightRow3, 0.01);
86+
Assertions.assertEquals(heightRow1, heightRow2, 0.01);
87+
}
88+
document.close();
5789
}
5890

5991
@Test
@@ -76,8 +108,9 @@ public void threeWithLargeRowspanCellTest() {
76108
float heightRow2 = table.getRows().get(1).getMaxHeights();
77109
float heightRow3 = table.getRows().get(2).getMaxHeights();
78110
document.close();
79-
Assertions.assertEquals(0, heightRow2 - heightRow3);
80-
Assertions.assertEquals(0, heightRow1 - heightRow2);
111+
Assertions.assertEquals(heightRow2, heightRow3, 0.01);
112+
Assertions.assertEquals(heightRow1, heightRow2, 0.01);
113+
81114
}
82115

83116
@Test
@@ -100,8 +133,8 @@ public void threeWithLargeRowspanCellTestUnevenDistribution() {
100133
float heightRow2 = table.getRows().get(1).getMaxHeights();
101134
float heightRow3 = table.getRows().get(2).getMaxHeights();
102135
document.close();
103-
Assertions.assertEquals(0, heightRow2 - heightRow3);
104-
Assertions.assertNotEquals(0, heightRow1 - heightRow2);
136+
Assertions.assertEquals(heightRow2, heightRow3, 0.01);
137+
Assertions.assertNotEquals(heightRow1, heightRow2, 0.01);
105138
Assertions.assertTrue(heightRow1 > heightRow2);
106139
}
107140
}

0 commit comments

Comments
 (0)