Skip to content

Commit e324437

Browse files
committed
Distinguish between the whole row being partially placed and only certain cells being able to be placed
These changes fix an infinite loop DEVSIX-3356
1 parent a611dd0 commit e324437

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,13 @@ public LayoutResult layout(LayoutContext layoutContext) {
888888
}
889889
// Apply borders if there is no footer
890890
if (null == footerRenderer) {
891-
if (0 != this.childRenderers.size()) {
891+
// If split renderer does not have any rows, it can mean two things:
892+
// - either nothing is placed and the top border, which have been already applied,
893+
// should be reverted
894+
// - or the only placed row is placed partially.
895+
// In the latter case the number of added child renderers should equal to the number of the cells
896+
// in the current row (currChildRenderers stands for it)
897+
if (!splitResult[0].rows.isEmpty() || currChildRenderers.size() == childRenderers.size()) {
892898
bordersHandler.applyBottomTableBorder(occupiedArea.getBBox(), layoutBox, false);
893899
} else {
894900
bordersHandler.applyTopTableBorder(occupiedArea.getBBox(), layoutBox, true);

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3054,6 +3054,74 @@ public void inheritHeaderPropsWhileMinMaxWidthCalculationsTest() throws IOExcept
30543054
sourceFolder + "cmp_" + filename, destinationFolder, "diff"));
30553055
}
30563056

3057+
@Test
3058+
@LogMessages(messages = {
3059+
@LogMessage(messageTemplate = LogMessageConstant.ELEMENT_DOES_NOT_FIT_AREA)
3060+
})
3061+
public void infiniteLoopOnUnfitCellAndBigRowspanTest() throws IOException, InterruptedException {
3062+
String testName = "infiniteLoopOnUnfitCellAndBigRowspanTest.pdf";
3063+
String outFileName = destinationFolder + testName;
3064+
String cmpFileName = sourceFolder + "cmp_" + testName;
3065+
3066+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
3067+
Document doc = new Document(pdfDoc, PageSize.A4.rotate());
3068+
3069+
Table table = new Table(38);
3070+
table.useAllAvailableWidth();
3071+
table.setFixedLayout();
3072+
3073+
Cell cellNum1 = new Cell(1, 1);
3074+
table.addCell(cellNum1);
3075+
3076+
Cell cellNum2 = new Cell(2, 2);
3077+
Image img = new Image(ImageDataFactory.create(sourceFolder + "itext.png"));
3078+
cellNum2.add(img);
3079+
table.addCell(cellNum2);
3080+
3081+
Cell cellNum3 = new Cell(2, 36);
3082+
cellNum3.add(new Paragraph("text"));
3083+
table.addCell(cellNum3);
3084+
3085+
doc.add(table);
3086+
doc.close();
3087+
3088+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, testName + "_diff"));
3089+
}
3090+
3091+
@Test
3092+
@LogMessages(messages = {
3093+
@LogMessage(messageTemplate = LogMessageConstant.ELEMENT_DOES_NOT_FIT_AREA),
3094+
@LogMessage(messageTemplate = LogMessageConstant.TABLE_WIDTH_IS_MORE_THAN_EXPECTED_DUE_TO_MIN_WIDTH)
3095+
})
3096+
public void firstRowNotFitBigRowspanTest() throws IOException, InterruptedException {
3097+
String testName = "firstRowNotFitBigRowspanTest.pdf";
3098+
String outFileName = destinationFolder + testName;
3099+
String cmpFileName = sourceFolder + "cmp_" + testName;
3100+
3101+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
3102+
Document doc = new Document(pdfDoc, PageSize.A4);
3103+
3104+
Table table = new Table(4);
3105+
3106+
table.addCell("row 1 col 1");
3107+
3108+
Cell notFitCell = new Cell(2, 1);
3109+
notFitCell.add(new Paragraph("row 1-2 col 2"));
3110+
notFitCell.setFontSize(1000);
3111+
table.addCell(notFitCell);
3112+
3113+
Cell fitCell = new Cell(2, 2);
3114+
fitCell.add(new Paragraph("row 1-2 col 3-4"));
3115+
table.addCell(fitCell);
3116+
3117+
table.addCell("row 2 col 1");
3118+
3119+
doc.add(table);
3120+
doc.close();
3121+
3122+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, testName + "_diff"));
3123+
}
3124+
30573125
@Test
30583126
// TODO DEVSIX-5250 The first column should be fully red
30593127
public void bigRowSpanTooFarFullTest() throws IOException, InterruptedException {

0 commit comments

Comments
 (0)