Skip to content

Commit 8ade291

Browse files
vitali-priText-CI
authored andcommitted
Fix LayoutExceptionMessageConstant.INVALID_CELL_INDEXES exception
DEVSIX-8427 Autoported commit. Original commit hash: [14594176c]
1 parent 1534c4e commit 8ade291

File tree

8 files changed

+85
-31
lines changed

8 files changed

+85
-31
lines changed

itext.tests/itext.layout.tests/itext/layout/element/GridContainerTest.cs

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ You should have received a copy of the GNU Affero General Public License
2828
using iText.Kernel.Utils;
2929
using iText.Layout;
3030
using iText.Layout.Borders;
31-
using iText.Layout.Exceptions;
3231
using iText.Layout.Properties;
3332
using iText.Layout.Properties.Grid;
3433
using iText.Test;
@@ -344,12 +343,48 @@ public virtual void FixedColumnRowGoesFirstTest() {
344343
}
345344

346345
[NUnit.Framework.Test]
347-
public virtual void OverlapWithExistingColumnTest() {
348-
String filename = DESTINATION_FOLDER + "overlapWithExistingColumnTest.pdf";
346+
public virtual void OverlapWithExistingItemTest() {
347+
String filename = DESTINATION_FOLDER + "overlapWithExistingItemTest.pdf";
348+
String cmpName = SOURCE_FOLDER + "cmp_overlapWithExistingItemTest.pdf";
349349
IList<TemplateValue> templateColumns = new List<TemplateValue>();
350-
templateColumns.Add(new PointValue(100.0f));
351-
templateColumns.Add(new PointValue(100.0f));
352-
templateColumns.Add(new PointValue(100.0f));
350+
templateColumns.Add(MinContentValue.VALUE);
351+
templateColumns.Add(MinContentValue.VALUE);
352+
templateColumns.Add(MinContentValue.VALUE);
353+
using (Document document = new Document(new PdfDocument(new PdfWriter(filename)))) {
354+
GridContainer grid = new GridContainer();
355+
SolidBorder border = new SolidBorder(ColorConstants.BLUE, 1);
356+
grid.SetProperty(Property.GRID_TEMPLATE_COLUMNS, templateColumns);
357+
Paragraph paragraph1 = new Paragraph("Two");
358+
paragraph1.SetProperty(Property.GRID_COLUMN_START, 1);
359+
paragraph1.SetProperty(Property.GRID_COLUMN_END, 3);
360+
paragraph1.SetProperty(Property.GRID_ROW_START, 1);
361+
paragraph1.SetProperty(Property.GRID_ROW_END, 3);
362+
paragraph1.SetBorder(border);
363+
grid.Add(paragraph1);
364+
grid.Add(new Paragraph("Three").SetBorder(border));
365+
grid.Add(new Paragraph("Four").SetBorder(border));
366+
grid.Add(new Paragraph("Five").SetBorder(border));
367+
Paragraph paragraph2 = new Paragraph("One (long content)");
368+
paragraph2.SetProperty(Property.GRID_COLUMN_START, 1);
369+
paragraph2.SetProperty(Property.GRID_COLUMN_END, 2);
370+
paragraph2.SetProperty(Property.GRID_ROW_START, 1);
371+
paragraph2.SetProperty(Property.GRID_ROW_END, 2);
372+
paragraph2.SetBorder(border);
373+
grid.Add(paragraph2);
374+
document.Add(grid);
375+
}
376+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(filename, cmpName, DESTINATION_FOLDER, "diff_"
377+
));
378+
}
379+
380+
[NUnit.Framework.Test]
381+
public virtual void CoverExistingItemTest() {
382+
String filename = DESTINATION_FOLDER + "coverExistingItemTest.pdf";
383+
String cmpName = SOURCE_FOLDER + "cmp_coverExistingItemTest.pdf";
384+
IList<TemplateValue> templateColumns = new List<TemplateValue>();
385+
templateColumns.Add(MinContentValue.VALUE);
386+
templateColumns.Add(MinContentValue.VALUE);
387+
templateColumns.Add(MinContentValue.VALUE);
353388
using (Document document = new Document(new PdfDocument(new PdfWriter(filename)))) {
354389
GridContainer grid = new GridContainer();
355390
SolidBorder border = new SolidBorder(ColorConstants.BLUE, 1);
@@ -364,16 +399,17 @@ public virtual void OverlapWithExistingColumnTest() {
364399
grid.Add(new Paragraph("Three").SetBorder(border));
365400
grid.Add(new Paragraph("Four").SetBorder(border));
366401
grid.Add(new Paragraph("Five").SetBorder(border));
367-
Paragraph paragraph2 = new Paragraph("One");
402+
Paragraph paragraph2 = new Paragraph("One (long content)");
368403
paragraph2.SetProperty(Property.GRID_COLUMN_START, 1);
369404
paragraph2.SetProperty(Property.GRID_COLUMN_END, 3);
370405
paragraph2.SetProperty(Property.GRID_ROW_START, 1);
371406
paragraph2.SetProperty(Property.GRID_ROW_END, 3);
372407
paragraph2.SetBorder(border);
373408
grid.Add(paragraph2);
374-
Exception e = NUnit.Framework.Assert.Catch(typeof(ArgumentException), () => document.Add(grid));
375-
NUnit.Framework.Assert.AreEqual(LayoutExceptionMessageConstant.INVALID_CELL_INDEXES, e.Message);
409+
document.Add(grid);
376410
}
411+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(filename, cmpName, DESTINATION_FOLDER, "diff_"
412+
));
377413
}
378414

