Skip to content

Commit 1318d89

Browse files
Move, add pattern color tests to kernel repository, add Logger to PdfCanvasProcessor, add LogMessageConstant class for kernel module
DEVSIX-3727
1 parent afdb928 commit 1318d89

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2020 iText Group NV
4+
Authors: Bruno Lowagie, Paulo Soares, et al.
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+
*/
43+
44+
package com.itextpdf.kernel;
45+
46+
/**
47+
* Class that bundles all the error message templates as constants.
48+
*/
49+
public final class KernelLogMessageConstant {
50+
51+
private KernelLogMessageConstant() {
52+
53+
//Private constructor will prevent the instantiation of this class directly
54+
}
55+
56+
/** The Constant UNABLE_TO_PARSE_COLOR_WITHIN_COLORSPACE */
57+
public static final String UNABLE_TO_PARSE_COLOR_WITHIN_COLORSPACE = "Unable to parse color {0} within {1} color space";
58+
}

kernel/src/main/java/com/itextpdf/kernel/pdf/canvas/parser/PdfCanvasProcessor.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ This file is part of the iText (R) project.
4848
import com.itextpdf.io.source.RandomAccessFileOrArray;
4949
import com.itextpdf.io.source.RandomAccessSourceFactory;
5050
import com.itextpdf.io.util.MessageFormatUtil;
51+
import com.itextpdf.kernel.KernelLogMessageConstant;
5152
import com.itextpdf.kernel.PdfException;
5253
import com.itextpdf.kernel.colors.CalGray;
5354
import com.itextpdf.kernel.colors.CalRgb;
@@ -91,6 +92,8 @@ This file is part of the iText (R) project.
9192
import com.itextpdf.kernel.pdf.colorspace.PdfColorSpace;
9293
import com.itextpdf.kernel.pdf.colorspace.PdfPattern;
9394
import com.itextpdf.kernel.pdf.colorspace.PdfSpecialCs;
95+
96+
import java.util.Objects;
9497
import org.slf4j.Logger;
9598
import org.slf4j.LoggerFactory;
9699

@@ -1027,6 +1030,11 @@ else if (PdfName.Pattern.equals(csType)) {
10271030
}
10281031
}
10291032
}
1033+
1034+
Logger logger = LoggerFactory.getLogger(PdfCanvasProcessor.class);
1035+
logger.warn(MessageFormatUtil.format(KernelLogMessageConstant.UNABLE_TO_PARSE_COLOR_WITHIN_COLORSPACE,
1036+
Arrays.toString((Object[])operands.toArray()), pdfColorSpace.getPdfObject()));
1037+
10301038
return null;
10311039
}
10321040

kernel/src/test/java/com/itextpdf/kernel/pdf/canvas/parser/PdfCanvasProcessorTest.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ This file is part of the iText (R) project.
4444

4545
import com.itextpdf.io.LogMessageConstant;
4646
import com.itextpdf.io.source.ByteArrayOutputStream;
47+
import com.itextpdf.kernel.KernelLogMessageConstant;
48+
import com.itextpdf.kernel.PdfException;
49+
import com.itextpdf.kernel.colors.Color;
4750
import com.itextpdf.kernel.pdf.PdfDocument;
4851
import com.itextpdf.kernel.pdf.PdfPage;
4952
import com.itextpdf.kernel.pdf.PdfReader;
@@ -55,6 +58,8 @@ This file is part of the iText (R) project.
5558
import com.itextpdf.kernel.pdf.canvas.parser.data.TextRenderInfo;
5659
import com.itextpdf.kernel.pdf.canvas.parser.listener.IEventListener;
5760
import com.itextpdf.kernel.pdf.canvas.parser.listener.LocationTextExtractionStrategy;
61+
import com.itextpdf.kernel.pdf.colorspace.PdfColorSpace;
62+
import com.itextpdf.kernel.pdf.colorspace.PdfSpecialCs;
5863
import com.itextpdf.test.ExtendedITextTest;
5964
import com.itextpdf.test.annotations.LogMessage;
6065
import com.itextpdf.test.annotations.LogMessages;
@@ -63,6 +68,8 @@ This file is part of the iText (R) project.
6368
import java.nio.charset.StandardCharsets;
6469
import java.nio.file.Files;
6570
import java.nio.file.Paths;
71+
import java.util.ArrayList;
72+
import java.util.List;
6673
import org.junit.Assert;
6774
import org.junit.Ignore;
6875
import org.junit.Rule;
@@ -143,6 +150,82 @@ public void parseCircularReferencesInResourcesTest() throws IOException {
143150
pdfDocument.close();
144151
}
145152

