Skip to content

Commit 3e17cce

Browse files
Alexander KozloviText-CI
authored andcommitted
Add safe-guard check if occupiedArea is initialized in renderers #move method
DEVSIX-6760 Autoported commit. Original commit hash: [ee879e0bf]
1 parent 2221f5c commit 3e17cce

File tree

6 files changed

+88
-1
lines changed

6 files changed

+88
-1
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using iText.Kernel.Colors;
3+
using iText.Kernel.Pdf;
4+
using iText.Kernel.Utils;
5+
using iText.Layout.Borders;
6+
using iText.Layout.Element;
7+
using iText.Test;
8+
using iText.Test.Attributes;
9+
10+
namespace iText.Layout {
11+
public class FixedHeightTest : ExtendedITextTest {
12+
private static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
13+
.CurrentContext.TestDirectory) + "/resources/itext/layout/FloatAndAlignmentTest/";
14+
15+
private static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
16+
+ "/test/itext/layout/FloatAndAlignmentTest/";
17+
18+
private const String textByron = "When a man hath no freedom to fight for at home,\n" + " Let him combat for that of his neighbours;\n"
19+
+ "Let him think of the glories of Greece and of Rome,\n" + " And get knocked on the head for his labours.\n"
20+
+ "\n" + "To do good to Mankind is the chivalrous plan,\n" + " And is always as nobly requited;\n"
21+
+ "Then battle for Freedom wherever you can,\n" + " And, if not shot or hanged, you'll get knighted.";
22+
23+
[NUnit.Framework.OneTimeSetUp]
24+
public static void BeforeClass() {
25+
CreateOrClearDestinationFolder(destinationFolder);
26+
}
27+
28+
[LogMessage(iText.IO.Logs.IoLogMessageConstant.CLIP_ELEMENT, Count = 1)]
29+
// TODO DEVSIX-1977 partial layout result due to fixed height should not contain not layouted kids
30+
[LogMessage(iText.IO.Logs.IoLogMessageConstant.OCCUPIED_AREA_HAS_NOT_BEEN_INITIALIZED, Count = 6)]
31+
[NUnit.Framework.Test]
32+
public virtual void DivWithParagraphsAndFixedPositionTest() {
33+
String outFileName = destinationFolder + "blockWithLimitedHeightAndFixedPositionTest.pdf";
34+
String cmpFileName = sourceFolder + "cmp_blockWithLimitedHeightAndFixedPositionTest.pdf";
35+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
36+
Document doc = new Document(pdfDocument);
37+
Div block = new Div();
38+
block.SetBorder(new SolidBorder(ColorConstants.BLUE, 1));
39+
block.SetHeight(120);
40+
foreach (String line in iText.Commons.Utils.StringUtil.Split(textByron, "\n")) {
41+
Paragraph p = new Paragraph();
42+
p.Add(new Text(line));
43+
p.SetBorder(new SolidBorder(0.5f));
44+
block.Add(p);
45+
}
46+
block.SetFixedPosition(100, 600, 300);
47+
doc.Add(block);
48+
doc.Close();
49+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
50+
));
51+
}
52+
53+
[LogMessage(iText.IO.Logs.IoLogMessageConstant.CLIP_ELEMENT, Count = 1)]
54+
// TODO DEVSIX-1977 partial layout result due to fixed height should not contain not layouted kids
55+
[LogMessage(iText.IO.Logs.IoLogMessageConstant.OCCUPIED_AREA_HAS_NOT_BEEN_INITIALIZED, Count = 2)]
56+
[NUnit.Framework.Test]
57+
public virtual void ListWithFixedPositionTest() {
58+
String outFileName = destinationFolder + "listWithFixedPositionTest.pdf";
59+
String cmpFileName = sourceFolder + "cmp_listWithFixedPositionTest.pdf";
60+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
61+
Document doc = new Document(pdfDocument);
62+
List list = new List();
63+
list.SetBorder(new SolidBorder(ColorConstants.BLUE, 1));
64+
list.SetHeight(120);
65+
foreach (String line in iText.Commons.Utils.StringUtil.Split(textByron, "\n")) {
66+
list.Add(line);
67+
}
68+
list.SetFixedPosition(100, 600, 300);
69+
doc.Add(list);
70+
doc.Close();
71+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
72+
));
73+
}
74+
}
75+
}

itext/itext.layout/itext/layout/renderer/AbstractRenderer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,12 @@ public virtual IRenderer GetParent() {
10681068

10691069
/// <summary><inheritDoc/></summary>
10701070
public virtual void Move(float dxRight, float dyUp) {
1071+
ILogger logger = ITextLogManager.GetLogger(typeof(iText.Layout.Renderer.AbstractRenderer));
1072+
if (occupiedArea == null) {
1073+
logger.LogError(MessageFormatUtil.Format(iText.IO.Logs.IoLogMessageConstant.OCCUPIED_AREA_HAS_NOT_BEEN_INITIALIZED
1074+
, "Moving won't be performed."));
1075+
return;
1076+
}
10711077
occupiedArea.GetBBox().MoveRight(dxRight);
10721078
occupiedArea.GetBBox().MoveUp(dyUp);
10731079
foreach (IRenderer childRenderer in childRenderers) {

itext/itext.layout/itext/layout/renderer/ParagraphRenderer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,12 @@ public override void DrawChildren(DrawContext drawContext) {
532532

533533
/// <summary><inheritDoc/></summary>
534534
public override void Move(float dxRight, float dyUp) {
535+
ILogger logger = ITextLogManager.GetLogger(typeof(iText.Layout.Renderer.ParagraphRenderer));
536+
if (occupiedArea == null) {
537+
logger.LogError(MessageFormatUtil.Format(iText.IO.Logs.IoLogMessageConstant.OCCUPIED_AREA_HAS_NOT_BEEN_INITIALIZED
538+
, "Moving won't be performed."));
539+
return;
540+
}
535541
occupiedArea.GetBBox().MoveRight(dxRight);
536542
occupiedArea.GetBBox().MoveUp(dyUp);
537543
if (null != lines) {

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9f7192e712c44cdf2ec82ec88dc5c58da5942036
1+
ee879e0bf2b0ca201f39060bba59ce79e52ec62f

0 commit comments

Comments
 (0)