Skip to content

Commit 2ec5ebc

Browse files
ars18wrwiText-CI
authored andcommitted
Do not recalculate table's minmax width once the table has been split.
DEVSIX-3008
1 parent 8f794fa commit 2ec5ebc

File tree

4 files changed

+95
-16
lines changed

4 files changed

+95
-16
lines changed

layout/src/main/java/com/itextpdf/layout/renderer/TableRenderer.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,18 +1336,18 @@ protected Float retrieveWidth(float parentBoxWidth) {
13361336

13371337
@Override
13381338
public MinMaxWidth getMinMaxWidth() {
1339-
initializeTableLayoutBorders();
1339+
if (isOriginalNonSplitRenderer) {
1340+
initializeTableLayoutBorders();
1341+
}
13401342
float rightMaxBorder = bordersHandler.getRightBorderMaxWidth();
13411343
float leftMaxBorder = bordersHandler.getLeftBorderMaxWidth();
13421344
TableWidths tableWidths = new TableWidths(this, MinMaxWidthUtils.getInfWidth(), true, rightMaxBorder, leftMaxBorder);
1343-
float[] columns = tableWidths.layout();
1344-
float minWidth = tableWidths.getMinWidth();
1345-
cleanTableLayoutBorders();
1346-
13471345
float maxColTotalWidth = 0;
1346+
float[] columns = isOriginalNonSplitRenderer ? tableWidths.layout() : countedColumnWidth;
13481347
for (float column : columns) {
13491348
maxColTotalWidth += column;
13501349
}
1350+
float minWidth = isOriginalNonSplitRenderer ? tableWidths.getMinWidth() : maxColTotalWidth;
13511351
UnitValue marginRightUV = this.getPropertyAsUnitValue(Property.MARGIN_RIGHT);
13521352
if (!marginRightUV.isPointValue()) {
13531353
Logger logger = LoggerFactory.getLogger(TableRenderer.class);
@@ -1390,17 +1390,6 @@ private void initializeTableLayoutBorders() {
13901390
correctRowRange();
13911391
}
13921392

1393-
private void cleanTableLayoutBorders() {
1394-
footerRenderer = null;
1395-
headerRenderer = null;
1396-
// we may have deleted empty rows and now need to update table's rowrange
1397-
this.rowRange = new Table.RowRange(rowRange.getStartRow(), bordersHandler.getFinishRow());
1398-
//TODO do we need it?
1399-
// delete set properties
1400-
deleteOwnProperty(Property.BORDER_BOTTOM);
1401-
deleteOwnProperty(Property.BORDER_TOP);
1402-
}
1403-
14041393
private void correctRowRange() {
14051394
if (rows.size() < rowRange.getFinishRow() - rowRange.getStartRow() + 1) {
14061395
rowRange = new Table.RowRange(rowRange.getStartRow(), rowRange.getStartRow() + rows.size() - 1);

layout/src/test/java/com/itextpdf/layout/FloatTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2962,4 +2962,35 @@ public void floatOverflowAlongWithNewContent02() throws IOException, Interrupted
29622962

29632963
Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFileName, destinationFolder, "diff_overflowNewContent02_"));
29642964
}
2965+
2966+
@Test
2967+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.TABLE_WIDTH_IS_MORE_THAN_EXPECTED_DUE_TO_MIN_WIDTH))
2968+
public void floatTableTest01() throws IOException, InterruptedException {
2969+
String cmpFileName = sourceFolder + "cmp_floatTableTest01.pdf";
2970+
String outFile = destinationFolder + "floatTableTest01.pdf";
2971+
2972+
PdfWriter writer = new PdfWriter(outFile);
2973+
PdfDocument pdfDoc = new PdfDocument(writer);
2974+
Document doc = new Document(pdfDoc);
2975+
2976+
Div div = new Div();
2977+
div.setWidth(38);
2978+
2979+
Div floatDiv = new Div();
2980+
floatDiv.setProperty(Property.FLOAT, FloatPropertyValue.LEFT);
2981+
2982+
Table table = new Table(2);
2983+
for (int i = 0; i < 26; i++) {
2984+
table.addCell(new Cell().add(new Paragraph("abba a")));
2985+
table.addCell(new Cell().add(new Paragraph("ab ab ab")));
2986+
}
2987+
2988+
floatDiv.add(table);
2989+
div.add(floatDiv);
2990+
2991+
doc.add(div);
2992+
doc.close();
2993+
2994+
Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFileName, destinationFolder, "diff03_"));
2995+
}
29652996
}

