From 49d9dcb42dfbbddbb4d683f65627480de19592fb Mon Sep 17 00:00:00 2001 From: Andreas Rosdal <41650711+andreasrosdal@users.noreply.github.com> Date: Mon, 8 Jan 2024 08:02:48 +0100 Subject: [PATCH] Revert "Fix height calculation for table rows with rowspan (#865)" This reverts commit 70880c2c4d806afaaeaca370762fdf852eafa231. --- .../java/com/lowagie/text/pdf/PdfPTable.java | 82 ++++++++----------- .../text/pdf/TableRowSpanEvenSplitTest.java | 49 ----------- 2 files changed, 32 insertions(+), 99 deletions(-) diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/PdfPTable.java b/openpdf/src/main/java/com/lowagie/text/pdf/PdfPTable.java index d7f4f7b70..7ab3fc43e 100644 --- a/openpdf/src/main/java/com/lowagie/text/pdf/PdfPTable.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/PdfPTable.java @@ -50,7 +50,6 @@ package com.lowagie.text.pdf; import java.util.ArrayList; -import java.util.Comparator; import com.lowagie.text.error_messages.MessageLocalization; @@ -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 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. * @@ -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; } /** diff --git a/openpdf/src/test/java/com/lowagie/text/pdf/TableRowSpanEvenSplitTest.java b/openpdf/src/test/java/com/lowagie/text/pdf/TableRowSpanEvenSplitTest.java index 60899a173..b434cf01f 100644 --- a/openpdf/src/test/java/com/lowagie/text/pdf/TableRowSpanEvenSplitTest.java +++ b/openpdf/src/test/java/com/lowagie/text/pdf/TableRowSpanEvenSplitTest.java @@ -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); - } }