Skip to content

Commit 963e936

Browse files
committed
Wrap negative offsets in datamatrix
Add tests for IndexOutOfBounds Update width&height samples in javadocs. DEVSIX-1845
1 parent 81ee78e commit 963e936

File tree

2 files changed

+56
-27
lines changed

2 files changed

+56
-27
lines changed

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

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -402,24 +402,25 @@ public int getHeight() {
402402
}
403403

404404
/**
405-
* Sets the height of the barcode. If the height is zero it will be calculated. This height doesn't include the whitespace border, if any.
405+
* Sets the height of the barcode. If the height is zero it will be calculated.
406+
* This height doesn't include the whitespace border, if any.
406407
*
407-
* The allowed dimensions are (height, width):<p>
408+
* The allowed dimensions are (width, height):<p>
408409
* 10, 10<br>
409410
* 12, 12<br>
410-
* 8, 18<br>
411+
* 18, 8<br>
411412
* 14, 14<br>
412-
* 8, 32<br>
413+
* 32, 8<br>
413414
* 16, 16<br>
414-
* 12, 26<br>
415+
* 26, 12<br>
415416
* 18, 18<br>
416417
* 20, 20<br>
417-
* 12, 36<br>
418+
* 36, 12<br>
418419
* 22, 22<br>
419-
* 16, 36<br>
420+
* 36, 16<br>
420421
* 24, 24<br>
421422
* 26, 26<br>
422-
* 16, 48<br>
423+
* 48, 16<br>
423424
* 32, 32<br>
424425
* 36, 36<br>
425426
* 40, 40<br>
@@ -453,24 +454,25 @@ public int getWidth() {
453454
}
454455

455456
/**
456-
* Sets the width of the barcode. If the width is zero it will be calculated. This width doesn't include the whitespace border, if any.
457+
* Sets the width of the barcode. If the width is zero it will be calculated.
458+
* This width doesn't include the whitespace border, if any.
457459
*
458-
* The allowed dimensions are (height, width):<p>
460+
* The allowed dimensions are (width, height):<p>
459461
* 10, 10<br>
460462
* 12, 12<br>
461-
* 8, 18<br>
463+
* 18, 8<br>
462464
* 14, 14<br>
463-
* 8, 32<br>
465+
* 32, 8<br>
464466
* 16, 16<br>
465-
* 12, 26<br>
467+
* 26, 12<br>
466468
* 18, 18<br>
467469
* 20, 20<br>
468-
* 12, 36<br>
470+
* 36, 12<br>
469471
* 22, 22<br>
470-
* 16, 36<br>
472+
* 36, 16<br>
471473
* 24, 24<br>
472474
* 26, 26<br>
473-
* 16, 48<br>
475+
* 48, 16<br>
474476
* 32, 32<br>
475477
* 36, 36<br>
476478
* 40, 40<br>
@@ -1072,7 +1074,7 @@ private int C40OrTextEncodation(byte[] text, int textOffset, int textLength, byt
10721074
if (j == 1)
10731075
dataOffsetNew = requiredCapacityForASCII;
10741076
}
1075-
addLatch = unlatch < 0 ? true : (dataOffset - requiredCapacityForASCII != unlatch);
1077+
addLatch = (unlatch < 0) || ((dataOffset - requiredCapacityForASCII) != unlatch);
10761078
if (requiredCapacityForC40orText % 3 == 0 &&
10771079
requiredCapacityForC40orText / 3 * 2 + (addLatch ? 2 : 0) < requiredCapacityForASCII) {
10781080
usingASCII = false;
@@ -1086,15 +1088,17 @@ private int C40OrTextEncodation(byte[] text, int textOffset, int textLength, byt
10861088
i--;
10871089
}
10881090
}
1089-
} else if (symbolIndex != -1)
1091+
} else if (symbolIndex != -1) {
10901092
usingASCII = true;
1091-
if (usingASCII)
1093+
}
1094+
if (dataOffset < 0) {
1095+
return -1;
1096+
}
1097+
if (usingASCII) {
10921098
return asciiEncodation(text, textOffset, 1, data, dataOffset, dataLength, prevEnc == mode ? 1 : -1, DM_ASCII, origDataOffset);
1099+
}
10931100
if (addLatch) {
1094-
if (c40)
1095-
data[dataOffset + ptrOut++] = LATCH_C40;
1096-
else
1097-
data[dataOffset + ptrOut++] = LATCH_TEXT;
1101+
data[dataOffset + ptrOut++] = c40 ? LATCH_C40 : LATCH_TEXT;
10981102
}
10991103
int[] enc = new int[textLength * 4 + 10];
11001104
encPtr = 0;
@@ -1396,5 +1400,4 @@ else if (eci < 16383) {
13961400
}
13971401
return -1;
13981402
}
1399-
14001403
}

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,15 @@ This file is part of the iText (R) project.
5252
import com.itextpdf.kernel.utils.CompareTool;
5353
import com.itextpdf.test.ExtendedITextTest;
5454
import com.itextpdf.test.annotations.type.IntegrationTest;
55-
56-
import java.io.IOException;
57-
5855
import org.junit.Assert;
5956
import org.junit.BeforeClass;
6057
import org.junit.Rule;
6158
import org.junit.Test;
6259
import org.junit.experimental.categories.Category;
6360
import org.junit.rules.ExpectedException;
6461

62+
import java.io.IOException;
63+
6564
@Category(IntegrationTest.class)
6665
public class BarcodeDataMatrixTest extends ExtendedITextTest {
6766

@@ -195,4 +194,31 @@ public void barcode07Test() {
195194
int result = bc.setCode(aCode);
196195
Assert.assertEquals(result, BarcodeDataMatrix.DM_ERROR_TEXT_TOO_BIG);
197196
}
197+
198+
@Test
199+
public void barcode08Test() {
200+
BarcodeDataMatrix barcodeDataMatrix = new BarcodeDataMatrix();
201+
barcodeDataMatrix.setWidth(18);
202+
barcodeDataMatrix.setHeight(18);
203+
int result = barcodeDataMatrix.setCode("AbcdFFghijklmnopqrstuWXSQ");
204+
Assert.assertEquals(BarcodeDataMatrix.DM_ERROR_TEXT_TOO_BIG, result);
205+
}
206+
207+
@Test
208+
public void barcode09Test() {
209+
BarcodeDataMatrix barcodeDataMatrix = new BarcodeDataMatrix();
210+
barcodeDataMatrix.setWidth(17);
211+
barcodeDataMatrix.setHeight(17);
212+
int result = barcodeDataMatrix.setCode("AbcdFFghijklmnopqrstuWXSQ");
213+
Assert.assertEquals(BarcodeDataMatrix.DM_ERROR_INVALID_SQUARE, result);
214+
}
215+
216+
@Test
217+
public void barcode10Test() {
218+
BarcodeDataMatrix barcodeDataMatrix = new BarcodeDataMatrix();
219+
barcodeDataMatrix.setWidth(26);
220+
barcodeDataMatrix.setHeight(12);
221+
int result = barcodeDataMatrix.setCode("AbcdFFghijklmnopqrstuWXSQ");
222+
Assert.assertEquals(BarcodeDataMatrix.DM_ERROR_TEXT_TOO_BIG, result);
223+
}
198224
}

0 commit comments

Comments
 (0)