Skip to content

Commit 2b141be

Browse files
Update currentArea in RootRenderer after endMarginsCollapse call is done
1 parent bd3b83b commit 2b141be

File tree

4 files changed

+111
-4
lines changed

4 files changed

+111
-4
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public void addChild(IRenderer renderer) {
118118
while (currentArea != null && renderer != null && (result = renderer.setParent(this).layout(
119119
new LayoutContext(currentArea.clone(), childMarginsInfo, floatRendererAreas)))
120120
.getStatus() != LayoutResult.FULL) {
121+
boolean currentAreaNeedsToBeUpdated = false;
121122
if (result.getStatus() == LayoutResult.PARTIAL) {
122123
if (rendererIsFloat) {
123124
waitingNextPageRenderers.add(result.getOverflowRenderer());
@@ -131,7 +132,7 @@ public void addChild(IRenderer renderer) {
131132
currentPageNumber = nextStoredArea.getPageNumber();
132133
nextStoredArea = null;
133134
} else {
134-
updateCurrentAndInitialArea(result);
135+
currentAreaNeedsToBeUpdated = true;
135136
}
136137
}
137138
} else if (result.getStatus() == LayoutResult.NOTHING) {
@@ -141,7 +142,7 @@ public void addChild(IRenderer renderer) {
141142
waitingNextPageRenderers.add(result.getOverflowRenderer());
142143
break;
143144
}
144-
updateCurrentAndInitialArea(result);
145+
currentAreaNeedsToBeUpdated = true;
145146
} else {
146147
((ImageRenderer) result.getOverflowRenderer()).autoScale(currentArea);
147148
result.getOverflowRenderer().setProperty(Property.FORCED_PLACEMENT, true);
@@ -193,7 +194,7 @@ public void addChild(IRenderer renderer) {
193194
waitingNextPageRenderers.add(result.getOverflowRenderer());
194195
break;
195196
}
196-
updateCurrentAndInitialArea(result);
197+
currentAreaNeedsToBeUpdated = true;
197198
}
198199
}
199200
}
@@ -203,6 +204,11 @@ public void addChild(IRenderer renderer) {
203204

204205
if (marginsCollapsingEnabled) {
205206
marginsCollapseHandler.endChildMarginsHandling(currentArea.getBBox());
207+
}
208+
if (currentAreaNeedsToBeUpdated) {
209+
updateCurrentAndInitialArea(result);
210+
}
211+
if (marginsCollapsingEnabled) {
206212
marginsCollapseHandler = new MarginsCollapseHandler(this, null);
207213
childMarginsInfo = marginsCollapseHandler.startChildMarginsHandling(renderer, currentArea.getBBox());
208214
}

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

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ This file is part of the iText (R) project.
4444

4545

4646
import com.itextpdf.io.LogMessageConstant;
47+
import com.itextpdf.io.image.ImageData;
4748
import com.itextpdf.io.image.ImageDataFactory;
4849
import com.itextpdf.kernel.color.Color;
50+
import com.itextpdf.kernel.geom.PageSize;
4951
import com.itextpdf.kernel.geom.Rectangle;
5052
import com.itextpdf.kernel.pdf.PdfDocument;
5153
import com.itextpdf.kernel.pdf.PdfPage;
@@ -56,10 +58,12 @@ This file is part of the iText (R) project.
5658
import com.itextpdf.layout.element.Cell;
5759
import com.itextpdf.layout.element.Div;
5860
import com.itextpdf.layout.element.Image;
61+
import com.itextpdf.layout.element.List;
5962
import com.itextpdf.layout.element.Paragraph;
6063
import com.itextpdf.layout.element.Table;
6164
import com.itextpdf.layout.property.ClearPropertyValue;
6265
import com.itextpdf.layout.property.FloatPropertyValue;
66+
import com.itextpdf.layout.property.ListNumberingType;
6367
import com.itextpdf.layout.property.Property;
6468
import com.itextpdf.layout.property.UnitValue;
6569
import com.itextpdf.test.ExtendedITextTest;
@@ -1074,7 +1078,6 @@ public void floatsOnPageSplit13() throws IOException, InterruptedException {
10741078
Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFileName, destinationFolder, "diff32_"));
10751079
}
10761080

