Skip to content

Commit 2fc204d

Browse files
committed
port SHA-1: 2dfb2054af1e1872ac5354e6d8218931fb88e021
* Fixing issue #1831 (#1879) * throw a more explicit exception when trying to PDF417/TEXT encode something outside of 0...255 * refactor PDF417HighLevelEncoder to avoid code duplication extend UT to new method PDF417HighLevelEncoder#checkCharset * fix javadoc typo make UT more stringent on PDF417HighLevelEncoder#checkCharset * restrict TEXT to 0...127 test with CP437 and Greek chars * reinstate testEncodeAuto UT * refactor testEncodeAuto UT * address codacy findings * formatting * fix issue #1831 make PDF417#determineDimensions to enable finer UT add UT coverage to validate fix for #1831 * fix javadoc for PDF417#determineDimensions remove stacktrace when UT fails * make UT Java 8 compliant
1 parent 6f4634e commit 2fc204d

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

Source/lib/Properties/AssemblyInfo.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
#if NETFX_CORE && !WINDOWS_UWP
5353
[assembly: AssemblyTitle("zxing.net for windows rt")]
5454
#endif
55-
[assembly: AssemblyDescription("port of the java based barcode scanning library for .net (java zxing 22.10.2024 15:24:36)")]
55+
[assembly: AssemblyDescription("port of the java based barcode scanning library for .net (java zxing 17.11.2024 17:25:04)")]
5656
[assembly: AssemblyCompany("ZXing.Net Development")]
5757
[assembly: AssemblyProduct("ZXing.Net")]
5858
[assembly: AssemblyCopyright("Copyright © 2012")]

Source/lib/pdf417/encoder/PDF417.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ private void appendMacroOptionalField(PDF417OptionalMacroFields field, string va
970970
/// <param name="shortDimension">The short dimension of the barcode, used for rows</param>
971971
/// <param name="aspectRatio">The height of a row, will alter this parameter if aspectRatio>4 (aspectRatio==AUTO)</param>
972972
/// <returns>dimension object containing cols as width and rows as height</returns>
973-
private int[] determineDimensions(int sourceCodeWords, int errorCorrectionCodeWords, int longDimension, int shortDimension, ref int aspectRatio)
973+
internal int[] determineDimensions(int sourceCodeWords, int errorCorrectionCodeWords, int longDimension, int shortDimension, ref int aspectRatio)
974974
{
975975
int startWidth = BarcodeMatrix.COLUMN_WIDTH * 2;
976976
int endWidth = (compact ? 0 : 2) * BarcodeMatrix.COLUMN_WIDTH + 1;

Source/test/src/pdf417/encoder/PDF417EncoderTestCase.cs

+47
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
using NUnit.Framework;
2121
using ZXing.Common;
22+
using static ZXing.Datamatrix.Encoder.MinimalEncoder;
2223

2324
namespace ZXing.PDF417.Internal.Test
2425
{
@@ -170,5 +171,51 @@ public void testEncodeEmptyString()
170171
{
171172
Assert.Throws<ArgumentException>(() => PDF417HighLevelEncoder.encodeHighLevel("", Compaction.AUTO, null, false, false));
172173
}
174+
175+
176+
[Test]
177+
public void testDimensions()
178+
{
179+
// test https://github.com/zxing/zxing/issues/1831
180+
String input = "0000000001000000022200000003330444400888888881010101010";
181+
testDimensions(input, new Dimensions(1, 30, 7, 10));
182+
testDimensions(input, new Dimensions(1, 40, 1, 7));
183+
testDimensions(input, new Dimensions(10, 30, 1, 5));
184+
testDimensions(input, new Dimensions(1, 3, 1, 15));
185+
testDimensions(input, new Dimensions(5, 30, 7, 7));
186+
testDimensions(input, new Dimensions(12, 12, 1, 17));
187+
testDimensions(input, new Dimensions(1, 30, 7, 8));
188+
}
189+
190+
public static void testDimensions(String input, Dimensions dimensions)
191+
{
192+
var sourceCodeWords = 20;
193+
var errorCorrectionCodeWords = 8;
194+
195+
//var calculated = PDF417.determineDimensions(dimensions.MinCols, dimensions.MaxCols,
196+
// dimensions.MinRows, dimensions.MaxRows, sourceCodeWords, errorCorrectionCodeWords);
197+
var aspectRatio = 4;
198+
var pdf417 = new PDF417();
199+
pdf417.setDimensions(dimensions.MaxCols, dimensions.MinCols, dimensions.MaxRows, dimensions.MinRows);
200+
var calculated = pdf417.determineDimensions(sourceCodeWords, errorCorrectionCodeWords, 0, 0, ref aspectRatio);
201+
202+
Assert.That(calculated, Is.Not.Null);
203+
Assert.That(calculated.Length, Is.EqualTo(2));
204+
Assert.That(dimensions.MinCols <= calculated[0], Is.True);
205+
Assert.That(dimensions.MaxCols >= calculated[0], Is.True);
206+
Assert.That(dimensions.MinRows <= calculated[1], Is.True);
207+
Assert.That(dimensions.MaxRows >= calculated[1], Is.True);
208+
Assert.That(generatePDF417BitMatrix(input, 371, null, dimensions), Is.Not.Null);
209+
}
210+
211+
public static BitMatrix generatePDF417BitMatrix(String barcodeText, int width, int? heightRequested, Dimensions dimensions)
212+
{
213+
var barcodeWriter = new PDF417Writer();
214+
var height = heightRequested == null ? width / 4 : heightRequested.Value;
215+
var hints = new System.Collections.Generic.Dictionary<EncodeHintType, object>();
216+
hints[EncodeHintType.MARGIN] = 0;
217+
hints[EncodeHintType.PDF417_DIMENSIONS] = dimensions;
218+
return barcodeWriter.encode(barcodeText, BarcodeFormat.PDF_417, width, height, hints);
219+
}
173220
}
174221
}

0 commit comments

Comments
 (0)