Skip to content

Commit 1a0cb5a

Browse files
BezrukovMiText-CI
authored andcommitted
Add PdfDocument as an argument for build linear gradient
DEVSIX-2086 Autoported commit. Original commit hash: [8e30869a8]
1 parent 5a36873 commit 1a0cb5a

File tree

6 files changed

+24
-16
lines changed

6 files changed

+24
-16
lines changed

itext.tests/itext.kernel.tests/itext/kernel/colors/gradients/LinearGradientBuilderTest.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static void BeforeClass() {
4646
public virtual void BuildWithNullArgumentsAndWithoutSettersTest() {
4747
Rectangle targetBoundingBox = new Rectangle(50f, 450f, 300f, 300f);
4848
AbstractLinearGradientBuilder gradientBuilder = new LinearGradientBuilder();
49-
NUnit.Framework.Assert.IsNull(gradientBuilder.BuildColor(targetBoundingBox, null));
49+
NUnit.Framework.Assert.IsNull(gradientBuilder.BuildColor(targetBoundingBox, null, null));
5050
}
5151

5252
[NUnit.Framework.Test]
@@ -206,7 +206,7 @@ public virtual void BuildWithNullArgumentsAndWithoutStopsTest() {
206206
AbstractLinearGradientBuilder gradientBuilder = new LinearGradientBuilder().SetGradientVector(targetBoundingBox
207207
.GetLeft() + 100f, targetBoundingBox.GetBottom() + 100f, targetBoundingBox.GetRight() - 100f, targetBoundingBox
208208
.GetTop() - 100f).SetSpreadMethod(GradientSpreadMethod.PAD);
209-
NUnit.Framework.Assert.IsNull(gradientBuilder.BuildColor(null, null));
209+
NUnit.Framework.Assert.IsNull(gradientBuilder.BuildColor(null, null, null));
210210
}
211211

212212
[NUnit.Framework.Test]
@@ -646,7 +646,7 @@ public virtual void BuildWithTwoStopsBeforeTheBeginningAndNoneTest() {
646646
.GetTop() - 100f).SetSpreadMethod(GradientSpreadMethod.NONE).AddColorStop(new GradientColorStop(ColorConstants
647647
.RED.GetColorValue(), -10d, GradientColorStop.OffsetType.RELATIVE)).AddColorStop(new GradientColorStop
648648
(ColorConstants.BLUE.GetColorValue(), -5d, GradientColorStop.OffsetType.RELATIVE));
649-
NUnit.Framework.Assert.IsNull(gradientBuilder.BuildColor(targetBoundingBox, null));
649+
NUnit.Framework.Assert.IsNull(gradientBuilder.BuildColor(targetBoundingBox, null, null));
650650
}
651651

652652
[NUnit.Framework.Test]
@@ -657,7 +657,7 @@ public virtual void BuildWithTwoStopsAfterEndAndNoneTest() {
657657
.GetTop() - 100f).SetSpreadMethod(GradientSpreadMethod.NONE).AddColorStop(new GradientColorStop(ColorConstants
658658
.RED.GetColorValue(), 5d, GradientColorStop.OffsetType.RELATIVE)).AddColorStop(new GradientColorStop(ColorConstants
659659
.BLUE.GetColorValue(), 10d, GradientColorStop.OffsetType.RELATIVE));
660-
NUnit.Framework.Assert.IsNull(gradientBuilder.BuildColor(targetBoundingBox, null));
660+
NUnit.Framework.Assert.IsNull(gradientBuilder.BuildColor(targetBoundingBox, null, null));
661661
}
662662

663663
[NUnit.Framework.Test]
@@ -668,7 +668,7 @@ public virtual void BuildWithTwoEqualOffsetsStopsAndNoneTest() {
668668
.GetTop() - 100f).SetSpreadMethod(GradientSpreadMethod.NONE).AddColorStop(new GradientColorStop(ColorConstants
669669
.RED.GetColorValue(), 0.5d, GradientColorStop.OffsetType.RELATIVE)).AddColorStop(new GradientColorStop
670670
(ColorConstants.BLUE.GetColorValue(), 0.5d, GradientColorStop.OffsetType.RELATIVE));
671-
NUnit.Framework.Assert.IsNull(gradientBuilder.BuildColor(targetBoundingBox, null));
671+
NUnit.Framework.Assert.IsNull(gradientBuilder.BuildColor(targetBoundingBox, null, null));
672672
}
673673

674674
[NUnit.Framework.Test]
@@ -702,8 +702,8 @@ private void GenerateAndComparePdfs(String fileName, Rectangle toDraw, AffineTra
702702
if (transform != null) {
703703
canvas.ConcatMatrix(transform);
704704
}
705-
canvas.SetFillColor(gradientBuilder.BuildColor(toDraw, transform)).SetStrokeColor(ColorConstants.BLACK).Rectangle
706-
(toDraw).FillStroke();
705+
canvas.SetFillColor(gradientBuilder.BuildColor(toDraw, transform, pdfDoc)).SetStrokeColor(ColorConstants.BLACK
706+
).Rectangle(toDraw).FillStroke();
707707
}
708708
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdfPath, sourceFolder + "cmp_" + fileName
709709
, destinationFolder, "diff"));
@@ -714,7 +714,7 @@ private void GenerateAndComparePdfsWithoutArgumentToBuild(String fileName, Recta
714714
String outPdfPath = destinationFolder + fileName;
715715
using (PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileInfo(outPdfPath)))) {
716716
PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
717-
canvas.SetFillColor(gradientBuilder.BuildColor(null, null)).SetStrokeColor(ColorConstants.BLACK).Rectangle
717+
canvas.SetFillColor(gradientBuilder.BuildColor(null, null, pdfDoc)).SetStrokeColor(ColorConstants.BLACK).Rectangle
718718
(toDraw).FillStroke();
719719
}
720720
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdfPath, sourceFolder + "cmp_" + fileName

