Skip to content

Commit

Permalink
port SHA-1: 2dfb2054af1e1872ac5354e6d8218931fb88e021
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
micjahn committed Dec 30, 2024
1 parent 6f4634e commit 2fc204d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Source/lib/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#if NETFX_CORE && !WINDOWS_UWP
[assembly: AssemblyTitle("zxing.net for windows rt")]
#endif
[assembly: AssemblyDescription("port of the java based barcode scanning library for .net (java zxing 22.10.2024 15:24:36)")]
[assembly: AssemblyDescription("port of the java based barcode scanning library for .net (java zxing 17.11.2024 17:25:04)")]
[assembly: AssemblyCompany("ZXing.Net Development")]
[assembly: AssemblyProduct("ZXing.Net")]
[assembly: AssemblyCopyright("Copyright © 2012")]
Expand Down
2 changes: 1 addition & 1 deletion Source/lib/pdf417/encoder/PDF417.cs
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ private void appendMacroOptionalField(PDF417OptionalMacroFields field, string va
/// <param name="shortDimension">The short dimension of the barcode, used for rows</param>
/// <param name="aspectRatio">The height of a row, will alter this parameter if aspectRatio>4 (aspectRatio==AUTO)</param>
/// <returns>dimension object containing cols as width and rows as height</returns>
private int[] determineDimensions(int sourceCodeWords, int errorCorrectionCodeWords, int longDimension, int shortDimension, ref int aspectRatio)
internal int[] determineDimensions(int sourceCodeWords, int errorCorrectionCodeWords, int longDimension, int shortDimension, ref int aspectRatio)
{
int startWidth = BarcodeMatrix.COLUMN_WIDTH * 2;
int endWidth = (compact ? 0 : 2) * BarcodeMatrix.COLUMN_WIDTH + 1;
Expand Down
47 changes: 47 additions & 0 deletions Source/test/src/pdf417/encoder/PDF417EncoderTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

using NUnit.Framework;
using ZXing.Common;
using static ZXing.Datamatrix.Encoder.MinimalEncoder;

namespace ZXing.PDF417.Internal.Test
{
Expand Down Expand Up @@ -170,5 +171,51 @@ public void testEncodeEmptyString()
{
Assert.Throws<ArgumentException>(() => PDF417HighLevelEncoder.encodeHighLevel("", Compaction.AUTO, null, false, false));
}


[Test]
public void testDimensions()
{
// test https://github.com/zxing/zxing/issues/1831
String input = "0000000001000000022200000003330444400888888881010101010";
testDimensions(input, new Dimensions(1, 30, 7, 10));
testDimensions(input, new Dimensions(1, 40, 1, 7));
testDimensions(input, new Dimensions(10, 30, 1, 5));
testDimensions(input, new Dimensions(1, 3, 1, 15));
testDimensions(input, new Dimensions(5, 30, 7, 7));
testDimensions(input, new Dimensions(12, 12, 1, 17));
testDimensions(input, new Dimensions(1, 30, 7, 8));
}

public static void testDimensions(String input, Dimensions dimensions)
{
var sourceCodeWords = 20;
var errorCorrectionCodeWords = 8;

//var calculated = PDF417.determineDimensions(dimensions.MinCols, dimensions.MaxCols,
// dimensions.MinRows, dimensions.MaxRows, sourceCodeWords, errorCorrectionCodeWords);
var aspectRatio = 4;
var pdf417 = new PDF417();
pdf417.setDimensions(dimensions.MaxCols, dimensions.MinCols, dimensions.MaxRows, dimensions.MinRows);
var calculated = pdf417.determineDimensions(sourceCodeWords, errorCorrectionCodeWords, 0, 0, ref aspectRatio);

Assert.That(calculated, Is.Not.Null);
Assert.That(calculated.Length, Is.EqualTo(2));
Assert.That(dimensions.MinCols <= calculated[0], Is.True);
Assert.That(dimensions.MaxCols >= calculated[0], Is.True);
Assert.That(dimensions.MinRows <= calculated[1], Is.True);
Assert.That(dimensions.MaxRows >= calculated[1], Is.True);
Assert.That(generatePDF417BitMatrix(input, 371, null, dimensions), Is.Not.Null);
}

public static BitMatrix generatePDF417BitMatrix(String barcodeText, int width, int? heightRequested, Dimensions dimensions)
{
var barcodeWriter = new PDF417Writer();
var height = heightRequested == null ? width / 4 : heightRequested.Value;
var hints = new System.Collections.Generic.Dictionary<EncodeHintType, object>();
hints[EncodeHintType.MARGIN] = 0;
hints[EncodeHintType.PDF417_DIMENSIONS] = dimensions;
return barcodeWriter.encode(barcodeText, BarcodeFormat.PDF_417, width, height, hints);
}
}
}

0 comments on commit 2fc204d

Please sign in to comment.