diff --git a/barcode/CHANGELOG.md b/barcode/CHANGELOG.md index db10a3d..15cfc93 100644 --- a/barcode/CHANGELOG.md +++ b/barcode/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- Add text display options to GS1-128 [NicolaVerbeeck] + ## 2.2.6 - Fix invalid UPC-E in some cases [shinbin] diff --git a/barcode/lib/src/barcode.dart b/barcode/lib/src/barcode.dart index e01e287..5a41f88 100644 --- a/barcode/lib/src/barcode.dart +++ b/barcode/lib/src/barcode.dart @@ -166,7 +166,15 @@ abstract class Barcode { bool useCode128C = true, bool escapes = false, }) => - BarcodeCode128(useCode128A, useCode128B, useCode128C, false, escapes); + BarcodeCode128( + useCode128A: useCode128A, + useCode128B: useCode128B, + useCode128C: useCode128C, + isGS1: false, + escapes: escapes, + addSpaceAfterParenthesis: false, + keepParenthesis: false, + ); /// GS1-128 [Barcode] /// @@ -192,13 +200,29 @@ abstract class Barcode { /// Use `"{1}"` for FNC1, `"{2}"` for FNC2, `"{3}"` for FNC3, `"{4}"` for FNC4. /// Example: `"Test{1}1233{3}45"` will be equivalent to `Test FNC1 1233 FNC3 45` /// for the reader application. + /// + /// When [addSpaceAfterParenthesis] is enabled a space is added after the + /// parenthesis to make the barcode more readable when text is enabled + /// + /// When [keepParenthesis] is enabled, the parenthesis are kept in the barcode + /// when text is enabled static Barcode gs128({ bool useCode128A = true, bool useCode128B = true, bool useCode128C = true, bool escapes = false, + bool addSpaceAfterParenthesis = true, + bool keepParenthesis = false, }) => - BarcodeCode128(useCode128A, useCode128B, useCode128C, true, escapes); + BarcodeCode128( + useCode128A: useCode128A, + useCode128B: useCode128B, + useCode128C: useCode128C, + isGS1: true, + escapes: escapes, + addSpaceAfterParenthesis: addSpaceAfterParenthesis, + keepParenthesis: keepParenthesis, + ); /// ITF-14 Barcode /// diff --git a/barcode/lib/src/code128.dart b/barcode/lib/src/code128.dart index 7528dc7..a8a0ee6 100644 --- a/barcode/lib/src/code128.dart +++ b/barcode/lib/src/code128.dart @@ -54,13 +54,15 @@ class BarcodeCode128Fnc { /// other attributes needed by the user. class BarcodeCode128 extends Barcode1D { /// Create a Code128 Barcode - const BarcodeCode128( - this.useCode128A, - this.useCode128B, - this.useCode128C, - this.isGS1, - this.escapes, - ) : assert(useCode128A || useCode128B || useCode128C, + const BarcodeCode128({ + required this.useCode128A, + required this.useCode128B, + required this.useCode128C, + required this.isGS1, + required this.escapes, + required this.keepParenthesis, + required this.addSpaceAfterParenthesis, + }) : assert(useCode128A || useCode128B || useCode128C, 'Enable at least one of the CODE 128 tables'); /// Use Code 128 A table @@ -78,6 +80,12 @@ class BarcodeCode128 extends Barcode1D { /// Generate a GS1-128 Barcode final bool isGS1; + /// Indicates that for text, the parenthesis should be kept + final bool keepParenthesis; + + /// Indicates that for text should add a space after the parenthesis + final bool addSpaceAfterParenthesis; + @override Iterable get charSet => BarcodeMaps.code128B.keys .where((int x) => useCode128B && x >= 0) @@ -299,8 +307,14 @@ class BarcodeCode128 extends Barcode1D { for (final match in RegExp(r'\(.+?\)').allMatches(data)) { result.write(data.substring(start, match.start)); result.write(BarcodeMaps.code128FNC1String); + if (text && keepParenthesis) { + result.write('('); + } result.write(data.substring(match.start + 1, match.end - 1)); - if (text) { + if (text && keepParenthesis) { + result.write(')'); + } + if (text && addSpaceAfterParenthesis) { result.write(' '); } start = match.end; diff --git a/barcode/test/code128_test.dart b/barcode/test/code128_test.dart index 49404c4..6ead639 100644 --- a/barcode/test/code128_test.dart +++ b/barcode/test/code128_test.dart @@ -199,7 +199,84 @@ void main() { ); } }); + test('Barcode GS1-128 text default', () { + final bc = Barcode.gs128(); + if (bc is BarcodeCode128) { + expect( + bc.adaptData('(3)0(92)', true), + equals('${BarcodeCode128Fnc.fnc1}3 0${BarcodeCode128Fnc.fnc1}92 '), + ); + + expect( + bc.adaptData('x(3)0(92)x', true), + equals('x${BarcodeCode128Fnc.fnc1}3 0${BarcodeCode128Fnc.fnc1}92 x'), + ); + + expect( + bc.adaptData('x(30(92)x', true), + equals('x${BarcodeCode128Fnc.fnc1}30(92 x'), + ); + } + }); + test('Barcode GS1-128 text keep parenthesis', () { + final bc = Barcode.gs128(keepParenthesis: true); + if (bc is BarcodeCode128) { + expect( + bc.adaptData('(3)0(92)', true), + equals('${BarcodeCode128Fnc.fnc1}(3) 0${BarcodeCode128Fnc.fnc1}(92) '), + ); + + expect( + bc.adaptData('x(3)0(92)x', true), + equals( + 'x${BarcodeCode128Fnc.fnc1}(3) 0${BarcodeCode128Fnc.fnc1}(92) x'), + ); + + expect( + bc.adaptData('x(30(92)x', true), + equals('x${BarcodeCode128Fnc.fnc1}(30(92) x'), + ); + } + }); + test('Barcode GS1-128 text no space', () { + final bc = Barcode.gs128(addSpaceAfterParenthesis: false); + if (bc is BarcodeCode128) { + expect( + bc.adaptData('(3)0(92)', true), + equals('${BarcodeCode128Fnc.fnc1}30${BarcodeCode128Fnc.fnc1}92'), + ); + + expect( + bc.adaptData('x(3)0(92)x', true), + equals('x${BarcodeCode128Fnc.fnc1}30${BarcodeCode128Fnc.fnc1}92x'), + ); + + expect( + bc.adaptData('x(30(92)x', true), + equals('x${BarcodeCode128Fnc.fnc1}30(92x'), + ); + } + }); + test('Barcode GS1-128 text keep parenthesis no space', () { + final bc = + Barcode.gs128(keepParenthesis: true, addSpaceAfterParenthesis: false); + if (bc is BarcodeCode128) { + expect( + bc.adaptData('(3)0(92)', true), + equals('${BarcodeCode128Fnc.fnc1}(3)0${BarcodeCode128Fnc.fnc1}(92)'), + ); + + expect( + bc.adaptData('x(3)0(92)x', true), + equals('x${BarcodeCode128Fnc.fnc1}(3)0${BarcodeCode128Fnc.fnc1}(92)x'), + ); + expect( + bc.adaptData('x(30(92)x', true), + equals('x${BarcodeCode128Fnc.fnc1}(30(92)x'), + ); + } + }); test('Barcode CODE-128 charSet', () { const code128A = { 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, //