Skip to content

Commit 8e30869

Browse files
committed
Add PdfDocument as an argument for build linear gradient
DEVSIX-2086
1 parent f7a0ca8 commit 8e30869

File tree

5 files changed

+17
-14
lines changed

5 files changed

+17
-14
lines changed

kernel/src/main/java/com/itextpdf/kernel/colors/gradients/AbstractLinearGradientBuilder.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ This file is part of the iText (R) project.
2323
package com.itextpdf.kernel.colors.gradients;
2424

2525
import com.itextpdf.io.LogMessageConstant;
26+
import com.itextpdf.kernel.PdfException;
2627
import com.itextpdf.kernel.colors.Color;
27-
import com.itextpdf.kernel.colors.DeviceRgb;
2828
import com.itextpdf.kernel.colors.PatternColor;
2929
import com.itextpdf.kernel.geom.AffineTransform;
3030
import com.itextpdf.kernel.geom.NoninvertibleTransformException;
3131
import com.itextpdf.kernel.geom.Point;
3232
import com.itextpdf.kernel.geom.Rectangle;
3333
import com.itextpdf.kernel.pdf.PdfArray;
34+
import com.itextpdf.kernel.pdf.PdfDocument;
3435
import com.itextpdf.kernel.pdf.PdfNumber;
3536
import com.itextpdf.kernel.pdf.colorspace.PdfDeviceCs;
3637
import com.itextpdf.kernel.pdf.colorspace.PdfPattern;
@@ -123,10 +124,12 @@ public GradientSpreadMethod getSpreadMethod() {
123124
* the current space. The {@code null} value is valid and can be used
124125
* if there is no transformation from base coordinates to current space
125126
* specified, or it is equal to identity transformation.
127+
* @param document the {@link PdfDocument} for which the linear gradient would be built.
126128
* @return the constructed {@link Color} or {@code null} if no color to be applied
127129
* or base gradient vector has been specified
128130
*/
129-
public Color buildColor(Rectangle targetBoundingBox, AffineTransform contextTransform) {
131+
// TODO: DEVSIX-4136 the document argument would be required for opaque gradients (as we would need to create a mask form xObject)
132+
public Color buildColor(Rectangle targetBoundingBox, AffineTransform contextTransform, PdfDocument document) {
130133
Point[] baseCoordinatesVector = getGradientVector(targetBoundingBox, contextTransform);
131134
if (baseCoordinatesVector == null || this.stops.isEmpty()) {
132135
// Can not create gradient color with 0 stops or null coordinates vector

kernel/src/test/java/com/itextpdf/kernel/colors/gradients/LinearGradientBuilderTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void buildWithNullArgumentsAndWithoutSettersTest() {
5757
Rectangle targetBoundingBox = new Rectangle(50f, 450f, 300f, 300f);
5858
AbstractLinearGradientBuilder gradientBuilder = new LinearGradientBuilder();
5959

60-
Assert.assertNull(gradientBuilder.buildColor(targetBoundingBox, null));
60+
Assert.assertNull(gradientBuilder.buildColor(targetBoundingBox, null, null));
6161
}
6262

6363
@Test
@@ -235,7 +235,7 @@ public void buildWithNullArgumentsAndWithoutStopsTest() {
235235
targetBoundingBox.getRight() - 100f, targetBoundingBox.getTop() - 100f)
236236
.setSpreadMethod(GradientSpreadMethod.PAD);
237237

238-
Assert.assertNull(gradientBuilder.buildColor(null, null));
238+
Assert.assertNull(gradientBuilder.buildColor(null, null, null));
239239
}
240240

241241
@Test
@@ -705,7 +705,7 @@ public void buildWithTwoStopsBeforeTheBeginningAndNoneTest() throws IOException,
705705
.addColorStop(new GradientColorStop(ColorConstants.RED.getColorValue(), -10d, OffsetType.RELATIVE))
706706
.addColorStop(new GradientColorStop(ColorConstants.BLUE.getColorValue(), -5d, OffsetType.RELATIVE));
707707

708-
Assert.assertNull(gradientBuilder.buildColor(targetBoundingBox, null));
708+
Assert.assertNull(gradientBuilder.buildColor(targetBoundingBox, null, null));
709709
}
710710

711711
@Test
@@ -718,7 +718,7 @@ public void buildWithTwoStopsAfterEndAndNoneTest() throws IOException, Interrupt
718718
.addColorStop(new GradientColorStop(ColorConstants.RED.getColorValue(), 5d, OffsetType.RELATIVE))
719719
.addColorStop(new GradientColorStop(ColorConstants.BLUE.getColorValue(), 10d, OffsetType.RELATIVE));
720720

721-
Assert.assertNull(gradientBuilder.buildColor(targetBoundingBox, null));
721+
Assert.assertNull(gradientBuilder.buildColor(targetBoundingBox, null, null));
722722
}
723723

724724
@Test
@@ -731,7 +731,7 @@ public void buildWithTwoEqualOffsetsStopsAndNoneTest() throws IOException, Inter
731731
.addColorStop(new GradientColorStop(ColorConstants.RED.getColorValue(), 0.5d, OffsetType.RELATIVE))
732732
.addColorStop(new GradientColorStop(ColorConstants.BLUE.getColorValue(), 0.5d, OffsetType.RELATIVE));
733733

734-
Assert.assertNull(gradientBuilder.buildColor(targetBoundingBox, null));
734+
Assert.assertNull(gradientBuilder.buildColor(targetBoundingBox, null, null));
735735
}
736736