layout/src/test/java/com/itextpdf/layout/TableTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ This file is part of the iText (R) project.
4747
import com.itextpdf.io.util.UrlUtil;
4848
import com.itextpdf.kernel.colors.ColorConstants;
4949
import com.itextpdf.kernel.geom.PageSize;
50+
import com.itextpdf.kernel.geom.Rectangle;
5051
import com.itextpdf.kernel.pdf.PdfDocument;
5152
import com.itextpdf.kernel.pdf.PdfWriter;
5253
import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
@@ -63,6 +64,10 @@ This file is part of the iText (R) project.
6364
import com.itextpdf.layout.element.Paragraph;
6465
import com.itextpdf.layout.element.Table;
6566
import com.itextpdf.layout.element.Text;
67+
import com.itextpdf.layout.layout.LayoutArea;
68+
import com.itextpdf.layout.layout.LayoutContext;
69+
import com.itextpdf.layout.layout.LayoutResult;
70+
import com.itextpdf.layout.minmaxwidth.MinMaxWidth;
6671
import com.itextpdf.layout.property.BorderCollapsePropertyValue;
6772
import com.itextpdf.layout.property.CaptionSide;
6873
import com.itextpdf.layout.property.HorizontalAlignment;
@@ -82,6 +87,7 @@ This file is part of the iText (R) project.
8287
import org.junit.experimental.categories.Category;
8388
import org.junit.rules.ExpectedException;
8489

90+
import java.io.ByteArrayOutputStream;
8591
import java.io.IOException;
8692

8793
@Category(IntegrationTest.class)
@@ -2432,6 +2438,59 @@ public void tableMinMaxWidthTest06() throws IOException, InterruptedException {
24322438
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, testName + "_diff"));
24332439
}
24342440

2441+
@Test
2442+
@LogMessages(messages = {
2443+
@LogMessage(messageTemplate = LogMessageConstant.TABLE_WIDTH_IS_MORE_THAN_EXPECTED_DUE_TO_MIN_WIDTH)
2444+
})
2445+
public void splitTableMinMaxWidthTest01() {
2446+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
2447+
Document doc = new Document(pdfDoc);
2448+
2449+
Table table = new Table(2);
2450+
for (int i = 0; i < 26; i++) {
2451+
table.addCell(new Cell().add(new Paragraph("abba a")));
2452+
table.addCell(new Cell().add(new Paragraph("ab ab ab")));
2453+
}
2454+
2455+
// not enough to place even if min-width approach is used
2456+
float areaWidth = 20;
2457+
2458+
LayoutResult result = table.createRendererSubTree().setParent(doc.getRenderer())
2459+
.layout(new LayoutContext(new LayoutArea(1, new Rectangle(areaWidth, 100))));
2460+
TableRenderer overflowRenderer = (TableRenderer) result.getOverflowRenderer();
2461+
2462+
MinMaxWidth minMaxWidth = overflowRenderer.getMinMaxWidth();
2463+
2464+
Assert.assertEquals(result.getOccupiedArea().getBBox().getWidth(), minMaxWidth.getMaxWidth(), 0.0001);
2465+
Assert.assertEquals(minMaxWidth.getMaxWidth(), minMaxWidth.getMinWidth(), 0.0001);
2466+
2467+
// not enough to place using max-width approach, but more than required for min-width approach
2468+
areaWidth = 70;
2469+
2470+
result = table.createRendererSubTree().setParent(doc.getRenderer())
2471+
.layout(new LayoutContext(new LayoutArea(1, new Rectangle(areaWidth, 100))));
2472+
overflowRenderer = (TableRenderer) result.getOverflowRenderer();
2473+
2474+
minMaxWidth = overflowRenderer.getMinMaxWidth();
2475+
2476+
Assert.assertEquals(result.getOccupiedArea().getBBox().getWidth(), minMaxWidth.getMaxWidth(), 0.0001);
2477+
Assert.assertEquals(minMaxWidth.getMaxWidth(), minMaxWidth.getMinWidth(), 0.0001);
2478+
2479+
2480+
// enough to place using max-width approach
2481+
areaWidth = 400f;
2482+
2483+
result = table.createRendererSubTree().setParent(doc.getRenderer())
2484+
.layout(new LayoutContext(new LayoutArea(1, new Rectangle(areaWidth, 100))));
2485+
overflowRenderer = (TableRenderer) result.getOverflowRenderer();
2486+
2487+
minMaxWidth = overflowRenderer.getMinMaxWidth();
2488+
2489+
Assert.assertEquals(result.getOccupiedArea().getBBox().getWidth(), minMaxWidth.getMaxWidth(), 0.0001);
2490+
Assert.assertEquals(minMaxWidth.getMaxWidth(), minMaxWidth.getMinWidth(), 0.0001);
2491+
}
2492+
2493+
24352494
@Test
24362495
public void marginPaddingTest01() throws IOException, InterruptedException {
24372496
String testName = "marginPaddingTest01.pdf";
Binary file not shown.

0 commit comments

Comments
 (0)