Skip to content

Commit

Permalink
Refactored data clumps with the help of LLMs (research project) (#1140)
Browse files Browse the repository at this point in the history
* refactored a data clump
* improved readability and removed options from BarCodeDimensions

Co-authored-by: tschoemaker <=>
  • Loading branch information
compf authored Apr 11, 2024
1 parent dfbdda2 commit 585bca9
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 25 deletions.
52 changes: 27 additions & 25 deletions openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDatamatrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ public class BarcodeDatamatrix {
private int extOut;
private short[] place;
private byte[] image;
private int height;
private int width;
private int ws;
private BarcodeDimensions dimensions;
private int options;

/**
Expand Down Expand Up @@ -589,8 +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 ws = dimensions.getBorder();
int xByte = (dm.width + ws * 2 + 7) / 8;
Arrays.fill(image, (byte) 0);

//alignment patterns
//dotted horizontal line
for (i = ws; i < dm.height + ws; i += dm.heightSection) {
Expand Down Expand Up @@ -633,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 ((options & DM_EXTENSION) == 0) {
if ((this.options & DM_EXTENSION) == 0) {
return 0;
}
order = 0;
Expand Down Expand Up @@ -767,10 +767,10 @@ public int generate(byte[] text, int textOffset, int textSize) {
return DM_ERROR_EXTENSION;
}
e = -1;
if (height == 0 || width == 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,
options, false);
this.options, false);
if (e < 0) {
return DM_ERROR_TEXT_TOO_BIG;
}
Expand All @@ -781,11 +781,11 @@ public int generate(byte[] text, int textOffset, int textSize) {
}
}
dm = dmSizes[k];
height = dm.height;
width = dm.width;
dimensions.setHeight(dm.height);
dimensions.setWidth(dm.width);
} else {
for (k = 0; k < dmSizes.length; ++k) {
if (height == dmSizes[k].height && width == dmSizes[k].width) {
if (dimensions.getHeight() == dmSizes[k].height && dimensions.getWidth() == dmSizes[k].width) {
break;
}
}
Expand All @@ -794,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,
options, true);
this.options, true);
if (e < 0) {
return DM_ERROR_TEXT_TOO_BIG;
}
e += extCount;
}
if ((options & DM_TEST) != 0) {
if ((this.options & 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 * 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));
Expand All @@ -824,8 +824,10 @@ 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);
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);
}

/**
Expand All @@ -844,8 +846,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 = 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;
Expand Down Expand Up @@ -879,7 +881,7 @@ public byte[] getImage() {
* @return the height of the barcode
*/
public int getHeight() {
return height;
return dimensions.getHeight();
}

/**
Expand All @@ -894,7 +896,7 @@ public int getHeight() {
* @param height the height of the barcode
*/
public void setHeight(int height) {
this.height = height;
dimensions.setHeight(height);
}

/**
Expand All @@ -904,7 +906,7 @@ public void setHeight(int height) {
* @return the width of the barcode
*/
public int getWidth() {
return width;
return dimensions.getWidth();
}

/**
Expand All @@ -919,25 +921,25 @@ public int getWidth() {
* @param width the width of the barcode
*/
public void setWidth(int width) {
this.width = width;
dimensions.setWidth(width);
}

/**
* Gets the whitespace border around the barcode.
*
* @return the whitespace border around the barcode
*/
public int getWs() {
return ws;
public int getBorder() {
return dimensions.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 setWs(int ws) {
this.ws = ws;
public void setBorder(int border) {
dimensions.setBorder(border);
}

/**
Expand Down
44 changes: 44 additions & 0 deletions openpdf/src/main/java/com/lowagie/text/pdf/BarcodeDimensions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.lowagie.text.pdf;

public class BarcodeDimensions {

private int height;
private int width;
private int border;

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 getBorder() {
return border;
}

public void setBorder(int border) {
this.border = border;
}

public BarcodeDimensions(int width, int height, int border) {
this.width = width;
this.height = height;
this.border = border;
}

public BarcodeDimensions() {
this(0, 0, 0);
}


}

0 comments on commit 585bca9

Please sign in to comment.