Skip to content

Commit 90e2620

Browse files
New Datamatrix param to set the initial encoding. (#87)
Co-authored-by: CROCHARD Flavien <[email protected]>
1 parent 1ec056e commit 90e2620

File tree

6 files changed

+80
-19
lines changed

6 files changed

+80
-19
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,15 @@ Create a composer.json in your projects root-directory:
147147
```json
148148
{
149149
"require": {
150-
"tecnickcom/tc-lib-barcode": "^2.0"
150+
"tecnickcom/tc-lib-barcode": "^2.3"
151151
}
152152
}
153153
```
154154

155155
Or add to an existing project with:
156156

157157
```bash
158-
composer require tecnickcom/tc-lib-barcode ^2.0
158+
composer require tecnickcom/tc-lib-barcode ^2.3
159159
```
160160

161161
## Packaging
@@ -166,7 +166,7 @@ this library includes make targets for building these packages (`make rpm` and `
166166
The packages are generated under the `target` directory.
167167

168168
When this library is installed using an RPM or DEB package, you can use it your code by including the autoloader:
169-
```
169+
```php
170170
require_once ('/usr/share/php/Com/Tecnick/Barcode/autoload.php');
171171
```
172172

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.2.3
1+
2.3.0

src/Type/Square/Datamatrix.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,34 @@ class Datamatrix extends \Com\Tecnick\Barcode\Type\Square
7272
*/
7373
protected bool $gsonemode = false;
7474

75+
/**
76+
* Datamatrix default encoding.
77+
* See Data::SWITCHCDW for valid values.
78+
*/
79+
protected int $defenc = Data::ENC_ASCII;
80+
7581
/**
7682
* Set extra (optional) parameters:
77-
* 1: SHAPE - S=square (default), R=rectangular
78-
* 2: MODE - N=default, GS1 = the FNC1 codeword is added in the first position of Data Matrix ECC 200 version
83+
* 1: SHAPE: S=square (default), R=rectangular.
84+
* 2: MODE: N=default, GS1 = the FNC1 codeword is added in the first position of Data Matrix ECC 200 version.
85+
* 3: ENCODING: ASCII (default), C40, TXT, X12, EDIFACT, BASE256.
7986
*/
8087
protected function setParameters(): void
8188
{
8289
parent::setParameters();
8390

8491
// shape
85-
if (isset($this->params[0]) && ($this->params[0] == 'R')) {
92+
if (isset($this->params[0]) && ($this->params[0] === 'R')) {
8693
$this->shape = 'R';
8794
}
8895

8996
// mode
90-
if (! isset($this->params[1])) {
91-
return;
92-
}
97+
$this->gsonemode = (isset($this->params[1]) && ($this->params[1] === 'GS1'));
9398

94-
if ($this->params[1] != 'GS1') {
95-
return;
99+
// encoding
100+
if (isset($this->params[2])) {
101+
$this->defenc = Data::ENCOPTS[$this->params[2]] ?? Data::ENC_ASCII;
96102
}
97-
98-
$this->gsonemode = true;
99103
}
100104

101105
/**
@@ -226,14 +230,21 @@ protected function setGrid(
226230
*/
227231
protected function getHighLevelEncoding(string $data): array
228232
{
229-
// STEP A. Start in ASCII encodation.
230-
$enc = Data::ENC_ASCII; // current encoding mode
233+
// STEP A. Start in predefined encodation.
234+
$enc = $this->defenc; // current encoding mode
231235
$this->dmx->last_enc = $enc; // last used encoding
232236
$pos = 0; // current position
233237
$cdw = []; // array of codewords to be returned
234238
$cdw_num = 0; // number of data codewords
235239
$data_length = strlen($data); // number of chars
236240
$field_length = 0; // number of chars in current field
241+
242+
// Switch to predefined encoding (no action needed if ASCII because it's the default encoding)
243+
if ($this->defenc !== Data::ENC_ASCII) {
244+
$cdw[] = $this->dmx->getSwitchEncodingCodeword($this->defenc);
245+
++$cdw_num;
246+
}
247+
237248
while ($pos < $data_length) {
238249
// Determine if current char is FNC1 (don't encode it, just pass it through)
239250
if ($this->gsonemode && ($data[$pos] == chr(232))) {

src/Type/Square/Datamatrix/Data.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ class Data
8989
*/
9090
public const ENC_ASCII_NUM = 7;
9191

92+
/**
93+
* Encoding options that can be specified as input parameter.
94+
*
95+
* @var array<string, int>
96+
*/
97+
public const ENCOPTS = [
98+
'ASCII' => Data::ENC_ASCII,
99+
'C40' => Data::ENC_C40,
100+
'TXT' => Data::ENC_TXT,
101+
'X12' => Data::ENC_X12,
102+
'EDF' => Data::ENC_EDF,
103+
'BASE256' => Data::ENC_BASE256,
104+
];
105+
92106
/**
93107
* Switch codewords.
94108
*

src/Type/Square/Datamatrix/Modes.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,12 @@ protected function getMaxDataCodewords(int $numcw): int
203203

204204
/**
205205
* Get the switching codeword to a new encoding mode (latch codeword)
206+
*
206207
* @param int $mode New encoding mode.
208+
*
207209
* @return int Switch codeword.
208-
* @protected
209210
*/
210-
protected function getSwitchEncodingCodeword(int $mode): int
211+
public function getSwitchEncodingCodeword(int $mode): int
211212
{
212213
$cdw = Data::SWITCHCDW[$mode];
213214
if ($cdw != 254) {

test/Square/DatamatrixTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,42 @@ public static function getGridDataProvider(): array
391391
"\xE8" . '01034531200000111712050810ABCD1234' . "\xE8" . '4109501101020917',
392392
'a29a330a01cce34a346cf7049e2259ee',
393393
],
394-
394+
// Different encoding datamatrix
395+
[
396+
'DATAMATRIX,S,N,ASCII',
397+
'01234567890',
398+
'ac7dd9e1ebdb42d07fe928fb33cd307b'
399+
],
400+
[
401+
'DATAMATRIX,S,N,C40',
402+
'01234567890',
403+
'958a7a3bcd036d7135489eb703a25633'
404+
],
405+
[
406+
'DATAMATRIX,S,N,TXT',
407+
'01234567890',
408+
'057981dfbf527b029ae59d65fb55f61d'
409+
],
410+
[
411+
'DATAMATRIX,S,N,X12',
412+
'01234567890',
413+
'8d75b0fcfb2d0977abd95004a6ba98dd'
414+
],
415+
[
416+
'DATAMATRIX,S,N,EDF',
417+
'01234567890',
418+
'989eab3ca16c97e05dd2307bef32f64b'
419+
],
420+
[
421+
'DATAMATRIX,S,N,BASE256',
422+
'01234567890',
423+
'8b4f688a774130bc654e39dfcfadb482'
424+
],
425+
[
426+
'DATAMATRIX,S,GS1,C40',
427+
"\xE8" . '01095011010209171719050810ABCD1234' . "\xE8" . '2110',
428+
'ba117111dfa40a40e1bb968c719d2eef'
429+
]
395430
];
396431
}
397432

0 commit comments

Comments
 (0)