Skip to content

Commit

Permalink
Revert "Fix height calculation for table rows with rowspan (#865)"
Browse files Browse the repository at this point in the history
This reverts commit 70880c2.
  • Loading branch information
andreasrosdal authored Jan 8, 2024
1 parent d4f7a40 commit 49d9dcb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 99 deletions.
82 changes: 32 additions & 50 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfPTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
package com.lowagie.text.pdf;

import java.util.ArrayList;
import java.util.Comparator;

import com.lowagie.text.error_messages.MessageLocalization;

Expand Down Expand Up @@ -847,52 +846,6 @@ public float getTotalHeight() {
public float getRowHeight(int idx) {
return getRowHeight(idx, false);
}

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();
for (PdfPCell cell : cells) {
if (cell != null && cell.getRowspan() > 1) {
float existingHeights = 0;
for (int r = rowIdx; r < rows.size() && r < rowIdx + cell.getRowspan(); r++) {
existingHeights += rows.get(r).getMaxHeights();
}
float heightToDistribute = cell.getMaxHeight() - existingHeights;
if (heightToDistribute > delta) {
ArrayList<Integer> rowsByHeight = new ArrayList<>(cell.getRowspan());
for (int r = rowIdx; r < rows.size() && r < rowIdx + cell.getRowspan(); r++) {
rowsByHeight.add(r);
}
rowsByHeight.sort(Comparator.comparing(r -> rows.get(r).getMaxHeights()));
for (int i = 0; i < rowsByHeight.size(); i++) {
if (heightToDistribute < delta) break;
float additionalHeightPerRow = heightToDistribute / (i + 1);
if (i + 1 < rowsByHeight.size()) {
float currentRowHeight = rows.get(rowsByHeight.get(i)).getMaxHeights();
float nextRowHeight = rows.get(rowsByHeight.get(i + 1)).getMaxHeights();
if (Math.abs(currentRowHeight - nextRowHeight) < delta) {
continue;
} else {
additionalHeightPerRow = Math.min(additionalHeightPerRow, nextRowHeight - currentRowHeight);
}
}
for (int j = 0; j <= i; j++) {
int r = rowsByHeight.get(j);
rows.get(r).setMaxHeights(rows.get(r).getMaxHeights() + additionalHeightPerRow);
heightToDistribute -= additionalHeightPerRow;
}
}
}
}
}
}
}

/**
* Gets the height of a particular row.
*
Expand All @@ -907,11 +860,40 @@ public float getRowHeight(int idx, boolean firsttime) {
PdfPRow row = rows.get(idx);
if (row == null)
return 0;
if (firsttime) {
if (firsttime)
row.setWidths(absoluteWidths);
this.redistributeRowspanHeight();
float height = row.getMaxHeights();
PdfPCell cell;
PdfPRow tmprow;
for (int i = 0; i < relativeWidths.length; i++) {
if(!rowSpanAbove(idx, i))
continue;
int rs = 1;
while (rowSpanAbove(idx - rs, i)) {
rs++;
}
tmprow = rows.get(idx - rs);
cell = tmprow.getCells()[i];
float tmp = 0;
if (cell != null && cell.getRowspan() == rs + 1) {
tmp = cell.getMaxHeight();
float avgheight = tmp / (rs + 1);
PdfPRow iterrow;
for (int rowidx = idx - rs; rowidx < idx; rowidx++) {
iterrow = rows.get(rowidx);
if (avgheight > iterrow.getMaxHeights()) {
iterrow.setMaxHeights(height);
tmp -= avgheight;
}else{
tmp -= iterrow.getMaxHeights();
}
}
}
if (tmp > height)
height = tmp;
}
return row.getMaxHeights();
row.setMaxHeights(height);
return height;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,53 +55,4 @@ public void threeWithOneUnevenTest() {
Assertions.assertEquals(0, heightRow2 - heightRow3);
Assertions.assertNotEquals(0, heightRow1 - heightRow2);
}

@Test
public void threeWithLargeRowspanCellTest() {
Document document = new Document(PageSize.A4);
ByteArrayOutputStream pdfOut = new ByteArrayOutputStream();
PdfWriter.getInstance(document, pdfOut);
PdfPTable table = new PdfPTable(2);
PdfPCell cell = new PdfPCell();
cell.setRowspan(3);
cell.addElement(new Chunk("rowspan\nrowspan\nrowspan\nrowspan\nrowspan\nrowspan\nrowspan"));
table.addCell(cell);

table.addCell("row1");
table.addCell("row2");
table.addCell("row3");
document.open();
document.add(table);
float heightRow1 = table.getRows().get(0).getMaxHeights();
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);
}

@Test
public void threeWithLargeRowspanCellTestUnevenDistribution() {
Document document = new Document(PageSize.A4);
ByteArrayOutputStream pdfOut = new ByteArrayOutputStream();
PdfWriter.getInstance(document, pdfOut);
PdfPTable table = new PdfPTable(2);
PdfPCell cell = new PdfPCell();
cell.setRowspan(3);
cell.addElement(new Chunk("rowspan\nrowspan\nrowspan\nrowspan\nrowspan\nrowspan\nrowspan"));
table.addCell(cell);

table.addCell("row1\nrow1\nrow1\nrow1\nrow1\nrow1");
table.addCell("row2");
table.addCell("row3");
document.open();
document.add(table);
float heightRow1 = table.getRows().get(0).getMaxHeights();
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.assertTrue(heightRow1 > heightRow2);
}
}

0 comments on commit 49d9dcb

Please sign in to comment.