Skip to content

Commit 3b0d07f

Browse files
yulian-gaponenkoiText-CI
authored andcommitted
Implement new pattern color tests
1 parent 760317b commit 3b0d07f

File tree

8 files changed

+310
-5
lines changed

8 files changed

+310
-5
lines changed

kernel/src/main/java/com/itextpdf/kernel/colors/PatternColor.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ public PatternColor(PdfPattern.Tiling uncoloredPattern, Color color) {
6464
}
6565

6666
public PatternColor(PdfPattern.Tiling uncoloredPattern, PdfColorSpace underlyingCS, float[] colorValue) {
67-
super(new PdfSpecialCs.UncoloredTilingPattern(underlyingCS), colorValue);
68-
if (underlyingCS instanceof PdfSpecialCs.Pattern)
69-
throw new IllegalArgumentException("underlyingCS");
70-
this.pattern = uncoloredPattern;
71-
this.underlyingColor = makeColor(underlyingCS, colorValue);
67+
this(uncoloredPattern, new PdfSpecialCs.UncoloredTilingPattern(ensureNotPatternCs(underlyingCS)), colorValue);
7268
}
7369

7470
public PatternColor(PdfPattern.Tiling uncoloredPattern, PdfSpecialCs.UncoloredTilingPattern uncoloredTilingCS, float[] colorValue) {
@@ -81,6 +77,19 @@ public PdfPattern getPattern() {
8177
return pattern;
8278
}
8379

80+
@Override
81+
public void setColorValue(float[] value) {
82+
super.setColorValue(value);
83+
underlyingColor.setColorValue(value);
84+
}
85+
86+
/**
87+
* Changes pattern for {@link PatternColor}. Be sure to only set uncolored patterns for uncolored {@link PatternColor},
88+
* and vice versa, only set colored patterns for colored {@link PatternColor}.
89+
* @param pattern a pattern to be set for this instance.
90+
* @deprecated To be removed in iText 7.2. In order to change pattern one shall create a new {@link PatternColor}.
91+
*/
92+
@Deprecated
8493
public void setPattern(PdfPattern pattern) {
8594
this.pattern = pattern;
8695
}
@@ -94,4 +103,10 @@ public boolean equals(Object o) {
94103
return pattern.equals(color.pattern) &&
95104
(underlyingColor != null ? underlyingColor.equals(color.underlyingColor) : color.underlyingColor == null);
96105
}
106+
107+
private static PdfColorSpace ensureNotPatternCs(PdfColorSpace underlyingCS) {
108+
if (underlyingCS instanceof PdfSpecialCs.Pattern)
109+
throw new IllegalArgumentException("underlyingCS");
110+
return underlyingCS;
111+
}
97112
}

kernel/src/test/java/com/itextpdf/kernel/pdf/canvas/PdfCanvasColorTest.java

Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
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
package com.itextpdf.kernel.pdf.canvas;
244

345
import com.itextpdf.io.source.ByteArrayOutputStream;
@@ -6,10 +48,13 @@
648
import com.itextpdf.kernel.colors.Color;
749
import com.itextpdf.kernel.colors.ColorConstants;
850
import com.itextpdf.kernel.colors.DeviceCmyk;
51+
import com.itextpdf.kernel.colors.DeviceGray;
952
import com.itextpdf.kernel.colors.DeviceN;
53+
import com.itextpdf.kernel.colors.DeviceRgb;
1054
import com.itextpdf.kernel.colors.IccBased;
1155
import com.itextpdf.kernel.colors.Indexed;
1256
import com.itextpdf.kernel.colors.Lab;
57+
import com.itextpdf.kernel.colors.PatternColor;
1358
import com.itextpdf.kernel.colors.Separation;
1459
import com.itextpdf.kernel.font.PdfFontFactory;
1560
import com.itextpdf.kernel.pdf.CompressionConstants;
@@ -36,14 +81,19 @@
3681
import java.util.ArrayList;
3782
import org.junit.Assert;
3883
import org.junit.BeforeClass;
84+
import org.junit.Rule;
3985
import org.junit.Test;
4086
import org.junit.experimental.categories.Category;
87+
import org.junit.rules.ExpectedException;
4188

