Skip to content

Commit 014fb72

Browse files
author
glenn.volckaert
committed
Add unit test for new function classes and related classes
DEVSIX-3538
1 parent 8117de7 commit 014fb72

File tree

7 files changed

+314
-76
lines changed

7 files changed

+314
-76
lines changed

kernel/src/main/java/com/itextpdf/kernel/exceptions/KernelExceptionMessageConstant.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,14 @@ public final class KernelExceptionMessageConstant {
415415
public static final String PDF_TYPE0_FUNCTION_INVALID_SAMPLES = "Invalid samples array for PDF function of type 0";
416416
public static final String PDF_TYPE0_FUNCTION_BITS_PER_SAMPLE_INVALID_VALUE =
417417
"bitsPerSample value must be from {1, 2, 4, 8, 12, 16, 24, 32}";
418+
public static final String FUCTIONFACTORY_INVALID_OBJECT_TYPE =
419+
"Invalid object type, a function must be either a Dictionary or a Stream";
420+
public static final String FUCTIONFACTORY_INVALID_FUNCTION_TYPE = "Invalid function type {0}";
421+
public static final String FUCTIONFACTORY_INVALID_OBJECT_TYPE_TYPE4 =
422+
"Invalid object type, a function type 4 requires a stream object";
423+
public static final String FUCTIONFACTORY_INVALID_OBJECT_TYPE_TYPE0 =
424+
"Invalid object type, a function type 0 requires a stream object";
425+
418426

419427
private KernelExceptionMessageConstant(){}
420428
}

kernel/src/main/java/com/itextpdf/kernel/pdf/function/PdfFunctionFactory.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ This file is part of the iText (R) project.
4343
*/
4444
package com.itextpdf.kernel.pdf.function;
4545

46+
import com.itextpdf.commons.utils.MessageFormatUtil;
47+
import com.itextpdf.kernel.exceptions.KernelExceptionMessageConstant;
4648
import com.itextpdf.kernel.exceptions.PdfException;
4749
import com.itextpdf.kernel.pdf.PdfDictionary;
4850
import com.itextpdf.kernel.pdf.PdfName;
@@ -75,7 +77,7 @@ public static IPdfFunction create(PdfObject pdfObject) {
7577
switch (dict.getAsNumber(PdfName.FunctionType).intValue()) {
7678
case FUNCTION_TYPE_0:
7779
if (pdfObject.getType() != PdfObject.STREAM) {
78-
throw new PdfException("Invalid object type, a function type 0 requires a stream object");
80+
throw new PdfException(KernelExceptionMessageConstant.FUCTIONFACTORY_INVALID_OBJECT_TYPE_TYPE0);
7981
}
8082
return new PdfType0Function((PdfStream) pdfObject);
8183
case FUNCTION_TYPE_2:
@@ -84,14 +86,15 @@ public static IPdfFunction create(PdfObject pdfObject) {
8486
return new PdfType3Function(dict);
8587
case FUNCTION_TYPE_4:
8688
if (pdfObject.getType() != PdfObject.STREAM) {
87-
throw new PdfException("Invalid object type, a function type 4 requires a stream object");
89+
throw new PdfException(KernelExceptionMessageConstant.FUCTIONFACTORY_INVALID_OBJECT_TYPE_TYPE4);
8890
}
8991
return new PdfType4Function((PdfStream) pdfObject);
9092
default:
91-
throw new PdfException("Invalid function type %s",
92-
dict.getAsNumber(PdfName.FunctionType).intValue());
93+
throw new PdfException(MessageFormatUtil.format(
94+
KernelExceptionMessageConstant.FUCTIONFACTORY_INVALID_FUNCTION_TYPE ,
95+
dict.getAsNumber(PdfName.FunctionType).intValue()));
9396
}
9497
}
95-
throw new PdfException("Invalid object type, a function must be either a Dictionary or a Stream");
98+
throw new PdfException(KernelExceptionMessageConstant.FUCTIONFACTORY_INVALID_OBJECT_TYPE);
9699
}
97100
}

kernel/src/main/java/com/itextpdf/kernel/pdf/function/PdfType2Function.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ public PdfType2Function(PdfDictionary dict) {
101101
}
102102

