Skip to content

Commit

Permalink
Fix DataMatrix encoding errors, update example
Browse files Browse the repository at this point in the history
  • Loading branch information
w512work committed Jan 22, 2024
1 parent 1eca731 commit aa86f92
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
4 changes: 4 additions & 0 deletions examples/example_050.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@
$pdf->write2DBarcode('http://www.tcpdf.org', 'DATAMATRIX', 80, 150, 50, 50, $style, 'N');
$pdf->Text(80, 145, 'DATAMATRIX (ISO/IEC 16022:2006)');

// DATAMATRIX,R : rectangular code variant
$pdf->write2DBarcode('http://www.tcpdf.org', 'DATAMATRIX,R', 140, 160, 50, 50, $style, 'N');
$pdf->Text(140, 155, 'DATAMATRIX rectangular');

// -------------------------------------------------------------------

// new style
Expand Down
33 changes: 16 additions & 17 deletions include/barcodes/datamatrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// File name : datamatrix.php
// Version : 1.0.008
// Begin : 2010-06-07
// Last Update : 2024-01-19
// Last Update : 2024-01-22
// Author : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
// Author : Urs Wettstein (implementation of rectangular code)
// License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
Expand Down Expand Up @@ -283,12 +283,16 @@ public function __construct($code, $shape = 'S') {
return false;
} elseif ($params[11] > $nd) {
// add padding
if ((($params[11] - $nd) > 1) AND ($cw[($nd - 1)] != 254)) {
if ($this->last_enc == ENC_EDF) {
if ($this->last_enc == ENC_EDF) {
// For EDIFACT encoding, the last two remaining code words are automatically switched to ASCII mode (without unlatch).
// Otherwise switch manually.
if (($params[11] - $nd) > 2) {
// switch to ASCII encoding
$cw[] = 124;
++$nd;
} elseif (($this->last_enc != ENC_ASCII) AND ($this->last_enc != ENC_BASE256)) {
}
} elseif ((($params[11] - $nd) > 1) AND ($cw[($nd - 1)] != 254)) {
if (($this->last_enc != ENC_ASCII) AND ($this->last_enc != ENC_BASE256)) {
// switch to ASCII encoding
$cw[] = 254;
++$nd;
Expand Down Expand Up @@ -685,7 +689,7 @@ protected function getSwitchEncodingCodeword($mode) {
case ENC_ASCII: { // ASCII character 0 to 127
$cw = 254;
if ($this->last_enc == ENC_EDF) {
$cw = 124;
$cw = 124; // 31 coded in the first 6 bits
}
break;
}
Expand Down Expand Up @@ -934,26 +938,21 @@ protected function getHighLevelEncoding($data) {
$temp_cw[] = 0x1f;
++$field_length;
// fill empty characters
for ($i = $field_length; $i < 4; ++$i) {
$temp_cw[] = 0;
}
$temp_cw[] = 0; // avoid index out of bounds access below
$enc = ENC_ASCII;
$this->last_enc = $enc;
}
// encodes four data characters in three codewords
$tcw = (($temp_cw[0] & 0x3F) << 2) + (($temp_cw[1] & 0x30) >> 4);
if ($tcw > 0) {
$cw[] = $tcw;
if($field_length >= 1) {
$cw[] = (($temp_cw[0] & 0x3F) << 2) + (($temp_cw[1] & 0x30) >> 4);
$cw_num++;
}
$tcw= (($temp_cw[1] & 0x0F) << 4) + (($temp_cw[2] & 0x3C) >> 2);
if ($tcw > 0) {
$cw[] = $tcw;
if($field_length >= 2) {
$cw[] = (($temp_cw[1] & 0x0F) << 4) + (($temp_cw[2] & 0x3C) >> 2);
$cw_num++;
}
$tcw = (($temp_cw[2] & 0x03) << 6) + ($temp_cw[3] & 0x3F);
if ($tcw > 0) {
$cw[] = $tcw;
if($field_length >= 3) {
$cw[] = (($temp_cw[2] & 0x03) << 6) + ($temp_cw[3] & 0x3F);
$cw_num++;
}
$temp_cw = array();
Expand Down

0 comments on commit aa86f92

Please sign in to comment.