From 47d629aa321f6b4bb2425f22b08ec24765f990da Mon Sep 17 00:00:00 2001 From: tschoemaker <=> Date: Wed, 10 Apr 2024 01:51:45 +0200 Subject: [PATCH 1/4] refactored a data clump --- .../lowagie/text/pdf/BarcodeDatamatrix.java | 50 +++++++++---------- .../lowagie/text/pdf/BarcodeDimensions.java | 42 ++++++++++++++++ 2 files changed, 66 insertions(+), 26 deletions(-) create mode 100644 openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDimensions.java diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java b/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java index 7d1d99b30..3e1b0785b 100644 --- a/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java @@ -160,10 +160,7 @@ public class BarcodeDatamatrix { private int extOut; private short[] place; private byte[] image; - private int height; - private int width; - private int ws; - private int options; + private BarcodeDimensions barcodeDimensions; /** * Creates an instance of this class. @@ -589,8 +586,9 @@ private void setBit(int x, int y, int xByte) { private void draw(byte[] data, int dataSize, DmParams dm) { int i, j, p, x, y, xs, ys, z; - int xByte = (dm.width + ws * 2 + 7) / 8; + int xByte = (dm.width + barcodeDimensions.getWs() * 2 + 7) / 8; Arrays.fill(image, (byte) 0); + int ws = barcodeDimensions.getWs(); //alignment patterns //dotted horizontal line for (i = ws; i < dm.height + ws; i += dm.heightSection) { @@ -633,7 +631,7 @@ private void draw(byte[] data, int dataSize, DmParams dm) { private int processExtensions(byte[] text, int textOffset, int textSize, byte[] data) { int order, ptrIn, ptrOut, eci, fn, ft, fi, c; - if ((options & DM_EXTENSION) == 0) { + if ((this.barcodeDimensions.getOptions() & DM_EXTENSION) == 0) { return 0; } order = 0; @@ -767,10 +765,10 @@ public int generate(byte[] text, int textOffset, int textSize) { return DM_ERROR_EXTENSION; } e = -1; - if (height == 0 || width == 0) { + if (barcodeDimensions.getHeight() == 0 || barcodeDimensions.getWidth() == 0) { last = dmSizes[dmSizes.length - 1]; e = getEncodation(text, textOffset + extOut, textSize - extOut, data, extCount, last.dataSize - extCount, - options, false); + barcodeDimensions.getOptions(), false); if (e < 0) { return DM_ERROR_TEXT_TOO_BIG; } @@ -781,11 +779,11 @@ public int generate(byte[] text, int textOffset, int textSize) { } } dm = dmSizes[k]; - height = dm.height; - width = dm.width; + barcodeDimensions.setHeight(dm.height); + barcodeDimensions.setWidth(dm.width); } else { for (k = 0; k < dmSizes.length; ++k) { - if (height == dmSizes[k].height && width == dmSizes[k].width) { + if (barcodeDimensions.getHeight() == dmSizes[k].height && barcodeDimensions.getWidth() == dmSizes[k].width) { break; } } @@ -794,16 +792,16 @@ public int generate(byte[] text, int textOffset, int textSize) { } dm = dmSizes[k]; e = getEncodation(text, textOffset + extOut, textSize - extOut, data, extCount, dm.dataSize - extCount, - options, true); + barcodeDimensions.getOptions(), true); if (e < 0) { return DM_ERROR_TEXT_TOO_BIG; } e += extCount; } - if ((options & DM_TEST) != 0) { + if ((barcodeDimensions.getOptions() & DM_TEST) != 0) { return DM_NO_ERROR; } - image = new byte[(((dm.width + 2 * ws) + 7) / 8) * (dm.height + 2 * ws)]; + image = new byte[(((dm.width + 2 * barcodeDimensions.getWs()) + 7) / 8) * (dm.height + 2 * barcodeDimensions.getWs())]; makePadding(data, e, dm.dataSize - e); place = Placement.doPlacement(dm.height - (dm.height / dm.heightSection * 2), dm.width - (dm.width / dm.widthSection * 2)); @@ -824,8 +822,8 @@ public Image createImage() throws BadElementException { if (image == null) { return null; } - byte[] g4 = CCITTG4Encoder.compress(image, width + 2 * ws, height + 2 * ws); - return Image.getInstance(width + 2 * ws, height + 2 * ws, false, Image.CCITTG4, 0, g4, null); + byte[] g4 = CCITTG4Encoder.compress(image, barcodeDimensions.getWidth() + 2 * barcodeDimensions.getWs(), barcodeDimensions.getHeight() + 2 * barcodeDimensions.getWs()); + return Image.getInstance(barcodeDimensions.getWidth() + 2 * barcodeDimensions.getWs(), barcodeDimensions.getHeight() + 2 * barcodeDimensions.getWs(), false, Image.CCITTG4, 0, g4, null); } /** @@ -844,8 +842,8 @@ public java.awt.Image createAwtImage(Color foreground, Color background) { int g = background.getRGB(); Canvas canvas = new Canvas(); - int w = width + 2 * ws; - int h = height + 2 * ws; + int w = barcodeDimensions.getWidth() + 2 * barcodeDimensions.getWs(); + int h = barcodeDimensions.getHeight() + 2 * barcodeDimensions.getWs(); int[] pix = new int[w * h]; int stride = (w + 7) / 8; int ptr = 0; @@ -879,7 +877,7 @@ public byte[] getImage() { * @return the height of the barcode */ public int getHeight() { - return height; + return barcodeDimensions.getHeight(); } /** @@ -894,7 +892,7 @@ public int getHeight() { * @param height the height of the barcode */ public void setHeight(int height) { - this.height = height; + barcodeDimensions.setHeight(height); } /** @@ -904,7 +902,7 @@ public void setHeight(int height) { * @return the width of the barcode */ public int getWidth() { - return width; + return barcodeDimensions.getWidth(); } /** @@ -919,7 +917,7 @@ public int getWidth() { * @param width the width of the barcode */ public void setWidth(int width) { - this.width = width; + barcodeDimensions.setWidth(width); } /** @@ -928,7 +926,7 @@ public void setWidth(int width) { * @return the whitespace border around the barcode */ public int getWs() { - return ws; + return barcodeDimensions.getWs(); } /** @@ -937,7 +935,7 @@ public int getWs() { * @param ws the whitespace border around the barcode */ public void setWs(int ws) { - this.ws = ws; + barcodeDimensions.setWs(ws); } /** @@ -946,7 +944,7 @@ public void setWs(int ws) { * @return the barcode options */ public int getOptions() { - return options; + return barcodeDimensions.getOptions(); } /** @@ -972,7 +970,7 @@ public int getOptions() { * @param options the barcode options */ public void setOptions(int options) { - this.options = options; + barcodeDimensions.setOptions(options); } private static class DmParams { diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDimensions.java b/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDimensions.java new file mode 100644 index 000000000..4cfc67e6b --- /dev/null +++ b/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDimensions.java @@ -0,0 +1,42 @@ +package com.lowagie.text.pdf; + +public class BarcodeDimensions { + + private int height; + private int width; + private int ws; + private int options; + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getWs() { + return ws; + } + + public void setWs(int ws) { + this.ws = ws; + } + + public int getOptions() { + return options; + } + + public void setOptions(int options) { + this.options = options; + } + +} \ No newline at end of file From 93b36c5ac389c9f77487a6150da4f266d8d86880 Mon Sep 17 00:00:00 2001 From: tschoemaker <=> Date: Wed, 10 Apr 2024 22:29:53 +0200 Subject: [PATCH 2/4] improved readability and removed options from BarCodeDimensions --- .../lowagie/text/pdf/BarcodeDatamatrix.java | 56 ++++++++++--------- .../lowagie/text/pdf/BarcodeDimensions.java | 22 ++++---- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java b/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java index 3e1b0785b..ed727ecd8 100644 --- a/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java @@ -160,7 +160,8 @@ public class BarcodeDatamatrix { private int extOut; private short[] place; private byte[] image; - private BarcodeDimensions barcodeDimensions; + private BarcodeDimensions dimensions; + private int options; /** * Creates an instance of this class. @@ -586,9 +587,10 @@ private void setBit(int x, int y, int xByte) { private void draw(byte[] data, int dataSize, DmParams dm) { int i, j, p, x, y, xs, ys, z; - int xByte = (dm.width + barcodeDimensions.getWs() * 2 + 7) / 8; + int ws = dimensions.getBorder(); + int xByte = (dm.width + ws * 2 + 7) / 8; Arrays.fill(image, (byte) 0); - int ws = barcodeDimensions.getWs(); + //alignment patterns //dotted horizontal line for (i = ws; i < dm.height + ws; i += dm.heightSection) { @@ -631,7 +633,7 @@ private void draw(byte[] data, int dataSize, DmParams dm) { private int processExtensions(byte[] text, int textOffset, int textSize, byte[] data) { int order, ptrIn, ptrOut, eci, fn, ft, fi, c; - if ((this.barcodeDimensions.getOptions() & DM_EXTENSION) == 0) { + if ((this.options & DM_EXTENSION) == 0) { return 0; } order = 0; @@ -765,10 +767,10 @@ public int generate(byte[] text, int textOffset, int textSize) { return DM_ERROR_EXTENSION; } e = -1; - if (barcodeDimensions.getHeight() == 0 || barcodeDimensions.getWidth() == 0) { + if (dimensions.getHeight() == 0 || dimensions.getWidth() == 0) { last = dmSizes[dmSizes.length - 1]; e = getEncodation(text, textOffset + extOut, textSize - extOut, data, extCount, last.dataSize - extCount, - barcodeDimensions.getOptions(), false); + this.options, false); if (e < 0) { return DM_ERROR_TEXT_TOO_BIG; } @@ -779,11 +781,11 @@ public int generate(byte[] text, int textOffset, int textSize) { } } dm = dmSizes[k]; - barcodeDimensions.setHeight(dm.height); - barcodeDimensions.setWidth(dm.width); + dimensions.setHeight(dm.height); + dimensions.setWidth(dm.width); } else { for (k = 0; k < dmSizes.length; ++k) { - if (barcodeDimensions.getHeight() == dmSizes[k].height && barcodeDimensions.getWidth() == dmSizes[k].width) { + if (dimensions.getHeight() == dmSizes[k].height && dimensions.getWidth() == dmSizes[k].width) { break; } } @@ -792,16 +794,16 @@ public int generate(byte[] text, int textOffset, int textSize) { } dm = dmSizes[k]; e = getEncodation(text, textOffset + extOut, textSize - extOut, data, extCount, dm.dataSize - extCount, - barcodeDimensions.getOptions(), true); + this.options, true); if (e < 0) { return DM_ERROR_TEXT_TOO_BIG; } e += extCount; } - if ((barcodeDimensions.getOptions() & DM_TEST) != 0) { + if ((this.options & DM_TEST) != 0) { return DM_NO_ERROR; } - image = new byte[(((dm.width + 2 * barcodeDimensions.getWs()) + 7) / 8) * (dm.height + 2 * barcodeDimensions.getWs())]; + image = new byte[(((dm.width + 2 * dimensions.getBorder()) + 7) / 8) * (dm.height + 2 * dimensions.getBorder())]; makePadding(data, e, dm.dataSize - e); place = Placement.doPlacement(dm.height - (dm.height / dm.heightSection * 2), dm.width - (dm.width / dm.widthSection * 2)); @@ -822,8 +824,10 @@ public Image createImage() throws BadElementException { if (image == null) { return null; } - byte[] g4 = CCITTG4Encoder.compress(image, barcodeDimensions.getWidth() + 2 * barcodeDimensions.getWs(), barcodeDimensions.getHeight() + 2 * barcodeDimensions.getWs()); - return Image.getInstance(barcodeDimensions.getWidth() + 2 * barcodeDimensions.getWs(), barcodeDimensions.getHeight() + 2 * barcodeDimensions.getWs(), false, Image.CCITTG4, 0, g4, null); + int width=dimensions.getWidth() + 2 * dimensions.getBorder(); + int height=dimensions.getHeight() + 2 * dimensions.getBorder(); + byte[] g4 = CCITTG4Encoder.compress(image, width,height); + return Image.getInstance(width,height, false, Image.CCITTG4, 0, g4, null); } /** @@ -842,8 +846,8 @@ public java.awt.Image createAwtImage(Color foreground, Color background) { int g = background.getRGB(); Canvas canvas = new Canvas(); - int w = barcodeDimensions.getWidth() + 2 * barcodeDimensions.getWs(); - int h = barcodeDimensions.getHeight() + 2 * barcodeDimensions.getWs(); + int w = dimensions.getWidth() + 2 * dimensions.getBorder(); + int h = dimensions.getHeight() + 2 * dimensions.getBorder(); int[] pix = new int[w * h]; int stride = (w + 7) / 8; int ptr = 0; @@ -877,7 +881,7 @@ public byte[] getImage() { * @return the height of the barcode */ public int getHeight() { - return barcodeDimensions.getHeight(); + return dimensions.getHeight(); } /** @@ -892,7 +896,7 @@ public int getHeight() { * @param height the height of the barcode */ public void setHeight(int height) { - barcodeDimensions.setHeight(height); + dimensions.setHeight(height); } /** @@ -902,7 +906,7 @@ public void setHeight(int height) { * @return the width of the barcode */ public int getWidth() { - return barcodeDimensions.getWidth(); + return dimensions.getWidth(); } /** @@ -917,7 +921,7 @@ public int getWidth() { * @param width the width of the barcode */ public void setWidth(int width) { - barcodeDimensions.setWidth(width); + dimensions.setWidth(width); } /** @@ -925,8 +929,8 @@ public void setWidth(int width) { * * @return the whitespace border around the barcode */ - public int getWs() { - return barcodeDimensions.getWs(); + public int getBorder() { + return dimensions.getBorder(); } /** @@ -934,8 +938,8 @@ public int getWs() { * * @param ws the whitespace border around the barcode */ - public void setWs(int ws) { - barcodeDimensions.setWs(ws); + public void setBorder(int border) { + dimensions.setBorder(border); } /** @@ -944,7 +948,7 @@ public void setWs(int ws) { * @return the barcode options */ public int getOptions() { - return barcodeDimensions.getOptions(); + return options; } /** @@ -970,7 +974,7 @@ public int getOptions() { * @param options the barcode options */ public void setOptions(int options) { - barcodeDimensions.setOptions(options); + this.options = options; } private static class DmParams { diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDimensions.java b/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDimensions.java index 4cfc67e6b..672090dd2 100644 --- a/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDimensions.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDimensions.java @@ -4,8 +4,7 @@ public class BarcodeDimensions { private int height; private int width; - private int ws; - private int options; + private int border; public int getHeight() { return height; @@ -23,20 +22,23 @@ public void setWidth(int width) { this.width = width; } - public int getWs() { - return ws; + public int getBorder() { + return border; } - public void setWs(int ws) { - this.ws = ws; + public void setBorder(int border) { + this.border = border; } - public int getOptions() { - return options; + public BarcodeDimensions(int width, int height, int border) { + this.width = width; + this.height = height; + this.border = border; } - public void setOptions(int options) { - this.options = options; + public BarcodeDimensions(){ + this(0, 0, 0); } + } \ No newline at end of file From 4e590c11f90f1e53f3ea8a5e69b70b3b5a9f6274 Mon Sep 17 00:00:00 2001 From: tschoemaker <=> Date: Wed, 10 Apr 2024 22:31:42 +0200 Subject: [PATCH 3/4] added whitespaces --- .../main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java b/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java index ed727ecd8..6586c2e9e 100644 --- a/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java @@ -824,10 +824,10 @@ public Image createImage() throws BadElementException { if (image == null) { return null; } - int width=dimensions.getWidth() + 2 * dimensions.getBorder(); - int height=dimensions.getHeight() + 2 * dimensions.getBorder(); - byte[] g4 = CCITTG4Encoder.compress(image, width,height); - return Image.getInstance(width,height, false, Image.CCITTG4, 0, g4, null); + int width = dimensions.getWidth() + 2 * dimensions.getBorder(); + int height = dimensions.getHeight() + 2 * dimensions.getBorder(); + byte[] g4 = CCITTG4Encoder.compress(image, width, height); + return Image.getInstance(width, height, false, Image.CCITTG4, 0, g4, null); } /** From 182af6b7322cb99df6e41d54a0ebf58db3b4cd00 Mon Sep 17 00:00:00 2001 From: tschoemaker <=> Date: Wed, 10 Apr 2024 22:33:46 +0200 Subject: [PATCH 4/4] renamed @param --- .../src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java | 2 +- .../src/main/java/com/lowagie/text/pdf/BarcodeDimensions.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java b/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java index 6586c2e9e..365ef95c0 100644 --- a/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java @@ -936,7 +936,7 @@ public int getBorder() { /** * Sets the whitespace border around the barcode. * - * @param ws the whitespace border around the barcode + * @param border the whitespace border around the barcode */ public void setBorder(int border) { dimensions.setBorder(border); diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDimensions.java b/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDimensions.java index 672090dd2..7ff62ff53 100644 --- a/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDimensions.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDimensions.java @@ -36,7 +36,7 @@ public BarcodeDimensions(int width, int height, int border) { this.border = border; } - public BarcodeDimensions(){ + public BarcodeDimensions() { this(0, 0, 0); }