737737
@Test
@@ -770,7 +770,7 @@ private void generateAndComparePdfs(String fileName, Rectangle toDraw, AffineTra
770770
canvas.concatMatrix(transform);
771771
}
772772

773-
canvas.setFillColor(gradientBuilder.buildColor(toDraw, transform))
773+
canvas.setFillColor(gradientBuilder.buildColor(toDraw, transform, pdfDoc))
774774
.setStrokeColor(ColorConstants.BLACK)
775775
.rectangle(toDraw)
776776
.fillStroke();
@@ -786,7 +786,7 @@ private void generateAndComparePdfsWithoutArgumentToBuild(String fileName, Recta
786786
try (PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new File(outPdfPath)))) {
787787
PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
788788

789-
canvas.setFillColor(gradientBuilder.buildColor(null, null))
789+
canvas.setFillColor(gradientBuilder.buildColor(null, null, pdfDoc))
790790
.setStrokeColor(ColorConstants.BLACK)
791791
.rectangle(toDraw)
792792
.fillStroke();

kernel/src/test/java/com/itextpdf/kernel/colors/gradients/StrategyBasedLinearGradientBuilderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void beforeClass() {
5959
@Test
6060
public void noSettersTest() {
6161
Assert.assertNull(new StrategyBasedLinearGradientBuilder()
62-
.buildColor(new Rectangle(50f, 450f, 500f, 300f), null));
62+
.buildColor(new Rectangle(50f, 450f, 500f, 300f), null, null));
6363
}
6464

6565
@Test
@@ -68,7 +68,7 @@ public void noRectangleTest() {
6868
.addColorStop(new GradientColorStop(ColorConstants.RED.getColorValue(), 0d, OffsetType.RELATIVE))
6969
.addColorStop(new GradientColorStop(ColorConstants.GREEN.getColorValue(), 0.5, OffsetType.RELATIVE))
7070
.addColorStop(new GradientColorStop(ColorConstants.BLUE.getColorValue(), 1d, OffsetType.RELATIVE))
71-
.buildColor(null, null));
71+
.buildColor(null, null, null));
7272
}
7373

7474
@Test
@@ -276,7 +276,7 @@ private void generateAndComparePdfs(String fileName, AffineTransform transform,
276276
}
277277

278278
Rectangle toDraw = new Rectangle(50f, 450f, 500f, 300f);
279-
canvas.setFillColor(gradientBuilder.buildColor(toDraw, transform))
279+
canvas.setFillColor(gradientBuilder.buildColor(toDraw, transform, pdfDoc))
280280
.setStrokeColor(ColorConstants.BLACK)
281281
.rectangle(toDraw)
282282
.fillStroke();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ public static PdfFormXObject createXObject(AbstractLinearGradientBuilder linearG
588588
Rectangle formBBox = new Rectangle(0, 0, xObjectArea.getWidth(), xObjectArea.getHeight());
589589
PdfFormXObject xObject = new PdfFormXObject(formBBox);
590590
if (linearGradientBuilder != null) {
591-
Color gradientColor = linearGradientBuilder.buildColor(formBBox, null);
591+
Color gradientColor = linearGradientBuilder.buildColor(formBBox, null, document);
592592
if (gradientColor != null) {
593593
new PdfCanvas(xObject, document)
594594
.setColor(gradientColor, true)

svg/src/main/java/com/itextpdf/svg/renderers/impl/LinearGradientSvgNodeRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public Color createColor(SvgDrawContext context, Rectangle objectBoundingBox, fl
7171

7272
return builder.buildColor(
7373
objectBoundingBox.applyMargins(objectBoundingBoxMargin, objectBoundingBoxMargin, objectBoundingBoxMargin, objectBoundingBoxMargin, true),
74-
context.getCurrentCanvasTransform()
74+
context.getCurrentCanvasTransform(), context.getCurrentCanvas().getDocument()
7575
);
7676
}
7777

0 commit comments

Comments
 (0)