You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1176 lines
42 KiB

  1. <?php
  2. //============================================================+
  3. // File name : datamatrix.php
  4. // Version : 1.0.008
  5. // Begin : 2010-06-07
  6. // Last Update : 2014-05-06
  7. // Author : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
  8. // License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
  9. // -------------------------------------------------------------------
  10. // Copyright (C) 2010-2014 Nicola Asuni - Tecnick.com LTD
  11. //
  12. // This file is part of TCPDF software library.
  13. //
  14. // TCPDF is free software: you can redistribute it and/or modify it
  15. // under the terms of the GNU Lesser General Public License as
  16. // published by the Free Software Foundation, either version 3 of the
  17. // License, or (at your option) any later version.
  18. //
  19. // TCPDF is distributed in the hope that it will be useful, but
  20. // WITHOUT ANY WARRANTY; without even the implied warranty of
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. // See the GNU Lesser General Public License for more details.
  23. //
  24. // You should have received a copy of the GNU Lesser General Public License
  25. // along with TCPDF. If not, see <http://www.gnu.org/licenses/>.
  26. //
  27. // See LICENSE.TXT file for more information.
  28. // -------------------------------------------------------------------
  29. //
  30. // DESCRIPTION :
  31. //
  32. // Class to create DataMatrix ECC 200 barcode arrays for TCPDF class.
  33. // DataMatrix (ISO/IEC 16022:2006) is a 2-dimensional bar code.
  34. //============================================================+
  35. /**
  36. * @file
  37. * Class to create DataMatrix ECC 200 barcode arrays for TCPDF class.
  38. * DataMatrix (ISO/IEC 16022:2006) is a 2-dimensional bar code.
  39. *
  40. * @package com.tecnick.tcpdf
  41. * @author Nicola Asuni
  42. * @version 1.0.008
  43. */
  44. // custom definitions
  45. if (!defined('DATAMATRIXDEFS')) {
  46. /**
  47. * Indicate that definitions for this class are set
  48. */
  49. define('DATAMATRIXDEFS', true);
  50. // -----------------------------------------------------
  51. } // end of custom definitions
  52. // #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
  53. /**
  54. * ASCII encoding: ASCII character 0 to 127 (1 byte per CW)
  55. */
  56. define('ENC_ASCII', 0);
  57. /**
  58. * C40 encoding: Upper-case alphanumeric (3/2 bytes per CW)
  59. */
  60. define('ENC_C40', 1);
  61. /**
  62. * TEXT encoding: Lower-case alphanumeric (3/2 bytes per CW)
  63. */
  64. define('ENC_TXT', 2);
  65. /**
  66. * X12 encoding: ANSI X12 (3/2 byte per CW)
  67. */
  68. define('ENC_X12', 3);
  69. /**
  70. * EDIFACT encoding: ASCII character 32 to 94 (4/3 bytes per CW)
  71. */
  72. define('ENC_EDF', 4);
  73. /**
  74. * BASE 256 encoding: ASCII character 0 to 255 (1 byte per CW)
  75. */
  76. define('ENC_BASE256', 5);
  77. /**
  78. * ASCII extended encoding: ASCII character 128 to 255 (1/2 byte per CW)
  79. */
  80. define('ENC_ASCII_EXT', 6);
  81. /**
  82. * ASCII number encoding: ASCII digits (2 bytes per CW)
  83. */
  84. define('ENC_ASCII_NUM', 7);
  85. /**
  86. * @class Datamatrix
  87. * Class to create DataMatrix ECC 200 barcode arrays for TCPDF class.
  88. * DataMatrix (ISO/IEC 16022:2006) is a 2-dimensional bar code.
  89. *
  90. * @package com.tecnick.tcpdf
  91. * @author Nicola Asuni
  92. * @version 1.0.004
  93. */
  94. class Datamatrix {
  95. /**
  96. * Barcode array to be returned which is readable by TCPDF.
  97. * @protected
  98. */
  99. protected $barcode_array = array();
  100. /**
  101. * Store last used encoding for data codewords.
  102. * @protected
  103. */
  104. protected $last_enc = ENC_ASCII;
  105. /**
  106. * Table of Data Matrix ECC 200 Symbol Attributes:<ul>
  107. * <li>total matrix rows (including finder pattern)</li>
  108. * <li>total matrix cols (including finder pattern)</li>
  109. * <li>total matrix rows (without finder pattern)</li>
  110. * <li>total matrix cols (without finder pattern)</li>
  111. * <li>region data rows (with finder pattern)</li>
  112. * <li>region data col (with finder pattern)</li>
  113. * <li>region data rows (without finder pattern)</li>
  114. * <li>region data col (without finder pattern)</li>
  115. * <li>horizontal regions</li>
  116. * <li>vertical regions</li>
  117. * <li>regions</li>
  118. * <li>data codewords</li>
  119. * <li>error codewords</li>
  120. * <li>blocks</li>
  121. * <li>data codewords per block</li>
  122. * <li>error codewords per block</li>
  123. * </ul>
  124. * @protected
  125. */
  126. protected $symbattr = array(
  127. // square form ---------------------------------------------------------------------------------------
  128. array(0x00a,0x00a,0x008,0x008,0x00a,0x00a,0x008,0x008,0x001,0x001,0x001,0x003,0x005,0x001,0x003,0x005), // 10x10
  129. array(0x00c,0x00c,0x00a,0x00a,0x00c,0x00c,0x00a,0x00a,0x001,0x001,0x001,0x005,0x007,0x001,0x005,0x007), // 12x12
  130. array(0x00e,0x00e,0x00c,0x00c,0x00e,0x00e,0x00c,0x00c,0x001,0x001,0x001,0x008,0x00a,0x001,0x008,0x00a), // 14x14
  131. array(0x010,0x010,0x00e,0x00e,0x010,0x010,0x00e,0x00e,0x001,0x001,0x001,0x00c,0x00c,0x001,0x00c,0x00c), // 16x16
  132. array(0x012,0x012,0x010,0x010,0x012,0x012,0x010,0x010,0x001,0x001,0x001,0x012,0x00e,0x001,0x012,0x00e), // 18x18
  133. array(0x014,0x014,0x012,0x012,0x014,0x014,0x012,0x012,0x001,0x001,0x001,0x016,0x012,0x001,0x016,0x012), // 20x20
  134. array(0x016,0x016,0x014,0x014,0x016,0x016,0x014,0x014,0x001,0x001,0x001,0x01e,0x014,0x001,0x01e,0x014), // 22x22
  135. array(0x018,0x018,0x016,0x016,0x018,0x018,0x016,0x016,0x001,0x001,0x001,0x024,0x018,0x001,0x024,0x018), // 24x24
  136. array(0x01a,0x01a,0x018,0x018,0x01a,0x01a,0x018,0x018,0x001,0x001,0x001,0x02c,0x01c,0x001,0x02c,0x01c), // 26x26
  137. array(0x020,0x020,0x01c,0x01c,0x010,0x010,0x00e,0x00e,0x002,0x002,0x004,0x03e,0x024,0x001,0x03e,0x024), // 32x32
  138. array(0x024,0x024,0x020,0x020,0x012,0x012,0x010,0x010,0x002,0x002,0x004,0x056,0x02a,0x001,0x056,0x02a), // 36x36
  139. array(0x028,0x028,0x024,0x024,0x014,0x014,0x012,0x012,0x002,0x002,0x004,0x072,0x030,0x001,0x072,0x030), // 40x40
  140. array(0x02c,0x02c,0x028,0x028,0x016,0x016,0x014,0x014,0x002,0x002,0x004,0x090,0x038,0x001,0x090,0x038), // 44x44
  141. array(0x030,0x030,0x02c,0x02c,0x018,0x018,0x016,0x016,0x002,0x002,0x004,0x0ae,0x044,0x001,0x0ae,0x044), // 48x48
  142. array(0x034,0x034,0x030,0x030,0x01a,0x01a,0x018,0x018,0x002,0x002,0x004,0x0cc,0x054,0x002,0x066,0x02a), // 52x52
  143. array(0x040,0x040,0x038,0x038,0x010,0x010,0x00e,0x00e,0x004,0x004,0x010,0x118,0x070,0x002,0x08c,0x038), // 64x64
  144. array(0x048,0x048,0x040,0x040,0x012,0x012,0x010,0x010,0x004,0x004,0x010,0x170,0x090,0x004,0x05c,0x024), // 72x72
  145. array(0x050,0x050,0x048,0x048,0x014,0x014,0x012,0x012,0x004,0x004,0x010,0x1c8,0x0c0,0x004,0x072,0x030), // 80x80
  146. array(0x058,0x058,0x050,0x050,0x016,0x016,0x014,0x014,0x004,0x004,0x010,0x240,0x0e0,0x004,0x090,0x038), // 88x88
  147. array(0x060,0x060,0x058,0x058,0x018,0x018,0x016,0x016,0x004,0x004,0x010,0x2b8,0x110,0x004,0x0ae,0x044), // 96x96
  148. array(0x068,0x068,0x060,0x060,0x01a,0x01a,0x018,0x018,0x004,0x004,0x010,0x330,0x150,0x006,0x088,0x038), // 104x104
  149. array(0x078,0x078,0x06c,0x06c,0x014,0x014,0x012,0x012,0x006,0x006,0x024,0x41a,0x198,0x006,0x0af,0x044), // 120x120
  150. array(0x084,0x084,0x078,0x078,0x016,0x016,0x014,0x014,0x006,0x006,0x024,0x518,0x1f0,0x008,0x0a3,0x03e), // 132x132
  151. array(0x090,0x090,0x084,0x084,0x018,0x018,0x016,0x016,0x006,0x006,0x024,0x616,0x26c,0x00a,0x09c,0x03e), // 144x144
  152. // rectangular form (currently unused) ---------------------------------------------------------------------------
  153. array(0x008,0x012,0x006,0x010,0x008,0x012,0x006,0x010,0x001,0x001,0x001,0x005,0x007,0x001,0x005,0x007), // 8x18
  154. array(0x008,0x020,0x006,0x01c,0x008,0x010,0x006,0x00e,0x001,0x002,0x002,0x00a,0x00b,0x001,0x00a,0x00b), // 8x32
  155. array(0x00c,0x01a,0x00a,0x018,0x00c,0x01a,0x00a,0x018,0x001,0x001,0x001,0x010,0x00e,0x001,0x010,0x00e), // 12x26
  156. array(0x00c,0x024,0x00a,0x020,0x00c,0x012,0x00a,0x010,0x001,0x002,0x002,0x00c,0x012,0x001,0x00c,0x012), // 12x36
  157. array(0x010,0x024,0x00e,0x020,0x010,0x012,0x00e,0x010,0x001,0x002,0x002,0x020,0x018,0x001,0x020,0x018), // 16x36
  158. array(0x010,0x030,0x00e,0x02c,0x010,0x018,0x00e,0x016,0x001,0x002,0x002,0x031,0x01c,0x001,0x031,0x01c) // 16x48
  159. );
  160. /**
  161. * Map encodation modes whit character sets.
  162. * @protected
  163. */
  164. protected $chset_id = array(ENC_C40 => 'C40', ENC_TXT => 'TXT', ENC_X12 =>'X12');
  165. /**
  166. * Basic set of characters for each encodation mode.
  167. * @protected
  168. */
  169. protected $chset = array(
  170. 'C40' => array( // Basic set for C40 ----------------------------------------------------------------------------
  171. 'S1'=>0x00,'S2'=>0x01,'S3'=>0x02,0x20=>0x03,0x30=>0x04,0x31=>0x05,0x32=>0x06,0x33=>0x07,0x34=>0x08,0x35=>0x09, //
  172. 0x36=>0x0a,0x37=>0x0b,0x38=>0x0c,0x39=>0x0d,0x41=>0x0e,0x42=>0x0f,0x43=>0x10,0x44=>0x11,0x45=>0x12,0x46=>0x13, //
  173. 0x47=>0x14,0x48=>0x15,0x49=>0x16,0x4a=>0x17,0x4b=>0x18,0x4c=>0x19,0x4d=>0x1a,0x4e=>0x1b,0x4f=>0x1c,0x50=>0x1d, //
  174. 0x51=>0x1e,0x52=>0x1f,0x53=>0x20,0x54=>0x21,0x55=>0x22,0x56=>0x23,0x57=>0x24,0x58=>0x25,0x59=>0x26,0x5a=>0x27),//
  175. 'TXT' => array( // Basic set for TEXT ---------------------------------------------------------------------------
  176. 'S1'=>0x00,'S2'=>0x01,'S3'=>0x02,0x20=>0x03,0x30=>0x04,0x31=>0x05,0x32=>0x06,0x33=>0x07,0x34=>0x08,0x35=>0x09, //
  177. 0x36=>0x0a,0x37=>0x0b,0x38=>0x0c,0x39=>0x0d,0x61=>0x0e,0x62=>0x0f,0x63=>0x10,0x64=>0x11,0x65=>0x12,0x66=>0x13, //
  178. 0x67=>0x14,0x68=>0x15,0x69=>0x16,0x6a=>0x17,0x6b=>0x18,0x6c=>0x19,0x6d=>0x1a,0x6e=>0x1b,0x6f=>0x1c,0x70=>0x1d, //
  179. 0x71=>0x1e,0x72=>0x1f,0x73=>0x20,0x74=>0x21,0x75=>0x22,0x76=>0x23,0x77=>0x24,0x78=>0x25,0x79=>0x26,0x7a=>0x27),//
  180. 'SH1' => array( // Shift 1 set ----------------------------------------------------------------------------------
  181. 0x00=>0x00,0x01=>0x01,0x02=>0x02,0x03=>0x03,0x04=>0x04,0x05=>0x05,0x06=>0x06,0x07=>0x07,0x08=>0x08,0x09=>0x09, //
  182. 0x0a=>0x0a,0x0b=>0x0b,0x0c=>0x0c,0x0d=>0x0d,0x0e=>0x0e,0x0f=>0x0f,0x10=>0x10,0x11=>0x11,0x12=>0x12,0x13=>0x13, //
  183. 0x14=>0x14,0x15=>0x15,0x16=>0x16,0x17=>0x17,0x18=>0x18,0x19=>0x19,0x1a=>0x1a,0x1b=>0x1b,0x1c=>0x1c,0x1d=>0x1d, //
  184. 0x1e=>0x1e,0x1f=>0x1f), //
  185. 'SH2' => array( // Shift 2 set ----------------------------------------------------------------------------------
  186. 0x21=>0x00,0x22=>0x01,0x23=>0x02,0x24=>0x03,0x25=>0x04,0x26=>0x05,0x27=>0x06,0x28=>0x07,0x29=>0x08,0x2a=>0x09, //
  187. 0x2b=>0x0a,0x2c=>0x0b,0x2d=>0x0c,0x2e=>0x0d,0x2f=>0x0e,0x3a=>0x0f,0x3b=>0x10,0x3c=>0x11,0x3d=>0x12,0x3e=>0x13, //
  188. 0x3f=>0x14,0x40=>0x15,0x5b=>0x16,0x5c=>0x17,0x5d=>0x18,0x5e=>0x19,0x5f=>0x1a,'F1'=>0x1b,'US'=>0x1e), //
  189. 'S3C' => array( // Shift 3 set for C40 --------------------------------------------------------------------------
  190. 0x60=>0x00,0x61=>0x01,0x62=>0x02,0x63=>0x03,0x64=>0x04,0x65=>0x05,0x66=>0x06,0x67=>0x07,0x68=>0x08,0x69=>0x09, //
  191. 0x6a=>0x0a,0x6b=>0x0b,0x6c=>0x0c,0x6d=>0x0d,0x6e=>0x0e,0x6f=>0x0f,0x70=>0x10,0x71=>0x11,0x72=>0x12,0x73=>0x13, //
  192. 0x74=>0x14,0x75=>0x15,0x76=>0x16,0x77=>0x17,0x78=>0x18,0x79=>0x19,0x7a=>0x1a,0x7b=>0x1b,0x7c=>0x1c,0x7d=>0x1d, //
  193. 0x7e=>0x1e,0x7f=>0x1f),
  194. 'S3T' => array( // Shift 3 set for TEXT -------------------------------------------------------------------------
  195. 0x60=>0x00,0x41=>0x01,0x42=>0x02,0x43=>0x03,0x44=>0x04,0x45=>0x05,0x46=>0x06,0x47=>0x07,0x48=>0x08,0x49=>0x09, //
  196. 0x4a=>0x0a,0x4b=>0x0b,0x4c=>0x0c,0x4d=>0x0d,0x4e=>0x0e,0x4f=>0x0f,0x50=>0x10,0x51=>0x11,0x52=>0x12,0x53=>0x13, //
  197. 0x54=>0x14,0x55=>0x15,0x56=>0x16,0x57=>0x17,0x58=>0x18,0x59=>0x19,0x5a=>0x1a,0x7b=>0x1b,0x7c=>0x1c,0x7d=>0x1d, //
  198. 0x7e=>0x1e,0x7f=>0x1f), //
  199. 'X12' => array( // Set for X12 ----------------------------------------------------------------------------------
  200. 0x0d=>0x00,0x2a=>0x01,0x3e=>0x02,0x20=>0x03,0x30=>0x04,0x31=>0x05,0x32=>0x06,0x33=>0x07,0x34=>0x08,0x35=>0x09, //
  201. 0x36=>0x0a,0x37=>0x0b,0x38=>0x0c,0x39=>0x0d,0x41=>0x0e,0x42=>0x0f,0x43=>0x10,0x44=>0x11,0x45=>0x12,0x46=>0x13, //
  202. 0x47=>0x14,0x48=>0x15,0x49=>0x16,0x4a=>0x17,0x4b=>0x18,0x4c=>0x19,0x4d=>0x1a,0x4e=>0x1b,0x4f=>0x1c,0x50=>0x1d, //
  203. 0x51=>0x1e,0x52=>0x1f,0x53=>0x20,0x54=>0x21,0x55=>0x22,0x56=>0x23,0x57=>0x24,0x58=>0x25,0x59=>0x26,0x5a=>0x27) //
  204. );
  205. // -----------------------------------------------------------------------------
  206. /**
  207. * This is the class constructor.
  208. * Creates a datamatrix object
  209. * @param $code (string) Code to represent using Datamatrix.
  210. * @public
  211. */
  212. public function __construct($code) {
  213. $barcode_array = array();
  214. if ((is_null($code)) OR ($code == '\0') OR ($code == '')) {
  215. return false;
  216. }
  217. // get data codewords
  218. $cw = $this->getHighLevelEncoding($code);
  219. // number of data codewords
  220. $nd = count($cw);
  221. // check size
  222. if ($nd > 1558) {
  223. return false;
  224. }
  225. // get minimum required matrix size.
  226. foreach ($this->symbattr as $params) {
  227. if ($params[11] >= $nd) {
  228. break;
  229. }
  230. }
  231. if ($params[11] < $nd) {
  232. // too much data
  233. return false;
  234. } elseif ($params[11] > $nd) {
  235. // add padding
  236. if ((($params[11] - $nd) > 1) AND ($cw[($nd - 1)] != 254)) {
  237. if ($this->last_enc == ENC_EDF) {
  238. // switch to ASCII encoding
  239. $cw[] = 124;
  240. ++$nd;
  241. } elseif (($this->last_enc != ENC_ASCII) AND ($this->last_enc != ENC_BASE256)) {
  242. // switch to ASCII encoding
  243. $cw[] = 254;
  244. ++$nd;
  245. }
  246. }
  247. if ($params[11] > $nd) {
  248. // add first pad
  249. $cw[] = 129;
  250. ++$nd;
  251. // add remaining pads
  252. for ($i = $nd; $i < $params[11]; ++$i) {
  253. $cw[] = $this->get253StateCodeword(129, $i);
  254. }
  255. }
  256. }
  257. // add error correction codewords
  258. $cw = $this->getErrorCorrection($cw, $params[13], $params[14], $params[15]);
  259. // initialize empty arrays
  260. $grid = array_fill(0, ($params[2] * $params[3]), 0);
  261. // get placement map
  262. $places = $this->getPlacementMap($params[2], $params[3]);
  263. // fill the grid with data
  264. $grid = array();
  265. $i = 0;
  266. // region data row max index
  267. $rdri = ($params[4] - 1);
  268. // region data column max index
  269. $rdci = ($params[5] - 1);
  270. // for each vertical region
  271. for ($vr = 0; $vr < $params[9]; ++$vr) {
  272. // for each row on region
  273. for ($r = 0; $r < $params[4]; ++$r) {
  274. // get row
  275. $row = (($vr * $params[4]) + $r);
  276. // for each horizontal region
  277. for ($hr = 0; $hr < $params[8]; ++$hr) {
  278. // for each column on region
  279. for ($c = 0; $c < $params[5]; ++$c) {
  280. // get column
  281. $col = (($hr * $params[5]) + $c);
  282. // braw bits by case
  283. if ($r == 0) {
  284. // top finder pattern
  285. if ($c % 2) {
  286. $grid[$row][$col] = 0;
  287. } else {
  288. $grid[$row][$col] = 1;
  289. }
  290. } elseif ($r == $rdri) {
  291. // bottom finder pattern
  292. $grid[$row][$col] = 1;
  293. } elseif ($c == 0) {
  294. // left finder pattern
  295. $grid[$row][$col] = 1;
  296. } elseif ($c == $rdci) {
  297. // right finder pattern
  298. if ($r % 2) {
  299. $grid[$row][$col] = 1;
  300. } else {
  301. $grid[$row][$col] = 0;
  302. }
  303. } else { // data bit
  304. if ($places[$i] < 2) {
  305. $grid[$row][$col] = $places[$i];
  306. } else {
  307. // codeword ID
  308. $cw_id = (floor($places[$i] / 10) - 1);
  309. // codeword BIT mask
  310. $cw_bit = pow(2, (8 - ($places[$i] % 10)));
  311. $grid[$row][$col] = (($cw[$cw_id] & $cw_bit) == 0) ? 0 : 1;
  312. }
  313. ++$i;
  314. }
  315. }
  316. }
  317. }
  318. }
  319. $this->barcode_array['num_rows'] = $params[0];
  320. $this->barcode_array['num_cols'] = $params[1];
  321. $this->barcode_array['bcode'] = $grid;
  322. }
  323. /**
  324. * Returns a barcode array which is readable by TCPDF
  325. * @return array barcode array readable by TCPDF;
  326. * @public
  327. */
  328. public function getBarcodeArray() {
  329. return $this->barcode_array;
  330. }
  331. /**
  332. * Product of two numbers in a Power-of-Two Galois Field
  333. * @param $a (int) first number to multiply.
  334. * @param $b (int) second number to multiply.
  335. * @param $log (array) Log table.
  336. * @param $alog (array) Anti-Log table.
  337. * @param $gf (array) Number of Factors of the Reed-Solomon polynomial.
  338. * @return int product
  339. * @protected
  340. */
  341. protected function getGFProduct($a, $b, $log, $alog, $gf) {
  342. if (($a == 0) OR ($b == 0)) {
  343. return 0;
  344. }
  345. return ($alog[($log[$a] + $log[$b]) % ($gf - 1)]);
  346. }
  347. /**
  348. * Add error correction codewords to data codewords array (ANNEX E).
  349. * @param $wd (array) Array of datacodewords.
  350. * @param $nb (int) Number of blocks.
  351. * @param $nd (int) Number of data codewords per block.
  352. * @param $nc (int) Number of correction codewords per block.
  353. * @param $gf (int) numner of fields on log/antilog table (power of 2).
  354. * @param $pp (int) The value of its prime modulus polynomial (301 for ECC200).
  355. * @return array data codewords + error codewords
  356. * @protected
  357. */
  358. protected function getErrorCorrection($wd, $nb, $nd, $nc, $gf=256, $pp=301) {
  359. // generate the log ($log) and antilog ($alog) tables
  360. $log[0] = 0;
  361. $alog[0] = 1;
  362. for ($i = 1; $i < $gf; ++$i) {
  363. $alog[$i] = ($alog[($i - 1)] * 2);
  364. if ($alog[$i] >= $gf) {
  365. $alog[$i] ^= $pp;
  366. }
  367. $log[$alog[$i]] = $i;
  368. }
  369. ksort($log);
  370. // generate the polynomial coefficients (c)
  371. $c = array_fill(0, ($nc + 1), 0);
  372. $c[0] = 1;
  373. for ($i = 1; $i <= $nc; ++$i) {
  374. $c[$i] = $c[($i-1)];
  375. for ($j = ($i - 1); $j >= 1; --$j) {
  376. $c[$j] = $c[($j - 1)] ^ $this->getGFProduct($c[$j], $alog[$i], $log, $alog, $gf);
  377. }
  378. $c[0] = $this->getGFProduct($c[0], $alog[$i], $log, $alog, $gf);
  379. }
  380. ksort($c);
  381. // total number of data codewords
  382. $num_wd = ($nb * $nd);
  383. // total number of error codewords
  384. $num_we = ($nb * $nc);
  385. // for each block
  386. for ($b = 0; $b < $nb; ++$b) {
  387. // create interleaved data block
  388. $block = array();
  389. for ($n = $b; $n < $num_wd; $n += $nb) {
  390. $block[] = $wd[$n];
  391. }
  392. // initialize error codewords
  393. $we = array_fill(0, ($nc + 1), 0);
  394. // calculate error correction codewords for this block
  395. for ($i = 0; $i < $nd; ++$i) {
  396. $k = ($we[0] ^ $block[$i]);
  397. for ($j = 0; $j < $nc; ++$j) {
  398. $we[$j] = ($we[($j + 1)] ^ $this->getGFProduct($k, $c[($nc - $j - 1)], $log, $alog, $gf));
  399. }
  400. }
  401. // add error codewords at the end of data codewords
  402. $j = 0;
  403. for ($i = $b; $i < $num_we; $i += $nb) {
  404. $wd[($num_wd + $i)] = $we[$j];
  405. ++$j;
  406. }
  407. }
  408. // reorder codewords
  409. ksort($wd);
  410. return $wd;
  411. }
  412. /**
  413. * Return the 253-state codeword
  414. * @param $cwpad (int) Pad codeword.
  415. * @param $cwpos (int) Number of data codewords from the beginning of encoded data.
  416. * @return pad codeword
  417. * @protected
  418. */
  419. protected function get253StateCodeword($cwpad, $cwpos) {
  420. $pad = ($cwpad + (((149 * $cwpos) % 253) + 1));
  421. if ($pad > 254) {
  422. $pad -= 254;
  423. }
  424. return $pad;
  425. }
  426. /**
  427. * Return the 255-state codeword
  428. * @param $cwpad (int) Pad codeword.
  429. * @param $cwpos (int) Number of data codewords from the beginning of encoded data.
  430. * @return pad codeword
  431. * @protected
  432. */
  433. protected function get255StateCodeword($cwpad, $cwpos) {
  434. $pad = ($cwpad + (((149 * $cwpos) % 255) + 1));
  435. if ($pad > 255) {
  436. $pad -= 256;
  437. }
  438. return $pad;
  439. }
  440. /**
  441. * Returns true if the char belongs to the selected mode
  442. * @param $chr (int) Character (byte) to check.
  443. * @param $mode (int) Current encoding mode.
  444. * @return boolean true if the char is of the selected mode.
  445. * @protected
  446. */
  447. protected function isCharMode($chr, $mode) {
  448. $status = false;
  449. switch ($mode) {
  450. case ENC_ASCII: { // ASCII character 0 to 127
  451. $status = (($chr >= 0) AND ($chr <= 127));
  452. break;
  453. }
  454. case ENC_C40: { // Upper-case alphanumeric
  455. $status = (($chr == 32) OR (($chr >= 48) AND ($chr <= 57)) OR (($chr >= 65) AND ($chr <= 90)));
  456. break;
  457. }
  458. case ENC_TXT: { // Lower-case alphanumeric
  459. $status = (($chr == 32) OR (($chr >= 48) AND ($chr <= 57)) OR (($chr >= 97) AND ($chr <= 122)));
  460. break;
  461. }
  462. case ENC_X12: { // ANSI X12
  463. $status = (($chr == 13) OR ($chr == 42) OR ($chr == 62));
  464. break;
  465. }
  466. case ENC_EDF: { // ASCII character 32 to 94
  467. $status = (($chr >= 32) AND ($chr <= 94));
  468. break;
  469. }
  470. case ENC_BASE256: { // Function character (FNC1, Structured Append, Reader Program, or Code Page)
  471. $status = (($chr == 232) OR ($chr == 233) OR ($chr == 234) OR ($chr == 241));
  472. break;
  473. }
  474. case ENC_ASCII_EXT: { // ASCII character 128 to 255
  475. $status = (($chr >= 128) AND ($chr <= 255));
  476. break;
  477. }
  478. case ENC_ASCII_NUM: { // ASCII digits
  479. $status = (($chr >= 48) AND ($chr <= 57));
  480. break;
  481. }
  482. }
  483. return $status;
  484. }
  485. /**
  486. * The look-ahead test scans the data to be encoded to find the best mode (Annex P - steps from J to S).
  487. * @param $data (string) data to encode
  488. * @param $pos (int) current position
  489. * @param $mode (int) current encoding mode
  490. * @return int encoding mode
  491. * @protected
  492. */
  493. protected function lookAheadTest($data, $pos, $mode) {
  494. $data_length = strlen($data);
  495. if ($pos >= $data_length) {
  496. return $mode;
  497. }
  498. $charscount = 0; // count processed chars
  499. // STEP J
  500. if ($mode == ENC_ASCII) {
  501. $numch = array(0, 1, 1, 1, 1, 1.25);
  502. } else {
  503. $numch = array(1, 2, 2, 2, 2, 2.25);
  504. $numch[$mode] = 0;
  505. }
  506. while (true) {
  507. // STEP K
  508. if (($pos + $charscount) == $data_length) {
  509. if ($numch[ENC_ASCII] <= ceil(min($numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_EDF], $numch[ENC_BASE256]))) {
  510. return ENC_ASCII;
  511. }
  512. if ($numch[ENC_BASE256] < ceil(min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_EDF]))) {
  513. return ENC_BASE256;
  514. }
  515. if ($numch[ENC_EDF] < ceil(min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_BASE256]))) {
  516. return ENC_EDF;
  517. }
  518. if ($numch[ENC_TXT] < ceil(min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_X12], $numch[ENC_EDF], $numch[ENC_BASE256]))) {
  519. return ENC_TXT;
  520. }
  521. if ($numch[ENC_X12] < ceil(min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_EDF], $numch[ENC_BASE256]))) {
  522. return ENC_X12;
  523. }
  524. return ENC_C40;
  525. }
  526. // get char
  527. $chr = ord($data[$pos + $charscount]);
  528. $charscount++;
  529. // STEP L
  530. if ($this->isCharMode($chr, ENC_ASCII_NUM)) {
  531. $numch[ENC_ASCII] += (1 / 2);
  532. } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
  533. $numch[ENC_ASCII] = ceil($numch[ENC_ASCII]);
  534. $numch[ENC_ASCII] += 2;
  535. } else {
  536. $numch[ENC_ASCII] = ceil($numch[ENC_ASCII]);
  537. $numch[ENC_ASCII] += 1;
  538. }
  539. // STEP M
  540. if ($this->isCharMode($chr, ENC_C40)) {
  541. $numch[ENC_C40] += (2 / 3);
  542. } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
  543. $numch[ENC_C40] += (8 / 3);
  544. } else {
  545. $numch[ENC_C40] += (4 / 3);
  546. }
  547. // STEP N
  548. if ($this->isCharMode($chr, ENC_TXT)) {
  549. $numch[ENC_TXT] += (2 / 3);
  550. } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
  551. $numch[ENC_TXT] += (8 / 3);
  552. } else {
  553. $numch[ENC_TXT] += (4 / 3);
  554. }
  555. // STEP O
  556. if ($this->isCharMode($chr, ENC_X12) OR $this->isCharMode($chr, ENC_C40)) {
  557. $numch[ENC_X12] += (2 / 3);
  558. } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
  559. $numch[ENC_X12] += (13 / 3);
  560. } else {
  561. $numch[ENC_X12] += (10 / 3);
  562. }
  563. // STEP P
  564. if ($this->isCharMode($chr, ENC_EDF)) {
  565. $numch[ENC_EDF] += (3 / 4);
  566. } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
  567. $numch[ENC_EDF] += (17 / 4);
  568. } else {
  569. $numch[ENC_EDF] += (13 / 4);
  570. }
  571. // STEP Q
  572. if ($this->isCharMode($chr, ENC_BASE256)) {
  573. $numch[ENC_BASE256] += 4;
  574. } else {
  575. $numch[ENC_BASE256] += 1;
  576. }
  577. // STEP R
  578. if ($charscount >= 4) {
  579. if (($numch[ENC_ASCII] + 1) <= min($numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_EDF], $numch[ENC_BASE256])) {
  580. return ENC_ASCII;
  581. }
  582. if ((($numch[ENC_BASE256] + 1) <= $numch[ENC_ASCII])
  583. OR (($numch[ENC_BASE256] + 1) < min($numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_EDF]))) {
  584. return ENC_BASE256;
  585. }
  586. if (($numch[ENC_EDF] + 1) < min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_BASE256])) {
  587. return ENC_EDF;
  588. }
  589. if (($numch[ENC_TXT] + 1) < min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_X12], $numch[ENC_EDF], $numch[ENC_BASE256])) {
  590. return ENC_TXT;
  591. }
  592. if (($numch[ENC_X12] + 1) < min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_EDF], $numch[ENC_BASE256])) {
  593. return ENC_X12;
  594. }
  595. if (($numch[ENC_C40] + 1) < min($numch[ENC_ASCII], $numch[ENC_TXT], $numch[ENC_EDF], $numch[ENC_BASE256])) {
  596. if ($numch[ENC_C40] < $numch[ENC_X12]) {
  597. return ENC_C40;
  598. }
  599. if ($numch[ENC_C40] == $numch[ENC_X12]) {
  600. $k = ($pos + $charscount + 1);
  601. while ($k < $data_length) {
  602. $tmpchr = ord($data[$k]);
  603. if ($this->isCharMode($tmpchr, ENC_X12)) {
  604. return ENC_X12;
  605. } elseif (!($this->isCharMode($tmpchr, ENC_X12) OR $this->isCharMode($tmpchr, ENC_C40))) {
  606. break;
  607. }
  608. ++$k;
  609. }
  610. return ENC_C40;
  611. }
  612. }
  613. }
  614. } // end of while
  615. }
  616. /**
  617. * Get the switching codeword to a new encoding mode (latch codeword)
  618. * @param $mode (int) New encoding mode.
  619. * @return (int) Switch codeword.
  620. * @protected
  621. */
  622. protected function getSwitchEncodingCodeword($mode) {
  623. switch ($mode) {
  624. case ENC_ASCII: { // ASCII character 0 to 127
  625. $cw = 254;
  626. if ($this->last_enc == ENC_EDF) {
  627. $cw = 124;
  628. }
  629. break;
  630. }
  631. case ENC_C40: { // Upper-case alphanumeric
  632. $cw = 230;
  633. break;
  634. }
  635. case ENC_TXT: { // Lower-case alphanumeric
  636. $cw = 239;
  637. break;
  638. }
  639. case ENC_X12: { // ANSI X12
  640. $cw = 238;
  641. break;
  642. }
  643. case ENC_EDF: { // ASCII character 32 to 94
  644. $cw = 240;
  645. break;
  646. }
  647. case ENC_BASE256: { // Function character (FNC1, Structured Append, Reader Program, or Code Page)
  648. $cw = 231;
  649. break;
  650. }
  651. }
  652. return $cw;
  653. }
  654. /**
  655. * Choose the minimum matrix size and return the max number of data codewords.
  656. * @param $numcw (int) Number of current codewords.
  657. * @return number of data codewords in matrix
  658. * @protected
  659. */
  660. protected function getMaxDataCodewords($numcw) {
  661. foreach ($this->symbattr as $key => $matrix) {
  662. if ($matrix[11] >= $numcw) {
  663. return $matrix[11];
  664. }
  665. }
  666. return 0;
  667. }
  668. /**
  669. * Get high level encoding using the minimum symbol data characters for ECC 200
  670. * @param $data (string) data to encode
  671. * @return array of codewords
  672. * @protected
  673. */
  674. protected function getHighLevelEncoding($data) {
  675. // STEP A. Start in ASCII encodation.
  676. $enc = ENC_ASCII; // current encoding mode
  677. $pos = 0; // current position
  678. $cw = array(); // array of codewords to be returned
  679. $cw_num = 0; // number of data codewords
  680. $data_length = strlen($data); // number of chars
  681. while ($pos < $data_length) {
  682. // set last used encoding
  683. $this->last_enc = $enc;
  684. switch ($enc) {
  685. case ENC_ASCII: { // STEP B. While in ASCII encodation
  686. if (($data_length > 1) AND ($pos < ($data_length - 1)) AND ($this->isCharMode(ord($data[$pos]), ENC_ASCII_NUM) AND $this->isCharMode(ord($data[$pos + 1]), ENC_ASCII_NUM))) {
  687. // 1. If the next data sequence is at least 2 consecutive digits, encode the next two digits as a double digit in ASCII mode.
  688. $cw[] = (intval(substr($data, $pos, 2)) + 130);
  689. ++$cw_num;
  690. $pos += 2;
  691. } else {
  692. // 2. If the look-ahead test (starting at step J) indicates another mode, switch to that mode.
  693. $newenc = $this->lookAheadTest($data, $pos, $enc);
  694. if ($newenc != $enc) {
  695. // switch to new encoding
  696. $enc = $newenc;
  697. $cw[] = $this->getSwitchEncodingCodeword($enc);
  698. ++$cw_num;
  699. } else {
  700. // get new byte
  701. $chr = ord($data[$pos]);
  702. ++$pos;
  703. if ($this->isCharMode($chr, ENC_ASCII_EXT)) {
  704. // 3. If the next data character is extended ASCII (greater than 127) encode it in ASCII mode first using the Upper Shift (value 235) character.
  705. $cw[] = 235;
  706. $cw[] = ($chr - 127);
  707. $cw_num += 2;
  708. } else {
  709. // 4. Otherwise process the next data character in ASCII encodation.
  710. $cw[] = ($chr + 1);
  711. ++$cw_num;
  712. }
  713. }
  714. }
  715. break;
  716. }
  717. case ENC_C40 : // Upper-case alphanumeric
  718. case ENC_TXT : // Lower-case alphanumeric
  719. case ENC_X12 : { // ANSI X12
  720. $temp_cw = array();
  721. $p = 0;
  722. $epos = $pos;
  723. // get charset ID
  724. $set_id = $this->chset_id[$enc];
  725. // get basic charset for current encoding
  726. $charset = $this->chset[$set_id];
  727. do {
  728. // 2. process the next character in C40 encodation.
  729. $chr = ord($data[$epos]);
  730. ++$epos;
  731. // check for extended character
  732. if ($chr & 0x80) {
  733. if ($enc == ENC_X12) {
  734. return false;
  735. }
  736. $chr = ($chr & 0x7f);
  737. $temp_cw[] = 1; // shift 2
  738. $temp_cw[] = 30; // upper shift
  739. $p += 2;
  740. }
  741. if (isset($charset[$chr])) {
  742. $temp_cw[] = $charset[$chr];
  743. ++$p;
  744. } else {
  745. if (isset($this->chset['SH1'][$chr])) {
  746. $temp_cw[] = 0; // shift 1
  747. $shiftset = $this->chset['SH1'];
  748. } elseif (isset($chr, $this->chset['SH2'][$chr])) {
  749. $temp_cw[] = 1; // shift 2
  750. $shiftset = $this->chset['SH2'];
  751. } elseif (($enc == ENC_C40) AND isset($this->chset['S3C'][$chr])) {
  752. $temp_cw[] = 2; // shift 3
  753. $shiftset = $this->chset['S3C'];
  754. } elseif (($enc == ENC_TXT) AND isset($this->chset['S3T'][$chr])) {
  755. $temp_cw[] = 2; // shift 3
  756. $shiftset = $this->chset['S3T'];
  757. } else {
  758. return false;
  759. }
  760. $temp_cw[] = $shiftset[$chr];
  761. $p += 2;
  762. }
  763. if ($p >= 3) {
  764. $c1 = array_shift($temp_cw);
  765. $c2 = array_shift($temp_cw);
  766. $c3 = array_shift($temp_cw);
  767. $p -= 3;
  768. $tmp = ((1600 * $c1) + (40 * $c2) + $c3 + 1);
  769. $cw[] = ($tmp >> 8);
  770. $cw[] = ($tmp % 256);
  771. $cw_num += 2;
  772. $pos = $epos;
  773. // 1. If the C40 encoding is at the point of starting a new double symbol character and if the look-ahead test (starting at step J) indicates another mode, switch to that mode.
  774. $newenc = $this->lookAheadTest($data, $pos, $enc);
  775. if ($newenc != $enc) {
  776. // switch to new encoding
  777. $enc = $newenc;
  778. if ($enc != ENC_ASCII) {
  779. // set unlatch character
  780. $cw[] = $this->getSwitchEncodingCodeword(ENC_ASCII);
  781. ++$cw_num;
  782. }
  783. $cw[] = $this->getSwitchEncodingCodeword($enc);
  784. ++$cw_num;
  785. $pos -= $p;
  786. $p = 0;
  787. break;
  788. }
  789. }
  790. } while (($p > 0) AND ($epos < $data_length));
  791. // process last data (if any)
  792. if ($p > 0) {
  793. // get remaining number of data symbols
  794. $cwr = ($this->getMaxDataCodewords($cw_num) - $cw_num);
  795. if (($cwr == 1) AND ($p == 1)) {
  796. // d. If one symbol character remains and one C40 value (data character) remains to be encoded
  797. $c1 = array_shift($temp_cw);
  798. --$p;
  799. $cw[] = ($chr + 1);
  800. ++$cw_num;
  801. $pos = $epos;
  802. $enc = ENC_ASCII;
  803. $this->last_enc = $enc;
  804. } elseif (($cwr == 2) AND ($p == 1)) {
  805. // c. If two symbol characters remain and only one C40 value (data character) remains to be encoded
  806. $c1 = array_shift($temp_cw);
  807. --$p;
  808. $cw[] = 254;
  809. $cw[] = ($chr + 1);
  810. $cw_num += 2;
  811. $pos = $epos;
  812. $enc = ENC_ASCII;
  813. $this->last_enc = $enc;
  814. } elseif (($cwr == 2) AND ($p == 2)) {
  815. // b. If two symbol characters remain and two C40 values remain to be encoded
  816. $c1 = array_shift($temp_cw);
  817. $c2 = array_shift($temp_cw);
  818. $p -= 2;
  819. $tmp = ((1600 * $c1) + (40 * $c2) + 1);
  820. $cw[] = ($tmp >> 8);
  821. $cw[] = ($tmp % 256);
  822. $cw_num += 2;
  823. $pos = $epos;
  824. $enc = ENC_ASCII;
  825. $this->last_enc = $enc;
  826. } else {
  827. // switch to ASCII encoding
  828. if ($enc != ENC_ASCII) {
  829. $enc = ENC_ASCII;
  830. $this->last_enc = $enc;
  831. $cw[] = $this->getSwitchEncodingCodeword($enc);
  832. ++$cw_num;
  833. $pos = ($epos - $p);
  834. }
  835. }
  836. }
  837. break;
  838. }
  839. case ENC_EDF: { // F. While in EDIFACT (EDF) encodation
  840. // initialize temporary array with 0 length
  841. $temp_cw = array();
  842. $epos = $pos;
  843. $field_length = 0;
  844. $newenc = $enc;
  845. do {
  846. // 2. process the next character in EDIFACT encodation.
  847. $chr = ord($data[$epos]);
  848. if ($this->isCharMode($chr, ENC_EDF)) {
  849. ++$epos;
  850. $temp_cw[] = $chr;
  851. ++$field_length;
  852. }
  853. if (($field_length == 4) OR ($epos == $data_length) OR !$this->isCharMode($chr, ENC_EDF)) {
  854. if (($epos == $data_length) AND ($field_length < 3)) {
  855. $enc = ENC_ASCII;
  856. $cw[] = $this->getSwitchEncodingCodeword($enc);
  857. ++$cw_num;
  858. break;
  859. }
  860. if ($field_length < 4) {
  861. // set unlatch character
  862. $temp_cw[] = 0x1f;
  863. ++$field_length;
  864. // fill empty characters
  865. for ($i = $field_length; $i < 4; ++$i) {
  866. $temp_cw[] = 0;
  867. }
  868. $enc = ENC_ASCII;
  869. $this->last_enc = $enc;
  870. }
  871. // encodes four data characters in three codewords
  872. $tcw = (($temp_cw[0] & 0x3F) << 2) + (($temp_cw[1] & 0x30) >> 4);
  873. if ($tcw > 0) {
  874. $cw[] = $tcw;
  875. $cw_num++;
  876. }
  877. $tcw= (($temp_cw[1] & 0x0F) << 4) + (($temp_cw[2] & 0x3C) >> 2);
  878. if ($tcw > 0) {
  879. $cw[] = $tcw;
  880. $cw_num++;
  881. }
  882. $tcw = (($temp_cw[2] & 0x03) << 6) + ($temp_cw[3] & 0x3F);
  883. if ($tcw > 0) {
  884. $cw[] = $tcw;
  885. $cw_num++;
  886. }
  887. $temp_cw = array();
  888. $pos = $epos;
  889. $field_length = 0;
  890. if ($enc == ENC_ASCII) {
  891. break; // exit from EDIFACT mode
  892. }
  893. }
  894. } while ($epos < $data_length);
  895. break;
  896. }
  897. case ENC_BASE256: { // G. While in Base 256 (B256) encodation
  898. // initialize temporary array with 0 length
  899. $temp_cw = array();
  900. $field_length = 0;
  901. while (($pos < $data_length) AND ($field_length <= 1555)) {
  902. $newenc = $this->lookAheadTest($data, $pos, $enc);
  903. if ($newenc != $enc) {
  904. // 1. If the look-ahead test (starting at step J) indicates another mode, switch to that mode.
  905. $enc = $newenc;
  906. break; // exit from B256 mode
  907. } else {
  908. // 2. Otherwise, process the next character in Base 256 encodation.
  909. $chr = ord($data[$pos]);
  910. ++$pos;
  911. $temp_cw[] = $chr;
  912. ++$field_length;
  913. }
  914. }
  915. // set field length
  916. if ($field_length <= 249) {
  917. $cw[] = $this->get255StateCodeword($field_length, ($cw_num + 1));
  918. ++$cw_num;
  919. } else {
  920. $cw[] = $this->get255StateCodeword((floor($field_length / 250) + 249), ($cw_num + 1));
  921. $cw[] = $this->get255StateCodeword(($field_length % 250), ($cw_num + 2));
  922. $cw_num += 2;
  923. }
  924. if (!empty($temp_cw)) {
  925. // add B256 field
  926. foreach ($temp_cw as $p => $cht) {
  927. $cw[] = $this->get255StateCodeword($cht, ($cw_num + $p + 1));
  928. }
  929. }
  930. break;
  931. }
  932. } // end of switch enc
  933. } // end of while
  934. return $cw;
  935. }
  936. /**
  937. * Places "chr+bit" with appropriate wrapping within array[].
  938. * (Annex F - ECC 200 symbol character placement)
  939. * @param $marr (array) Array of symbols.
  940. * @param $nrow (int) Number of rows.
  941. * @param $ncol (int) Number of columns.
  942. * @param $row (int) Row number.
  943. * @param $col (int) Column number.
  944. * @param $chr (int) Char byte.
  945. * @param $bit (int) Bit.
  946. * @return array
  947. * @protected
  948. */
  949. protected function placeModule($marr, $nrow, $ncol, $row, $col, $chr, $bit) {
  950. if ($row < 0) {
  951. $row += $nrow;
  952. $col += (4 - (($nrow + 4) % 8));
  953. }
  954. if ($col < 0) {
  955. $col += $ncol;
  956. $row += (4 - (($ncol + 4) % 8));
  957. }
  958. $marr[(($row * $ncol) + $col)] = ((10 * $chr) + $bit);
  959. return $marr;
  960. }
  961. /**
  962. * Places the 8 bits of a utah-shaped symbol character.
  963. * (Annex F - ECC 200 symbol character placement)
  964. * @param $marr (array) Array of symbols.
  965. * @param $nrow (int) Number of rows.
  966. * @param $ncol (int) Number of columns.
  967. * @param $row (int) Row number.
  968. * @param $col (int) Column number.
  969. * @param $chr (int) Char byte.
  970. * @return array
  971. * @protected
  972. */
  973. protected function placeUtah($marr, $nrow, $ncol, $row, $col, $chr) {
  974. $marr = $this->placeModule($marr, $nrow, $ncol, $row-2, $col-2, $chr, 1);
  975. $marr = $this->placeModule($marr, $nrow, $ncol, $row-2, $col-1, $chr, 2);
  976. $marr = $this->placeModule($marr, $nrow, $ncol, $row-1, $col-2, $chr, 3);
  977. $marr = $this->placeModule($marr, $nrow, $ncol, $row-1, $col-1, $chr, 4);
  978. $marr = $this->placeModule($marr, $nrow, $ncol, $row-1, $col, $chr, 5);
  979. $marr = $this->placeModule($marr, $nrow, $ncol, $row, $col-2, $chr, 6);
  980. $marr = $this->placeModule($marr, $nrow, $ncol, $row, $col-1, $chr, 7);
  981. $marr = $this->placeModule($marr, $nrow, $ncol, $row, $col, $chr, 8);
  982. return $marr;
  983. }
  984. /**
  985. * Places the 8 bits of the first special corner case.
  986. * (Annex F - ECC 200 symbol character placement)
  987. * @param $marr (array) Array of symbols.
  988. * @param $nrow (int) Number of rows.
  989. * @param $ncol (int) Number of columns.
  990. * @param $chr (int) Char byte.
  991. * @return array
  992. * @protected
  993. */
  994. protected function placeCornerA($marr, $nrow, $ncol, $chr) {
  995. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 0, $chr, 1);
  996. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 1, $chr, 2);
  997. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 2, $chr, 3);
  998. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-2, $chr, 4);
  999. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-1, $chr, 5);
  1000. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-1, $chr, 6);
  1001. $marr = $this->placeModule($marr, $nrow, $ncol, 2, $ncol-1, $chr, 7);
  1002. $marr = $this->placeModule($marr, $nrow, $ncol, 3, $ncol-1, $chr, 8);
  1003. return $marr;
  1004. }
  1005. /**
  1006. * Places the 8 bits of the second special corner case.
  1007. * (Annex F - ECC 200 symbol character placement)
  1008. * @param $marr (array) Array of symbols.
  1009. * @param $nrow (int) Number of rows.
  1010. * @param $ncol (int) Number of columns.
  1011. * @param $chr (int) Char byte.
  1012. * @return array
  1013. * @protected
  1014. */
  1015. protected function placeCornerB($marr, $nrow, $ncol, $chr) {
  1016. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-3, 0, $chr, 1);
  1017. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-2, 0, $chr, 2);
  1018. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 0, $chr, 3);
  1019. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-4, $chr, 4);
  1020. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-3, $chr, 5);
  1021. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-2, $chr, 6);
  1022. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-1, $chr, 7);
  1023. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-1, $chr, 8);
  1024. return $marr;
  1025. }
  1026. /**
  1027. * Places the 8 bits of the third special corner case.
  1028. * (Annex F - ECC 200 symbol character placement)
  1029. * @param $marr (array) Array of symbols.
  1030. * @param $nrow (int) Number of rows.
  1031. * @param $ncol (int) Number of columns.
  1032. * @param $chr (int) Char byte.
  1033. * @return array
  1034. * @protected
  1035. */
  1036. protected function placeCornerC($marr, $nrow, $ncol, $chr) {
  1037. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-3, 0, $chr, 1);
  1038. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-2, 0, $chr, 2);
  1039. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 0, $chr, 3);
  1040. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-2, $chr, 4);
  1041. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-1, $chr, 5);
  1042. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-1, $chr, 6);
  1043. $marr = $this->placeModule($marr, $nrow, $ncol, 2, $ncol-1, $chr, 7);
  1044. $marr = $this->placeModule($marr, $nrow, $ncol, 3, $ncol-1, $chr, 8);
  1045. return $marr;
  1046. }
  1047. /**
  1048. * Places the 8 bits of the fourth special corner case.
  1049. * (Annex F - ECC 200 symbol character placement)
  1050. * @param $marr (array) Array of symbols.
  1051. * @param $nrow (int) Number of rows.
  1052. * @param $ncol (int) Number of columns.
  1053. * @param $chr (int) Char byte.
  1054. * @return array
  1055. * @protected
  1056. */
  1057. protected function placeCornerD($marr, $nrow, $ncol, $chr) {
  1058. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 0, $chr, 1);
  1059. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, $ncol-1, $chr, 2);
  1060. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-3, $chr, 3);
  1061. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-2, $chr, 4);
  1062. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-1, $chr, 5);
  1063. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-3, $chr, 6);
  1064. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-2, $chr, 7);
  1065. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-1, $chr, 8);
  1066. return $marr;
  1067. }
  1068. /**
  1069. * Build a placement map.
  1070. * (Annex F - ECC 200 symbol character placement)
  1071. * @param $nrow (int) Number of rows.
  1072. * @param $ncol (int) Number of columns.
  1073. * @return array
  1074. * @protected
  1075. */
  1076. protected function getPlacementMap($nrow, $ncol) {
  1077. // initialize array with zeros
  1078. $marr = array_fill(0, ($nrow * $ncol), 0);
  1079. // set starting values
  1080. $chr = 1;
  1081. $row = 4;
  1082. $col = 0;
  1083. do {
  1084. // repeatedly first check for one of the special corner cases, then
  1085. if (($row == $nrow) AND ($col == 0)) {
  1086. $marr = $this->placeCornerA($marr, $nrow, $ncol, $chr);
  1087. ++$chr;
  1088. }
  1089. if (($row == ($nrow - 2)) AND ($col == 0) AND ($ncol % 4)) {
  1090. $marr = $this->placeCornerB($marr, $nrow, $ncol, $chr);
  1091. ++$chr;
  1092. }
  1093. if (($row == ($nrow - 2)) AND ($col == 0) AND (($ncol % 8) == 4)) {
  1094. $marr = $this->placeCornerC($marr, $nrow, $ncol, $chr);
  1095. ++$chr;
  1096. }
  1097. if (($row == ($nrow + 4)) AND ($col == 2) AND (!($ncol % 8))) {
  1098. $marr = $this->placeCornerD($marr, $nrow, $ncol, $chr);
  1099. ++$chr;
  1100. }
  1101. // sweep upward diagonally, inserting successive characters,
  1102. do {
  1103. if (($row < $nrow) AND ($col >= 0) AND (!$marr[(($row * $ncol) + $col)])) {
  1104. $marr = $this->placeUtah($marr, $nrow, $ncol, $row, $col, $chr);
  1105. ++$chr;
  1106. }
  1107. $row -= 2;
  1108. $col += 2;
  1109. } while (($row >= 0) AND ($col < $ncol));
  1110. ++$row;
  1111. $col += 3;
  1112. // & then sweep downward diagonally, inserting successive characters,...
  1113. do {
  1114. if (($row >= 0) AND ($col < $ncol) AND (!$marr[(($row * $ncol) + $col)])) {
  1115. $marr = $this->placeUtah($marr, $nrow, $ncol, $row, $col, $chr);
  1116. ++$chr;
  1117. }
  1118. $row += 2;
  1119. $col -= 2;
  1120. } while (($row < $nrow) AND ($col >= 0));
  1121. $row += 3;
  1122. ++$col;
  1123. // ... until the entire array is scanned
  1124. } while (($row < $nrow) OR ($col < $ncol));
  1125. // lastly, if the lower righthand corner is untouched, fill in fixed pattern
  1126. if (!$marr[(($nrow * $ncol) - 1)]) {
  1127. $marr[(($nrow * $ncol) - 1)] = 1;
  1128. $marr[(($nrow * $ncol) - $ncol - 2)] = 1;
  1129. }
  1130. return $marr;
  1131. }
  1132. } // end DataMatrix class
  1133. //============================================================+
  1134. // END OF FILE
  1135. //============================================================+