Skip to content

Commit

Permalink
Add option to turn off spacers for CODE 39 (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolaVerbeeck authored Apr 22, 2024
1 parent 8dfbee1 commit 067964d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
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.8

- Add option to turn off spacers for CODE 39 [NicolaVerbeeck]

## 2.2.7

- Add text formatting options to GS1-128 [NicolaVerbeeck]
Expand Down
5 changes: 3 additions & 2 deletions barcode/lib/src/barcode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
/// <img width="250" alt="CODE 39" src="https://raw.githubusercontent.com/DavBfr/dart_barcode/master/img/code-39.svg?sanitize=true">
static Barcode code39() => const BarcodeCode39();
static Barcode code39({bool drawSpacers = true}) =>
BarcodeCode39(drawSpacers);

/// Code 93 [Barcode]
///
Expand Down
9 changes: 6 additions & 3 deletions barcode/lib/src/code39.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> get charSet => BarcodeMaps.code39.keys;
Expand Down Expand Up @@ -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(
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.7
version: 2.2.8

environment:
sdk: ">=2.12.0 <4.0.0"
Expand Down
21 changes: 21 additions & 0 deletions barcode/test/code39_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,25 @@ void main() {
expect(Barcode.fromType(BarcodeType.Code39),
equals(const TypeMatcher<BarcodeCode39>()));
});

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<BarcodeText>();

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<BarcodeText>();

expect(textElements.first.text, '*');
expect(textElements.last.text, '*');
});
}

0 comments on commit 067964d

Please sign in to comment.