4289
@Category(IntegrationTest.class)
4390
public class PdfCanvasColorTest extends ExtendedITextTest {
4491
public static final String sourceFolder = "./src/test/resources/com/itextpdf/kernel/pdf/canvas/PdfCanvasColorTest/";
4592
public static final String destinationFolder = "./target/test/com/itextpdf/kernel/pdf/canvas/PdfCanvasColorTest/";
4693

94+
@Rule
95+
public ExpectedException junitExpectedException = ExpectedException.none();
96+
4797
@BeforeClass
4898
public static void beforeClass() {
4999
createOrClearDestinationFolder(destinationFolder);
@@ -325,4 +375,244 @@ private void setColorSameColorSpacesTest(String pdfName, boolean pattern) throws
325375

326376
Assert.assertNull(new CompareTool().compareByContent(destFile, cmpFile, destinationFolder, "diff_"));
327377
}
378+
379+
@Test
380+
public void patternColorColoredAxialPatternTest() throws Exception {
381+
String name = "patternColorColoredAxialPatternTest.pdf";
382+
PdfWriter writer = new PdfWriter(destinationFolder + name);
383+
writer.setCompressionLevel(CompressionConstants.NO_COMPRESSION);
384+
PdfDocument document = new PdfDocument(writer);
385+
PdfPage page = document.addNewPage();
386+
387+
PdfCanvas canvas = new PdfCanvas(page);
388+
389+
PdfShading axial = new PdfShading.Axial(
390+
new PdfDeviceCs.Rgb(),
391+
36, 716, new float[]{1, .784f, 0},
392+
396, 788, new float[]{0, 0, 1},
393+
new boolean[] {true, true}
394+
);
395+
396+
canvas.setFillColor(new PatternColor(new PdfPattern.Shading(axial)));
397+
canvas.rectangle(30, 300, 400, 400).fill();
398+
399+
canvas.release();
400+
document.close();
401+
402+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name, sourceFolder + "cmp_" + name, destinationFolder));
403+
}
404+
405+
@Test
406+
public void patternColorColoredRadialPatternTest() throws Exception {
407+
String name = "patternColorColoredRadialPatternTest.pdf";
408+
PdfWriter writer = new PdfWriter(destinationFolder + name);
409+
writer.setCompressionLevel(CompressionConstants.NO_COMPRESSION);
410+
PdfDocument document = new PdfDocument(writer);
411+
PdfPage page = document.addNewPage();
412+
413+
PdfCanvas canvas = new PdfCanvas(page);
414+
415+
PdfShading radial = new PdfShading.Radial(
416+
new PdfDeviceCs.Rgb(),
417+
200, 700, 50, new float[] {1, 0.968f, 0.58f},
418+
300, 700, 100, new float[] {0.968f, 0.541f, 0.42f}
419+
);
420+
421+
canvas.setFillColor(new PatternColor(new PdfPattern.Shading(radial)));
422+
canvas.rectangle(30, 300, 400, 400).fill();
423+
424+
canvas.release();
425+
document.close();
426+
427+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name, sourceFolder + "cmp_" + name, destinationFolder));
428+
}
429+
430+
@Test
431+
public void patternColorUncoloredCircleRgbTest() throws Exception {
432+
String name = "patternColorUncoloredCircleRgbTest.pdf";
433+
PdfWriter writer = new PdfWriter(destinationFolder + name);
434+
writer.setCompressionLevel(CompressionConstants.NO_COMPRESSION);
435+
PdfDocument document = new PdfDocument(writer);
436+
PdfPage page = document.addNewPage();
437+
438+
PdfCanvas canvas = new PdfCanvas(page);
439+
440+
PdfPattern.Tiling circle = new PdfPattern.Tiling(15, 15, 10, 20, false);
441+
new PdfPatternCanvas(circle, document).circle(7.5f, 7.5f, 2.5f).fill().release();
442+
443+
PdfSpecialCs.UncoloredTilingPattern uncoloredRgbCs
444+
= new PdfSpecialCs.UncoloredTilingPattern(new PdfDeviceCs.Rgb());
445+
446+
float[] green = {0, 1, 0};
447+
448+
canvas.setFillColor(new PatternColor(circle, uncoloredRgbCs, green));
449+
canvas.rectangle(30, 300, 400, 400).fill();
450+
451+
canvas.release();
452+
document.close();
453+
454+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name, sourceFolder + "cmp_" + name, destinationFolder));
455+
}
456+
457+
@Test
458+
public void patternColorUncoloredLineGrayTest() throws Exception {
459+
String name = "patternColorUncoloredLineGrayTest.pdf";
460+
PdfWriter writer = new PdfWriter(destinationFolder + name);
461+
writer.setCompressionLevel(CompressionConstants.NO_COMPRESSION);
462+
PdfDocument document = new PdfDocument(writer);
463+
PdfPage page = document.addNewPage();
464+
465+
PdfCanvas canvas = new PdfCanvas(page);
466+
467+
PdfPattern.Tiling line = new PdfPattern.Tiling(5, 10, false);
468+
new PdfPatternCanvas(line, document).setLineWidth(1).moveTo(3, -1).lineTo(3, 11).stroke().release();
469+
470+
canvas.setFillColor(new PatternColor(line, new DeviceGray()));
471+
canvas.rectangle(30, 300, 400, 400).fill();
472+
473+
canvas.release();
474+
document.close();
475+
476+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name, sourceFolder + "cmp_" + name, destinationFolder));
477+
}
478+
479+
@Test
480+
public void patternColorColoredSetTwiceTest() throws Exception {
481+
String name = "patternColorColoredSetTwiceTest.pdf";
482+
PdfWriter writer = new PdfWriter(destinationFolder + name);
483+
writer.setCompressionLevel(CompressionConstants.NO_COMPRESSION);
484+
PdfDocument document = new PdfDocument(writer);
485+
PdfPage page = document.addNewPage();
486+
487+
PdfCanvas canvas = new PdfCanvas(page);
488+
489+
490+
PdfPattern.Tiling square = new PdfPattern.Tiling(15, 15);
491+
new PdfPatternCanvas(square, document).setFillColor(new DeviceRgb(0xFF, 0xFF, 0x00))
492+
.setStrokeColor(new DeviceRgb(0xFF, 0x00, 0x00))
493+
.rectangle(5, 5, 5, 5)
494+
.fillStroke()
495+
.release();
496+
497+
PdfPattern.Tiling ellipse = new PdfPattern.Tiling(15, 10, 20, 25);
498+
new PdfPatternCanvas(ellipse, document)
499+
.setFillColor(new DeviceRgb(0xFF, 0xFF, 0x00))
500+
.setStrokeColor(new DeviceRgb(0xFF, 0x00, 0x00))
501+
.ellipse(2, 2, 13, 8)
502+
.fillStroke()
503+
.release();
504+
505+
506+
canvas.setFillColor(new PatternColor(square));
507+
canvas.rectangle(36, 696, 126, 126).fill();
508+
canvas.setFillColor(new PatternColor(square));
509+
canvas.rectangle(180, 696, 126, 126).fill();
510+
canvas.setFillColor(new PatternColor(ellipse));
511+
canvas.rectangle(360, 696, 126, 126).fill();
512+
513+
byte[] pageContentStreamBytes = canvas.getContentStream().getBytes();
514+
515+
canvas.release();
516+
document.close();
517+
518+
String contentStreamString = new String(pageContentStreamBytes, StandardCharsets.US_ASCII);
519+
int p1Count = countSubstringOccurrences(contentStreamString, "/P1 scn");
520+
int p2Count = countSubstringOccurrences(contentStreamString, "/P2 scn");
521+
Assert.assertEquals(1, p1Count);
522+
Assert.assertEquals(1, p2Count);
523+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name, sourceFolder + "cmp_" + name, destinationFolder));
524+
}
525+
526+
@Test
527+
public void patternColorUncoloredSetTwiceTest() throws Exception {
528+
String name = "patternColorUncoloredSetTwiceTest.pdf";
529+
PdfWriter writer = new PdfWriter(destinationFolder + name);
530+
writer.setCompressionLevel(CompressionConstants.NO_COMPRESSION);
531+
PdfDocument document = new PdfDocument(writer);
532+
PdfPage page = document.addNewPage();
533+
534+
PdfCanvas canvas = new PdfCanvas(page);
535+
536+
PdfPattern.Tiling circle = new PdfPattern.Tiling(15, 15, 10, 20, false);
537+
new PdfPatternCanvas(circle, document).circle(7.5f, 7.5f, 2.5f).fill().release();
538+
539+
PdfPattern.Tiling line = new PdfPattern.Tiling(5, 10, false);
540+
new PdfPatternCanvas(line, document).setLineWidth(1).moveTo(3, -1).lineTo(3, 11).stroke().release();
541+
542+
PatternColor patternColorCircle = new PatternColor(circle, ColorConstants.RED);
543+
544+
float[] cyan = {1, 0, 0, 0};
545+
float[] magenta = {0, 1, 0, 0};
546+
PdfSpecialCs.UncoloredTilingPattern uncoloredTilingCmykCs = new PdfSpecialCs.UncoloredTilingPattern(new PdfDeviceCs.Cmyk());
547+
PatternColor patternColorLine = new PatternColor(line, uncoloredTilingCmykCs, magenta);
548+
549+
canvas.setFillColor(patternColorCircle);
550+
canvas.rectangle(36, 696, 126, 126).fill();
551+
canvas.setFillColor(patternColorCircle);
552+
canvas.rectangle(180, 696, 126, 126).fill();
553+
554+
canvas.setFillColor(patternColorLine);
555+
canvas.rectangle(36, 576, 126, 126).fill();
556+
557+
patternColorLine.setColorValue(cyan);
558+
canvas.setFillColor(patternColorLine);
559+
canvas.rectangle(180, 576, 126, 126).fill();
560+
561+
// this case will be removed when deprecated method is removed
562+
patternColorLine.setPattern(circle);
563+
canvas.setFillColor(patternColorLine);
564+
canvas.rectangle(360, 696, 126, 126).fill();
565+
566+
byte[] pageContentStreamBytes = canvas.getContentStream().getBytes();
567+
568+
canvas.release();
569+
document.close();
570+
571+
String contentStreamString = new String(pageContentStreamBytes, StandardCharsets.US_ASCII);
572+
int p1Count = countSubstringOccurrences(contentStreamString, "/P1 scn");
573+
int p2Count = countSubstringOccurrences(contentStreamString, "/P2 scn");
574+
Assert.assertEquals(3, p1Count);
575+
Assert.assertEquals(2, p2Count);
576+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name, sourceFolder + "cmp_" + name, destinationFolder));
577+
}
578+
579+
@Test
580+
public void patternColorUncoloredPatternCsUnitTest() {
581+
junitExpectedException.expect(IllegalArgumentException.class);
582+
583+
PdfDocument doc = new PdfDocument(new PdfWriter(new java.io.ByteArrayOutputStream()));
584+
585+
PdfPattern.Tiling circle = new PdfPattern.Tiling(15, 15, 10, 20, false);
586+
new PdfPatternCanvas(circle, doc).circle(7.5f, 7.5f, 2.5f).fill().release();
587+
588+
new PatternColor(circle, new PdfSpecialCs.Pattern(), new float[0]);
589+
}
590+
591+
@Test
592+
public void patternColorUncoloredPatternColorUnitTest() {
593+
junitExpectedException.expect(IllegalArgumentException.class);
594+
595+
PdfDocument doc = new PdfDocument(new PdfWriter(new java.io.ByteArrayOutputStream()));
596+
597+
PdfPattern.Tiling circle = new PdfPattern.Tiling(15, 15, 10, 20, false);
598+
new PdfPatternCanvas(circle, doc).circle(7.5f, 7.5f, 2.5f).fill().release();
599+
600+
PatternColor redCirclePattern = new PatternColor(circle, ColorConstants.RED);
601+
new PatternColor(circle, redCirclePattern);
602+
}
603+
604+
private static int countSubstringOccurrences(String str, String findStr) {
605+
int lastIndex = 0;
606+
int count = 0;
607+
608+
while(lastIndex != -1){
609+
lastIndex = str.indexOf(findStr, lastIndex);
610+
611+
if(lastIndex != -1){
612+
++count;
613+
lastIndex += findStr.length();
614+
}
615+
}
616+
return count;
617+
}
328618
}

0 commit comments

Comments
 (0)