Skip to content

Commit

Permalink
Implemented rectangular data matrix barcode
Browse files Browse the repository at this point in the history
  • Loading branch information
w512work committed Jan 19, 2024
1 parent 5fce932 commit 1eca731
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 25 deletions.
81 changes: 60 additions & 21 deletions include/barcodes/datamatrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// File name : datamatrix.php
// Version : 1.0.008
// Begin : 2010-06-07
// Last Update : 2014-05-06
// Last Update : 2024-01-19
// 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)
// -------------------------------------------------------------------
// Copyright (C) 2010-2014 Nicola Asuni - Tecnick.com LTD
Expand Down Expand Up @@ -121,6 +122,12 @@ class Datamatrix {
*/
protected $last_enc = ENC_ASCII;

/**
* Store whether the code is rectangular or not (square).
* @protected
*/
protected $rectangular = false;

/**
* Table of Data Matrix ECC 200 Symbol Attributes:<ul>
* <li>total matrix rows (including finder pattern)</li>
Expand Down Expand Up @@ -168,13 +175,13 @@ class Datamatrix {
array(0x078,0x078,0x06c,0x06c,0x014,0x014,0x012,0x012,0x006,0x006,0x024,0x41a,0x198,0x006,0x0af,0x044), // 120x120
array(0x084,0x084,0x078,0x078,0x016,0x016,0x014,0x014,0x006,0x006,0x024,0x518,0x1f0,0x008,0x0a3,0x03e), // 132x132
array(0x090,0x090,0x084,0x084,0x018,0x018,0x016,0x016,0x006,0x006,0x024,0x616,0x26c,0x00a,0x09c,0x03e), // 144x144
// rectangular form (currently unused) ---------------------------------------------------------------------------
// rectangular form ----------------------------------------------------------------------------------------------
array(0x008,0x012,0x006,0x010,0x008,0x012,0x006,0x010,0x001,0x001,0x001,0x005,0x007,0x001,0x005,0x007), // 8x18
array(0x008,0x020,0x006,0x01c,0x008,0x010,0x006,0x00e,0x001,0x002,0x002,0x00a,0x00b,0x001,0x00a,0x00b), // 8x32
array(0x008,0x020,0x006,0x01c,0x008,0x010,0x006,0x00e,0x002,0x001,0x002,0x00a,0x00b,0x001,0x00a,0x00b), // 8x32
array(0x00c,0x01a,0x00a,0x018,0x00c,0x01a,0x00a,0x018,0x001,0x001,0x001,0x010,0x00e,0x001,0x010,0x00e), // 12x26
array(0x00c,0x024,0x00a,0x020,0x00c,0x012,0x00a,0x010,0x001,0x002,0x002,0x00c,0x012,0x001,0x00c,0x012), // 12x36
array(0x010,0x024,0x00e,0x020,0x010,0x012,0x00e,0x010,0x001,0x002,0x002,0x020,0x018,0x001,0x020,0x018), // 16x36
array(0x010,0x030,0x00e,0x02c,0x010,0x018,0x00e,0x016,0x001,0x002,0x002,0x031,0x01c,0x001,0x031,0x01c) // 16x48
array(0x00c,0x024,0x00a,0x020,0x00c,0x012,0x00a,0x010,0x002,0x001,0x002,0x016,0x012,0x001,0x016,0x012), // 12x36
array(0x010,0x024,0x00e,0x020,0x010,0x012,0x00e,0x010,0x002,0x001,0x002,0x020,0x018,0x001,0x020,0x018), // 16x36
array(0x010,0x030,0x00e,0x02c,0x010,0x018,0x00e,0x016,0x002,0x001,0x002,0x031,0x01c,0x001,0x031,0x01c) // 16x48
);

/**
Expand Down Expand Up @@ -230,27 +237,47 @@ class Datamatrix {
* This is the class constructor.
* Creates a datamatrix object
* @param string $code Code to represent using Datamatrix.
* @param string $shape Shape of datamatrix code (R = rectangular, S = square)
* @public
*/
public function __construct($code) {
public function __construct($code, $shape = 'S') {
$barcode_array = array();
if ((is_null($code)) OR ($code == '\0') OR ($code == '')) {
return false;
}
// store code shape
if($shape == 'R') {
$this->rectangular = true;
}
// get data codewords
$cw = $this->getHighLevelEncoding($code);
// number of data codewords
$nd = count($cw);
// check size
if ($nd > 1558) {
return false;
}
// get minimum required matrix size.
foreach ($this->symbattr as $params) {
if ($params[11] >= $nd) {
break;
if($this->rectangular === false) {
// check size
if ($nd > 1558) {
return false;
}
// get minimum required matrix size.
foreach ($this->symbattr as $params) {
if ($params[11] >= $nd) {
break;
}
}
} else {
// check size
if ($nd > 49) {
return false;
}
// get minimum required matrix size.
for($i = 24; $i < sizeof($this->symbattr); $i++) {
$params = $this->symbattr[$i];
if ($params[11] >= $nd) {
break;
}
}
}

if ($params[11] < $nd) {
// too much data
return false;
Expand Down Expand Up @@ -280,11 +307,14 @@ public function __construct($code) {
// add error correction codewords
$cw = $this->getErrorCorrection($cw, $params[13], $params[14], $params[15]);
// initialize empty arrays
$grid = array_fill(0, ($params[2] * $params[3]), 0);
$grid = array();
for($r = 0; $r < $params[0]; $r++)
{
$grid[] = array_fill(0, $params[1], 0);
}
// get placement map
$places = $this->getPlacementMap($params[2], $params[3]);
// fill the grid with data
$grid = array();
$i = 0;
// region data row max index
$rdri = ($params[4] - 1);
Expand Down Expand Up @@ -445,7 +475,7 @@ protected function getErrorCorrection($wd, $nb, $nd, $nc, $gf=256, $pp=301) {
*/
protected function get253StateCodeword($cwpad, $cwpos) {
$pad = ($cwpad + (((149 * $cwpos) % 253) + 1));
if ($pad > 254) {
if ($pad > 253) {
$pad -= 254;
}
return $pad;
Expand Down Expand Up @@ -690,9 +720,18 @@ protected function getSwitchEncodingCodeword($mode) {
* @protected
*/
protected function getMaxDataCodewords($numcw) {
foreach ($this->symbattr as $key => $matrix) {
if ($matrix[11] >= $numcw) {
return $matrix[11];
if($this->rectangular === false) {
foreach ($this->symbattr as $key => $matrix) {
if ($matrix[11] >= $numcw) {
return $matrix[11];
}
}
} else {
for($i = 24; $i < sizeof($this->symbattr); $i++) {
$params = $this->symbattr[$i];
if ($params[11] >= $numcw) {
return $params[11];
}
}
}
return 0;
Expand Down
9 changes: 5 additions & 4 deletions tcpdf_barcodes_2d.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// File name : tcpdf_barcodes_2d.php
// Version : 1.0.015
// Begin : 2009-04-07
// Last Update : 2014-05-20
// Last Update : 2024-01-19
// Author : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
// Author : Urs Wettstein (implementation of rectangular data matrix code)
// License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
// -------------------------------------------------------------------
// Copyright (C) 2009-2014 Nicola Asuni - Tecnick.com LTD
Expand Down Expand Up @@ -63,7 +64,7 @@ class TCPDF2DBarcode {
* <li>$arrcode['num_cols'] required number of columns</li>
* <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul>
* @param string $code code to print
* @param string $type type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parameters are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
* @param string $type type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>DATAMATRIX,R : Datamatrix rectangular (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parameters are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
*/
public function __construct($code, $type) {
$this->setBarcode($code, $type);
Expand Down Expand Up @@ -246,7 +247,7 @@ public function getBarcodePngData($w=3, $h=3, $color=array(0,0,0)) {
/**
* Set the barcode.
* @param string $code code to print
* @param string $type type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parameters are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
* @param string $type type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>DATAMATRIX,R : Datamatrix rectangular (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parameters are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
* @return void
*/
public function setBarcode($code, $type) {
Expand All @@ -255,7 +256,7 @@ public function setBarcode($code, $type) {
switch ($qrtype) {
case 'DATAMATRIX': { // DATAMATRIX (ISO/IEC 16022)
require_once(dirname(__FILE__).'/include/barcodes/datamatrix.php');
$qrcode = new Datamatrix($code);
$qrcode = new Datamatrix($code, count($mode) > 1 ? $mode[1] : 'S');
$this->barcode_array = $qrcode->getBarcodeArray();
$this->barcode_array['code'] = $code;
break;
Expand Down

0 comments on commit 1eca731

Please sign in to comment.