Skip to content

Commit 728b461

Browse files
BlackEgoistiText-CI
authored andcommitted
Add color spots number check
DEVSIX-4049 Autoported commit. Original commit hash: [d6ef46467]
1 parent db396fe commit 728b461

File tree

8 files changed

+137
-5
lines changed

8 files changed

+137
-5
lines changed

itext.tests/itext.pdfa.tests/itext/pdfa/checker/PdfA1ImplementationLimitsCheckerTest.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ source product.
4141
4242
*/
4343
using System;
44+
using System.Collections.Generic;
4445
using iText.Kernel.Colors;
4546
using iText.Kernel.Font;
4647
using iText.Kernel.Pdf;
4748
using iText.Kernel.Pdf.Colorspace;
49+
using iText.Kernel.Pdf.Function;
4850
using iText.Kernel.Pdf.Xobject;
4951
using iText.Pdfa;
5052
using iText.Test;
@@ -405,6 +407,25 @@ public virtual void LongStringInType3FontTest() {
405407
;
406408
}
407409

410+
[NUnit.Framework.Test]
411+
public virtual void DeviceNColorspaceWithMoreThan8Components() {
412+
NUnit.Framework.Assert.That(() => {
413+
CheckColorspace(BuildDeviceNColorspace(10));
414+
}
415+
, NUnit.Framework.Throws.InstanceOf<PdfAConformanceException>().With.Message.EqualTo(PdfAConformanceException.THE_NUMBER_OF_COLOR_COMPONENTS_IN_DEVICE_N_COLORSPACE_SHOULD_NOT_EXCEED))
416+
;
417+
}
418+
419+
[NUnit.Framework.Test]
420+
public virtual void DeviceNColorspaceWith8Components() {
421+
CheckColorspace(BuildDeviceNColorspace(8));
422+
}
423+
424+
[NUnit.Framework.Test]
425+
public virtual void DeviceNColorspaceWithLessThan8Components() {
426+
CheckColorspace(BuildDeviceNColorspace(2));
427+
}
428+
408429
private PdfString BuildLongString() {
409430
int maxAllowedLength = pdfA1Checker.GetMaxStringLength();
410431
int testLength = maxAllowedLength + 1;
@@ -512,5 +533,23 @@ private void CheckInType3Font(PdfObject @object) {
512533
dictionary.Put(PdfName.CharProcs, charProcs);
513534
pdfA1Checker.CheckFont(font);
514535
}
536+
537+
private void CheckColorspace(PdfColorSpace colorSpace) {
538+
PdfDictionary currentColorSpaces = new PdfDictionary();
539+
pdfA1Checker.CheckColorSpace(colorSpace, currentColorSpaces, false, false);
540+
}
541+
542+
private PdfColorSpace BuildDeviceNColorspace(int numberOfComponents) {
543+
IList<String> tmpArray = new List<String>(numberOfComponents);
544+
float[] transformArray = new float[numberOfComponents * 2];
545+
for (int i = 0; i < numberOfComponents; i++) {
546+
tmpArray.Add("MyColor" + i + 1);
547+
transformArray[i * 2] = 0;
548+
transformArray[i * 2 + 1] = 1;
549+
}
550+
PdfFunction.Type4 function = new PdfFunction.Type4(new PdfArray(transformArray), new PdfArray(new float[]
551+
{ 0, 1, 0, 1, 0, 1 }), "{0}".GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1));
552+
return new PdfSpecialCs.DeviceN(tmpArray, new PdfDeviceCs.Rgb(), function);
553+
}
515554
}
516555
}

itext.tests/itext.pdfa.tests/itext/pdfa/checker/PdfA2CheckerTest.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ source product.
4040
For more information, please contact iText Software Corp. at this
4141
4242
*/
43+
using System;
44+
using System.Collections.Generic;
4345
using iText.Kernel.Pdf;
46+
using iText.Kernel.Pdf.Colorspace;
47+
using iText.Kernel.Pdf.Function;
4448
using iText.Pdfa;
4549
using iText.Test;
4650

