Skip to content

Commit 5bbb74e

Browse files
yulian-gaponenkoiText-CI
authored andcommitted
Implement new pattern color tests
Autoported commit. Original commit hash: [3b0d07f7b]
1 parent 6e03204 commit 5bbb74e

File tree

9 files changed

+267
-7
lines changed

9 files changed

+267
-7
lines changed

itext.tests/itext.kernel.tests/itext/kernel/pdf/canvas/PdfCanvasColorTest.cs

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,49 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2019 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
143
using System;
244
using System.Collections.Generic;
345
using System.IO;
46+
using System.Text;
447
using iText.IO.Source;
548
using iText.Kernel.Colors;
649
using iText.Kernel.Font;
@@ -274,5 +317,201 @@ private void SetColorSameColorSpacesTest(String pdfName, bool pattern) {
274317
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destFile, cmpFile, destinationFolder, "diff_"
275318
));
276319
}
320+
321+
/// <exception cref="System.Exception"/>
322+
[NUnit.Framework.Test]
323+
public virtual void PatternColorColoredAxialPatternTest() {
324+
String name = "patternColorColoredAxialPatternTest.pdf";
325+
PdfWriter writer = new PdfWriter(destinationFolder + name);
326+
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
327+
PdfDocument document = new PdfDocument(writer);
328+
PdfPage page = document.AddNewPage();
329+
PdfCanvas canvas = new PdfCanvas(page);
330+
PdfShading axial = new PdfShading.Axial(new PdfDeviceCs.Rgb(), 36, 716, new float[] { 1, .784f, 0 }, 396,
331+
788, new float[] { 0, 0, 1 }, new bool[] { true, true });
332+
canvas.SetFillColor(new PatternColor(new PdfPattern.Shading(axial)));
333+
canvas.Rectangle(30, 300, 400, 400).Fill();
334+
canvas.Release();
335+
document.Close();
336+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + name, sourceFolder +
337+
"cmp_" + name, destinationFolder));
338+
}
339+
340+
/// <exception cref="System.Exception"/>
341+
[NUnit.Framework.Test]
342+
public virtual void PatternColorColoredRadialPatternTest() {
343+
String name = "patternColorColoredRadialPatternTest.pdf";
344+
PdfWriter writer = new PdfWriter(destinationFolder + name);
345+
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
346+
PdfDocument document = new PdfDocument(writer);
347+
PdfPage page = document.AddNewPage();
348+
PdfCanvas canvas = new PdfCanvas(page);
349+
PdfShading radial = new PdfShading.Radial(new PdfDeviceCs.Rgb(), 200, 700, 50, new float[] { 1, 0.968f, 0.58f
350+
}, 300, 700, 100, new float[] { 0.968f, 0.541f, 0.42f });
351+
canvas.SetFillColor(new PatternColor(new PdfPattern.Shading(radial)));
352+
canvas.Rectangle(30, 300, 400, 400).Fill();
353+
canvas.Release();
354+
document.Close();
355+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + name, sourceFolder +
356+
"cmp_" + name, destinationFolder));
357+
}
358+
359+
/// <exception cref="System.Exception"/>
360+
[NUnit.Framework.Test]
361+
public virtual void PatternColorUncoloredCircleRgbTest() {
362+
String name = "patternColorUncoloredCircleRgbTest.pdf";
363+
PdfWriter writer = new PdfWriter(destinationFolder + name);
364+
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
365+
PdfDocument document = new PdfDocument(writer);
366+
PdfPage page = document.AddNewPage();
367+
PdfCanvas canvas = new PdfCanvas(page);
368+
PdfPattern.Tiling circle = new PdfPattern.Tiling(15, 15, 10, 20, false);
369+
new PdfPatternCanvas(circle, document).Circle(7.5f, 7.5f, 2.5f).Fill().Release();
370+
PdfSpecialCs.UncoloredTilingPattern uncoloredRgbCs = new PdfSpecialCs.UncoloredTilingPattern(new PdfDeviceCs.Rgb
371+
());
372+
float[] green = new float[] { 0, 1, 0 };
373+
canvas.SetFillColor(new PatternColor(circle, uncoloredRgbCs, green));
374+
canvas.Rectangle(30, 300, 400, 400).Fill();
375+
canvas.Release();
376+
document.Close();
377+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + name, sourceFolder +
378+
"cmp_" + name, destinationFolder));
379+
}
380+
381+
/// <exception cref="System.Exception"/>
382+
[NUnit.Framework.Test]
383+
public virtual void PatternColorUncoloredLineGrayTest() {
384+
String name = "patternColorUncoloredLineGrayTest.pdf";
385+
PdfWriter writer = new PdfWriter(destinationFolder + name);
386+
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
387+
PdfDocument document = new PdfDocument(writer);
388+
PdfPage page = document.AddNewPage();
389+
PdfCanvas canvas = new PdfCanvas(page);
390+
PdfPattern.Tiling line = new PdfPattern.Tiling(5, 10, false);
391+
new PdfPatternCanvas(line, document).SetLineWidth(1).MoveTo(3, -1).LineTo(3, 11).Stroke().Release();
392+
canvas.SetFillColor(new PatternColor(line, new DeviceGray()));
393+
canvas.Rectangle(30, 300, 400, 400).Fill();
394+
canvas.Release();
395+
document.Close();
396+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + name, sourceFolder +
397+
"cmp_" + name, destinationFolder));
398+
}
399+
400+
/// <exception cref="System.Exception"/>
401+
[NUnit.Framework.Test]
402+
public virtual void PatternColorColoredSetTwiceTest() {
403+
String name = "patternColorColoredSetTwiceTest.pdf";
404+
PdfWriter writer = new PdfWriter(destinationFolder + name);
405+
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
406+
PdfDocument document = new PdfDocument(writer);
407+
PdfPage page = document.AddNewPage();
408+
PdfCanvas canvas = new PdfCanvas(page);
409+
PdfPattern.Tiling square = new PdfPattern.Tiling(15, 15);
410+
new PdfPatternCanvas(square, document).SetFillColor(new DeviceRgb(0xFF, 0xFF, 0x00)).SetStrokeColor(new DeviceRgb
411+
(0xFF, 0x00, 0x00)).Rectangle(5, 5, 5, 5).FillStroke().Release();
412+
PdfPattern.Tiling ellipse = new PdfPattern.Tiling(15, 10, 20, 25);
413+
new PdfPatternCanvas(ellipse, document).SetFillColor(new DeviceRgb(0xFF, 0xFF, 0x00)).SetStrokeColor(new DeviceRgb
414+
(0xFF, 0x00, 0x00)).Ellipse(2, 2, 13, 8).FillStroke().Release();
415+
canvas.SetFillColor(new PatternColor(square));
416+
canvas.Rectangle(36, 696, 126, 126).Fill();
417+
canvas.SetFillColor(new PatternColor(square));
418+
canvas.Rectangle(180, 696, 126, 126).Fill();
419+
canvas.SetFillColor(new PatternColor(ellipse));
420+
canvas.Rectangle(360, 696, 126, 126).Fill();
421+
byte[] pageContentStreamBytes = canvas.GetContentStream().GetBytes();
422+
canvas.Release();
423+
document.Close();
424+
String contentStreamString = iText.IO.Util.JavaUtil.GetStringForBytes(pageContentStreamBytes, Encoding.ASCII
425+
);
426+
int p1Count = CountSubstringOccurrences(contentStreamString, "/P1 scn");
427+
int p2Count = CountSubstringOccurrences(contentStreamString, "/P2 scn");
428+
NUnit.Framework.Assert.AreEqual(1, p1Count);
429+
NUnit.Framework.Assert.AreEqual(1, p2Count);
430+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + name, sourceFolder +
431+
"cmp_" + name, destinationFolder));
432+
}
433+
434+
/// <exception cref="System.Exception"/>
435+
[NUnit.Framework.Test]
436+
public virtual void PatternColorUncoloredSetTwiceTest() {
437+
String name = "patternColorUncoloredSetTwiceTest.pdf";
438+
PdfWriter writer = new PdfWriter(destinationFolder + name);
439+
writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
440+
PdfDocument document = new PdfDocument(writer);
441+
PdfPage page = document.AddNewPage();
442+
PdfCanvas canvas = new PdfCanvas(page);
443+
PdfPattern.Tiling circle = new PdfPattern.Tiling(15, 15, 10, 20, false);
444+
new PdfPatternCanvas(circle, document).Circle(7.5f, 7.5f, 2.5f).Fill().Release();
445+
PdfPattern.Tiling line = new PdfPattern.Tiling(5, 10, false);
446+
new PdfPatternCanvas(line, document).SetLineWidth(1).MoveTo(3, -1).LineTo(3, 11).Stroke().Release();
447+
PatternColor patternColorCircle = new PatternColor(circle, ColorConstants.RED);
448+
float[] cyan = new float[] { 1, 0, 0, 0 };
449+
float[] magenta = new float[] { 0, 1, 0, 0 };
450+
PdfSpecialCs.UncoloredTilingPattern uncoloredTilingCmykCs = new PdfSpecialCs.UncoloredTilingPattern(new PdfDeviceCs.Cmyk
451+
());
452+
PatternColor patternColorLine = new PatternColor(line, uncoloredTilingCmykCs, magenta);
453+
canvas.SetFillColor(patternColorCircle);
454+
canvas.Rectangle(36, 696, 126, 126).Fill();
455+
canvas.SetFillColor(patternColorCircle);
456+
canvas.Rectangle(180, 696, 126, 126).Fill();
457+
canvas.SetFillColor(patternColorLine);
458+
canvas.Rectangle(36, 576, 126, 126).Fill();
459+
patternColorLine.SetColorValue(cyan);
460+
canvas.SetFillColor(patternColorLine);
461+
canvas.Rectangle(180, 576, 126, 126).Fill();
462+
// this case will be removed when deprecated method is removed
463+
patternColorLine.SetPattern(circle);
464+
canvas.SetFillColor(patternColorLine);
465+
canvas.Rectangle(360, 696, 126, 126).Fill();
466+
byte[] pageContentStreamBytes = canvas.GetContentStream().GetBytes();
467+
canvas.Release();
468+
document.Close();
469+
String contentStreamString = iText.IO.Util.JavaUtil.GetStringForBytes(pageContentStreamBytes, Encoding.ASCII
470+
);
471+
int p1Count = CountSubstringOccurrences(contentStreamString, "/P1 scn");
472+
int p2Count = CountSubstringOccurrences(contentStreamString, "/P2 scn");
473+
NUnit.Framework.Assert.AreEqual(3, p1Count);
474+
NUnit.Framework.Assert.AreEqual(2, p2Count);
475+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + name, sourceFolder +
476+
"cmp_" + name, destinationFolder));
477+
}
478+
479+
[NUnit.Framework.Test]
480+
public virtual void PatternColorUncoloredPatternCsUnitTest() {
481+
NUnit.Framework.Assert.That(() => {
482+
PdfDocument doc = new PdfDocument(new PdfWriter(new MemoryStream()));
483+
PdfPattern.Tiling circle = new PdfPattern.Tiling(15, 15, 10, 20, false);
484+
new PdfPatternCanvas(circle, doc).Circle(7.5f, 7.5f, 2.5f).Fill().Release();
485+
new PatternColor(circle, new PdfSpecialCs.Pattern(), new float[0]);
486+
}
487+
, NUnit.Framework.Throws.InstanceOf<ArgumentException>())
488+
;
489+
}
490+
491+
[NUnit.Framework.Test]
492+
public virtual void PatternColorUncoloredPatternColorUnitTest() {
493+
NUnit.Framework.Assert.That(() => {
494+
PdfDocument doc = new PdfDocument(new PdfWriter(new MemoryStream()));
495+
PdfPattern.Tiling circle = new PdfPattern.Tiling(15, 15, 10, 20, false);
496+
new PdfPatternCanvas(circle, doc).Circle(7.5f, 7.5f, 2.5f).Fill().Release();
497+
PatternColor redCirclePattern = new PatternColor(circle, ColorConstants.RED);
498+
new PatternColor(circle, redCirclePattern);
499+
}
500+
, NUnit.Framework.Throws.InstanceOf<ArgumentException>())
501+
;
502+
}
503+
504+
private static int CountSubstringOccurrences(String str, String findStr) {
505+
int lastIndex = 0;
506+
int count = 0;
507+
while (lastIndex != -1) {
508+
lastIndex = str.IndexOf(findStr, lastIndex);
509+
if (lastIndex != -1) {
510+
++count;
511+
lastIndex += findStr.Length;
512+
}
513+
}
514+
return count;
515+
}
277516
}
278517
}

