Skip to content

Commit c52e807

Browse files
committed
Add default colors for 1D barcode images
Feature added to mitigate potential NPE if null is passed to createAwtImage(). Refactored tests to use new ColorConstants class.
1 parent 6bfec14 commit c52e807

File tree

9 files changed

+19
-31
lines changed

9 files changed

+19
-31
lines changed

barcodes/src/main/java/com/itextpdf/barcodes/Barcode128.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,8 @@ public void setCode(String code) {
713713
*/
714714
@Override
715715
public java.awt.Image createAwtImage(java.awt.Color foreground, java.awt.Color background) {
716-
int f = foreground.getRGB();
717-
int g = background.getRGB();
716+
int f = (foreground == null) ? DEFAULT_BAR_FOREGROUND_COLOR.getRGB() : foreground.getRGB();
717+
int g = (background == null) ? DEFAULT_BAR_BACKGROUND_COLOR.getRGB() : background.getRGB();
718718
java.awt.Canvas canvas = new java.awt.Canvas();
719719
String bCode;
720720
if (codeType == CODE128_RAW) {

barcodes/src/main/java/com/itextpdf/barcodes/Barcode1D.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@ public void setAltText(String altText) {
502502
* Creates a <CODE>java.awt.Image</CODE>. This image only
503503
* contains the bars without any text.
504504
*
505-
* @param foreground the color of the bars
506-
* @param background the color of the background
505+
* @param foreground the color of the bars. If <CODE>null</CODE> defaults to {@link Barcode1D#DEFAULT_BAR_FOREGROUND_COLOR}
506+
* @param background the color of the background. If <CODE>null</CODE> defaults to {@link Barcode1D#DEFAULT_BAR_BACKGROUND_COLOR}
507507
* @return the image
508508
*/
509509
public abstract java.awt.Image createAwtImage(java.awt.Color foreground, java.awt.Color background);

barcodes/src/main/java/com/itextpdf/barcodes/Barcode39.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ public Rectangle placeBarcode(PdfCanvas canvas, Color barColor, Color textColor)
384384
*/
385385
@Override
386386
public Image createAwtImage(java.awt.Color foreground, java.awt.Color background) {
387-
int f = foreground.getRGB();
388-
int g = background.getRGB();
387+
int f = (foreground == null) ? DEFAULT_BAR_FOREGROUND_COLOR.getRGB() : foreground.getRGB();
388+
int g = (background == null) ? DEFAULT_BAR_BACKGROUND_COLOR.getRGB() : background.getRGB();
389389
java.awt.Canvas canvas = new java.awt.Canvas();
390390
String bCode = code;
391391
if (extended) {

barcodes/src/main/java/com/itextpdf/barcodes/BarcodeCodabar.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ public Rectangle placeBarcode(PdfCanvas canvas, Color barColor, Color textColor)
313313
* @return the image
314314
*/
315315
public java.awt.Image createAwtImage(java.awt.Color foreground, java.awt.Color background) {
316-
int f = foreground.getRGB();
317-
int g = background.getRGB();
316+
int f = (foreground == null) ? DEFAULT_BAR_FOREGROUND_COLOR.getRGB() : foreground.getRGB();
317+
int g = (background == null) ? DEFAULT_BAR_BACKGROUND_COLOR.getRGB() : background.getRGB();
318318
java.awt.Canvas canvas = new java.awt.Canvas();
319319

320320
byte[] bars = getBarsCodabar(generateChecksum ? calculateChecksum(code) : code);

barcodes/src/main/java/com/itextpdf/barcodes/BarcodeEAN.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,8 @@ public Rectangle placeBarcode(PdfCanvas canvas, Color barColor, Color textColor)
731731
*/
732732
@Override
733733
public Image createAwtImage(java.awt.Color foreground, java.awt.Color background) {
734-
int f = foreground.getRGB();
735-
int g = background.getRGB();
734+
int f = (foreground == null) ? DEFAULT_BAR_FOREGROUND_COLOR.getRGB() : foreground.getRGB();
735+
int g = (background == null) ? DEFAULT_BAR_BACKGROUND_COLOR.getRGB() : background.getRGB();
736736
java.awt.Canvas canvas = new java.awt.Canvas();
737737

738738
int width;

barcodes/src/main/java/com/itextpdf/barcodes/BarcodeInter25.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ public Rectangle placeBarcode(PdfCanvas canvas, Color barColor, Color textColor)
331331
*/
332332
@Override
333333
public Image createAwtImage(java.awt.Color foreground, java.awt.Color background) {
334-
int f = foreground.getRGB();
335-
int g = background.getRGB();
334+
int f = (foreground == null) ? DEFAULT_BAR_FOREGROUND_COLOR.getRGB() : foreground.getRGB();
335+
int g = (background == null) ? DEFAULT_BAR_BACKGROUND_COLOR.getRGB() : background.getRGB();
336336
java.awt.Canvas canvas = new java.awt.Canvas();
337337
String bCode = keepNumbers(code);
338338
if (generateChecksum) {

barcodes/src/main/java/com/itextpdf/barcodes/BarcodeMSI.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -260,20 +260,8 @@ public Rectangle placeBarcode(PdfCanvas canvas, Color barColor, Color textColor)
260260
*/
261261
@Override
262262
public Image createAwtImage(java.awt.Color foreground, java.awt.Color background) {
263-
int foregroundColor;
264-
int backgroundColor;
265-
if (foreground == null) {
266-
foregroundColor = DEFAULT_BAR_FOREGROUND_COLOR.getRGB();
267-
} else {
268-
foregroundColor = foreground.getRGB();
269-
}
270-
271-
if (background == null) {
272-
backgroundColor = DEFAULT_BAR_BACKGROUND_COLOR.getRGB();
273-
} else {
274-
backgroundColor = background.getRGB();
275-
}
276-
263+
int foregroundColor = (foreground == null) ? DEFAULT_BAR_FOREGROUND_COLOR.getRGB() : foreground.getRGB();
264+
int backgroundColor = (background == null) ? DEFAULT_BAR_BACKGROUND_COLOR.getRGB() : background.getRGB();
277265
java.awt.Canvas canvas = new java.awt.Canvas();
278266
String bCode = this.code;
279267
if (this.generateChecksum) {

barcodes/src/main/java/com/itextpdf/barcodes/BarcodePostnet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ public Rectangle placeBarcode(PdfCanvas canvas, Color barColor, Color textColor)
139139

140140
@Override
141141
public Image createAwtImage(java.awt.Color foreground, java.awt.Color background) {
142-
int f = foreground.getRGB();
143-
int g = background.getRGB();
142+
int f = (foreground == null) ? DEFAULT_BAR_FOREGROUND_COLOR.getRGB() : foreground.getRGB();
143+
int g = (background == null) ? DEFAULT_BAR_BACKGROUND_COLOR.getRGB() : background.getRGB();
144144
java.awt.Canvas canvas = new java.awt.Canvas();
145145
int barWidth = (int)x;
146146
if (barWidth <= 0)

barcodes/src/test/java/com/itextpdf/barcodes/BarcodeMSITest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ This file is part of the iText (R) project.
4343
package com.itextpdf.barcodes;
4444

4545
import com.itextpdf.kernel.PdfException;
46-
import com.itextpdf.kernel.color.Color;
46+
import com.itextpdf.kernel.color.ColorConstants;
4747
import com.itextpdf.kernel.pdf.PdfDocument;
4848
import com.itextpdf.kernel.pdf.PdfPage;
4949
import com.itextpdf.kernel.pdf.PdfReader;
@@ -82,7 +82,7 @@ public void barcode01Test() throws IOException, PdfException, InterruptedExcepti
8282
barcode.setCode("123456789");
8383
barcode.setGenerateChecksum(true);
8484
barcode.setTextAlignment(Barcode1D.ALIGN_LEFT);
85-
barcode.placeBarcode(canvas, Color.BLACK, Color.WHITE);
85+
barcode.placeBarcode(canvas, ColorConstants.BLACK, ColorConstants.WHITE);
8686
document.close();
8787
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff01_"));
8888
}
@@ -98,7 +98,7 @@ public void barcode02Test() throws IOException, InterruptedException {
9898
Barcode1D barcode = new BarcodeMSI(document);
9999
barcode.setCode("9781935182610");
100100
barcode.setTextAlignment(Barcode1D.ALIGN_LEFT);
101-
barcode.placeBarcode(canvas, Color.BLACK, Color.WHITE);
101+
barcode.placeBarcode(canvas, ColorConstants.BLACK, ColorConstants.WHITE);
102102
document.close();
103103
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff02_"));
104104
}

0 commit comments

Comments
 (0)