@@ -277,6 +281,30 @@ public virtual void CheckCatalogDictionaryWithoutRequirements() {
277281
pdfA2Checker.CheckCatalogValidEntries(catalog);
278282
}
279283
, NUnit.Framework.Throws.InstanceOf<PdfAConformanceException>().With.Message.EqualTo(PdfAConformanceException.A_CATALOG_DICTIONARY_SHALL_NOT_CONTAIN_REQUIREMENTS_ENTRY))
284+
;
285+
}
286+
287+
[NUnit.Framework.Test]
288+
public virtual void DeviceNColorspaceNoAttributesDictionary() {
289+
NUnit.Framework.Assert.That(() => {
290+
//TODO DEVSIX-4203 should not cause an IndexOutOfBoundException.
291+
// Should throw PdfAConformanceException as Colorants dictionary always must be present
292+
// for Pdf/A-2
293+
int numberOfComponents = 2;
294+
IList<String> tmpArray = new List<String>(numberOfComponents);
295+
float[] transformArray = new float[numberOfComponents * 2];
296+
for (int i = 0; i < numberOfComponents; i++) {
297+
tmpArray.Add("MyColor" + i + 1);
298+
transformArray[i * 2] = 0;
299+
transformArray[i * 2 + 1] = 1;
300+
}
301+
PdfFunction.Type4 function = new PdfFunction.Type4(new PdfArray(transformArray), new PdfArray(new float[]
302+
{ 0, 1, 0, 1, 0, 1 }), "{0}".GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1));
303+
PdfDictionary currentColorSpaces = new PdfDictionary();
304+
pdfA2Checker.CheckColorSpace(new PdfSpecialCs.DeviceN(tmpArray, new PdfDeviceCs.Rgb(), function), currentColorSpaces
305+
, true, false);
306+
}
307+
, NUnit.Framework.Throws.InstanceOf<Exception>())
280308
;
281309
}
282310
}

itext.tests/itext.pdfa.tests/itext/pdfa/checker/PdfA2ImplementationLimitsCheckerTest.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ source product.
4141
4242
*/
4343
using System;
44+
using System.Collections.Generic;
4445
using iText.Kernel.Pdf;
46+
using iText.Kernel.Pdf.Colorspace;
47+
using iText.Kernel.Pdf.Function;
4548
using iText.Pdfa;
4649
using iText.Test;
4750

@@ -119,5 +122,47 @@ public virtual void IndependentLargeRealTest() {
119122
, NUnit.Framework.Throws.InstanceOf<PdfAConformanceException>().With.Message.EqualTo(PdfAConformanceException.INTEGER_NUMBER_IS_OUT_OF_RANGE))
120123
;
121124
}
125+
126+
[NUnit.Framework.Test]
127+
public virtual void DeviceNColorspaceWithMoreThan32Components() {
128+
NUnit.Framework.Assert.That(() => {
129+
CheckColorspace(BuildDeviceNColorspace(34));
130+
}
131+
, NUnit.Framework.Throws.InstanceOf<PdfAConformanceException>().With.Message.EqualTo(PdfAConformanceException.THE_NUMBER_OF_COLOR_COMPONENTS_IN_DEVICE_N_COLORSPACE_SHOULD_NOT_EXCEED))
132+
;
133+
}
134+
135+
[NUnit.Framework.Test]
136+
public virtual void DeviceNColorspaceWithLessThan32Components() {
137+
CheckColorspace(BuildDeviceNColorspace(16));
138+
}
139+
140+
[NUnit.Framework.Test]
141+
public virtual void DeviceNColorspaceWith32Components() {
142+
CheckColorspace(BuildDeviceNColorspace(32));
143+
}
144+
145+
private void CheckColorspace(PdfColorSpace colorSpace) {
146+
PdfDictionary currentColorSpaces = new PdfDictionary();
147+
pdfA2Checker.CheckColorSpace(colorSpace, currentColorSpaces, true, false);
148+
}
149+
150+
private PdfColorSpace BuildDeviceNColorspace(int numberOfComponents) {
151+
IList<String> tmpArray = new List<String>(numberOfComponents);
152+
float[] transformArray = new float[numberOfComponents * 2];
153+
for (int i = 0; i < numberOfComponents; i++) {
154+
tmpArray.Add("MyColor" + i + 1);
155+
transformArray[i * 2] = 0;
156+
transformArray[i * 2 + 1] = 1;
157+
}
158+
PdfFunction.Type4 function = new PdfFunction.Type4(new PdfArray(transformArray), new PdfArray(new float[]
159+
{ 0, 1, 0, 1, 0, 1 }), "{0}".GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1));
160+
//TODO DEVSIX-4205 Replace with a constructor with 4 parameters or use a setter for attributes dictionary
161+
PdfArray deviceNAsArray = ((PdfArray)(new PdfSpecialCs.DeviceN(tmpArray, new PdfDeviceCs.Rgb(), function))
162+
.GetPdfObject());
163+
PdfDictionary attributes = new PdfDictionary();
164+
deviceNAsArray.Add(attributes);
165+
return new PdfSpecialCs.DeviceN(deviceNAsArray);
166+
}
122167
}
123168
}

