PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/datamatrix.php

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