Skip to content

Commit

Permalink
Fix Datamatrix encodeEDFfour encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni committed Dec 20, 2020
1 parent eb6d1e8 commit af643dd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
1.17.0
1.17.1
13 changes: 5 additions & 8 deletions src/Type/Square/Datamatrix.php
Expand Up @@ -105,6 +105,9 @@ protected function setParameters()
*/
protected function addPadding($size, $ncw)
{
if ($size <= $ncw) {
return;
}
if (($this->dmx->last_enc != Data::ENC_ASCII) && ($this->dmx->last_enc != Data::ENC_BASE256)) {
// return to ASCII encodation before padding
if ($this->dmx->last_enc == Data::ENC_EDF) {
Expand Down Expand Up @@ -150,14 +153,8 @@ protected function getCodewords()
}

// get minimum required matrix size.
foreach (Data::$symbattr[$this->shape] as $params) {
if ($params[11] >= $ncw) {
break;
}
}
if ($params[11] > $ncw) {
$this->addPadding($params[11], $ncw);
}
$params = Data::getPaddingSize($this->shape, $ncw);
$this->addPadding($params[11], $ncw);

$errorCorrection = new \Com\Tecnick\Barcode\Type\Square\Datamatrix\ErrorCorrection;
$this->cdw = $errorCorrection->getErrorCorrection($this->cdw, $params[13], $params[14], $params[15]);
Expand Down
17 changes: 17 additions & 0 deletions src/Type/Square/Datamatrix/Data.php
Expand Up @@ -214,4 +214,21 @@ class Data
0x47=>0x14,0x48=>0x15,0x49=>0x16,0x4a=>0x17,0x4b=>0x18,0x4c=>0x19,0x4d=>0x1a,0x4e=>0x1b,0x4f=>0x1c,0x50=>0x1d,
0x51=>0x1e,0x52=>0x1f,0x53=>0x20,0x54=>0x21,0x55=>0x22,0x56=>0x23,0x57=>0x24,0x58=>0x25,0x59=>0x26,0x5a=>0x27)
);

