From 067964d05f3e6a7ab26ffd2289d6f2cb8718624c Mon Sep 17 00:00:00 2001 From: Nicola Verbeeck Date: Mon, 22 Apr 2024 12:59:58 +0200 Subject: [PATCH] Add option to turn off spacers for CODE 39 (#70) --- barcode/CHANGELOG.md | 4 ++++ barcode/lib/src/barcode.dart | 5 +++-- barcode/lib/src/code39.dart | 9 ++++++--- barcode/pubspec.yaml | 2 +- barcode/test/code39_test.dart | 21 +++++++++++++++++++++ 5 files changed, 35 insertions(+), 6 deletions(-) diff --git a/barcode/CHANGELOG.md b/barcode/CHANGELOG.md index 0efafc5..49d295d 100644 --- a/barcode/CHANGELOG.md +++ b/barcode/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2.2.8 + +- Add option to turn off spacers for CODE 39 [NicolaVerbeeck] + ## 2.2.7 - Add text formatting options to GS1-128 [NicolaVerbeeck] diff --git a/barcode/lib/src/barcode.dart b/barcode/lib/src/barcode.dart index 5a41f88..f8f15ac 100644 --- a/barcode/lib/src/barcode.dart +++ b/barcode/lib/src/barcode.dart @@ -112,10 +112,11 @@ abstract class Barcode { /// characters (-, ., \$, /, +, %, and space). /// /// An additional character (denoted '*') is used for both start and stop - /// delimiters. + /// delimiters, this can be controlled with the [drawSpacers] parameter. /// /// CODE 39 - static Barcode code39() => const BarcodeCode39(); + static Barcode code39({bool drawSpacers = true}) => + BarcodeCode39(drawSpacers); /// Code 93 [Barcode] /// diff --git a/barcode/lib/src/code39.dart b/barcode/lib/src/code39.dart index f9550a5..844166a 100644 --- a/barcode/lib/src/code39.dart +++ b/barcode/lib/src/code39.dart @@ -26,10 +26,13 @@ import 'barcode_operations.dart'; /// characters (-, ., \$, /, +, %, and space). /// /// An additional character (denoted '*') is used for both start and stop -/// delimiters. +/// delimiters. This can be disabled by setting [drawSpacers] to false. class BarcodeCode39 extends Barcode1D { /// Create a code 39 Barcode - const BarcodeCode39(); + const BarcodeCode39(this.drawSpacers); + + /// Draw the start '*' and end '*' chars in the left and right margins + final bool drawSpacers; @override Iterable get charSet => BarcodeMaps.code39.keys; @@ -62,7 +65,7 @@ class BarcodeCode39 extends Barcode1D { double textPadding, double lineWidth, ) sync* { - final text = '*$data*'; + final text = drawSpacers ? '*$data*' : data; for (var i = 0; i < text.length; i++) { yield BarcodeText( diff --git a/barcode/pubspec.yaml b/barcode/pubspec.yaml index 49d44e1..24371a3 100644 --- a/barcode/pubspec.yaml +++ b/barcode/pubspec.yaml @@ -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.7 +version: 2.2.8 environment: sdk: ">=2.12.0 <4.0.0" diff --git a/barcode/test/code39_test.dart b/barcode/test/code39_test.dart index aff85e3..e24c114 100644 --- a/barcode/test/code39_test.dart +++ b/barcode/test/code39_test.dart @@ -66,4 +66,25 @@ void main() { expect(Barcode.fromType(BarcodeType.Code39), equals(const TypeMatcher())); }); + + test('Barcode CODE 39 without spacers', () { + final bc = Barcode.code39(drawSpacers: false); + final elements = bc.make('CODE-39', + width: 100, height: 100, drawText: true, fontHeight: 1, textPadding: 1); + + final textElements = elements.whereType(); + + expect(textElements.any((element) => element.text.contains('*')), false); + }); + + test('Barcode CODE 39 with spacers', () { + final bc = Barcode.code39(drawSpacers: true); + final elements = bc.make('CODE-39', + width: 100, height: 100, drawText: true, fontHeight: 1, textPadding: 1); + + final textElements = elements.whereType(); + + expect(textElements.first.text, '*'); + expect(textElements.last.text, '*'); + }); }