379415
[NUnit.Framework.Test]

itext/itext.layout/itext/layout/exceptions/LayoutExceptionMessageConstant.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ public sealed class LayoutExceptionMessageConstant {
5959

6060
public const String INVALID_COLUMN_PROPERTIES = "Invalid column-count/column-width/column-gap properties, they're absent or have negative value";
6161

62-
public const String INVALID_CELL_INDEXES = "Invalid grid-column/grid-row properties, cells overlapping";
63-
6462
public const String INVALID_FONT_PROPERTY_VALUE = "Invalid FONT property value type.";
6563

6664
public const String TAGGING_HINTKEY_SHOULD_HAVE_ACCES = "TaggingHintKey should have accessibility properties";

itext/itext.layout/itext/layout/renderer/Grid.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ You should have received a copy of the GNU Affero General Public License
2424
using System.Collections.Generic;
2525
using System.Linq;
2626
using iText.Commons.Utils;
27-
using iText.Layout.Exceptions;
2827
using iText.Layout.Properties.Grid;
2928

3029
namespace iText.Layout.Renderer {
@@ -42,6 +41,8 @@ internal class Grid {
4241
//Using array list instead of array for .NET portability
4342
private readonly IList<ICollection<GridCell>> uniqueCells = new List<ICollection<GridCell>>(2);
4443

44+
private readonly IList<GridCell> itemsWithoutPlace = new List<GridCell>();
45+
4546
//\cond DO_NOT_DOCUMENT
4647
/// <summary>Creates a new grid instance.</summary>
4748
/// <param name="initialRowsCount">initial number of row for the grid</param>
@@ -138,6 +139,7 @@ internal virtual ICollection<GridCell> GetUniqueGridCells(Grid.GridOrder iterati
138139
}
139140
}
140141
}
142+
result.AddAll(itemsWithoutPlace);
141143
uniqueCells[(int)(iterationOrder)] = result;
142144
return result;
143145
}
@@ -149,6 +151,7 @@ internal virtual ICollection<GridCell> GetUniqueGridCells(Grid.GridOrder iterati
149151
}
150152
}
151153
}
154+
result.AddAll(itemsWithoutPlace);
152155
uniqueCells[(int)(iterationOrder)] = result;
153156
return result;
154157
}
@@ -224,11 +227,18 @@ internal virtual int CollapseNullLines(Grid.GridOrder order, int minSize) {
224227
/// <summary>Add cell in the grid, checking that it would fit and initializing it bottom left corner (x, y).</summary>
225228
/// <param name="cell">cell to and in the grid</param>
226229
private void AddCell(GridCell cell) {
230+
bool placeFound = false;
227231
for (int i = cell.GetRowStart(); i < cell.GetRowEnd(); ++i) {
228232
for (int j = cell.GetColumnStart(); j < cell.GetColumnEnd(); ++j) {
229-
rows[i][j] = cell;
233+
if (rows[i][j] == null) {
234+
rows[i][j] = cell;
235+
placeFound = true;
236+
}
230237
}
231238
}
239+
if (!placeFound) {
240+
itemsWithoutPlace.Add(cell);
241+
}
232242
}
233243

234244
private int DetermineNullLinesStart(Grid.GridOrder order) {
@@ -471,9 +481,10 @@ internal virtual void Fit(GridCell cell) {
471481
//Move grid view cursor
472482
pos = view.Next();
473483
}
474-
//If cell restricts both x and y position grow and can't be fitted on a grid, throw an excpetion
484+
// If cell restricts both x and y position grow and can't be fitted on a grid,
485+
// exit occupying fixed position
475486
if (view.IsFixed()) {
476-
throw new ArgumentException(LayoutExceptionMessageConstant.INVALID_CELL_INDEXES);
487+
break;
477488
}
478489
//If cell was not fitted while iterating grid, then there is not enough space to fit it, and grid
479490
//has to be resized

itext/itext.layout/itext/layout/renderer/GridContainerRenderer.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ public override LayoutResult Layout(LayoutContext layoutContext) {
9393

9494
/// <summary><inheritDoc/></summary>
9595
public override void AddChild(IRenderer renderer) {
96+
// The grid's items are not affected by the 'float' and 'clear' properties.
97+
// Still let clear them on renderer level not model element
98+
renderer.SetProperty(Property.FLOAT, null);
9699
renderer.SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
97100
renderer.SetProperty(Property.OVERFLOW_Y, OverflowPropertyValue.VISIBLE);
98101
renderer.SetProperty(Property.COLLAPSING_MARGINS, DetermineCollapsingMargins(renderer));
@@ -171,20 +174,19 @@ private GridContainerRenderer.GridLayoutResult LayoutGrid(LayoutContext layoutCo
171174

172175
private static int ProcessLayoutResult(GridContainerRenderer.GridLayoutResult layoutResult, GridCell cell,
173176
LayoutResult cellResult) {
174-
IRenderer cellToRenderer = cell.GetValue();
177+
IRenderer overflowRenderer = cellResult.GetOverflowRenderer();
175178
if (cellResult.GetStatus() == LayoutResult.NOTHING) {
176-
cellToRenderer.SetProperty(Property.GRID_COLUMN_START, cell.GetColumnStart() + 1);
177-
cellToRenderer.SetProperty(Property.GRID_COLUMN_END, cell.GetColumnEnd() + 1);
178-
cellToRenderer.SetProperty(Property.GRID_ROW_START, cell.GetRowStart() + 1);
179-
cellToRenderer.SetProperty(Property.GRID_ROW_END, cell.GetRowEnd() + 1);
180-
layoutResult.GetOverflowRenderers().Add(cellToRenderer);
179+
overflowRenderer.SetProperty(Property.GRID_COLUMN_START, cell.GetColumnStart() + 1);
180+
overflowRenderer.SetProperty(Property.GRID_COLUMN_END, cell.GetColumnEnd() + 1);
181+
overflowRenderer.SetProperty(Property.GRID_ROW_START, cell.GetRowStart() + 1);
182+
overflowRenderer.SetProperty(Property.GRID_ROW_END, cell.GetRowEnd() + 1);
183+
layoutResult.GetOverflowRenderers().Add(overflowRenderer);
181184
layoutResult.SetCauseOfNothing(cellResult.GetCauseOfNothing());
182185
return cell.GetRowStart();
183186
}
184187
// PARTIAL + FULL result handling
185-
layoutResult.GetSplitRenderers().Add(cellToRenderer);
188+
layoutResult.GetSplitRenderers().Add(cell.GetValue());
186189
if (cellResult.GetStatus() == LayoutResult.PARTIAL) {
187-
IRenderer overflowRenderer = cellResult.GetOverflowRenderer();
188190
overflowRenderer.SetProperty(Property.GRID_COLUMN_START, cell.GetColumnStart() + 1);
189191
overflowRenderer.SetProperty(Property.GRID_COLUMN_END, cell.GetColumnEnd() + 1);
190192
int rowStart = cell.GetRowStart() + 1;

itext/itext.layout/itext/layout/renderer/GridItemRenderer.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,14 @@ public override T1 GetProperty<T1>(int key) {
7070
case Property.GRID_ROW_START:
7171
case Property.GRID_ROW_END:
7272
case Property.GRID_ROW_SPAN: {
73-
return renderer.GetProperty<T1>(key);
73+
T1 ownValue = this.GetOwnProperty<T1>(key);
74+
if (ownValue != null) {
75+
return ownValue;
76+
}
77+
else {
78+
return renderer.GetProperty<T1>(key);
79+
}
80+
goto default;
7481
}
7582

7683
default: {
@@ -93,18 +100,18 @@ public override void SetProperty(int property, Object value) {
93100
break;
94101
}
95102

96-
case Property.FILL_AVAILABLE_AREA:
103+
case Property.FILL_AVAILABLE_AREA: {
104+
renderer.SetProperty(property, value);
105+
break;
106+
}
107+
108+
case Property.COLLAPSING_MARGINS:
97109
case Property.GRID_COLUMN_START:
98110
case Property.GRID_COLUMN_END:
99111
case Property.GRID_COLUMN_SPAN:
100112
case Property.GRID_ROW_START:
101113
case Property.GRID_ROW_END:
102114
case Property.GRID_ROW_SPAN: {
103-
renderer.SetProperty(property, value);
104-
break;
105-
}
106-
107-
case Property.COLLAPSING_MARGINS: {
108115
base.SetProperty(property, value);
109116
break;
110117
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
e054c7abf957145ef4dcd2d3a8628b7c8bf24b7a
1+
14594176c3df587ff53e54994152bf09ca64d596

0 commit comments

Comments
 (0)