Skip to content

Commit c367aa5

Browse files
committed
Support continuous container for grid
DEVSIX-8331
1 parent 413dbba commit c367aa5

File tree

11 files changed

+145
-12
lines changed

11 files changed

+145
-12
lines changed

layout/src/main/java/com/itextpdf/layout/margincollapse/MarginsCollapseHandler.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.layout.margincollapse;
2424

25-
import com.itextpdf.io.logs.IoLogMessageConstant;
2625
import com.itextpdf.commons.utils.MessageFormatUtil;
26+
import com.itextpdf.io.logs.IoLogMessageConstant;
2727
import com.itextpdf.kernel.geom.Rectangle;
2828
import com.itextpdf.layout.IPropertyContainer;
29+
import com.itextpdf.layout.properties.ContinuousContainer;
2930
import com.itextpdf.layout.properties.FloatPropertyValue;
3031
import com.itextpdf.layout.properties.Property;
3132
import com.itextpdf.layout.properties.UnitValue;
@@ -37,11 +38,11 @@ This file is part of the iText (R) project.
3738
import com.itextpdf.layout.renderer.IRenderer;
3839
import com.itextpdf.layout.renderer.LineRenderer;
3940
import com.itextpdf.layout.renderer.TableRenderer;
40-
import org.slf4j.Logger;
41-
import org.slf4j.LoggerFactory;
4241

4342
import java.util.ArrayList;
4443
import java.util.List;
44+
import org.slf4j.Logger;
45+
import org.slf4j.LoggerFactory;
4546

