Skip to content

Commit

Permalink
Enable different initial encoding for datamatrix
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviencrochard committed Aug 23, 2024
1 parent 1ec056e commit 8d50120
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.3
2.2.4
27 changes: 19 additions & 8 deletions src/Type/Square/Datamatrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class Datamatrix extends \Com\Tecnick\Barcode\Type\Square
*/
protected bool $gsonemode = false;

/**
* Datamatrix default encoding (0=ASCII (default), 1=C40, 2=TXT, 3=X12, 4=EDF, 5=BASE256)
*/
protected int $encoding = Data::ENC_ASCII;

/**
* Set extra (optional) parameters:
* 1: SHAPE - S=square (default), R=rectangular
Expand All @@ -87,15 +92,14 @@ protected function setParameters(): void
}

// mode
if (! isset($this->params[1])) {
return;
if (isset($this->params[1]) && ($this->params[1] === 'GS1')) {
$this->gsonemode = true;
}

if ($this->params[1] != 'GS1') {
return;
// encoding
if (isset($this->params[2]) && array_key_exists(intval($this->params[2]), Data::SWITCHCDW)) {
$this->encoding = intval($this->params[2]);
}

$this->gsonemode = true;
}

/**
Expand Down Expand Up @@ -226,14 +230,21 @@ protected function setGrid(
*/
protected function getHighLevelEncoding(string $data): array
{
// STEP A. Start in ASCII encodation.
$enc = Data::ENC_ASCII; // current encoding mode
// STEP A. Start in predefined encodation.
$enc = $this->encoding; // current encoding mode
$this->dmx->last_enc = $enc; // last used encoding
$pos = 0; // current position
$cdw = []; // array of codewords to be returned
$cdw_num = 0; // number of data codewords
$data_length = strlen($data); // number of chars
$field_length = 0; // number of chars in current field

// Switch to predefined encoding (no action needed if ASCII because it's the default encoding)
if ($this->encoding !== Data::ENC_ASCII) {
$cdw[] = $this->dmx->getSwitchEncodingCodeword($this->encoding);
++$cdw_num;
}

while ($pos < $data_length) {
// Determine if current char is FNC1 (don't encode it, just pass it through)
if ($this->gsonemode && ($data[$pos] == chr(232))) {
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Square/Datamatrix/Modes.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ protected function getMaxDataCodewords(int $numcw): int
* @return int Switch codeword.
* @protected
*/
protected function getSwitchEncodingCodeword(int $mode): int
public function getSwitchEncodingCodeword(int $mode): int
{
$cdw = Data::SWITCHCDW[$mode];
if ($cdw != 254) {
Expand Down
37 changes: 36 additions & 1 deletion test/Square/DatamatrixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,42 @@ public static function getGridDataProvider(): array
"\xE8" . '01034531200000111712050810ABCD1234' . "\xE8" . '4109501101020917',
'a29a330a01cce34a346cf7049e2259ee',
],

// Different encoding datamatrix
[
'DATAMATRIX,S,N,0', // ASCII
'01234567890',
'ac7dd9e1ebdb42d07fe928fb33cd307b'
],
[
'DATAMATRIX,S,N,1', // C40
'01234567890',
'958a7a3bcd036d7135489eb703a25633'
],
[
'DATAMATRIX,S,N,2', // TXT
'01234567890',
'057981dfbf527b029ae59d65fb55f61d'
],
[
'DATAMATRIX,S,N,3', // X12
'01234567890',
'8d75b0fcfb2d0977abd95004a6ba98dd'
],
[
'DATAMATRIX,S,N,4', // EDF
'01234567890',
'989eab3ca16c97e05dd2307bef32f64b'
],
[
'DATAMATRIX,S,N,5', // BASE256
'01234567890',
'8b4f688a774130bc654e39dfcfadb482'
],
[
'DATAMATRIX,S,GS1,1', // GS1 + C40
"\xE8" . '01095011010209171719050810ABCD1234' . "\xE8" . '2110',
'ba117111dfa40a40e1bb968c719d2eef'
]
];
}

Expand Down

0 comments on commit 8d50120

Please sign in to comment.