Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add text formatting options to GS1-128 barcodes #69

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions barcode/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.2.7

- Add text formatting options to GS1-128 [NicolaVerbeeck]

## 2.2.6

- Fix invalid UPC-E in some cases [shinbin]
Expand Down
28 changes: 26 additions & 2 deletions barcode/lib/src/barcode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]
///
Expand All @@ -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
///
Expand Down
30 changes: 22 additions & 8 deletions barcode/lib/src/code128.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<int> get charSet => BarcodeMaps.code128B.keys
.where((int x) => useCode128B && x >= 0)
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion barcode/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: >-
homepage: https://github.com/DavBfr/dart_barcode/tree/master/barcode
repository: https://github.com/DavBfr/dart_barcode
issue_tracker: https://github.com/DavBfr/dart_barcode/issues
version: 2.2.6
version: 2.2.7

environment:
sdk: ">=2.12.0 <4.0.0"
Expand Down
77 changes: 77 additions & 0 deletions barcode/test/code128_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <int>{
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, //
Expand Down
Loading