/**
* Get the required codewords padding size
*
* @return array params
*
* @throws BarcodeException in case of error
*/
public static function getPaddingSize($shape, $ncw)
{
foreach (Data::$symbattr[$shape] as $params) {
if ($params[11] >= $ncw) {
return $params;
}
}
throw new BarcodeException('Unable to find the correct size');
}
}
19 changes: 12 additions & 7 deletions src/Type/Square/Datamatrix/Encode.php
Expand Up @@ -124,10 +124,13 @@ public function encodeASCII(&$cdw, &$cdw_num, &$pos, &$data_length, &$data, &$en
*/
public function encodeEDFfour($epos, &$cdw, &$cdw_num, &$pos, &$data_length, &$field_length, &$enc, &$temp_cw)
{
if (($epos == $data_length) && ($field_length < 3)) {
if (($epos == $data_length)) {
$enc = Data::ENC_ASCII;
$cdw[] = $this->getSwitchEncodingCodeword($enc);
++$cdw_num;
$params = Data::getPaddingSize($this->shape, ($cdw_num + $field_length));
if (($params[11] - $cdw_num) > 2) {
$cdw[] = $this->getSwitchEncodingCodeword($enc);
++$cdw_num;
}
return true;
}
if ($field_length < 4) {
Expand All @@ -142,10 +145,12 @@ public function encodeEDFfour($epos, &$cdw, &$cdw_num, &$pos, &$data_length, &$f
$this->last_enc = $enc;
}
// encodes four data characters in three codewords
$cdw[] = (($temp_cw[0] & 0x3F) << 2) + (($temp_cw[1] & 0x30) >> 4);
$cdw[] = (($temp_cw[1] & 0x0F) << 4) + (($temp_cw[2] & 0x3C) >> 2);
$cdw[] = (($temp_cw[2] & 0x03) << 6) + ($temp_cw[3] & 0x3F);
$cdw_num += 3;
if ($field_length > 2) {
$cdw[] = (($temp_cw[0] & 0x3F) << 2) + (($temp_cw[1] & 0x30) >> 4);
$cdw[] = (($temp_cw[1] & 0x0F) << 4) + (($temp_cw[2] & 0x3C) >> 2);
$cdw[] = (($temp_cw[2] & 0x03) << 6) + ($temp_cw[3] & 0x3F);
$cdw_num += 3;
}
$temp_cw = array();
$pos = $epos;
$field_length = 0;
Expand Down
12 changes: 7 additions & 5 deletions test/Square/DatamatrixTest.php
Expand Up @@ -89,8 +89,10 @@ public function testGetGrid($mode, $code, $expected)
public function getGridDataProvider()
{
return array(
array('DATAMATRIX', '(400)BS2WZ64PA(00)0', '183514ca2f0465170de1d404a5d7dabd'),
array('DATAMATRIX', '(400)BS2WZ64QA(00)0', '4293cb60df5ca208922b6f4ce65dbb7c'),
array('DATAMATRIX', '0&0&0&0&0&0&_', 'fffdfdaec33af0788d24cdfa8cba5ac6'),
array('DATAMATRIX', '0&0&0&0&0&0&0', '10d0faf5a6e7b71829f268218df7e6af'),
array('DATAMATRIX', '(400)BS2WZ64PA(00)0', '9cb7f1c2aa5989909229ef8e4252d61d'),
array('DATAMATRIX', '(400)BS2WZ64QA(00)0', '0494f709138a1feef5a1c9f14852dbe5'),
array('DATAMATRIX', 'LD2B 1 CLNGP', 'f806889d1dbe0908dcfb530f86098041'),
array('DATAMATRIX', 'XXXXXXXXXNGP', 'c6f2b7b293a2943bae74f2a191ec4aea'),
array('DATAMATRIX', 'XXXXXXXXXXXXNGP', 'f7679d5a7ab4a8edf12571a6866d92bc'),
Expand Down Expand Up @@ -129,7 +131,7 @@ public function getGridDataProvider()
array(
'DATAMATRIX',
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*(),./\\1234567890',
'4744c06c576088b40b3523c7d27cf051'
'7360a5a6c25476711139ae1244f56c29'
),
array(
'DATAMATRIX', chr(254).chr(253)
Expand All @@ -147,7 +149,7 @@ public function getGridDataProvider()
'e524bb17821d0461f3db6f313d35018f'),
array('DATAMATRIX', 'ec:b47'.chr(127).'4#P d*b}gI2#DB|hl{!~[EYH*=cmR{lf'
.chr(127).'=gcGIa.st286. #*"!eG[.Ryr?Kn,1mIyQqC3 6\'3N>',
'6d12a9d2d36f76667d56f270649232b0'
'57fbb9bfb7d542e2e5eadb615e6be549'
),
array('DATAMATRIX', 'eA211101A2raJTGL/r9o93CVk4gtpEvWd2A2Qz8jvPc7l8ybD3m'
.'Wel91ih727kldinPeHJCjhr7fIBX1KQQfsN7BFMX00nlS8FlZG+',
Expand Down Expand Up @@ -179,7 +181,7 @@ public function getGridDataProvider()
.chr(29).chr(28).chr(27).chr(26).chr(25).chr(24).chr(23).chr(22).chr(21).chr(20).chr(19).chr(18)
.chr(17).chr(16).chr(15).chr(14).chr(13).chr(12).chr(11).chr(10).chr(9).chr(8).chr(7).chr(6)
.chr(5).chr(4).chr(3).chr(2).chr(1),
'9dccdf9b0b6d99c7d420af5540a9edfc'
'4d755e3863cfdc79dc7ed5b8a781e479'
),
// Rectangular shape
array('DATAMATRIX,R', '01234567890', 'd3811e018f960beed6d3fa5e675e290e'),
Expand Down

0 comments on commit af643dd

Please sign in to comment.