Skip to content

Commit 9b2dfd9

Browse files
Return adjusted by float and clear properties area for elements which have PARTIAL layout result
1 parent e3aa7a2 commit 9b2dfd9

File tree

6 files changed

+91
-15
lines changed

6 files changed

+91
-15
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ public LayoutResult layout(LayoutContext layoutContext) {
186186
occupiedArea.setBBox(Rectangle.getCommonRectangle(occupiedArea.getBBox(), result.getOccupiedArea().getBBox()));
187187
}
188188

189+
if (FloatingHelper.isRendererFloating(this) || isCellRenderer) {
190+
FloatingHelper.includeChildFloatsInOccupiedArea(floatRendererAreas, this);
191+
}
192+
189193
if (result.getSplitRenderer() != null) {
190194
// Use occupied area's bbox width so that for absolutely positioned renderers we do not align using full width
191195
// in case when parent box should wrap around child boxes.
@@ -217,9 +221,6 @@ public LayoutResult layout(LayoutContext layoutContext) {
217221
} else {
218222
if (result.getStatus() == LayoutResult.PARTIAL) {
219223
if (currentAreaPos + 1 == areas.size()) {
220-
if (FloatingHelper.isRendererFloating(this) || isCellRenderer) {
221-
FloatingHelper.includeChildFloatsInOccupiedArea(floatRendererAreas, this);
222-
}
223224

224225
AbstractRenderer splitRenderer = createSplitRenderer(LayoutResult.PARTIAL);
225226
splitRenderer.childRenderers = new ArrayList<>(childRenderers.subList(0, childPos));
@@ -254,10 +255,13 @@ public LayoutResult layout(LayoutContext layoutContext) {
254255
applyPaddings(occupiedArea.getBBox(), paddings, true);
255256
applyBorderBox(occupiedArea.getBBox(), borders, true);
256257
applyMargins(occupiedArea.getBBox(), true);
258+
259+
LayoutArea editedArea = FloatingHelper.adjustResultOccupiedAreaForFloatAndClear(this, layoutContext.getFloatRendererAreas(), layoutContext.getArea().getBBox(), clearHeightCorrection, marginsCollapsingEnabled);
260+
257261
if (wasHeightClipped) {
258-
return new LayoutResult(LayoutResult.FULL, occupiedArea, splitRenderer, null);
262+
return new LayoutResult(LayoutResult.FULL, editedArea, splitRenderer, null);
259263
} else {
260-
return new LayoutResult(LayoutResult.PARTIAL, occupiedArea, splitRenderer, overflowRenderer, causeOfNothing);
264+
return new LayoutResult(LayoutResult.PARTIAL, editedArea, splitRenderer, overflowRenderer, causeOfNothing);
261265
}
262266
} else {
263267
childRenderers.set(childPos, result.getSplitRenderer());
@@ -320,11 +324,13 @@ public LayoutResult layout(LayoutContext layoutContext) {
320324
applyMargins(occupiedArea.getBBox(), true);
321325
//splitRenderer.occupiedArea = occupiedArea.clone();
322326

327+
LayoutArea editedArea = FloatingHelper.adjustResultOccupiedAreaForFloatAndClear(this, layoutContext.getFloatRendererAreas(), layoutContext.getArea().getBBox(), clearHeightCorrection, marginsCollapsingEnabled);
328+
323329
if (Boolean.TRUE.equals(getPropertyAsBoolean(Property.FORCED_PLACEMENT)) || wasHeightClipped) {
324-
return new LayoutResult(LayoutResult.FULL, occupiedArea, splitRenderer, null, null);
330+
return new LayoutResult(LayoutResult.FULL, editedArea, splitRenderer, null, null);
325331
} else {
326332
if (layoutResult != LayoutResult.NOTHING) {
327-
return new LayoutResult(layoutResult, occupiedArea, splitRenderer, overflowRenderer, null).setAreaBreak(result.getAreaBreak());
333+
return new LayoutResult(layoutResult, editedArea, splitRenderer, overflowRenderer, null).setAreaBreak(result.getAreaBreak());
328334
} else {
329335
return new LayoutResult(layoutResult, null, null, overflowRenderer, result.getCauseOfNothing()).setAreaBreak(result.getAreaBreak());
330336
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,15 @@ public LayoutResult layout(LayoutContext layoutContext) {
301301
applyBorderBox(occupiedArea.getBBox(), borders, true);
302302
applyMargins(occupiedArea.getBBox(), true);
303303

304+
LayoutArea editedArea = FloatingHelper.adjustResultOccupiedAreaForFloatAndClear(this, layoutContext.getFloatRendererAreas(), layoutContext.getArea().getBBox(), clearHeightCorrection, marginsCollapsingEnabled);
304305
if (wasHeightClipped) {
305-
return new MinMaxWidthLayoutResult(LayoutResult.FULL, occupiedArea, split[0], null).setMinMaxWidth(minMaxWidth);
306+
return new MinMaxWidthLayoutResult(LayoutResult.FULL, editedArea, split[0], null).setMinMaxWidth(minMaxWidth);
306307
} else if (anythingPlaced) {
307-
return new MinMaxWidthLayoutResult(LayoutResult.PARTIAL, occupiedArea, split[0], split[1]).setMinMaxWidth(minMaxWidth);
308+
return new MinMaxWidthLayoutResult(LayoutResult.PARTIAL, editedArea, split[0], split[1]).setMinMaxWidth(minMaxWidth);
308309
} else {
309310
if (Boolean.TRUE.equals(getPropertyAsBoolean(Property.FORCED_PLACEMENT))) {
310311
occupiedArea.setBBox(Rectangle.getCommonRectangle(occupiedArea.getBBox(), currentRenderer.getOccupiedArea().getBBox()));
312+
editedArea.setBBox(Rectangle.getCommonRectangle(editedArea.getBBox(), occupiedArea.getBBox()));
311313
parent.setProperty(Property.FULL, true);
312314
lines.add(currentRenderer);
313315
// Force placement of children we have and do not force placement of the others
@@ -316,9 +318,9 @@ public LayoutResult layout(LayoutContext layoutContext) {
316318
int firstNotRendered = currentRenderer.childRenderers.indexOf(childNotRendered);
317319
currentRenderer.childRenderers.retainAll(currentRenderer.childRenderers.subList(0, firstNotRendered));
318320
split[1].childRenderers.removeAll(split[1].childRenderers.subList(0, firstNotRendered));
319-
return new MinMaxWidthLayoutResult(LayoutResult.PARTIAL, occupiedArea, this, split[1], null).setMinMaxWidth(minMaxWidth);
321+
return new MinMaxWidthLayoutResult(LayoutResult.PARTIAL, editedArea, this, split[1], null).setMinMaxWidth(minMaxWidth);
320322
} else {
321-
return new MinMaxWidthLayoutResult(LayoutResult.FULL, occupiedArea, null, null, this).setMinMaxWidth(minMaxWidth);
323+
return new MinMaxWidthLayoutResult(LayoutResult.FULL, editedArea, null, null, this).setMinMaxWidth(minMaxWidth);
322324
}
323325
} else {
324326
return new MinMaxWidthLayoutResult(LayoutResult.NOTHING, null, null, this, null == result.getCauseOfNothing() ? this : result.getCauseOfNothing());

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,8 @@ public LayoutResult layout(LayoutContext layoutContext) {
765765
}
766766
applyFixedXOrYPosition(false, layoutBox);
767767
applyMargins(occupiedArea.getBBox(), true);
768-
return new LayoutResult(LayoutResult.FULL, occupiedArea, splitResult[0], null);
768+
LayoutArea editedArea = FloatingHelper.adjustResultOccupiedAreaForFloatAndClear(this, siblingFloatRendererAreas, layoutContext.getArea().getBBox(), clearHeightCorrection, marginsCollapsingEnabled);
769+
return new LayoutResult(LayoutResult.FULL, editedArea, splitResult[0], null);
769770
} else {
770771
applyFixedXOrYPosition(false, layoutBox);
771772
applyMargins(occupiedArea.getBBox(), true);
@@ -778,7 +779,12 @@ public LayoutResult layout(LayoutContext layoutContext) {
778779
if (hasProperty(Property.MAX_HEIGHT)) {
779780
splitResult[1].setProperty(Property.MAX_HEIGHT, retrieveMaxHeight() - occupiedArea.getBBox().getHeight());
780781
}
781-
return new LayoutResult(status, status != LayoutResult.NOTHING ? occupiedArea : null, splitResult[0], splitResult[1], null == firstCauseOfNothing ? this : firstCauseOfNothing);
782+
783+
LayoutArea editedArea = null;
784+
if (status != LayoutResult.NOTHING) {
785+
editedArea = FloatingHelper.adjustResultOccupiedAreaForFloatAndClear(this, siblingFloatRendererAreas, layoutContext.getArea().getBBox(), clearHeightCorrection, marginsCollapsingEnabled);
786+
}
787+
return new LayoutResult(status, editedArea, splitResult[0], splitResult[1], null == firstCauseOfNothing ? this : firstCauseOfNothing);
782788
}
783789
}
784790
}

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

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ This file is part of the iText (R) project.
4343
package com.itextpdf.layout;
4444

4545

46+
import com.itextpdf.io.LogMessageConstant;
4647
import com.itextpdf.io.image.ImageDataFactory;
4748
import com.itextpdf.kernel.color.Color;
4849
import com.itextpdf.kernel.geom.Rectangle;
@@ -62,6 +63,8 @@ This file is part of the iText (R) project.
6263
import com.itextpdf.layout.property.Property;
6364
import com.itextpdf.layout.property.UnitValue;
6465
import com.itextpdf.test.ExtendedITextTest;
66+
import com.itextpdf.test.annotations.LogMessage;
67+
import com.itextpdf.test.annotations.LogMessages;
6568
import com.itextpdf.test.annotations.type.IntegrationTest;
6669
import org.junit.Assert;
6770
import org.junit.BeforeClass;
@@ -434,7 +437,7 @@ public void floatingImageInParagraph() throws IOException, InterruptedException
434437

435438
document.close();
436439

437-
Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFileName, destinationFolder, "diff10_"));
440+
Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFileName, destinationFolder, "diff11_"));
438441
}
439442

440443
@Test
@@ -472,7 +475,66 @@ public void floatsOnCanvas() throws IOException, InterruptedException {
472475
canvas.close();
473476
pdfDoc.close();
474477

475-
Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFileName, destinationFolder, "diff10_"));
478+
Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFileName, destinationFolder, "diff12_"));
479+
}
480+
481+
@Test
482+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.CLIP_ELEMENT, count = 3))
483+
public void floatFixedHeightContentNotFit() throws IOException, InterruptedException {
484+
String cmpFileName = sourceFolder + "cmp_floatFixedHeightContentNotFit.pdf";
485+
String outFile = destinationFolder + "floatFixedHeightContentNotFit.pdf";
486+
487+
Document document = new Document(new PdfDocument(new PdfWriter(outFile)));
488+
489+
Div div = new Div().setBorder(new SolidBorder(Color.RED, 2));
490+
div.add(new Paragraph("Floating div.")).add(new Paragraph(text));
491+
div.setHeight(200).setWidth(100);
492+
div.setProperty(Property.FLOAT, FloatPropertyValue.RIGHT);
493+
document.add(div);
494+
document.add(new Paragraph(text));
495+
496+
Paragraph p = new Paragraph("Floating p.\n" + text).setBorder(new SolidBorder(Color.RED, 2));
497+
p.setHeight(200).setWidth(100);
498+
p.setProperty(Property.FLOAT, FloatPropertyValue.RIGHT);
499+
document.add(p);
500+
document.add(new Paragraph(text));
501+
502+
Table table = new Table(UnitValue.createPercentArray(new float[]{0.3f, 0.7f})).setBorder(new SolidBorder(Color.RED, 2));
503+
table.addCell(new Paragraph("Floating table.")).addCell(new Paragraph(text));
504+
table.setHeight(200).setWidth(300);
505+
table.setProperty(Property.FLOAT, FloatPropertyValue.RIGHT);
506+
document.add(table);
507+
document.add(new Paragraph(text));
508+
509+
document.close();
510+
511+
Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFileName, destinationFolder, "diff13_"));
512+
}
513+
514+
@Test
515+
public void clearanceApplyingPageSplit() throws IOException, InterruptedException {
516+
String cmpFileName = sourceFolder + "cmp_clearanceApplyingPageSplit.pdf";
517+
String outFile = destinationFolder + "clearanceApplyingPageSplit.pdf";
518+
519+
Document document = new Document(new PdfDocument(new PdfWriter(outFile)));
520+
521+
document.add(new Paragraph(text));
522+
523+
Div div = new Div().setBorder(new SolidBorder(Color.RED, 2));
524+
div.add(new Paragraph("Floating div."));
525+
div.setHeight(200).setWidth(100);
526+
div.setProperty(Property.FLOAT, FloatPropertyValue.RIGHT);
527+
document.add(div);
528+
529+
Div divClear = new Div().setBackgroundColor(Color.GREEN);
530+
divClear.add(new Paragraph("Cleared div.")).add(new Paragraph(text));
531+
divClear.setHeight(400);
532+
divClear.setProperty(Property.CLEAR, ClearPropertyValue.BOTH);
533+
document.add(divClear);
534+
535+
document.close();
536+
537+
Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFileName, destinationFolder, "diff13_"));
476538
}
477539

478540
}

0 commit comments

Comments
 (0)