1077-
10781081
@Test
10791082
public void floatInlineBlockTest01() throws IOException, InterruptedException {
10801083
String cmpFileName = sourceFolder + "cmp_floatInlineBlockTest01.pdf";
@@ -1096,4 +1099,102 @@ public void floatInlineBlockTest01() throws IOException, InterruptedException {
10961099
doc.close();
10971100
Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFileName, destinationFolder, "diff14_"));
10981101
}
1102+
1103+
/**
1104+
* Suggested by Richard Cohn.
1105+
*/
1106+
@Test
1107+
public void floatRootElementNotFitPage01() throws IOException, InterruptedException {
1108+
String cmpFileName = sourceFolder + "cmp_floatRootElementNotFitPage01.pdf";
1109+
String outFile = destinationFolder + "floatRootElementNotFitPage01.pdf";
1110+
1111+
//Initialize PDF writer
1112+
PdfWriter writer = new PdfWriter(outFile);
1113+
1114+
//Initialize PDF document
1115+
PdfDocument pdf = new PdfDocument(writer);
1116+
pdf.setDefaultPageSize(new PageSize(600, 350));
1117+
pdf.setTagged();
1118+
1119+
// Initialize document
1120+
Document document = new Document(pdf);
1121+
1122+
// Document layout is correct if COLLAPSING_MARGINS is not true
1123+
document.setProperty(Property.COLLAPSING_MARGINS, true);
1124+
1125+
document.add(new Paragraph("Some text\nSome text\nSome text\nSome text\nSome text\nSome text"));
1126+
byte data[] = new byte[1];
1127+
ImageData raw = ImageDataFactory.create(1, 1, 1, 8, data, null);
1128+
Image image = new Image(raw).setHeight(200);
1129+
Div div = new Div();
1130+
div.add(image);
1131+
Div captionDiv = new Div();
1132+
captionDiv.add(new Paragraph("Caption line 1\n").add("line 2"));
1133+
div.add(captionDiv);
1134+
div.setProperty(Property.FLOAT, FloatPropertyValue.RIGHT);
1135+
//div.setKeepTogether(true);
1136+
document.add(div);
1137+
document.add(new Paragraph("After float"));
1138+
document.add(new List(ListNumberingType.DECIMAL)
1139+
.add("Some text\nSome text\nSome text\nSome text")
1140+
.add("Some text\nSome text\nSome text")
1141+
.add("Some text\nSome text")
1142+
.add("Some text\nSome text"));
1143+
1144+
document.close();
1145+
Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFileName, destinationFolder, "diff15_"));
1146+
}
1147+
1148+
/**
1149+
* Suggested by Richard Cohn.
1150+
*/
1151+
@Test
1152+
public void floatRootElementNotFitPage02() throws IOException, InterruptedException {
1153+
String cmpFileName = sourceFolder + "cmp_floatRootElementNotFitPage02.pdf";
1154+
String outFile = destinationFolder + "floatRootElementNotFitPage02.pdf";
1155+
1156+
//Initialize PDF writer
1157+
PdfWriter writer = new PdfWriter(outFile);
1158+
1159+
//Initialize PDF document
1160+
PdfDocument pdf = new PdfDocument(writer);
1161+
pdf.setDefaultPageSize(new PageSize(600, 350));
1162+
pdf.setTagged();
1163+
1164+
// Initialize document
1165+
Document document = new Document(pdf);
1166+
1167+
// Document layout is correct if COLLAPSING_MARGINS is not true
1168+
document.setProperty(Property.COLLAPSING_MARGINS, true);
1169+
1170+
document.add(new Paragraph("Some text\nSome text\nSome text\nSome text\nSome text\nSome text\nSome text"));
1171+
byte data[] = new byte[1];
1172+
ImageData raw = ImageDataFactory.create(1, 1, 1, 8, data, null);
1173+
Image image = new Image(raw).setHeight(200);
1174+
Div div = new Div();
1175+
div.add(image);
1176+
Div captionDiv = new Div();
1177+
captionDiv.add(new Paragraph("Caption line 1\n").add("line 2"));
1178+
div.add(captionDiv);
1179+
div.setProperty(Property.FLOAT, FloatPropertyValue.RIGHT);
1180+
div.setKeepTogether(true);
1181+
//document.add(div);
1182+
div = new Div();
1183+
image = new Image(raw).setHeight(200);
1184+
div.add(image);
1185+
div.add(captionDiv);
1186+
div.setProperty(Property.FLOAT, FloatPropertyValue.LEFT);
1187+
div.setKeepTogether(true);
1188+
document.add(div);
1189+
document.add(new Paragraph("After float").setKeepWithNext(true));
1190+
document.add(new List(ListNumberingType.DECIMAL)
1191+
.add("List text\nList text\nList text\nList text")
1192+
.add("List text\nList text\nList text")
1193+
.add("List text\nList text")
1194+
.add("List text\nList text"));
1195+
1196+
document.close();
1197+
1198+
Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFileName, destinationFolder, "diff16_"));
1199+
}
10991200
}

0 commit comments

Comments
 (0)