Skip to content

Commit 1459417

Browse files
committed
Fix LayoutExceptionMessageConstant.INVALID_CELL_INDEXES exception
DEVSIX-8427
1 parent e054c7a commit 1459417

File tree

7 files changed

+87
-27
lines changed

7 files changed

+87
-27
lines changed

layout/src/main/java/com/itextpdf/layout/exceptions/LayoutExceptionMessageConstant.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ public final class LayoutExceptionMessageConstant {
5353

5454
public static final String INVALID_COLUMN_PROPERTIES =
5555
"Invalid column-count/column-width/column-gap properties, they're absent or have negative value";
56-
public static final String INVALID_CELL_INDEXES =
57-
"Invalid grid-column/grid-row properties, cells overlapping";
5856
public static final String INVALID_FONT_PROPERTY_VALUE = "Invalid FONT property value type.";
5957
public static final String TAGGING_HINTKEY_SHOULD_HAVE_ACCES = "TaggingHintKey should have accessibility properties" ;
6058
public static final String GRID_AUTO_REPEAT_CAN_BE_USED_ONLY_ONCE

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Grid {
4343
private GridCell[][] rows = new GridCell[1][1];
4444
//Using array list instead of array for .NET portability
4545
private final List<Collection<GridCell>> uniqueCells = new ArrayList<>(2);
46+
private final List<GridCell> itemsWithoutPlace = new ArrayList<>();
4647

4748
/**
4849
* Creates a new grid instance.
@@ -139,6 +140,7 @@ Collection<GridCell> getUniqueGridCells(GridOrder iterationOrder) {
139140
}
140141
}
141142
}
143+
result.addAll(itemsWithoutPlace);
142144
uniqueCells.set(iterationOrder.ordinal(), result);
143145
return result;
144146
}
@@ -150,6 +152,7 @@ Collection<GridCell> getUniqueGridCells(GridOrder iterationOrder) {
150152
}
151153
}
152154
}
155+
result.addAll(itemsWithoutPlace);
153156
uniqueCells.set(iterationOrder.ordinal(), result);
154157
return result;
155158
}
@@ -219,11 +222,19 @@ int collapseNullLines(GridOrder order, int minSize) {
219222
* @param cell cell to and in the grid
220223
*/
221224
private void addCell(GridCell cell) {
225+
boolean placeFound = false;
222226
for (int i = cell.getRowStart(); i < cell.getRowEnd(); ++i) {
223227
for(int j = cell.getColumnStart(); j < cell.getColumnEnd(); ++j) {
224-
rows[i][j] = cell;
228+
if (rows[i][j] == null) {
229+
rows[i][j] = cell;
230+
placeFound = true;
231+
}
225232
}
226233
}
234+
235+
if (!placeFound) {
236+
itemsWithoutPlace.add(cell);
237+
}
227238
}
228239

229240
private int determineNullLinesStart(GridOrder order) {
@@ -462,9 +473,10 @@ void fit(GridCell cell) {
462473
//Move grid view cursor
463474
pos = view.next();
464475
}
465-
//If cell restricts both x and y position grow and can't be fitted on a grid, throw an excpetion
476+
// If cell restricts both x and y position grow and can't be fitted on a grid,
477+
// exit occupying fixed position
466478
if (view.isFixed()) {
467-
throw new IllegalArgumentException(LayoutExceptionMessageConstant.INVALID_CELL_INDEXES);
479+
break;
468480
}
469481
//If cell was not fitted while iterating grid, then there is not enough space to fit it, and grid
470482
//has to be resized

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ public LayoutResult layout(LayoutContext layoutContext) {
109109
*/
110110
@Override
111111
public void addChild(IRenderer renderer) {
112+
// The grid's items are not affected by the 'float' and 'clear' properties.
113+
// Still let clear them on renderer level not model element
114+
renderer.setProperty(Property.FLOAT, null);
115+
112116
renderer.setProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
113117
renderer.setProperty(Property.OVERFLOW_Y, OverflowPropertyValue.VISIBLE);
114118
renderer.setProperty(Property.COLLAPSING_MARGINS, determineCollapsingMargins(renderer));
@@ -193,21 +197,20 @@ private GridLayoutResult layoutGrid(LayoutContext layoutContext, Rectangle actua
193197
}
194198

195199
private static int processLayoutResult(GridLayoutResult layoutResult, GridCell cell, LayoutResult cellResult) {
196-
IRenderer cellToRenderer = cell.getValue();
200+
IRenderer overflowRenderer = cellResult.getOverflowRenderer();
197201
if (cellResult.getStatus() == LayoutResult.NOTHING) {
198-
cellToRenderer.setProperty(Property.GRID_COLUMN_START, cell.getColumnStart() + 1);
199-
cellToRenderer.setProperty(Property.GRID_COLUMN_END, cell.getColumnEnd() + 1);
200-
cellToRenderer.setProperty(Property.GRID_ROW_START, cell.getRowStart() + 1);
201-
cellToRenderer.setProperty(Property.GRID_ROW_END, cell.getRowEnd() + 1);
202-
layoutResult.getOverflowRenderers().add(cellToRenderer);
202+
overflowRenderer.setProperty(Property.GRID_COLUMN_START, cell.getColumnStart() + 1);
203+
overflowRenderer.setProperty(Property.GRID_COLUMN_END, cell.getColumnEnd() + 1);
204+
overflowRenderer.setProperty(Property.GRID_ROW_START, cell.getRowStart() + 1);
205+
overflowRenderer.setProperty(Property.GRID_ROW_END, cell.getRowEnd() + 1);
206+
layoutResult.getOverflowRenderers().add(overflowRenderer);
203207
layoutResult.setCauseOfNothing(cellResult.getCauseOfNothing());
204208
return cell.getRowStart();
205209
}
206210

207211
// PARTIAL + FULL result handling
208-
layoutResult.getSplitRenderers().add(cellToRenderer);
212+
layoutResult.getSplitRenderers().add(cell.getValue());
209213
if (cellResult.getStatus() == LayoutResult.PARTIAL) {
210-
IRenderer overflowRenderer = cellResult.getOverflowRenderer();
211214
overflowRenderer.setProperty(Property.GRID_COLUMN_START, cell.getColumnStart() + 1);
212215
overflowRenderer.setProperty(Property.GRID_COLUMN_END, cell.getColumnEnd() + 1);
213216
int rowStart = cell.getRowStart() + 1;

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ public <T1> T1 getProperty(int key) {
7777
case Property.GRID_ROW_START:
7878
case Property.GRID_ROW_END:
7979
case Property.GRID_ROW_SPAN:
80-
return renderer.<T1>getProperty(key);
80+
T1 ownValue = this.<T1>getOwnProperty(key);
81+
if (ownValue != null) {
82+
return ownValue;
83+
} else {
84+
return renderer.<T1>getProperty(key);
85+
}
8186

8287
default:
8388
break;
@@ -102,16 +107,17 @@ public void setProperty(int property, Object value) {
102107
break;
103108

104109
case Property.FILL_AVAILABLE_AREA:
110+
renderer.setProperty(property, value);
111+
break;
112+
113+
case Property.COLLAPSING_MARGINS:
114+
105115
case Property.GRID_COLUMN_START:
106116
case Property.GRID_COLUMN_END:
107117
case Property.GRID_COLUMN_SPAN:
108118
case Property.GRID_ROW_START:
109119
case Property.GRID_ROW_END:
110120
case Property.GRID_ROW_SPAN:
111-
renderer.setProperty(property, value);
112-
break;
113-
114-
case Property.COLLAPSING_MARGINS:
115121
super.setProperty(property, value);
116122
break;
117123

layout/src/test/java/com/itextpdf/layout/element/GridContainerTest.java

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.layout.element;
2424

25+
import com.itextpdf.io.logs.IoLogMessageConstant;
2526
import com.itextpdf.kernel.colors.ColorConstants;
2627
import com.itextpdf.kernel.pdf.PdfDocument;
2728
import com.itextpdf.kernel.pdf.PdfWriter;
@@ -44,6 +45,8 @@ This file is part of the iText (R) project.
4445
import com.itextpdf.layout.properties.grid.PointValue;
4546
import com.itextpdf.layout.properties.grid.TemplateValue;
4647
import com.itextpdf.test.ExtendedITextTest;
48+
import com.itextpdf.test.annotations.LogMessage;
49+
import com.itextpdf.test.annotations.LogMessages;
4750
import com.itextpdf.test.annotations.type.IntegrationTest;
4851

4952
import java.io.IOException;
@@ -374,13 +377,51 @@ public void fixedColumnRowGoesFirstTest() throws IOException, InterruptedExcepti
374377
}
375378

376379
@Test
377-
public void overlapWithExistingColumnTest() throws IOException {
378-
String filename = DESTINATION_FOLDER + "overlapWithExistingColumnTest.pdf";
380+
public void overlapWithExistingItemTest() throws IOException, InterruptedException {
381+
String filename = DESTINATION_FOLDER + "overlapWithExistingItemTest.pdf";
382+
String cmpName = SOURCE_FOLDER + "cmp_overlapWithExistingItemTest.pdf";
379383

380384
java.util.List<TemplateValue> templateColumns = new ArrayList<>();
381-
templateColumns.add(new PointValue(100.0f));
382-
templateColumns.add(new PointValue(100.0f));
383-
templateColumns.add(new PointValue(100.0f));
385+
templateColumns.add(MinContentValue.VALUE);
386+
templateColumns.add(MinContentValue.VALUE);
387+
templateColumns.add(MinContentValue.VALUE);
388+
389+
try (Document document = new Document(new PdfDocument(new PdfWriter(filename)))) {
390+
GridContainer grid = new GridContainer();
391+
SolidBorder border = new SolidBorder(ColorConstants.BLUE, 1);
392+
grid.setProperty(Property.GRID_TEMPLATE_COLUMNS, templateColumns);
393+
Paragraph paragraph1 = new Paragraph("Two");
394+
paragraph1.setProperty(Property.GRID_COLUMN_START, 1);
395+
paragraph1.setProperty(Property.GRID_COLUMN_END, 3);
396+
paragraph1.setProperty(Property.GRID_ROW_START, 1);
397+
paragraph1.setProperty(Property.GRID_ROW_END, 3);
398+
paragraph1.setBorder(border);
399+
grid.add(paragraph1);
400+
grid.add(new Paragraph("Three").setBorder(border));
401+
grid.add(new Paragraph("Four").setBorder(border));
402+
grid.add(new Paragraph("Five").setBorder(border));
403+
Paragraph paragraph2 = new Paragraph("One (long content)");
404+
paragraph2.setProperty(Property.GRID_COLUMN_START, 1);
405+
paragraph2.setProperty(Property.GRID_COLUMN_END, 2);
406+
paragraph2.setProperty(Property.GRID_ROW_START, 1);
407+
paragraph2.setProperty(Property.GRID_ROW_END, 2);
408+
paragraph2.setBorder(border);
409+
grid.add(paragraph2);
410+
document.add(grid);
411+
}
412+
413+
Assert.assertNull(new CompareTool().compareByContent(filename, cmpName, DESTINATION_FOLDER, "diff_"));
414+
}
415+
416+
@Test
417+
public void coverExistingItemTest() throws IOException, InterruptedException {
418+
String filename = DESTINATION_FOLDER + "coverExistingItemTest.pdf";
419+
String cmpName = SOURCE_FOLDER + "cmp_coverExistingItemTest.pdf";
420+
421+
java.util.List<TemplateValue> templateColumns = new ArrayList<>();
422+
templateColumns.add(MinContentValue.VALUE);
423+
templateColumns.add(MinContentValue.VALUE);
424+
templateColumns.add(MinContentValue.VALUE);
384425

385426
try (Document document = new Document(new PdfDocument(new PdfWriter(filename)))) {
386427
GridContainer grid = new GridContainer();
@@ -396,17 +437,17 @@ public void overlapWithExistingColumnTest() throws IOException {
396437
grid.add(new Paragraph("Three").setBorder(border));
397438
grid.add(new Paragraph("Four").setBorder(border));
398439
grid.add(new Paragraph("Five").setBorder(border));
399-
Paragraph paragraph2 = new Paragraph("One");
440+
Paragraph paragraph2 = new Paragraph("One (long content)");
400441
paragraph2.setProperty(Property.GRID_COLUMN_START, 1);
401442
paragraph2.setProperty(Property.GRID_COLUMN_END, 3);
402443
paragraph2.setProperty(Property.GRID_ROW_START, 1);
403444
paragraph2.setProperty(Property.GRID_ROW_END, 3);
404445
paragraph2.setBorder(border);
405446
grid.add(paragraph2);
406-
Exception e = Assert.assertThrows(IllegalArgumentException.class, () ->
407-
document.add(grid));
408-
Assert.assertEquals(LayoutExceptionMessageConstant.INVALID_CELL_INDEXES, e.getMessage());
447+
document.add(grid);
409448
}
449+
450+
Assert.assertNull(new CompareTool().compareByContent(filename, cmpName, DESTINATION_FOLDER, "diff_"));
410451
}
411452

412453
@Test

0 commit comments

Comments
 (0)