itext.tests/itext.kernel.tests/itext/kernel/colors/gradients/StrategyBasedLinearGradientBuilderTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static void BeforeClass() {
5151
[NUnit.Framework.Test]
5252
public virtual void NoSettersTest() {
5353
NUnit.Framework.Assert.IsNull(new StrategyBasedLinearGradientBuilder().BuildColor(new Rectangle(50f, 450f,
54-
500f, 300f), null));
54+
500f, 300f), null, null));
5555
}
5656

5757
[NUnit.Framework.Test]
@@ -60,7 +60,7 @@ public virtual void NoRectangleTest() {
6060
ColorConstants.RED.GetColorValue(), 0d, GradientColorStop.OffsetType.RELATIVE)).AddColorStop(new GradientColorStop
6161
(ColorConstants.GREEN.GetColorValue(), 0.5, GradientColorStop.OffsetType.RELATIVE)).AddColorStop(new GradientColorStop
6262
(ColorConstants.BLUE.GetColorValue(), 1d, GradientColorStop.OffsetType.RELATIVE)).BuildColor(null, null
63-
));
63+
, null));
6464
}
6565

6666
[NUnit.Framework.Test]
@@ -253,8 +253,8 @@ private void GenerateAndComparePdfs(String fileName, AffineTransform transform,
253253
canvas.ConcatMatrix(transform);
254254
}
255255
Rectangle toDraw = new Rectangle(50f, 450f, 500f, 300f);
256-
canvas.SetFillColor(gradientBuilder.BuildColor(toDraw, transform)).SetStrokeColor(ColorConstants.BLACK).Rectangle
257-
(toDraw).FillStroke();
256+
canvas.SetFillColor(gradientBuilder.BuildColor(toDraw, transform, pdfDoc)).SetStrokeColor(ColorConstants.BLACK
257+
).Rectangle(toDraw).FillStroke();
258258
}
259259
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdfPath, sourceFolder + "cmp_" + fileName
260260
, destinationFolder, "diff"));

itext/itext.kernel/itext/kernel/colors/gradients/AbstractLinearGradientBuilder.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ public virtual GradientSpreadMethod GetSpreadMethod() {
113113
/// if there is no transformation from base coordinates to current space
114114
/// specified, or it is equal to identity transformation.
115115
/// </param>
116+
/// <param name="document">
117+
/// the
118+
/// <see cref="iText.Kernel.Pdf.PdfDocument"/>
119+
/// for which the linear gradient would be built.
120+
/// </param>
116121
/// <returns>
117122
/// the constructed
118123
/// <see cref="iText.Kernel.Colors.Color"/>
@@ -121,7 +126,9 @@ public virtual GradientSpreadMethod GetSpreadMethod() {
121126
/// if no color to be applied
122127
/// or base gradient vector has been specified
123128
/// </returns>
124-
public virtual Color BuildColor(Rectangle targetBoundingBox, AffineTransform contextTransform) {
129+
public virtual Color BuildColor(Rectangle targetBoundingBox, AffineTransform contextTransform, PdfDocument
130+
document) {
131+
// TODO: DEVSIX-4136 the document argument would be required for opaque gradients (as we would need to create a mask form xObject)
125132
Point[] baseCoordinatesVector = GetGradientVector(targetBoundingBox, contextTransform);
126133
if (baseCoordinatesVector == null || this.stops.IsEmpty()) {
127134
// Can not create gradient color with 0 stops or null coordinates vector

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ public static PdfFormXObject CreateXObject(AbstractLinearGradientBuilder linearG
562562
Rectangle formBBox = new Rectangle(0, 0, xObjectArea.GetWidth(), xObjectArea.GetHeight());
563563
PdfFormXObject xObject = new PdfFormXObject(formBBox);
564564
if (linearGradientBuilder != null) {
565-
Color gradientColor = linearGradientBuilder.BuildColor(formBBox, null);
565+
Color gradientColor = linearGradientBuilder.BuildColor(formBBox, null, document);
566566
if (gradientColor != null) {
567567
new PdfCanvas(xObject, document).SetColor(gradientColor, true).Rectangle(formBBox).Fill();
568568
}

itext/itext.svg/itext/svg/renderers/impl/LinearGradientSvgNodeRenderer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public override Color CreateColor(SvgDrawContext context, Rectangle objectBoundi
5454
);
5555
builder.SetCurrentSpaceToGradientVectorSpaceTransformation(gradientTransform);
5656
return builder.BuildColor(objectBoundingBox.ApplyMargins(objectBoundingBoxMargin, objectBoundingBoxMargin,
57-
objectBoundingBoxMargin, objectBoundingBoxMargin, true), context.GetCurrentCanvasTransform());
57+
objectBoundingBoxMargin, objectBoundingBoxMargin, true), context.GetCurrentCanvasTransform(), context.
58+
GetCurrentCanvas().GetDocument());
5859
}
5960

6061
public override ISvgNodeRenderer CreateDeepCopy() {

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
f7a0ca8169f8f8229aeece4fdbc9e4ee8786e6b0
1+
8e30869a8359508ad4050bc388c9c5cc51051a8f

0 commit comments

Comments
 (0)