Skip to content

Commit 8117de7

Browse files
author
glenn.volckaert
committed
Update samples to use new functions instead of deprecated ones
DEVSIX-6792
1 parent e5ed061 commit 8117de7

File tree

7 files changed

+231
-48
lines changed

7 files changed

+231
-48
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ This file is part of the iText (R) project.
4545

4646
import com.itextpdf.kernel.pdf.colorspace.PdfColorSpace;
4747
import com.itextpdf.kernel.pdf.colorspace.PdfSpecialCs;
48+
import com.itextpdf.kernel.pdf.function.IPdfFunction;
4849
import com.itextpdf.kernel.pdf.function.PdfFunction;
4950

5051
import java.util.Arrays;
@@ -61,10 +62,33 @@ public DeviceN(PdfSpecialCs.DeviceN cs, float[] value) {
6162
super(cs, value);
6263
}
6364

65+
/**
66+
* Creates a color in new DeviceN color space.
67+
*
68+
* @param names the names oif the components
69+
* @param alternateCs the alternate color space
70+
* @param tintTransform the function to transform color to the alternate color space
71+
* @param value the values for the components of this color
72+
*
73+
* @deprecated Use constructor {@link #DeviceN(List, PdfColorSpace, IPdfFunction, float[])} DeviceN} instead.
74+
*/
75+
@Deprecated
6476
public DeviceN(List<String> names, PdfColorSpace alternateCs, PdfFunction tintTransform, float[] value) {
6577
this(new PdfSpecialCs.DeviceN(names, alternateCs, tintTransform), value);
6678
}
6779

