Skip to content

Commit 6328d38

Browse files
committed
Skip KEEP_TOGETHER in case of AreaBreak
DEVSIX-5156
1 parent 504a9a8 commit 6328d38

File tree

6 files changed

+37
-7
lines changed

6 files changed

+37
-7
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2104,7 +2104,12 @@ protected boolean isAbsolutePosition() {
21042104
}
21052105

21062106
protected boolean isKeepTogether() {
2107-
return Boolean.TRUE.equals(getPropertyAsBoolean(Property.KEEP_TOGETHER));
2107+
return isKeepTogether(null);
2108+
}
2109+
2110+
boolean isKeepTogether(IRenderer causeOfNothing) {
2111+
return Boolean.TRUE.equals(getPropertyAsBoolean(Property.KEEP_TOGETHER))
2112+
&& !(causeOfNothing instanceof AreaBreakRenderer);
21082113
}
21092114

21102115
// Note! The second parameter is here on purpose. Currently occupied area is passed as a value of this parameter in

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public LayoutResult layout(LayoutContext layoutContext) {
196196
fixOccupiedAreaIfOverflowedX(overflowX, layoutBox);
197197

198198
result = new LayoutResult(LayoutResult.NOTHING, null, null, childRenderer);
199-
boolean isKeepTogether = isKeepTogether();
199+
boolean isKeepTogether = isKeepTogether(childRenderer);
200200
int layoutResult = anythingPlaced && !isKeepTogether ? LayoutResult.PARTIAL : LayoutResult.NOTHING;
201201
AbstractRenderer[] splitAndOverflowRenderers = createSplitAndOverflowRenderers(childPos, layoutResult, result, waitingFloatsSplitRenderers, waitingOverflowFloatRenderers);
202202

@@ -798,7 +798,7 @@ LayoutResult processNotFullChildResult(LayoutContext layoutContext,
798798
return null;
799799
}
800800
} else if (result.getStatus() == LayoutResult.NOTHING) {
801-
boolean keepTogether = isKeepTogether();
801+
boolean keepTogether = isKeepTogether(causeOfNothing);
802802
int layoutResult = anythingPlaced && !keepTogether ? LayoutResult.PARTIAL : LayoutResult.NOTHING;
803803

804804
AbstractRenderer[] splitAndOverflowRenderers = createSplitAndOverflowRenderers(childPos, layoutResult,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ LayoutResult processNotFullChildResult(LayoutContext layoutContext,
203203
Set<Rectangle> nonChildFloatingRendererAreas, IRenderer causeOfNothing,
204204
boolean anythingPlaced, int childPos, LayoutResult result) {
205205

206-
final boolean keepTogether = isKeepTogether();
206+
final boolean keepTogether = isKeepTogether(causeOfNothing);
207207

208208
final AbstractRenderer[] splitAndOverflowRenderers = createSplitAndOverflowRenderers(
209209
childPos, result.getStatus(), result, waitingFloatsSplitRenderers, waitingOverflowFloatRenderers);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ protected LayoutResult directLayout(LayoutContext layoutContext) {
311311
lastYLine = layoutBox.getY() + layoutBox.getHeight();
312312
firstLineInBox = true;
313313
} else {
314-
boolean keepTogether = isKeepTogether();
314+
boolean keepTogether = isKeepTogether(result.getCauseOfNothing());
315315
if (keepTogether) {
316316
floatRendererAreas.retainAll(nonChildFloatingRendererAreas);
317317
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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,8 +882,12 @@ public LayoutResult layout(LayoutContext layoutContext) {
882882
}
883883
}
884884

885-
if ((isKeepTogether() && 0 == lastFlushedRowBottomBorder.size()) && !Boolean.TRUE.equals(getPropertyAsBoolean(Property.FORCED_PLACEMENT))) {
886-
return new LayoutResult(LayoutResult.NOTHING, null, null, this, null == firstCauseOfNothing ? this : firstCauseOfNothing);
885+
if (isKeepTogether(firstCauseOfNothing)
886+
&& 0 == lastFlushedRowBottomBorder.size()
887+
&& !Boolean.TRUE.equals(getPropertyAsBoolean(Property.FORCED_PLACEMENT))) {
888+
return new LayoutResult(LayoutResult.NOTHING, null, null, this, null == firstCauseOfNothing
889+
? this
890+
: firstCauseOfNothing);
887891
} else {
888892
int status = ((occupiedArea.getBBox().getHeight()
889893
- (null == footerRenderer ? 0 : footerRenderer.getOccupiedArea().getBBox().getHeight())

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ This file is part of the iText (R) project.
7373
import com.itextpdf.test.annotations.LogMessage;
7474
import com.itextpdf.test.annotations.LogMessages;
7575
import com.itextpdf.test.annotations.type.IntegrationTest;
76+
77+
import java.io.ByteArrayOutputStream;
7678
import org.junit.Assert;
7779
import org.junit.BeforeClass;
7880
import org.junit.Ignore;
@@ -131,6 +133,25 @@ public void keepTogetherParagraphTest01() throws IOException, InterruptedExcepti
131133
Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFileName, destinationFolder, "diff"));
132134
}
133135

136+
@Test
137+
public void skipKeepTogetherInCaseOfAreaBreak() throws IOException, InterruptedException {
138+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
139+
Document doc = new Document(pdfDoc);
140+
141+
Div keptTogetherDiv = new Div();
142+
keptTogetherDiv.setKeepTogether(true);
143+
144+
AreaBreak areaBreak = new AreaBreak();
145+
keptTogetherDiv.add(areaBreak);
146+
147+
doc.add(keptTogetherDiv);
148+
149+
// If this line is not triggered, then an NPE occurred
150+
Assert.assertTrue(true);
151+
152+
doc.close();
153+
}
154+
134155
@Test
135156
@LogMessages(messages = {
136157
@LogMessage(messageTemplate = LogMessageConstant.ELEMENT_DOES_NOT_FIT_AREA)

0 commit comments

Comments
 (0)