Skip to content

Commit 367b900

Browse files
introfogiText-CI
authored andcommitted
Fix NullPointerException in AbstractRenderer class and update createXObject method
DEVSIX-4138 Autoported commit. Original commit hash: [34efacbce]
1 parent efca6e8 commit 367b900

File tree

3 files changed

+59
-13
lines changed

3 files changed

+59
-13
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using iText.IO.Source;
2+
using iText.Kernel.Colors;
3+
using iText.Kernel.Colors.Gradients;
4+
using iText.Kernel.Geom;
5+
using iText.Kernel.Pdf;
6+
using iText.Kernel.Pdf.Xobject;
7+
using iText.Test;
8+
9+
namespace iText.Layout.Renderer {
10+
public class AbstractRendererTest : ExtendedITextTest {
11+
[NUnit.Framework.Test]
12+
public virtual void CreateXObjectTest() {
13+
AbstractLinearGradientBuilder gradientBuilder = new StrategyBasedLinearGradientBuilder().SetGradientDirectionAsStrategy
14+
(StrategyBasedLinearGradientBuilder.GradientStrategy.TO_BOTTOM_LEFT).AddColorStop(new GradientColorStop
15+
(ColorConstants.RED.GetColorValue(), 0d, GradientColorStop.OffsetType.RELATIVE)).AddColorStop(new GradientColorStop
16+
(ColorConstants.GREEN.GetColorValue(), 0.5, GradientColorStop.OffsetType.RELATIVE));
17+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
18+
PdfXObject pdfXObject = AbstractRenderer.CreateXObject(gradientBuilder, new Rectangle(0, 0, 20, 20), pdfDocument
19+
);
20+
NUnit.Framework.Assert.IsNotNull(pdfXObject.GetPdfObject().Get(PdfName.Resources));
21+
}
22+
23+
[NUnit.Framework.Test]
24+
public virtual void CreateXObjectWithNullLinearGradientTest() {
25+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
26+
PdfXObject pdfXObject = AbstractRenderer.CreateXObject(null, new Rectangle(0, 0, 20, 20), pdfDocument);
27+
NUnit.Framework.Assert.IsNull(pdfXObject.GetPdfObject().Get(PdfName.Resources));
28+
}
29+
30+
[NUnit.Framework.Test]
31+
public virtual void CreateXObjectWithInvalidColorTest() {
32+
AbstractLinearGradientBuilder gradientBuilder = new StrategyBasedLinearGradientBuilder();
33+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
34+
PdfXObject pdfXObject = AbstractRenderer.CreateXObject(gradientBuilder, new Rectangle(0, 0, 20, 20), pdfDocument
35+
);
36+
NUnit.Framework.Assert.IsNull(pdfXObject.GetPdfObject().Get(PdfName.Resources));
37+
}
38+
}
39+
}

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,8 @@ public virtual void DrawBackground(DrawContext drawContext) {
504504
// for gradient uses width and height = 1. For all othe cases the logic left as it was.
505505
Rectangle imageRectangle;
506506
if (backgroundXObject == null) {
507-
backgroundXObject = CreateXObject(backgroundImage.GetLinearGradientBuilder(), backgroundArea, drawContext.
508-
GetDocument());
507+
backgroundXObject = iText.Layout.Renderer.AbstractRenderer.CreateXObject(backgroundImage.GetLinearGradientBuilder
508+
(), backgroundArea, drawContext.GetDocument());
509509
imageRectangle = new Rectangle(backgroundArea.GetX(), backgroundArea.GetTop() - backgroundXObject.GetHeight
510510
(), 1, 1);
511511
}
@@ -548,17 +548,24 @@ public virtual void DrawBackground(DrawContext drawContext) {
548548
}
549549
}
550550

551-
public virtual PdfFormXObject CreateXObject(AbstractLinearGradientBuilder linearGradientBuilder, Rectangle
552-
xObjectArea, PdfDocument document) {
553-
if (linearGradientBuilder == null) {
554-
return null;
555-
}
551+
/// <summary>
552+
/// Create a
553+
/// <see cref="iText.Kernel.Pdf.Xobject.PdfFormXObject"/>
554+
/// with the given area and containing a linear gradient inside.
555+
/// </summary>
556+
/// <param name="linearGradientBuilder">the linear gradient builder</param>
557+
/// <param name="xObjectArea">the result object area</param>
558+
/// <param name="document">the pdf document</param>
559+
/// <returns>the xObject with a specified area and a linear gradient</returns>
560+
public static PdfFormXObject CreateXObject(AbstractLinearGradientBuilder linearGradientBuilder, Rectangle
561+
xObjectArea, PdfDocument document) {
556562
Rectangle formBBox = new Rectangle(0, 0, xObjectArea.GetWidth(), xObjectArea.GetHeight());
557-
PdfFormXObject xObject = null;
558-
Color gradientColor = linearGradientBuilder.BuildColor(formBBox, null);
559-
if (gradientColor != null) {
560-
xObject = new PdfFormXObject(formBBox);
561-
new PdfCanvas(xObject, document).SetColor(gradientColor, true).Rectangle(formBBox).Fill();
563+
PdfFormXObject xObject = new PdfFormXObject(formBBox);
564+
if (linearGradientBuilder != null) {
565+
Color gradientColor = linearGradientBuilder.BuildColor(formBBox, null);
566+
if (gradientColor != null) {
567+
new PdfCanvas(xObject, document).SetColor(gradientColor, true).Rectangle(formBBox).Fill();
568+
}
562569
}
563570
return xObject;
564571
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fffd9ce335e05b267707f0ae3a5ce6093bf1d825
1+
34efacbce9d6048d19dd0efb6570d9a4ec9bc01f

0 commit comments

Comments
 (0)