itext/itext.kernel/itext/kernel/colors/PatternColor.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,8 @@ public PatternColor(PdfPattern.Tiling uncoloredPattern, Color color)
6161
}
6262

6363
public PatternColor(PdfPattern.Tiling uncoloredPattern, PdfColorSpace underlyingCS, float[] colorValue)
64-
: base(new PdfSpecialCs.UncoloredTilingPattern(underlyingCS), colorValue) {
65-
if (underlyingCS is PdfSpecialCs.Pattern) {
66-
throw new ArgumentException("underlyingCS");
67-
}
68-
this.pattern = uncoloredPattern;
69-
this.underlyingColor = MakeColor(underlyingCS, colorValue);
64+
: this(uncoloredPattern, new PdfSpecialCs.UncoloredTilingPattern(EnsureNotPatternCs(underlyingCS)), colorValue
65+
) {
7066
}
7167

7268
public PatternColor(PdfPattern.Tiling uncoloredPattern, PdfSpecialCs.UncoloredTilingPattern uncoloredTilingCS
@@ -80,6 +76,24 @@ public virtual PdfPattern GetPattern() {
8076
return pattern;
8177
}
8278

79+
public override void SetColorValue(float[] value) {
80+
base.SetColorValue(value);
81+
underlyingColor.SetColorValue(value);
82+
}
83+
84+
/// <summary>
85+
/// Changes pattern for
86+
/// <see cref="PatternColor"/>
87+
/// . Be sure to only set uncolored patterns for uncolored
88+
/// <see cref="PatternColor"/>
89+
/// ,
90+
/// and vice versa, only set colored patterns for colored
91+
/// <see cref="PatternColor"/>
92+
/// .
93+
/// </summary>
94+
/// <param name="pattern">a pattern to be set for this instance.</param>
95+
[System.ObsoleteAttribute(@"To be removed in iText 7.2. In order to change pattern one shall create a new PatternColor ."
96+
)]
8397
public virtual void SetPattern(PdfPattern pattern) {
8498
this.pattern = pattern;
8599
}
@@ -92,5 +106,12 @@ public override bool Equals(Object o) {
92106
return pattern.Equals(color.pattern) && (underlyingColor != null ? underlyingColor.Equals(color.underlyingColor
93107
) : color.underlyingColor == null);
94108
}
109+
110+
private static PdfColorSpace EnsureNotPatternCs(PdfColorSpace underlyingCS) {
111+
if (underlyingCS is PdfSpecialCs.Pattern) {
112+
throw new ArgumentException("underlyingCS");
113+
}
114+
return underlyingCS;
115+
}
95116
}
96117
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
760317bc355a82f1fd97b2fca7ee1bbe8c364b07
1+
3b0d07f7b864be476b654b2344f46cf01fd20131

0 commit comments

Comments
 (0)