itext/itext.kernel/itext/kernel/pdf/colorspace/PdfColorSpace.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public static iText.Kernel.Pdf.Colorspace.PdfColorSpace MakeColorSpace(PdfObject
108108
}
109109
else {
110110
if (PdfName.DeviceN.Equals(csType)) {
111+
//TODO DEVSIX-4205 Fix colorspace creation
111112
return array.Size() == 4 ? new PdfSpecialCs.DeviceN(array) : new PdfSpecialCs.NChannel(array);
112113
}
113114
else {

itext/itext.pdfa/itext/pdfa/PdfAConformanceException.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ public const String THE_F_KEYS_PRINT_FLAG_BIT_SHALL_BE_SET_TO_1_AND_ITS_HIDDEN_I
243243

244244
public const String THE_INTERACTIVE_FORM_DICTIONARY_SHALL_NOT_CONTAIN_THE_XFA_KEY = "The interactive form dictionary shall not contain the xfa key";
245245

246+
public const String THE_NUMBER_OF_COLOR_COMPONENTS_IN_DEVICE_N_COLORSPACE_SHOULD_NOT_EXCEED = "The number of color components in DeviceN colorspace should not exceed {0}";
247+
246248
public const String THE_NUMBER_OF_COLOUR_CHANNELS_IN_THE_JPEG2000_DATA_SHALL_BE_1_3_OR_4 = "The number of colour channels in the jpeg2000 data shall be 1, 3 or 4";
247249

248250
public const String THE_PAGE_DICTIONARY_SHALL_NOT_CONTAIN_AA_ENTRY = "The page dictionary shall not contain aa entry";

itext/itext.pdfa/itext/pdfa/checker/PdfA1Checker.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public class PdfA1Checker : PdfAChecker {
9090
protected internal static readonly ICollection<PdfName> allowedRenderingIntents = new HashSet<PdfName>(JavaUtil.ArraysAsList
9191
(PdfName.RelativeColorimetric, PdfName.AbsoluteColorimetric, PdfName.Perceptual, PdfName.Saturation));
9292

93+
private const int MAX_NUMBER_OF_DEVICEN_COLOR_COMPONENTS = 8;
94+
9395
/// <summary>Creates a PdfA1Checker with the required conformance level</summary>
9496
/// <param name="conformanceLevel">
9597
/// the required conformance level, <c>a</c> or
@@ -154,7 +156,12 @@ public override void CheckColorSpace(PdfColorSpace colorSpace, PdfDictionary cur
154156
}
155157
else {
156158
if (colorSpace is PdfSpecialCs.DeviceN) {
157-
colorSpace = ((PdfSpecialCs.DeviceN)colorSpace).GetBaseCs();
159+
PdfSpecialCs.DeviceN deviceNColorspace = (PdfSpecialCs.DeviceN)colorSpace;
160+
if (deviceNColorspace.GetNumberOfComponents() > MAX_NUMBER_OF_DEVICEN_COLOR_COMPONENTS) {
161+
throw new PdfAConformanceException(PdfAConformanceException.THE_NUMBER_OF_COLOR_COMPONENTS_IN_DEVICE_N_COLORSPACE_SHOULD_NOT_EXCEED
162+
, MAX_NUMBER_OF_DEVICEN_COLOR_COMPONENTS);
163+
}
164+
colorSpace = deviceNColorspace.GetBaseCs();
158165
}
159166
}
160167
if (colorSpace is PdfDeviceCs.Rgb) {

itext/itext.pdfa/itext/pdfa/checker/PdfA2Checker.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public class PdfA2Checker : PdfA1Checker {
8888

8989
internal const int MIN_PAGE_SIZE = 3;
9090

91+
private const int MAX_NUMBER_OF_DEVICEN_COLOR_COMPONENTS = 32;
92+
9193
private bool currentFillCsIsIccBasedCMYK = false;
9294

9395
private bool currentStrokeCsIsIccBasedCMYK = false;
@@ -146,7 +148,7 @@ public override void CheckColor(Color color, PdfDictionary currentColorSpaces, b
146148
PdfObject colorSpace = shadingDictionary.Get(PdfName.ColorSpace);
147149
CheckColorSpace(PdfColorSpace.MakeColorSpace(colorSpace), currentColorSpaces, true, true);
148150
PdfDictionary extGStateDict = ((PdfDictionary)pattern.GetPdfObject()).GetAsDictionary(PdfName.ExtGState);
149-
CanvasGraphicsState gState = new _CanvasGraphicsState_164(extGStateDict);
151+
CanvasGraphicsState gState = new _CanvasGraphicsState_165(extGStateDict);
150152
CheckExtGState(gState, contentStream);
151153
}
152154
else {
@@ -158,8 +160,8 @@ public override void CheckColor(Color color, PdfDictionary currentColorSpaces, b
158160
base.CheckColor(color, currentColorSpaces, fill, contentStream);
159161
}
160162

161-
private sealed class _CanvasGraphicsState_164 : CanvasGraphicsState {
162-
public _CanvasGraphicsState_164(PdfDictionary extGStateDict) {
163+
private sealed class _CanvasGraphicsState_165 : CanvasGraphicsState {
164+
public _CanvasGraphicsState_165(PdfDictionary extGStateDict) {
163165
this.extGStateDict = extGStateDict;
164166
{
165167
this.UpdateFromExtGState(new PdfExtGState(extGStateDict));
@@ -189,8 +191,16 @@ public override void CheckColorSpace(PdfColorSpace colorSpace, PdfDictionary cur
189191
else {
190192
if (colorSpace is PdfSpecialCs.DeviceN) {
191193
PdfSpecialCs.DeviceN deviceN = (PdfSpecialCs.DeviceN)colorSpace;
194+
if (deviceN.GetNumberOfComponents() > MAX_NUMBER_OF_DEVICEN_COLOR_COMPONENTS) {
195+
throw new PdfAConformanceException(PdfAConformanceException.THE_NUMBER_OF_COLOR_COMPONENTS_IN_DEVICE_N_COLORSPACE_SHOULD_NOT_EXCEED
196+
, MAX_NUMBER_OF_DEVICEN_COLOR_COMPONENTS);
197+
}
198+
//TODO DEVSIX-4203 Fix IndexOutOfBounds exception being thrown for DeviceN (not NChannel) colorspace without
199+
// attributes. According to the spec PdfAConformanceException should be thrown.
192200
PdfDictionary attributes = ((PdfArray)deviceN.GetPdfObject()).GetAsDictionary(4);
193201
PdfDictionary colorants = attributes.GetAsDictionary(PdfName.Colorants);
202+
//TODO DEVSIX-4203 Colorants dictionary is mandatory in PDF/A-2 spec. Need to throw an appropriate exception
203+
// if it is not present.
194204
if (colorants != null) {
195205
foreach (KeyValuePair<PdfName, PdfObject> entry in colorants.EntrySet()) {
196206
PdfArray separation = (PdfArray)entry.Value;

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cf313e7603fc7e749a4dba5d4cfd0ae04a280d92
1+
d6ef464670794641e575d3c84d24e7b79c0aa42e

0 commit comments

Comments
 (0)