153+
@Test
154+
@LogMessages(messages = @LogMessage(messageTemplate = KernelLogMessageConstant.UNABLE_TO_PARSE_COLOR_WITHIN_COLORSPACE))
155+
public void patternColorParsingNotValidPdfTest() throws IOException {
156+
String inputFile = sourceFolder + "patternColorParsingNotValidPdfTest.pdf";
157+
PdfDocument pdfDocument = new PdfDocument(new PdfReader(inputFile));
158+
159+
for (int i = 1; i <= pdfDocument.getNumberOfPages(); ++i) {
160+
PdfPage page = pdfDocument.getPage(i);
161+
162+
ColorParsingEventListener colorParsingEventListener = new ColorParsingEventListener();
163+
164+
PdfCanvasProcessor processor = new PdfCanvasProcessor(colorParsingEventListener);
165+
processor.processPageContent(page);
166+
167+
Color renderInfo = colorParsingEventListener.getEncounteredPath().getFillColor();
168+
169+
Assert.assertNull(renderInfo);
170+
}
171+
}
172+
173+
@Test
174+
public void patternColorParsingValidPdfTest() throws IOException {
175+
String inputFile = sourceFolder + "patternColorParsingValidPdfTest.pdf";
176+
PdfDocument pdfDocument = new PdfDocument(new PdfReader(inputFile));
177+
178+
for (int i = 1; i <= pdfDocument.getNumberOfPages(); ++i) {
179+
PdfPage page = pdfDocument.getPage(i);
180+
181+
ColorParsingEventListener colorParsingEventListener = new ColorParsingEventListener();
182+
183+
PdfCanvasProcessor processor = new PdfCanvasProcessor(colorParsingEventListener);
184+
processor.processPageContent(page);
185+
186+
PathRenderInfo renderInfo = colorParsingEventListener.getEncounteredPath();
187+
PdfColorSpace colorSpace = renderInfo.getGraphicsState().getFillColor().getColorSpace();
188+
189+
Assert.assertTrue(colorSpace instanceof PdfSpecialCs.Pattern);
190+
}
191+
}
192+
193+
private static class ColorParsingEventListener implements IEventListener {
194+
private List<IEventData> content = new ArrayList<>();
195+
private static final String pathDataExpected = "Path data expected.";
196+
197+
public void eventOccurred(IEventData data, EventType type) {
198+
if (type.equals(EventType.RENDER_PATH)) {
199+
PathRenderInfo pathRenderInfo = (PathRenderInfo) data;
200+
pathRenderInfo.preserveGraphicsState();
201+
content.add(data);
202+
}
203+
}
204+
205+
/**
206+
* Get the last encountered PathRenderInfo, then clears the internal buffer
207+
*
208+
* @return the PathRenderInfo object that was encountered when processing the last path rendering operation
209+
*/
210+
PathRenderInfo getEncounteredPath() {
211+
if (content.size() == 0) {
212+
return null;
213+
}
214+
215+
IEventData eventData = content.get(0);
216+
if (!(eventData instanceof PathRenderInfo)) {
217+
throw new PdfException(pathDataExpected);
218+
}
219+
content.clear();
220+
221+
return (PathRenderInfo) eventData;
222+
}
223+
224+
public Set<EventType> getSupportedEvents() {
225+
return null;
226+
}
227+
}
228+
146229
private static class NoOpEventListener implements IEventListener {
147230
@Override
148231
public void eventOccurred(IEventData data, EventType type) {

0 commit comments

Comments
 (0)