diff --git a/README.md b/README.md index 4f8321d..436977b 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ Create a composer.json in your projects root-directory: ```json { "require": { - "tecnickcom/tc-lib-barcode": "^2.0" + "tecnickcom/tc-lib-barcode": "^2.3" } } ``` @@ -155,7 +155,7 @@ Create a composer.json in your projects root-directory: Or add to an existing project with: ```bash -composer require tecnickcom/tc-lib-barcode ^2.0 +composer require tecnickcom/tc-lib-barcode ^2.3 ``` ## Packaging @@ -166,7 +166,7 @@ this library includes make targets for building these packages (`make rpm` and ` The packages are generated under the `target` directory. When this library is installed using an RPM or DEB package, you can use it your code by including the autoloader: -``` +```php require_once ('/usr/share/php/Com/Tecnick/Barcode/autoload.php'); ``` diff --git a/VERSION b/VERSION index 5859406..276cbf9 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.2.3 +2.3.0 diff --git a/src/Type/Square/Datamatrix.php b/src/Type/Square/Datamatrix.php index 238af17..ef7bba8 100644 --- a/src/Type/Square/Datamatrix.php +++ b/src/Type/Square/Datamatrix.php @@ -72,30 +72,34 @@ class Datamatrix extends \Com\Tecnick\Barcode\Type\Square */ protected bool $gsonemode = false; + /** + * Datamatrix default encoding. + * See Data::SWITCHCDW for valid values. + */ + protected int $defenc = Data::ENC_ASCII; + /** * Set extra (optional) parameters: - * 1: SHAPE - S=square (default), R=rectangular - * 2: MODE - N=default, GS1 = the FNC1 codeword is added in the first position of Data Matrix ECC 200 version + * 1: SHAPE: S=square (default), R=rectangular. + * 2: MODE: N=default, GS1 = the FNC1 codeword is added in the first position of Data Matrix ECC 200 version. + * 3: ENCODING: ASCII (default), C40, TXT, X12, EDIFACT, BASE256. */ protected function setParameters(): void { parent::setParameters(); // shape - if (isset($this->params[0]) && ($this->params[0] == 'R')) { + if (isset($this->params[0]) && ($this->params[0] === 'R')) { $this->shape = 'R'; } // mode - if (! isset($this->params[1])) { - return; - } + $this->gsonemode = (isset($this->params[1]) && ($this->params[1] === 'GS1')); - if ($this->params[1] != 'GS1') { - return; + // encoding + if (isset($this->params[2])) { + $this->defenc = Data::ENCOPTS[$this->params[2]] ?? Data::ENC_ASCII; } - - $this->gsonemode = true; } /** @@ -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->defenc; // 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->defenc !== Data::ENC_ASCII) { + $cdw[] = $this->dmx->getSwitchEncodingCodeword($this->defenc); + ++$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))) { diff --git a/src/Type/Square/Datamatrix/Data.php b/src/Type/Square/Datamatrix/Data.php index f1b5004..372d3a6 100644 --- a/src/Type/Square/Datamatrix/Data.php +++ b/src/Type/Square/Datamatrix/Data.php @@ -89,6 +89,20 @@ class Data */ public const ENC_ASCII_NUM = 7; + /** + * Encoding options that can be specified as input parameter. + * + * @var array + */ + public const ENCOPTS = [ + 'ASCII' => Data::ENC_ASCII, + 'C40' => Data::ENC_C40, + 'TXT' => Data::ENC_TXT, + 'X12' => Data::ENC_X12, + 'EDF' => Data::ENC_EDF, + 'BASE256' => Data::ENC_BASE256, + ]; + /** * Switch codewords. * diff --git a/src/Type/Square/Datamatrix/Modes.php b/src/Type/Square/Datamatrix/Modes.php index 5f621e5..a659089 100644 --- a/src/Type/Square/Datamatrix/Modes.php +++ b/src/Type/Square/Datamatrix/Modes.php @@ -203,11 +203,12 @@ protected function getMaxDataCodewords(int $numcw): int /** * Get the switching codeword to a new encoding mode (latch codeword) + * * @param int $mode New encoding mode. + * * @return int Switch codeword. - * @protected */ - protected function getSwitchEncodingCodeword(int $mode): int + public function getSwitchEncodingCodeword(int $mode): int { $cdw = Data::SWITCHCDW[$mode]; if ($cdw != 254) { diff --git a/test/Square/DatamatrixTest.php b/test/Square/DatamatrixTest.php index 04d299f..34777a5 100644 --- a/test/Square/DatamatrixTest.php +++ b/test/Square/DatamatrixTest.php @@ -391,7 +391,42 @@ public static function getGridDataProvider(): array "\xE8" . '01034531200000111712050810ABCD1234' . "\xE8" . '4109501101020917', 'a29a330a01cce34a346cf7049e2259ee', ], - + // Different encoding datamatrix + [ + 'DATAMATRIX,S,N,ASCII', + '01234567890', + 'ac7dd9e1ebdb42d07fe928fb33cd307b' + ], + [ + 'DATAMATRIX,S,N,C40', + '01234567890', + '958a7a3bcd036d7135489eb703a25633' + ], + [ + 'DATAMATRIX,S,N,TXT', + '01234567890', + '057981dfbf527b029ae59d65fb55f61d' + ], + [ + 'DATAMATRIX,S,N,X12', + '01234567890', + '8d75b0fcfb2d0977abd95004a6ba98dd' + ], + [ + 'DATAMATRIX,S,N,EDF', + '01234567890', + '989eab3ca16c97e05dd2307bef32f64b' + ], + [ + 'DATAMATRIX,S,N,BASE256', + '01234567890', + '8b4f688a774130bc654e39dfcfadb482' + ], + [ + 'DATAMATRIX,S,GS1,C40', + "\xE8" . '01095011010209171719050810ABCD1234' . "\xE8" . '2110', + 'ba117111dfa40a40e1bb968c719d2eef' + ] ]; }