80+
/**
81+
* Creates a color in a new DeviceN color space.
82+
*
83+
* @param names the names oif the components
84+
* @param alternateCs the alternate color space
85+
* @param tintTransform the function to transform color to the alternate color space
86+
* @param value the values for the components of this color
87+
*/
88+
public DeviceN(List<String> names, PdfColorSpace alternateCs, IPdfFunction tintTransform, float[] value) {
89+
this(new PdfSpecialCs.DeviceN(names, alternateCs, tintTransform), value);
90+
}
91+
6892
private static float[] getDefaultColorants(int numOfColorants) {
6993
float[] colorants = new float[numOfColorants];
7094
Arrays.fill(colorants, 1f);

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ This file is part of the iText (R) project.
4545

4646
import com.itextpdf.kernel.pdf.colorspace.PdfColorSpace;
4747
import com.itextpdf.kernel.pdf.colorspace.PdfSpecialCs;
48+
import com.itextpdf.kernel.pdf.function.IPdfFunction;
4849
import com.itextpdf.kernel.pdf.function.PdfFunction;
4950

5051
public class Separation extends Color {
@@ -58,8 +59,32 @@ public Separation(PdfSpecialCs.Separation cs, float value) {
5859
super(cs, new float[]{value});
5960
}
6061

62+
/**
63+
* Creates a color in a new separation color space.
64+
*
65+
* @param name the name for the separation color
66+
* @param alternateCs the alternative color space
67+
* @param tintTransform the function to transform color to the alternate colorspace
68+
* @param value the color value
69+
*
70+
* @deprecated Use constructor {@link #Separation(String, PdfColorSpace, IPdfFunction, float)} Separation}
71+
* instead
72+
*/
73+
@Deprecated
6174
public Separation(String name, PdfColorSpace alternateCs, PdfFunction tintTransform, float value) {
6275
this(new PdfSpecialCs.Separation(name, alternateCs, tintTransform), value);
6376
}
6477

78+
/**
79+
* Creates a color in a new separation color space.
80+
*
81+
* @param name the name for the separation color
82+
* @param alternateCs the alternative color space
83+
* @param tintTransform the function to transform color to the alternate colorspace
84+
* @param value the color value
85+
*/
86+
public Separation(String name, PdfColorSpace alternateCs, IPdfFunction tintTransform, float value) {
87+
this(new PdfSpecialCs.Separation(name, alternateCs, tintTransform), value);
88+
}
89+
6590
}

kernel/src/main/java/com/itextpdf/kernel/pdf/colorspace/PdfSpecialCs.java

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

46-
import com.itextpdf.kernel.exceptions.PdfException;
4746
import com.itextpdf.kernel.exceptions.KernelExceptionMessageConstant;
47+
import com.itextpdf.kernel.exceptions.PdfException;
4848
import com.itextpdf.kernel.pdf.PdfArray;
4949
import com.itextpdf.kernel.pdf.PdfDictionary;
5050
import com.itextpdf.kernel.pdf.PdfDocument;
5151
import com.itextpdf.kernel.pdf.PdfName;
5252
import com.itextpdf.kernel.pdf.PdfNumber;
5353
import com.itextpdf.kernel.pdf.PdfObject;
5454
import com.itextpdf.kernel.pdf.PdfString;
55-
import com.itextpdf.kernel.pdf.function.PdfFunctionFactory;
55+
import com.itextpdf.kernel.pdf.function.AbstractPdfFunction;
5656
import com.itextpdf.kernel.pdf.function.IPdfFunction;
5757
import com.itextpdf.kernel.pdf.function.PdfFunction;
58+
import com.itextpdf.kernel.pdf.function.PdfFunctionFactory;
5859

5960
import java.util.Arrays;
6061
import java.util.List;
@@ -125,6 +126,17 @@ public Separation(PdfName name, PdfObject alternateSpace, PdfObject tintTransfor
125126
this(getSeparationCsArray(name, alternateSpace, tintTransform));
126127
}
127128

129+
/**
130+
* Creates a new separation color space.
131+
*
132+
* @param name The name for the separation color
133+
* @param alternateSpace The alternate colorspace
134+
* @param tintTransform The function how the transform colors in the separation color space
135+
* to the alternate color space
136+
* @deprecated This constructor has been replaced
137+
* by {@link #Separation(String, PdfColorSpace, IPdfFunction)}
138+
*/
139+
@Deprecated
128140
public Separation(String name, PdfColorSpace alternateSpace, PdfFunction tintTransform) {
129141
this(new PdfName(name), alternateSpace.getPdfObject(), tintTransform.getPdfObject());
130142
if (!tintTransform.checkCompatibilityWithColorSpace(alternateSpace)) {
@@ -133,6 +145,22 @@ public Separation(String name, PdfColorSpace alternateSpace, PdfFunction tintTra
133145
}
134146
}
135147

148+
/**
149+
* Creates a new separation color space.
150+
*
151+
* @param name The name for the separation color
152+
* @param alternateSpace The alternate colorspace
153+
* @param tintTransform The function how the transform colors in the separation color space
154+
* to the alternate color space
155+
*/
156+
public Separation(String name, PdfColorSpace alternateSpace, IPdfFunction tintTransform) {
157+
this(new PdfName(name), alternateSpace.getPdfObject(), ((AbstractPdfFunction)tintTransform).getPdfObject());
158+
if (!tintTransform.checkCompatibilityWithColorSpace(alternateSpace)) {
159+
throw new PdfException(
160+
KernelExceptionMessageConstant.FUNCTION_IS_NOT_COMPATIBLE_WITH_COLOR_SPACE, this);
161+
}
162+
}
163+
136164
@Override
137165
public int getNumberOfComponents() {
138166
return 1;
@@ -180,9 +208,38 @@ public DeviceN(PdfArray names, PdfObject alternateSpace, PdfObject tintTransform
180208
this(getDeviceNCsArray(names, alternateSpace, tintTransform));
181209
}
182210

211+
/**
212+
* Creates a new DeviceN colorspace.
213+
*
214+
* @param names the names of the components
215+
* @param alternateSpace the alternate colorspace
216+
* @param tintTransform the function to transform colors to the alternate colorspace
217+
*
218+
* @deprecated Use constructor {@link #DeviceN(List, PdfColorSpace, IPdfFunction)} instead.
219+
*/
220+
221+
@Deprecated
183222
public DeviceN(List<String> names, PdfColorSpace alternateSpace, PdfFunction tintTransform) {
184223
this(new PdfArray(names, true), alternateSpace.getPdfObject(), tintTransform.getPdfObject());
185-
if (tintTransform.getInputSize() != getNumberOfComponents() || tintTransform.getOutputSize() != alternateSpace.getNumberOfComponents()) {
224+
if (tintTransform.getInputSize() != numOfComponents ||
225+
tintTransform.getOutputSize() != alternateSpace.getNumberOfComponents()) {
226+
throw new PdfException(
227+
KernelExceptionMessageConstant.FUNCTION_IS_NOT_COMPATIBLE_WITH_COLOR_SPACE, this);
228+
}
229+
}
230+
231+
/**
232+
* Creates a new DiviceN colorspace.
233+
*
234+
* @param names the names of the components
235+
* @param alternateSpace the alternate colorspace
236+
* @param tintTransform the function to transform colors to the alternate colorspace
237+
*/
238+
public DeviceN(List<String> names, PdfColorSpace alternateSpace, IPdfFunction tintTransform) {
239+
this(new PdfArray(names, true), alternateSpace.getPdfObject(),
240+
((AbstractPdfFunction)tintTransform).getPdfObject());
241+
if (tintTransform.getInputSize() != numOfComponents ||
242+
tintTransform.getOutputSize() != alternateSpace.getNumberOfComponents()) {
186243
throw new PdfException(
187244
KernelExceptionMessageConstant.FUNCTION_IS_NOT_COMPATIBLE_WITH_COLOR_SPACE, this);
188245
}
@@ -223,15 +280,49 @@ public NChannel(PdfArray names, PdfObject alternateSpace, PdfObject tintTransfor
223280
this(getNChannelCsArray(names, alternateSpace, tintTransform, attributes));
224281
}
225282

226-
public NChannel(List<String> names, PdfColorSpace alternateSpace, PdfFunction tintTransform, PdfDictionary attributes) {
283+
/**
284+
* Creates a new NChannel colorspace.
285+
*
286+
* @param names the names for the components
287+
* @param alternateSpace the alternative colorspace
288+
* @param tintTransform the function to transform colors to the alternate color space
289+
* @param attributes NChannel specific attributes
290+
* @deprecated Use constructor {@link #NChannel(PdfArray, PdfObject, PdfObject, PdfDictionary) NChannel} instead
291+
*/
292+
293+
@Deprecated
294+
public NChannel(List<String> names, PdfColorSpace alternateSpace, PdfFunction tintTransform,
295+
PdfDictionary attributes) {
227296
this(new PdfArray(names, true), alternateSpace.getPdfObject(), tintTransform.getPdfObject(), attributes);
228-
if (tintTransform.getInputSize() != 1 || tintTransform.getOutputSize() != alternateSpace.getNumberOfComponents()) {
297+
if (tintTransform.getInputSize() != 1 ||
298+
tintTransform.getOutputSize() != alternateSpace.getNumberOfComponents()) {
299+
throw new PdfException(
300+
KernelExceptionMessageConstant.FUNCTION_IS_NOT_COMPATIBLE_WITH_COLOR_SPACE, this);
301+
}
302+
}
303+
304+
/**
305+
* Creates a new NChannel colorspace.
306+
*
307+
* @param names the names for the components
308+
* @param alternateSpace the alternative colorspace
309+
* @param tintTransform the function to transform colors to the alternate color space
310+
* @param attributes NChannel specific attributes
311+
*/
312+
public NChannel(List<String> names, PdfColorSpace alternateSpace, IPdfFunction tintTransform,
313+
PdfDictionary attributes) {
314+
this(new PdfArray(names, true), alternateSpace.getPdfObject(),
315+
((AbstractPdfFunction)tintTransform).getPdfObject(), attributes);
316+
if (tintTransform.getInputSize() != 1 ||
317+
tintTransform.getOutputSize() != alternateSpace.getNumberOfComponents()) {
229318
throw new PdfException(
230319
KernelExceptionMessageConstant.FUNCTION_IS_NOT_COMPATIBLE_WITH_COLOR_SPACE, this);
231320
}
232321
}
233322

234-
protected static PdfArray getNChannelCsArray(PdfArray names, PdfObject alternateSpace, PdfObject tintTransform, PdfDictionary attributes) {
323+
324+
protected static PdfArray getNChannelCsArray(PdfArray names, PdfObject alternateSpace, PdfObject tintTransform,
325+
PdfDictionary attributes) {
235326
PdfArray nChannel = getDeviceNCsArray(names, alternateSpace, tintTransform);
236327
nChannel.add(attributes);
237328
return nChannel;
@@ -242,11 +333,6 @@ protected static PdfArray getNChannelCsArray(PdfArray names, PdfObject alternate
242333
public static class Pattern extends PdfColorSpace {
243334

244335

245-
@Override
246-
protected boolean isWrappedObjectMustBeIndirect() {
247-
return false;
248-
}
249-
250336
public Pattern() {
251337
super(PdfName.Pattern);
252338
}
@@ -258,12 +344,25 @@ protected Pattern(PdfObject pdfObj) {
258344
@Override
259345
public int getNumberOfComponents() {
260346
return 0;
347+
}
348+
349+
@Override
350+
protected boolean isWrappedObjectMustBeIndirect() {
351+
return false;
261352
}
262353
}
263354

264355
public static class UncoloredTilingPattern extends Pattern {
265356

266357

358+
public UncoloredTilingPattern(PdfArray pdfObject) {
359+
super(pdfObject);
360+
}
361+
362+
public UncoloredTilingPattern(PdfColorSpace underlyingColorSpace) {
363+
super(new PdfArray(Arrays.asList(PdfName.Pattern, underlyingColorSpace.getPdfObject())));
364+
}
365+
267366
/**
268367
* To manually flush a {@code PdfObject} behind this wrapper, you have to ensure
269368
* that this object is added to the document, i.e. it has an indirect reference.
@@ -276,26 +375,18 @@ public void flush() {
276375
super.flush();
277376
}
278377

279-
@Override
280-
protected boolean isWrappedObjectMustBeIndirect() {
281-
return true;
282-
}
283-
284-
public UncoloredTilingPattern(PdfArray pdfObject) {
285-
super(pdfObject);
286-
}
287-
288-
public UncoloredTilingPattern(PdfColorSpace underlyingColorSpace) {
289-
super(new PdfArray(Arrays.asList(PdfName.Pattern, underlyingColorSpace.getPdfObject())));
378+
public PdfColorSpace getUnderlyingColorSpace() {
379+
return PdfColorSpace.makeColorSpace(((PdfArray) getPdfObject()).get(1));
290380
}
291381

292382
@Override
293383
public int getNumberOfComponents() {
294384
return PdfColorSpace.makeColorSpace(((PdfArray) getPdfObject()).get(1)).getNumberOfComponents();
295385
}
296386

297-
public PdfColorSpace getUnderlyingColorSpace() {
298-
return PdfColorSpace.makeColorSpace(((PdfArray) getPdfObject()).get(1));
387+
@Override
388+
protected boolean isWrappedObjectMustBeIndirect() {
389+
return true;
299390
}
300391
}
301392

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ This file is part of the iText (R) project.
6565
*/
6666
public abstract class AbstractPdfFunction<T extends PdfDictionary> extends PdfObjectWrapper<T> implements IPdfFunction {
6767

68-
final private int functionType;
68+
private final int functionType;
6969
private double[] domain;
7070
private double[] range;
7171

@@ -211,6 +211,10 @@ public double[] getRange() {
211211
*/
212212
@Override
213213
public void setRange(double[] value) {
214+
if (value == null) {
215+
getPdfObject().remove(PdfName.Range);
216+
return;
217+
}
214218
range = Arrays.copyOf(value, value.length);
215219
getPdfObject().put(PdfName.Range, new PdfArray(range));
216220
}

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

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,49 @@
1+
/*
2+
3+
This file is part of the iText (R) project.
4+
Copyright (c) 1998-2022 iText Group NV
5+
Authors: Bruno Lowagie, Paulo Soares, et al.
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License version 3
9+
as published by the Free Software Foundation with the addition of the
10+
following permission added to Section 15 as permitted in Section 7(a):
11+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
12+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
13+
OF THIRD PARTY RIGHTS
14+
15+
This program is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17+
or FITNESS FOR A PARTICULAR PURPOSE.
18+
See the GNU Affero General Public License for more details.
19+
You should have received a copy of the GNU Affero General Public License
20+
along with this program; if not, see http://www.gnu.org/licenses or write to
21+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
23+
http://itextpdf.com/terms-of-use/
24+
25+
The interactive user interfaces in modified source and object code versions
26+
of this program must display Appropriate Legal Notices, as required under
27+
Section 5 of the GNU Affero General Public License.
28+
29+
In accordance with Section 7(b) of the GNU Affero General Public License,
30+
a covered work must retain the producer line in every PDF that is created
31+
or manipulated using iText.
32+
33+
You can be released from the requirements of the license by purchasing
34+
a commercial license. Buying such a license is mandatory as soon as you
35+
develop commercial activities involving the iText software without
36+
disclosing the source code of your own applications.
37+
These activities include: offering paid services to customers as an ASP,
38+
serving PDFs on the fly in a web application, shipping iText with a closed
39+
source product.
40+
41+
For more information, please contact iText Software Corp. at this
42+
43+
*/
144
package com.itextpdf.kernel.pdf.function;
245

346
import java.io.IOException;
4-
import java.util.Objects;
5-
import java.util.function.Function;
647

748
@FunctionalInterface
849
public interface IInputConversionFunction {

0 commit comments

Comments
 (0)