103103
public PdfType2Function(double[] domain, double[] range, double[] c0, double[] c1, int n) {
104-
super(new PdfDictionary());
105-
super.setDomain(domain);
106-
super.setRange(range);
104+
super(new PdfDictionary(), PdfFunctionFactory.FUNCTION_TYPE_2, domain, range);
107105
setC0(c0);
108106
setC1(c1);
109107
setN(n);
@@ -125,11 +123,6 @@ public double[] calculate(double[] input) {
125123
return clipOutput(output);
126124
}
127125

128-
@Override
129-
protected boolean isWrappedObjectMustBeIndirect() {
130-
return false;
131-
}
132-
133126
/**
134127
* Gets output size of function.
135128
*
@@ -200,6 +193,11 @@ public final void setN(int value) {
200193
n = value;
201194
}
202195

196+
@Override
197+
protected boolean isWrappedObjectMustBeIndirect() {
198+
return false;
199+
}
200+
203201
private static double[] initializeCArray(PdfArray c, PdfArray otherC, PdfArray range, double defaultValue) {
204202
if (c != null) {
205203
return c.toDoubleArray();

kernel/src/main/java/com/itextpdf/kernel/pdf/function/PdfType4Function.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public PdfType4Function(PdfStream dict) {
5252
super(dict);
5353
}
5454

55+
public PdfType4Function(double[] domain, double[] range, byte[] code) {
56+
super(new PdfStream(code), PdfFunctionFactory.FUNCTION_TYPE_4, domain, range);
57+
}
58+
5559
@Override
5660
public boolean checkCompatibilityWithColorSpace(PdfColorSpace alternateSpace) {
5761
return getInputSize() == 1 && getOutputSize() == alternateSpace.getNumberOfComponents();

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

Lines changed: 102 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,16 @@ This file is part of the iText (R) project.
7171
import com.itextpdf.kernel.pdf.colorspace.PdfPattern;
7272
import com.itextpdf.kernel.pdf.colorspace.PdfShading;
7373
import com.itextpdf.kernel.pdf.colorspace.PdfSpecialCs;
74+
import com.itextpdf.kernel.pdf.function.PdfType4Function;
7475
import com.itextpdf.kernel.utils.CompareTool;
7576
import com.itextpdf.test.ExtendedITextTest;
7677
import com.itextpdf.test.annotations.type.IntegrationTest;
7778

7879
import java.io.ByteArrayInputStream;
7980
import java.io.FileInputStream;
8081
import java.io.IOException;
81-
8282
import java.nio.charset.StandardCharsets;
83-
8483
import java.util.ArrayList;
85-
8684
import org.junit.Assert;
8785
import org.junit.BeforeClass;
8886
import org.junit.Test;
@@ -263,7 +261,8 @@ public void colorTest06() throws Exception {
263261
PdfDocument document = new PdfDocument(writer);
264262
PdfPage page = document.addNewPage();
265263

266-
PdfSpecialCs.Indexed indexed = new PdfSpecialCs.Indexed(com.itextpdf.kernel.pdf.PdfName.DeviceRGB, 255, new PdfString(new String(bytes, "UTF-8")));
264+
PdfSpecialCs.Indexed indexed = new PdfSpecialCs.Indexed(com.itextpdf.kernel.pdf.PdfName.DeviceRGB, 255, new PdfString(new String(bytes,
265+
StandardCharsets.UTF_8)));
267266
PdfCanvas canvas = new PdfCanvas(page);
268267
canvas.setFillColor(new Indexed(indexed, 85)).rectangle(50, 500, 50, 50).fill();
269268
canvas.setFillColor(new Indexed(indexed, 127)).rectangle(150, 500, 50, 50).fill();
@@ -276,7 +275,7 @@ public void colorTest06() throws Exception {
276275
}
277276

278277
@Test
279-
public void colorTest07() throws Exception {
278+
public void colorTest07Depr() throws Exception {
280279
PdfWriter writer = new PdfWriter(DESTINATION_FOLDER + "colorTest07.pdf");
281280
writer.setCompressionLevel(CompressionConstants.NO_COMPRESSION);
282281
PdfDocument document = new PdfDocument(writer);
@@ -297,7 +296,29 @@ public void colorTest07() throws Exception {
297296
}
298297

299298
@Test
300-
public void colorTest08() throws Exception {
299+
public void colorTest07() throws Exception {
300+
PdfWriter writer = new PdfWriter(DESTINATION_FOLDER + "colorTest07.pdf");
301+
writer.setCompressionLevel(CompressionConstants.NO_COMPRESSION);
302+
PdfDocument document = new PdfDocument(writer);
303+
PdfPage page = document.addNewPage();
304+
305+
//com.itextpdf.kernel.pdf.function.PdfFunction.Type4 function = new com.itextpdf.kernel.pdf.function.PdfFunction.Type4(new PdfArray(new float[]{0, 1}), new PdfArray(new float[]{0, 1, 0, 1, 0, 1}), "{0 0}".getBytes(StandardCharsets.ISO_8859_1));
306+
PdfType4Function function = new PdfType4Function(new double[]{0, 1}, new double[]{0, 1, 0, 1, 0, 1}, "{0 0}".getBytes(StandardCharsets.ISO_8859_1));
307+
PdfSpecialCs.Separation separation = new PdfSpecialCs.Separation("MyRed", new PdfDeviceCs.Rgb(), function);
308+
309+
PdfCanvas canvas = new PdfCanvas(page);
310+
canvas.setFillColor(new Separation(separation, 0.25f)).rectangle(50, 500, 50, 50).fill();
311+
canvas.setFillColor(new Separation(separation, 0.5f)).rectangle(150, 500, 50, 50).fill();
312+
canvas.setFillColor(new Separation(separation, 0.75f)).rectangle(250, 500, 50, 50).fill();
313+
canvas.release();
314+
document.close();
315+
316+
Assert.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + "colorTest07.pdf",
317+
SOURCE_FOLDER + "cmp_colorTest07.pdf", DESTINATION_FOLDER, "diff_"));
318+
}
319+
320+
@Test
321+
public void colorTest08Depr() throws Exception {
301322
PdfWriter writer = new PdfWriter(DESTINATION_FOLDER + "colorTest08.pdf");
302323
writer.setCompressionLevel(CompressionConstants.NO_COMPRESSION);
303324
PdfDocument document = new PdfDocument(writer);
@@ -322,63 +343,39 @@ public void colorTest08() throws Exception {
322343
}
323344

324345
@Test
325-
public void setColorsSameColorSpaces() throws IOException, InterruptedException {
326-
setColorSameColorSpacesTest("setColorsSameColorSpaces.pdf", false);
327-
}
328-
329-
@Test
330-
public void setColorsSameColorSpacesPattern() throws IOException, InterruptedException {
331-
setColorSameColorSpacesTest("setColorsSameColorSpacesPattern.pdf", true);
332-
}
346+
public void colorTest08() throws Exception {
347+
PdfWriter writer = new PdfWriter(DESTINATION_FOLDER + "colorTest08.pdf");
348+
writer.setCompressionLevel(CompressionConstants.NO_COMPRESSION);
349+
PdfDocument document = new PdfDocument(writer);
350+
PdfPage page = document.addNewPage();
333351

334-
private void setColorSameColorSpacesTest(String pdfName, boolean pattern) throws IOException, InterruptedException {
335-
String cmpFile = SOURCE_FOLDER + "cmp_" + pdfName;
336-
String destFile = DESTINATION_FOLDER + pdfName;
352+
PdfType4Function function = new PdfType4Function(new double[]{0, 1, 0, 1}, new double[]{0, 1, 0, 1, 0, 1}, "{0}".getBytes(StandardCharsets.ISO_8859_1));
337353

338-
PdfDocument document = new PdfDocument(new PdfWriter(destFile));
354+
ArrayList<String> tmpArray = new ArrayList<String>(2);
355+
tmpArray.add("MyRed");
356+
tmpArray.add("MyGreen");
357+
PdfSpecialCs.DeviceN deviceN = new PdfSpecialCs.DeviceN(tmpArray, new PdfDeviceCs.Rgb(), function);
339358

340-
PdfPage page = document.addNewPage();
341359
PdfCanvas canvas = new PdfCanvas(page);
360+
canvas.setFillColor(new DeviceN(deviceN, new float[]{0, 0})).rectangle(50, 500, 50, 50).fill();
361+
canvas.setFillColor(new DeviceN(deviceN, new float[]{0, 1})).rectangle(150, 500, 50, 50).fill();
362+
canvas.setFillColor(new DeviceN(deviceN, new float[]{1, 0})).rectangle(250, 500, 50, 50).fill();
363+
canvas.release();
364+
document.close();
342365

343-
PdfColorSpace space = pattern ? new PdfSpecialCs.Pattern() : PdfColorSpace.makeColorSpace(PdfName.DeviceRGB);
344-
float[] colorValue1 = pattern ? null : new float[]{1.0f, 0.6f, 0.7f};
345-
float[] colorValue2 = pattern ? null : new float[]{0.1f, 0.9f, 0.9f};
346-
347-
PdfPattern pattern1 = pattern? new PdfPattern.Shading(new PdfShading.Axial(new PdfDeviceCs.Rgb(), 45, 750, ColorConstants.PINK.getColorValue(),
348-
100, 760, ColorConstants.MAGENTA.getColorValue())) : null;
349-
PdfPattern pattern2 = pattern ? new PdfPattern.Shading(new PdfShading.Axial(new PdfDeviceCs.Rgb(), 45, 690, ColorConstants.BLUE.getColorValue(),
350-
100, 710, ColorConstants.CYAN.getColorValue())) : null;
351-
352-
canvas.setColor(space, colorValue1, pattern1, true);
353-
canvas.saveState();
354-
canvas.beginText()
355-
.moveText(50, 750)
356-
.setFontAndSize(PdfFontFactory.createFont(), 16)
357-
.showText("pinkish")
358-
.endText();
359-
canvas.saveState()
360-
.beginText()
361-
.setColor(space, colorValue2, pattern2, true)
362-
.moveText(50, 720)
363-
.setFontAndSize(PdfFontFactory.createFont(), 16)
364-
.showText("bluish")
365-
.endText()
366-
.restoreState();
367-
canvas.restoreState();
368-
canvas.saveState()
369-
.beginText()
370-
.moveText(50, 690)
371-
.setColor(space, colorValue2, pattern2, true)
372-
.setFontAndSize(PdfFontFactory.createFont(), 16)
373-
.showText("bluish")
374-
.endText()
375-
.restoreState();
366+
Assert.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + "colorTest08.pdf",
367+
SOURCE_FOLDER + "cmp_colorTest08.pdf", DESTINATION_FOLDER, "diff_"));
368+
}
376369

377-
canvas.release();
378370

379-
document.close();
371+
@Test
372+
public void setColorsSameColorSpaces() throws IOException, InterruptedException {
373+
setColorSameColorSpacesTest("setColorsSameColorSpaces.pdf", false);
374+
}
380375

381-
Assert.assertNull(new CompareTool().compareByContent(destFile, cmpFile, DESTINATION_FOLDER, "diff_"));
376+
@Test
377+
public void setColorsSameColorSpacesPattern() throws IOException, InterruptedException {
378+
setColorSameColorSpacesTest("setColorsSameColorSpacesPattern.pdf", true);
382379
}
383380

384381
@Test
@@ -628,6 +625,56 @@ public void patternColorUncoloredPatternColorUnitTest() {
628625
Assert.assertThrows(IllegalArgumentException.class, () -> new PatternColor(circle, redCirclePattern));
629626
}
630627

628+
private void setColorSameColorSpacesTest(String pdfName, boolean pattern) throws IOException, InterruptedException {
629+
String cmpFile = SOURCE_FOLDER + "cmp_" + pdfName;
630+
String destFile = DESTINATION_FOLDER + pdfName;
631+
632+
PdfDocument document = new PdfDocument(new PdfWriter(destFile));
633+
634+
PdfPage page = document.addNewPage();
635+
PdfCanvas canvas = new PdfCanvas(page);
636+
637+
PdfColorSpace space = pattern ? new PdfSpecialCs.Pattern() : PdfColorSpace.makeColorSpace(PdfName.DeviceRGB);
638+
float[] colorValue1 = pattern ? null : new float[]{1.0f, 0.6f, 0.7f};
639+
float[] colorValue2 = pattern ? null : new float[]{0.1f, 0.9f, 0.9f};
640+
641+
PdfPattern pattern1 = pattern? new PdfPattern.Shading(new PdfShading.Axial(new PdfDeviceCs.Rgb(), 45, 750, ColorConstants.PINK.getColorValue(),
642+
100, 760, ColorConstants.MAGENTA.getColorValue())) : null;
643+
PdfPattern pattern2 = pattern ? new PdfPattern.Shading(new PdfShading.Axial(new PdfDeviceCs.Rgb(), 45, 690, ColorConstants.BLUE.getColorValue(),
644+
100, 710, ColorConstants.CYAN.getColorValue())) : null;
645+
646+
canvas.setColor(space, colorValue1, pattern1, true);
647+
canvas.saveState();
648+
canvas.beginText()
649+
.moveText(50, 750)
650+
.setFontAndSize(PdfFontFactory.createFont(), 16)
651+
.showText("pinkish")
652+
.endText();
653+
canvas.saveState()
654+
.beginText()
655+
.setColor(space, colorValue2, pattern2, true)
656+
.moveText(50, 720)
657+
.setFontAndSize(PdfFontFactory.createFont(), 16)
658+
.showText("bluish")
659+
.endText()
660+
.restoreState();
661+
canvas.restoreState();
662+
canvas.saveState()
663+
.beginText()
664+
.moveText(50, 690)
665+
.setColor(space, colorValue2, pattern2, true)
666+
.setFontAndSize(PdfFontFactory.createFont(), 16)
667+
.showText("bluish")
668+
.endText()
669+
.restoreState();
670+
671+
canvas.release();
672+
673+
document.close();
674+
675+
Assert.assertNull(new CompareTool().compareByContent(destFile, cmpFile, DESTINATION_FOLDER, "diff_"));
676+
}
677+
631678
private static int countSubstringOccurrences(String str, String findStr) {
632679
int lastIndex = 0;
633680
int count = 0;

0 commit comments

Comments
 (0)