Skip to content

Commit ee879e0

Browse files
author
Alexander Kozlov
committed
Add safe-guard check if occupiedArea is initialized in renderers #move method
DEVSIX-6760
1 parent 9f7192e commit ee879e0

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,12 @@ public IRenderer getParent() {
11821182
*/
11831183
@Override
11841184
public void move(float dxRight, float dyUp) {
1185+
Logger logger = LoggerFactory.getLogger(AbstractRenderer.class);
1186+
if (occupiedArea == null) {
1187+
logger.error(MessageFormatUtil.format(IoLogMessageConstant.OCCUPIED_AREA_HAS_NOT_BEEN_INITIALIZED,
1188+
"Moving won't be performed."));
1189+
return;
1190+
}
11851191
occupiedArea.getBBox().moveRight(dxRight);
11861192
occupiedArea.getBBox().moveUp(dyUp);
11871193
for (IRenderer childRenderer : childRenderers) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ This file is part of the iText (R) project.
4444
package com.itextpdf.layout.renderer;
4545

4646
import com.itextpdf.commons.utils.MessageFormatUtil;
47+
import com.itextpdf.io.logs.IoLogMessageConstant;
4748
import com.itextpdf.kernel.geom.Rectangle;
4849
import com.itextpdf.layout.borders.Border;
4950
import com.itextpdf.layout.element.Paragraph;
@@ -68,6 +69,7 @@ This file is part of the iText (R) project.
6869
import com.itextpdf.layout.properties.TextAlignment;
6970
import com.itextpdf.layout.properties.UnitValue;
7071

72+
import org.slf4j.Logger;
7173
import org.slf4j.LoggerFactory;
7274

7375
import java.util.ArrayList;
@@ -572,6 +574,12 @@ public void drawChildren(DrawContext drawContext) {
572574
*/
573575
@Override
574576
public void move(float dxRight, float dyUp) {
577+
Logger logger = LoggerFactory.getLogger(ParagraphRenderer.class);
578+
if (occupiedArea == null) {
579+
logger.error(MessageFormatUtil.format(IoLogMessageConstant.OCCUPIED_AREA_HAS_NOT_BEEN_INITIALIZED,
580+
"Moving won't be performed."));
581+
return;
582+
}
575583
occupiedArea.getBBox().moveRight(dxRight);
576584
occupiedArea.getBBox().moveUp(dyUp);
577585
if (null != lines) {
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.itextpdf.layout;
2+
3+
import com.itextpdf.io.logs.IoLogMessageConstant;
4+
import com.itextpdf.kernel.colors.ColorConstants;
5+
import com.itextpdf.kernel.pdf.PdfDocument;
6+
import com.itextpdf.kernel.pdf.PdfWriter;
7+
import com.itextpdf.kernel.utils.CompareTool;
8+
import com.itextpdf.layout.borders.SolidBorder;
9+
import com.itextpdf.layout.element.Div;
10+
import com.itextpdf.layout.element.List;
11+
import com.itextpdf.layout.element.Paragraph;
12+
import com.itextpdf.layout.element.Text;
13+
import com.itextpdf.test.ExtendedITextTest;
14+
import com.itextpdf.test.annotations.LogMessage;
15+
import com.itextpdf.test.annotations.LogMessages;
16+
import com.itextpdf.test.annotations.type.IntegrationTest;
17+
18+
import java.io.IOException;
19+
import org.junit.Assert;
20+
import org.junit.BeforeClass;
21+
import org.junit.Test;
22+
import org.junit.experimental.categories.Category;
23+
24+
@Category(IntegrationTest.class)
25+
public class FixedHeightTest extends ExtendedITextTest {
26+
27+
private static final String sourceFolder = "./src/test/resources/com/itextpdf/layout/FloatAndAlignmentTest/";
28+
private static final String destinationFolder = "./target/test/com/itextpdf/layout/FloatAndAlignmentTest/";
29+
30+
private static final String textByron =
31+
"When a man hath no freedom to fight for at home,\n" +
32+
" Let him combat for that of his neighbours;\n" +
33+
"Let him think of the glories of Greece and of Rome,\n" +
34+
" And get knocked on the head for his labours.\n" +
35+
"\n" +
36+
"To do good to Mankind is the chivalrous plan,\n" +
37+
" And is always as nobly requited;\n" +
38+
"Then battle for Freedom wherever you can,\n" +
39+
" And, if not shot or hanged, you'll get knighted.";
40+
41+
@BeforeClass
42+
public static void beforeClass() {
43+
createOrClearDestinationFolder(destinationFolder);
44+
}
45+
46+
@LogMessages(messages = {
47+
@LogMessage(messageTemplate = IoLogMessageConstant.CLIP_ELEMENT, count = 1),
48+
// TODO DEVSIX-1977 partial layout result due to fixed height should not contain not layouted kids
49+
@LogMessage(messageTemplate = IoLogMessageConstant.OCCUPIED_AREA_HAS_NOT_BEEN_INITIALIZED, count = 6)
50+
})
51+
@Test
52+
public void divWithParagraphsAndFixedPositionTest() throws IOException, InterruptedException {
53+
String outFileName = destinationFolder + "blockWithLimitedHeightAndFixedPositionTest.pdf";
54+
String cmpFileName = sourceFolder + "cmp_blockWithLimitedHeightAndFixedPositionTest.pdf";
55+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
56+
57+
Document doc = new Document(pdfDocument);
58+
59+
Div block = new Div();
60+
block.setBorder(new SolidBorder(ColorConstants.BLUE, 1));
61+
block.setHeight(120);
62+
63+
for (String line : textByron.split("\n")) {
64+
Paragraph p = new Paragraph();
65+
p.add(new Text(line));
66+
p.setBorder(new SolidBorder(0.5f));
67+
block.add(p);
68+
}
69+
block.setFixedPosition(100, 600, 300);
70+
doc.add(block);
71+
72+
doc.close();
73+
74+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder));
75+
}
76+
77+
@LogMessages(messages = {
78+
@LogMessage(messageTemplate = IoLogMessageConstant.CLIP_ELEMENT, count = 1),
79+
// TODO DEVSIX-1977 partial layout result due to fixed height should not contain not layouted kids
80+
@LogMessage(messageTemplate = IoLogMessageConstant.OCCUPIED_AREA_HAS_NOT_BEEN_INITIALIZED, count = 2)
81+
})
82+
@Test
83+
public void listWithFixedPositionTest() throws IOException, InterruptedException {
84+
String outFileName = destinationFolder + "listWithFixedPositionTest.pdf";
85+
String cmpFileName = sourceFolder + "cmp_listWithFixedPositionTest.pdf";
86+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
87+
88+
Document doc = new Document(pdfDocument);
89+
90+
List list = new List();
91+
list.setBorder(new SolidBorder(ColorConstants.BLUE, 1));
92+
list.setHeight(120);
93+
94+
for (String line : textByron.split("\n")) {
95+
list.add(line);
96+
}
97+
list.setFixedPosition(100, 600, 300);
98+
doc.add(list);
99+
100+
doc.close();
101+
102+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder));
103+
}
104+
}

0 commit comments

Comments
 (0)