4647
/**
4748
* Rules of the margins collapsing are taken from Mozilla Developer Network:
@@ -575,6 +576,10 @@ private static void ignoreModelBottomMargin(IRenderer renderer) {
575576

576577
private static void overrideModelBottomMargin(IRenderer renderer, float collapsedMargins) {
577578
MarginsCollapseHandler.overrideModelMargin(renderer, Property.MARGIN_BOTTOM, collapsedMargins);
579+
if (renderer.hasProperty(Property.TREAT_AS_CONTINUOUS_CONTAINER_RESULT)) {
580+
ContinuousContainer continuousContainer = renderer.<ContinuousContainer>getProperty(Property.TREAT_AS_CONTINUOUS_CONTAINER_RESULT);
581+
continuousContainer.updateValueOfSavedProperty(Property.MARGIN_BOTTOM, UnitValue.createPointValue(collapsedMargins));
582+
}
578583
}
579584

580585
private static float defineMarginValueForCollapse(IRenderer renderer, int property) {

layout/src/main/java/com/itextpdf/layout/properties/ContinuousContainer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,14 @@ public void reApplyProperties(AbstractRenderer blockRenderer) {
115115
blockRenderer.setProperty(Property.BORDER_BOTTOM, allBorders);
116116
}
117117
}
118+
119+
/**
120+
* Updates values of the saved property.
121+
*
122+
* @param property the property to be updated
123+
* @param value the new value
124+
*/
125+
public void updateValueOfSavedProperty(int property, Object value) {
126+
properties.put(property, value);
127+
}
118128
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,8 @@ public LayoutResult layout(LayoutContext layoutContext) {
474474

475475
FloatingHelper.removeFloatsAboveRendererBottom(floatRendererAreas, this);
476476

477-
ContinuousContainer.clearPropertiesFromOverFlowRenderer(overflowRenderer);
478-
479477
if (layoutResult != LayoutResult.NOTHING) {
478+
ContinuousContainer.clearPropertiesFromOverFlowRenderer(overflowRenderer);
480479
LayoutArea editedArea = FloatingHelper.adjustResultOccupiedAreaForFloatAndClear(this,
481480
layoutContext.getFloatRendererAreas(), layoutContext.getArea().getBBox(), clearHeightCorrection,
482481
bfcHeightCorrection, marginsCollapsingEnabled);
@@ -676,7 +675,9 @@ AbstractRenderer[] createSplitAndOverflowRenderers(int childPos, int layoutStatu
676675
}
677676
overflowRenderer.childRenderers.addAll(childRenderers.subList(childPos + 1, childRenderers.size()));
678677

679-
ContinuousContainer.clearPropertiesFromOverFlowRenderer(overflowRenderer);
678+
if (layoutStatus != LayoutResult.NOTHING) {
679+
ContinuousContainer.clearPropertiesFromOverFlowRenderer(overflowRenderer);
680+
}
680681

681682
if (childResult.getStatus() == LayoutResult.PARTIAL) {
682683
// Apply forced placement only on split renderer

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ public IRenderer getNextRenderer() {
6767
*/
6868
@Override
6969
public LayoutResult layout(LayoutContext layoutContext) {
70-
//TODO DEVSIX-8331 enable continuous container
71-
//this.setProperty(Property.TREAT_AS_CONTINUOUS_CONTAINER, Boolean.TRUE);
70+
this.setProperty(Property.TREAT_AS_CONTINUOUS_CONTAINER, Boolean.TRUE);
7271

7372
Rectangle actualBBox = layoutContext.getArea().getBBox().clone();
7473
Float blockWidth = retrieveWidth(actualBBox.getWidth());
@@ -91,6 +90,9 @@ public LayoutResult layout(LayoutContext layoutContext) {
9190
GridLayoutResult layoutResult = layoutGrid(layoutContext, actualBBox, grid);
9291

9392
if (layoutResult.getOverflowRenderers().isEmpty()) {
93+
final ContinuousContainer continuousContainer = this.<ContinuousContainer>getProperty(Property.TREAT_AS_CONTINUOUS_CONTAINER_RESULT);
94+
continuousContainer.reApplyProperties(this);
95+
9496
this.occupiedArea = calculateContainerOccupiedArea(layoutContext, true);
9597
return new LayoutResult(LayoutResult.FULL, this.occupiedArea, null, null);
9698
} else if (layoutResult.getSplitRenderers().isEmpty()) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ This file is part of the iText (R) project.
2323
package com.itextpdf.layout.renderer;
2424

2525
import com.itextpdf.kernel.geom.Rectangle;
26+
import com.itextpdf.layout.borders.Border;
2627
import com.itextpdf.layout.element.Div;
2728
import com.itextpdf.layout.properties.Property;
29+
import com.itextpdf.layout.properties.UnitValue;
2830

2931
/**
3032
* Wrapper renderer around grid item. It's expected there is always exactly 1 child renderer.
@@ -155,6 +157,16 @@ float calculateHeight(float initialHeight) {
155157
final Rectangle rectangle = new Rectangle(0, 0, 0, initialHeight);
156158
if (AbstractRenderer.isBorderBoxSizing(renderer)) {
157159
renderer.applyMargins(rectangle, false);
160+
// In BlockRenderer#layout, after applying continuous container, we call AbstractRenderer#retrieveMaxHeight,
161+
// which calls AbstractRenderer#retrieveHeight where in case of BoxSizing we reduce the height for top
162+
// padding and border. So to reduce the height for top + bottom border, padding and margin here we apply
163+
// both top and bottom margin, but only bottom padding and border
164+
UnitValue paddingBottom = renderer.<UnitValue>getProperty(Property.PADDING_BOTTOM);
165+
if (paddingBottom.isPointValue()) {
166+
rectangle.decreaseHeight(paddingBottom.getValue());
167+
}
168+
Border borderBottom = renderer.getBorders()[AbstractRenderer.BOTTOM_SIDE];
169+
rectangle.decreaseHeight(borderBottom.getWidth());
158170
} else {
159171
renderer.applyMarginsBordersPaddings(rectangle, false);
160172
}

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

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

25-
import com.itextpdf.io.logs.IoLogMessageConstant;
2625
import com.itextpdf.kernel.colors.ColorConstants;
2726
import com.itextpdf.kernel.pdf.PdfDocument;
2827
import com.itextpdf.kernel.pdf.PdfWriter;
2928
import com.itextpdf.kernel.utils.CompareTool;
3029
import com.itextpdf.layout.Document;
3130
import com.itextpdf.layout.borders.SolidBorder;
32-
import com.itextpdf.layout.exceptions.LayoutExceptionMessageConstant;
31+
import com.itextpdf.layout.properties.BoxSizingPropertyValue;
3332
import com.itextpdf.layout.properties.Property;
3433
import com.itextpdf.layout.properties.UnitValue;
3534
import com.itextpdf.layout.properties.grid.AutoRepeatValue;
@@ -45,8 +44,6 @@ This file is part of the iText (R) project.
4544
import com.itextpdf.layout.properties.grid.PointValue;
4645
import com.itextpdf.layout.properties.grid.TemplateValue;
4746
import com.itextpdf.test.ExtendedITextTest;
48-
import com.itextpdf.test.annotations.LogMessage;
49-
import com.itextpdf.test.annotations.LogMessages;
5047
import com.itextpdf.test.annotations.type.IntegrationTest;
5148

5249
import java.io.IOException;
@@ -969,4 +966,110 @@ public void marginsCollapsingIssueTest() throws IOException, InterruptedExceptio
969966
}
970967
Assert.assertNull(new CompareTool().compareByContent(filename, cmpName, DESTINATION_FOLDER, "diff_"));
971968
}
969+
970+
@Test
971+
public void splitPageTest() throws IOException, InterruptedException {
972+
String filename = DESTINATION_FOLDER + "splitPageTest.pdf";
973+
String cmpName = SOURCE_FOLDER + "cmp_splitPageTest.pdf";
974+
java.util.List<TemplateValue> columnsTemplate = new ArrayList<>();
975+
columnsTemplate.add(new AutoRepeatValue(true, Arrays.asList((GridValue) new PointValue(200))));
976+
977+
try (Document document = new Document(new PdfDocument(new PdfWriter(filename)))) {
978+
GridContainer grid = new GridContainer();
979+
grid.setWidth(420);
980+
grid.setBorder(new SolidBorder(20));
981+
grid.setPadding(20);
982+
grid.setMargin(20);
983+
grid.setProperty(Property.GRID_TEMPLATE_COLUMNS, columnsTemplate);
984+
grid.setProperty(Property.COLUMN_GAP, 20.0f);
985+
grid.add(new Paragraph("One").setBackgroundColor(ColorConstants.CYAN));
986+
grid.add(new Paragraph("Two").setBackgroundColor(ColorConstants.CYAN));
987+
grid.add(new Paragraph("Tree").setBackgroundColor(ColorConstants.CYAN));
988+
grid.add(new Paragraph("Four").setBackgroundColor(ColorConstants.CYAN));
989+
grid.add(new Paragraph("Five").setBackgroundColor(ColorConstants.CYAN));
990+
grid.add(new Paragraph("Six").setBackgroundColor(ColorConstants.CYAN));
991+
grid.add(new Paragraph("Seven").setBackgroundColor(ColorConstants.CYAN));
992+
grid.add(new Paragraph("Eight").setBackgroundColor(ColorConstants.CYAN));
993+
grid.add(new Paragraph("Nine").setBackgroundColor(ColorConstants.CYAN));
994+
995+
Div emptyDiv = new Div();
996+
emptyDiv.setHeight(640);
997+
emptyDiv.setBackgroundColor(ColorConstants.LIGHT_GRAY);
998+
document.add(emptyDiv);
999+
document.add(grid);
1000+
}
1001+
Assert.assertNull(new CompareTool().compareByContent(filename, cmpName, DESTINATION_FOLDER, "diff_"));
1002+
}
1003+
1004+
@Test
1005+
public void borderBoxSizingTest() throws IOException, InterruptedException {
1006+
String filename = DESTINATION_FOLDER + "borderBoxSizingTest.pdf";
1007+
String cmpName = SOURCE_FOLDER + "cmp_borderBoxSizingTest.pdf";
1008+
java.util.List<TemplateValue> columnsTemplate = new ArrayList<>();
1009+
columnsTemplate.add(new AutoRepeatValue(true, Arrays.asList((GridValue) new PointValue(200))));
1010+
1011+
try (Document document = new Document(new PdfDocument(new PdfWriter(filename)))) {
1012+
GridContainer grid = new GridContainer();
1013+
grid.setWidth(420);
1014+
grid.setBorder(new SolidBorder(20));
1015+
grid.setPadding(20);
1016+
grid.setMargin(20);
1017+
grid.setProperty(Property.GRID_TEMPLATE_COLUMNS, columnsTemplate);
1018+
grid.setProperty(Property.COLUMN_GAP, 20.0f);
1019+
grid.add(new Paragraph("One").setBackgroundColor(ColorConstants.CYAN));
1020+
1021+
final Paragraph two = new Paragraph("Two").setBackgroundColor(ColorConstants.CYAN);
1022+
two.setProperty(Property.BOX_SIZING, BoxSizingPropertyValue.BORDER_BOX);
1023+
two.setPadding(10);
1024+
two.setMargin(10);
1025+
two.setBorder(new SolidBorder(ColorConstants.BLUE, 10));
1026+
1027+
grid.add(two);
1028+
grid.add(new Paragraph("Tree").setBackgroundColor(ColorConstants.CYAN));
1029+
grid.add(new Paragraph("Four").setBackgroundColor(ColorConstants.CYAN));
1030+
grid.add(new Paragraph("Five").setBackgroundColor(ColorConstants.CYAN));
1031+
grid.add(new Paragraph("Six").setBackgroundColor(ColorConstants.CYAN));
1032+
grid.add(new Paragraph("Seven").setBackgroundColor(ColorConstants.CYAN));
1033+
grid.add(new Paragraph("Eight").setBackgroundColor(ColorConstants.CYAN));
1034+
grid.add(new Paragraph("Nine").setBackgroundColor(ColorConstants.CYAN));
1035+
1036+
document.add(grid);
1037+
}
1038+
Assert.assertNull(new CompareTool().compareByContent(filename, cmpName, DESTINATION_FOLDER, "diff_"));
1039+
}
1040+
1041+
@Test
1042+
public void marginCollapsingTest() throws IOException, InterruptedException {
1043+
String filename = DESTINATION_FOLDER + "marginCollapsingTest.pdf";
1044+
String cmpName = SOURCE_FOLDER + "cmp_marginCollapsingTest.pdf";
1045+
java.util.List<TemplateValue> columnsTemplate = new ArrayList<>();
1046+
columnsTemplate.add(new AutoRepeatValue(true, Arrays.asList((GridValue) new PointValue(200))));
1047+
1048+
try (Document document = new Document(new PdfDocument(new PdfWriter(filename)))) {
1049+
GridContainer grid = new GridContainer();
1050+
grid.setWidth(420);
1051+
grid.setBorder(new SolidBorder(20));
1052+
grid.setPadding(20);
1053+
grid.setMargin(20);
1054+
grid.setProperty(Property.GRID_TEMPLATE_COLUMNS, columnsTemplate);
1055+
grid.setProperty(Property.COLUMN_GAP, 20.0f);
1056+
grid.add(new Paragraph("One").setBackgroundColor(ColorConstants.CYAN));
1057+
1058+
Div twoParent = new Div();
1059+
final Paragraph two = new Paragraph("Two").setBackgroundColor(ColorConstants.CYAN);
1060+
twoParent.add(two);
1061+
twoParent.add(new Div());
1062+
grid.add(twoParent);
1063+
grid.add(new Paragraph("Tree").setBackgroundColor(ColorConstants.CYAN));
1064+
grid.add(new Paragraph("Four").setBackgroundColor(ColorConstants.CYAN));
1065+
grid.add(new Paragraph("Five").setBackgroundColor(ColorConstants.CYAN));
1066+
grid.add(new Paragraph("Six").setBackgroundColor(ColorConstants.CYAN));
1067+
grid.add(new Paragraph("Seven").setBackgroundColor(ColorConstants.CYAN));
1068+
grid.add(new Paragraph("Eight").setBackgroundColor(ColorConstants.CYAN));
1069+
grid.add(new Paragraph("Nine").setBackgroundColor(ColorConstants.CYAN));
1070+
1071+
document.add(grid);
1072+
}
1073+
Assert.assertNull(new CompareTool().compareByContent(filename, cmpName, DESTINATION_FOLDER, "diff_"));
1074+
}
9721075
}

0 commit comments

Comments
 (0)