datamatrix.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. <?php
  2. //============================================================+
  3. // File name : datamatrix.php
  4. // Version : 1.0.004
  5. // Begin : 2010-06-07
  6. // Last Update : 2013-12-12
  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-2012 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.004
  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 ($this->last_enc == ENC_EDF) {
  237. // switch to ASCII encoding
  238. $cw[] = 124;
  239. ++$nd;
  240. } elseif (($this->last_enc != ENC_ASCII) AND ($this->last_enc != ENC_BASE256)) {
  241. // switch to ASCII encoding
  242. $cw[] = 254;
  243. ++$nd;
  244. }
  245. if ($params[11] > $nd) {
  246. // add first pad
  247. $cw[] = 129;
  248. ++$nd;
  249. // add remaining pads
  250. for ($i = $nd; $i < $params[11]; ++$i) {
  251. $cw[] = $this->get253StateCodeword(129, $i);
  252. }
  253. }
  254. }
  255. // add error correction codewords
  256. $cw = $this->getErrorCorrection($cw, $params[13], $params[14], $params[15]);
  257. // initialize empty arrays
  258. $grid = array_fill(0, ($params[2] * $params[3]), 0);
  259. // get placement map
  260. $places = $this->getPlacementMap($params[2], $params[3]);
  261. // fill the grid with data
  262. $grid = array();
  263. $i = 0;
  264. // region data row max index
  265. $rdri = ($params[4] - 1);
  266. // region data column max index
  267. $rdci = ($params[5] - 1);
  268. // for each vertical region
  269. for ($vr = 0; $vr < $params[9]; ++$vr) {
  270. // for each row on region
  271. for ($r = 0; $r < $params[4]; ++$r) {
  272. // get row
  273. $row = (($vr * $params[4]) + $r);
  274. // for each horizontal region
  275. for ($hr = 0; $hr < $params[8]; ++$hr) {
  276. // for each column on region
  277. for ($c = 0; $c < $params[5]; ++$c) {
  278. // get column
  279. $col = (($hr * $params[5]) + $c);
  280. // braw bits by case
  281. if ($r == 0) {
  282. // top finder pattern
  283. if ($c % 2) {
  284. $grid[$row][$col] = 0;
  285. } else {
  286. $grid[$row][$col] = 1;
  287. }
  288. } elseif ($r == $rdri) {
  289. // bottom finder pattern
  290. $grid[$row][$col] = 1;
  291. } elseif ($c == 0) {
  292. // left finder pattern
  293. $grid[$row][$col] = 1;
  294. } elseif ($c == $rdci) {
  295. // right finder pattern
  296. if ($r % 2) {
  297. $grid[$row][$col] = 1;
  298. } else {
  299. $grid[$row][$col] = 0;
  300. }
  301. } else { // data bit
  302. if ($places[$i] < 2) {
  303. $grid[$row][$col] = $places[$i];
  304. } else {
  305. // codeword ID
  306. $cw_id = (floor($places[$i] / 10) - 1);
  307. // codeword BIT mask
  308. $cw_bit = pow(2, (8 - ($places[$i] % 10)));
  309. $grid[$row][$col] = (($cw[$cw_id] & $cw_bit) == 0) ? 0 : 1;
  310. }
  311. ++$i;
  312. }
  313. }
  314. }
  315. }
  316. }
  317. $this->barcode_array['num_rows'] = $params[0];
  318. $this->barcode_array['num_cols'] = $params[1];
  319. $this->barcode_array['bcode'] = $grid;
  320. }
  321. /**
  322. * Returns a barcode array which is readable by TCPDF
  323. * @return array barcode array readable by TCPDF;
  324. * @public
  325. */
  326. public function getBarcodeArray() {
  327. return $this->barcode_array;
  328. }
  329. /**
  330. * Product of two numbers in a Power-of-Two Galois Field
  331. * @param $a (int) first number to multiply.
  332. * @param $b (int) second number to multiply.
  333. * @param $log (array) Log table.
  334. * @param $alog (array) Anti-Log table.
  335. * @param $gf (array) Number of Factors of the Reed-Solomon polynomial.
  336. * @return int product
  337. * @protected
  338. */
  339. protected function getGFProduct($a, $b, $log, $alog, $gf) {
  340. if (($a == 0) OR ($b == 0)) {
  341. return 0;
  342. }
  343. return ($alog[($log[$a] + $log[$b]) % ($gf - 1)]);
  344. }
  345. /**
  346. * Add error correction codewords to data codewords array (ANNEX E).
  347. * @param $wd (array) Array of datacodewords.
  348. * @param $nb (int) Number of blocks.
  349. * @param $nd (int) Number of data codewords per block.
  350. * @param $nc (int) Number of correction codewords per block.
  351. * @param $gf (int) numner of fields on log/antilog table (power of 2).
  352. * @param $pp (int) The value of its prime modulus polynomial (301 for ECC200).
  353. * @return array data codewords + error codewords
  354. * @protected
  355. */
  356. protected function getErrorCorrection($wd, $nb, $nd, $nc, $gf=256, $pp=301) {
  357. // generate the log ($log) and antilog ($alog) tables
  358. $log[0] = 0;
  359. $alog[0] = 1;
  360. for ($i = 1; $i < $gf; ++$i) {
  361. $alog[$i] = ($alog[($i - 1)] * 2);
  362. if ($alog[$i] >= $gf) {
  363. $alog[$i] ^= $pp;
  364. }
  365. $log[$alog[$i]] = $i;
  366. }
  367. ksort($log);
  368. // generate the polynomial coefficients (c)
  369. $c = array_fill(0, ($nc + 1), 0);
  370. $c[0] = 1;
  371. for ($i = 1; $i <= $nc; ++$i) {
  372. $c[$i] = $c[($i-1)];
  373. for ($j = ($i - 1); $j >= 1; --$j) {
  374. $c[$j] = $c[($j - 1)] ^ $this->getGFProduct($c[$j], $alog[$i], $log, $alog, $gf);
  375. }
  376. $c[0] = $this->getGFProduct($c[0], $alog[$i], $log, $alog, $gf);
  377. }
  378. ksort($c);
  379. // total number of data codewords
  380. $num_wd = ($nb * $nd);
  381. // total number of error codewords
  382. $num_we = ($nb * $nc);
  383. // for each block
  384. for ($b = 0; $b < $nb; ++$b) {
  385. // create interleaved data block
  386. $block = array();
  387. for ($n = $b; $n < $num_wd; $n += $nb) {
  388. $block[] = $wd[$n];
  389. }
  390. // initialize error codewords
  391. $we = array_fill(0, ($nc + 1), 0);
  392. // calculate error correction codewords for this block
  393. for ($i = 0; $i < $nd; ++$i) {
  394. $k = ($we[0] ^ $block[$i]);
  395. for ($j = 0; $j < $nc; ++$j) {
  396. $we[$j] = ($we[($j + 1)] ^ $this->getGFProduct($k, $c[($nc - $j - 1)], $log, $alog, $gf));
  397. }
  398. }
  399. // add error codewords at the end of data codewords
  400. $j = 0;
  401. for ($i = $b; $i < $num_we; $i += $nb) {
  402. $wd[($num_wd + $i)] = $we[$j];
  403. ++$j;
  404. }
  405. }
  406. // reorder codewords
  407. ksort($wd);
  408. return $wd;
  409. }
  410. /**
  411. * Return the 253-state codeword
  412. * @param $cwpad (int) Pad codeword.
  413. * @param $cwpos (int) Number of data codewords from the beginning of encoded data.
  414. * @return pad codeword
  415. * @protected
  416. */
  417. protected function get253StateCodeword($cwpad, $cwpos) {
  418. $pad = ($cwpad + (((149 * $cwpos) % 253) + 1));
  419. if ($pad > 254) {
  420. $pad -= 254;
  421. }
  422. return $pad;
  423. }
  424. /**
  425. * Return the 255-state codeword
  426. * @param $cwpad (int) Pad codeword.
  427. * @param $cwpos (int) Number of data codewords from the beginning of encoded data.
  428. * @return pad codeword
  429. * @protected
  430. */
  431. protected function get255StateCodeword($cwpad, $cwpos) {
  432. $pad = ($cwpad + (((149 * $cwpos) % 255) + 1));
  433. if ($pad > 255) {
  434. $pad -= 256;
  435. }
  436. return $pad;
  437. }
  438. /**
  439. * Returns true if the char belongs to the selected mode
  440. * @param $chr (int) Character (byte) to check.
  441. * @param $mode (int) Current encoding mode.
  442. * @return boolean true if the char is of the selected mode.
  443. * @protected
  444. */
  445. protected function isCharMode($chr, $mode) {
  446. $status = false;
  447. switch ($mode) {
  448. case ENC_ASCII: { // ASCII character 0 to 127
  449. $status = (($chr >= 0) AND ($chr <= 127));
  450. break;
  451. }
  452. case ENC_C40: { // Upper-case alphanumeric
  453. $status = (($chr == 32) OR (($chr >= 48) AND ($chr <= 57)) OR (($chr >= 65) AND ($chr <= 90)));
  454. break;
  455. }
  456. case ENC_TXT: { // Lower-case alphanumeric
  457. $status = (($chr == 32) OR (($chr >= 48) AND ($chr <= 57)) OR (($chr >= 97) AND ($chr <= 122)));
  458. break;
  459. }
  460. case ENC_X12: { // ANSI X12
  461. $status = (($chr == 13) OR ($chr == 42) OR ($chr == 62));
  462. break;
  463. }
  464. case ENC_EDF: { // ASCII character 32 to 94
  465. $status = (($chr >= 32) AND ($chr <= 94));
  466. break;
  467. }
  468. case ENC_BASE256: { // Function character (FNC1, Structured Append, Reader Program, or Code Page)
  469. $status = (($chr == 232) OR ($chr == 233) OR ($chr == 234) OR ($chr == 241));
  470. break;
  471. }
  472. case ENC_ASCII_EXT: { // ASCII character 128 to 255
  473. $status = (($chr >= 128) AND ($chr <= 255));
  474. break;
  475. }
  476. case ENC_ASCII_NUM: { // ASCII digits
  477. $status = (($chr >= 48) AND ($chr <= 57));
  478. break;
  479. }
  480. }
  481. return $status;
  482. }
  483. /**
  484. * The look-ahead test scans the data to be encoded to find the best mode (Annex P - steps from J to S).
  485. * @param $data (string) data to encode
  486. * @param $pos (int) current position
  487. * @param $mode (int) current encoding mode
  488. * @return int encoding mode
  489. * @protected
  490. */
  491. protected function lookAheadTest($data, $pos, $mode) {
  492. $data_length = strlen($data);
  493. if ($pos >= $data_length) {
  494. return $mode;
  495. }
  496. $charscount = 0; // count processed chars
  497. // STEP J
  498. if ($mode == ENC_ASCII) {
  499. $numch = array(0, 1, 1, 1, 1, 1.25);
  500. } else {
  501. $numch = array(1, 2, 2, 2, 2, 2.25);
  502. $numch[$mode] = 0;
  503. }
  504. while (true) {
  505. // STEP K
  506. if (($pos + $charscount) == $data_length) {
  507. if ($numch[ENC_ASCII] <= ceil(min($numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_EDF], $numch[ENC_BASE256]))) {
  508. return ENC_ASCII;
  509. }
  510. if ($numch[ENC_BASE256] < ceil(min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_EDF]))) {
  511. return ENC_BASE256;
  512. }
  513. if ($numch[ENC_EDF] < ceil(min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_BASE256]))) {
  514. return ENC_EDF;
  515. }
  516. if ($numch[ENC_TXT] < ceil(min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_X12], $numch[ENC_EDF], $numch[ENC_BASE256]))) {
  517. return ENC_TXT;
  518. }
  519. if ($numch[ENC_X12] < ceil(min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_EDF], $numch[ENC_BASE256]))) {
  520. return ENC_X12;
  521. }
  522. return ENC_C40;
  523. }
  524. // get char
  525. $chr = ord($data[$pos + $charscount]);
  526. $charscount++;
  527. // STEP L
  528. if ($this->isCharMode($chr, ENC_ASCII_NUM)) {
  529. $numch[ENC_ASCII] += (1 / 2);
  530. } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
  531. $numch[ENC_ASCII] = ceil($numch[ENC_ASCII]);
  532. $numch[ENC_ASCII] += 2;
  533. } else {
  534. $numch[ENC_ASCII] = ceil($numch[ENC_ASCII]);
  535. $numch[ENC_ASCII] += 1;
  536. }
  537. // STEP M
  538. if ($this->isCharMode($chr, ENC_C40)) {
  539. $numch[ENC_C40] += (2 / 3);
  540. } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
  541. $numch[ENC_C40] += (8 / 3);
  542. } else {
  543. $numch[ENC_C40] += (4 / 3);
  544. }
  545. // STEP N
  546. if ($this->isCharMode($chr, ENC_TXT)) {
  547. $numch[ENC_TXT] += (2 / 3);
  548. } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
  549. $numch[ENC_TXT] += (8 / 3);
  550. } else {
  551. $numch[ENC_TXT] += (4 / 3);
  552. }
  553. // STEP O
  554. if ($this->isCharMode($chr, ENC_X12) OR $this->isCharMode($chr, ENC_C40)) {
  555. $numch[ENC_X12] += (2 / 3);
  556. } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
  557. $numch[ENC_X12] += (13 / 3);
  558. } else {
  559. $numch[ENC_X12] += (10 / 3);
  560. }
  561. // STEP P
  562. if ($this->isCharMode($chr, ENC_EDF)) {
  563. $numch[ENC_EDF] += (3 / 4);
  564. } elseif ($this->isCharMode($chr, ENC_ASCII_EXT)) {
  565. $numch[ENC_EDF] += (17 / 4);
  566. } else {
  567. $numch[ENC_EDF] += (13 / 4);
  568. }
  569. // STEP Q
  570. if ($this->isCharMode($chr, ENC_BASE256)) {
  571. $numch[ENC_BASE256] += 4;
  572. } else {
  573. $numch[ENC_BASE256] += 1;
  574. }
  575. // STEP R
  576. if ($charscount >= 4) {
  577. if (($numch[ENC_ASCII] + 1) <= min($numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_EDF], $numch[ENC_BASE256])) {
  578. return ENC_ASCII;
  579. }
  580. if ((($numch[ENC_BASE256] + 1) <= $numch[ENC_ASCII])
  581. OR (($numch[ENC_BASE256] + 1) < min($numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_EDF]))) {
  582. return ENC_BASE256;
  583. }
  584. if (($numch[ENC_EDF] + 1) < min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_X12], $numch[ENC_BASE256])) {
  585. return ENC_EDF;
  586. }
  587. if (($numch[ENC_TXT] + 1) < min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_X12], $numch[ENC_EDF], $numch[ENC_BASE256])) {
  588. return ENC_TXT;
  589. }
  590. if (($numch[ENC_X12] + 1) < min($numch[ENC_ASCII], $numch[ENC_C40], $numch[ENC_TXT], $numch[ENC_EDF], $numch[ENC_BASE256])) {
  591. return ENC_X12;
  592. }
  593. if (($numch[ENC_C40] + 1) < min($numch[ENC_ASCII], $numch[ENC_TXT], $numch[ENC_EDF], $numch[ENC_BASE256])) {
  594. if ($numch[ENC_C40] < $numch[ENC_X12]) {
  595. return ENC_C40;
  596. }
  597. if ($numch[ENC_C40] == $numch[ENC_X12]) {
  598. $k = ($pos + $charscount + 1);
  599. while ($k < $data_length) {
  600. $tmpchr = ord($data{$k});
  601. if ($this->isCharMode($tmpchr, ENC_X12)) {
  602. return ENC_X12;
  603. } elseif (!($this->isCharMode($tmpchr, ENC_X12) OR $this->isCharMode($tmpchr, ENC_C40))) {
  604. break;
  605. }
  606. ++$k;
  607. }
  608. return ENC_C40;
  609. }
  610. }
  611. }
  612. } // end of while
  613. }
  614. /**
  615. * Get the switching codeword to a new encoding mode (latch codeword)
  616. * @param $mode (int) New encoding mode.
  617. * @return (int) Switch codeword.
  618. * @protected
  619. */
  620. protected function getSwitchEncodingCodeword($mode) {
  621. switch ($mode) {
  622. case ENC_ASCII: { // ASCII character 0 to 127
  623. $cw = 254;
  624. break;
  625. }
  626. case ENC_C40: { // Upper-case alphanumeric
  627. $cw = 230;
  628. break;
  629. }
  630. case ENC_TXT: { // Lower-case alphanumeric
  631. $cw = 239;
  632. break;
  633. }
  634. case ENC_X12: { // ANSI X12
  635. $cw = 238;
  636. break;
  637. }
  638. case ENC_EDF: { // ASCII character 32 to 94
  639. $cw = 240;
  640. break;
  641. }
  642. case ENC_BASE256: { // Function character (FNC1, Structured Append, Reader Program, or Code Page)
  643. $cw = 231;
  644. break;
  645. }
  646. }
  647. return $cw;
  648. }
  649. /**
  650. * Choose the minimum matrix size and return the max number of data codewords.
  651. * @param $numcw (int) Number of current codewords.
  652. * @return number of data codewords in matrix
  653. * @protected
  654. */
  655. protected function getMaxDataCodewords($numcw) {
  656. foreach ($this->symbattr as $key => $matrix) {
  657. if ($matrix[11] >= $numcw) {
  658. return $matrix[11];
  659. }
  660. }
  661. return 0;
  662. }
  663. /**
  664. * Get high level encoding using the minimum symbol data characters for ECC 200
  665. * @param $data (string) data to encode
  666. * @return array of codewords
  667. * @protected
  668. */
  669. protected function getHighLevelEncoding($data) {
  670. // STEP A. Start in ASCII encodation.
  671. $enc = ENC_ASCII; // current encoding mode
  672. $pos = 0; // current position
  673. $cw = array(); // array of codewords to be returned
  674. $cw_num = 0; // number of data codewords
  675. $data_lenght = strlen($data); // number of chars
  676. while ($pos < $data_lenght) {
  677. switch ($enc) {
  678. case ENC_ASCII: { // STEP B. While in ASCII encodation
  679. 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))) {
  680. // 1. If the next data sequence is at least 2 consecutive digits, encode the next two digits as a double digit in ASCII mode.
  681. $cw[] = (intval(substr($data, $pos, 2)) + 130);
  682. ++$cw_num;
  683. $pos += 2;
  684. } else {
  685. // 2. If the look-ahead test (starting at step J) indicates another mode, switch to that mode.
  686. $newenc = $this->lookAheadTest($data, $pos, $enc);
  687. if ($newenc != $enc) {
  688. // switch to new encoding
  689. $enc = $newenc;
  690. $cw[] = $this->getSwitchEncodingCodeword($enc);
  691. ++$cw_num;
  692. } else {
  693. // get new byte
  694. $chr = ord($data[$pos]);
  695. ++$pos;
  696. if ($this->isCharMode($chr, ENC_ASCII_EXT)) {
  697. // 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.
  698. $cw[] = 235;
  699. $cw[] = ($chr - 127);
  700. $cw_num += 2;
  701. } else {
  702. // 4. Otherwise process the next data character in ASCII encodation.
  703. $cw[] = ($chr + 1);
  704. ++$cw_num;
  705. }
  706. }
  707. }
  708. break;
  709. }
  710. case ENC_C40 : // Upper-case alphanumeric
  711. case ENC_TXT : // Lower-case alphanumeric
  712. case ENC_X12 : { // ANSI X12
  713. $temp_cw = array();
  714. $p = 0;
  715. $epos = $pos;
  716. // get charset ID
  717. $set_id = $this->chset_id[$enc];
  718. // get basic charset for current encoding
  719. $charset = $this->chset[$set_id];
  720. do {
  721. // 2. process the next character in C40 encodation.
  722. $chr = ord($data[$epos]);
  723. ++$epos;
  724. // check for extended character
  725. if ($chr & 0x80) {
  726. if ($enc == ENC_X12) {
  727. return false;
  728. }
  729. $chr = ($chr & 0x7f);
  730. $temp_cw[] = 1; // shift 2
  731. $temp_cw[] = 30; // upper shift
  732. $p += 2;
  733. }
  734. if (isset($charset[$chr])) {
  735. $temp_cw[] = $charset[$chr];
  736. ++$p;
  737. } else {
  738. if (isset($this->chset['SH1'][$chr])) {
  739. $temp_cw[] = 0; // shift 1
  740. $shiftset = $this->chset['SH1'];
  741. } elseif (isset($chr, $this->chset['SH2'][$chr])) {
  742. $temp_cw[] = 1; // shift 2
  743. $shiftset = $this->chset['SH2'];
  744. } elseif (($enc == ENC_C40) AND isset($this->chset['S3C'][$chr])) {
  745. $temp_cw[] = 2; // shift 3
  746. $shiftset = $this->chset['S3C'];
  747. } elseif (($enc == ENC_TXT) AND isset($this->chset['S3T'][$chr])) {
  748. $temp_cw[] = 2; // shift 3
  749. $shiftset = $this->chset['S3T'];
  750. } else {
  751. return false;
  752. }
  753. $temp_cw[] = $shiftset[$chr];
  754. $p += 2;
  755. }
  756. if ($p >= 3) {
  757. $c1 = array_shift($temp_cw);
  758. $c2 = array_shift($temp_cw);
  759. $c3 = array_shift($temp_cw);
  760. $p -= 3;
  761. $tmp = ((1600 * $c1) + (40 * $c2) + $c3 + 1);
  762. $cw[] = ($tmp >> 8);
  763. $cw[] = ($tmp % 256);
  764. $cw_num += 2;
  765. $pos = $epos;
  766. // 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.
  767. $newenc = $this->lookAheadTest($data, $pos, $enc);
  768. if ($newenc != $enc) {
  769. $enc = $newenc;
  770. $cw[] = $this->getSwitchEncodingCodeword($enc);
  771. ++$cw_num;
  772. $pos -= $p;
  773. $p = 0;
  774. break;
  775. }
  776. }
  777. } while (($p > 0) AND ($epos < $data_lenght));
  778. // process last data (if any)
  779. if ($p > 0) {
  780. // get remaining number of data symbols
  781. $cwr = ($this->getMaxDataCodewords($cw_num + 2) - $cw_num);
  782. if (($cwr == 1) AND ($p == 1)) {
  783. // d. If one symbol character remains and one C40 value (data character) remains to be encoded
  784. $c1 = array_shift($temp_cw);
  785. --$p;
  786. $cw[] = ($c1 + 1);
  787. ++$cw_num;
  788. } elseif (($cwr == 2) AND ($p == 1)) {
  789. // c. If two symbol characters remain and only one C40 value (data character) remains to be encoded
  790. $c1 = array_shift($temp_cw);
  791. --$p;
  792. $cw[] = 254;
  793. $cw[] = ($c1 + 1);
  794. $cw_num += 2;
  795. } elseif (($cwr == 2) AND ($p == 2)) {
  796. // b. If two symbol characters remain and two C40 values remain to be encoded
  797. $c1 = array_shift($temp_cw);
  798. $c2 = array_shift($temp_cw);
  799. $p -= 2;
  800. $tmp = ((1600 * $c1) + (40 * $c2) + 1);
  801. $cw[] = ($tmp >> 8);
  802. $cw[] = ($tmp % 256);
  803. $cw_num += 2;
  804. } else {
  805. // switch to ASCII encoding
  806. if ($enc != ENC_ASCII) {
  807. $enc = ENC_ASCII;
  808. $cw[] = $this->getSwitchEncodingCodeword($enc);
  809. ++$cw_num;
  810. }
  811. }
  812. }
  813. break;
  814. }
  815. case ENC_EDF: { // F. While in EDIFACT (EDF) encodation
  816. // initialize temporary array with 0 lenght
  817. $temp_cw = array();
  818. $epos = $pos;
  819. $field_lenght = 0;
  820. $newenc = $enc;
  821. do {
  822. // 2. process the next character in EDIFACT encodation.
  823. $chr = ord($data[$epos]);
  824. if ($this->isCharMode($chr, ENC_EDF)) {
  825. ++$epos;
  826. $temp_cw[] = $chr;
  827. ++$field_lenght;
  828. }
  829. if (($field_lenght == 4) OR ($epos == $data_lenght) OR !$this->isCharMode($chr, ENC_EDF)) {
  830. if (($epos == $data_lenght) AND ($field_lenght < 3)) {
  831. $enc = ENC_ASCII;
  832. break;
  833. }
  834. if ($field_lenght < 4) {
  835. // set unlatch character
  836. $temp_cw[] = 0x1f;
  837. ++$field_lenght;
  838. // fill empty characters
  839. for ($i = $field_lenght; $i < 4; ++$i) {
  840. $temp_cw[] = 0;
  841. }
  842. $enc = ENC_ASCII;
  843. }
  844. // encodes four data characters in three codewords
  845. $tcw = (($temp_cw[0] & 0x3F) << 2) + (($temp_cw[1] & 0x30) >> 4);
  846. if ($tcw > 0) {
  847. $cw[] = $tcw;
  848. $cw_num++;
  849. }
  850. $tcw= (($temp_cw[1] & 0x0F) << 4) + (($temp_cw[2] & 0x3C) >> 2);
  851. if ($tcw > 0) {
  852. $cw[] = $tcw;
  853. $cw_num++;
  854. }
  855. $tcw = (($temp_cw[2] & 0x03) << 6) + ($temp_cw[3] & 0x3F);
  856. if ($tcw > 0) {
  857. $cw[] = $tcw;
  858. $cw_num++;
  859. }
  860. $temp_cw = array();
  861. $pos = $epos;
  862. $field_lenght = 0;
  863. if ($enc == ENC_ASCII) {
  864. break; // exit from EDIFACT mode
  865. }
  866. }
  867. } while ($epos < $data_lenght);
  868. break;
  869. }
  870. case ENC_BASE256: { // G. While in Base 256 (B256) encodation
  871. // initialize temporary array with 0 lenght
  872. $temp_cw = array();
  873. $field_lenght = 0;
  874. while (($pos < $data_lenght) AND ($field_lenght <= 1555)) {
  875. $newenc = $this->lookAheadTest($data, $pos, $enc);
  876. if ($newenc != $enc) {
  877. // 1. If the look-ahead test (starting at step J) indicates another mode, switch to that mode.
  878. $enc = $newenc;
  879. $cw[] = $this->getSwitchEncodingCodeword($enc);
  880. ++$cw_num;
  881. break; // exit from B256 mode
  882. } else {
  883. // 2. Otherwise, process the next character in Base 256 encodation.
  884. $chr = ord($data[$pos]);
  885. ++$pos;
  886. $temp_cw[] = $chr;
  887. ++$field_lenght;
  888. }
  889. }
  890. // set field lenght
  891. if ($field_lenght <= 249) {
  892. $cw[] = $field_lenght;
  893. ++$cw_num;
  894. } else {
  895. $cw[] = (floor($field_lenght / 250) + 249);
  896. $cw[] = ($field_lenght % 250);
  897. $cw_num += 2;
  898. }
  899. if (!empty($temp_cw)) {
  900. // add B256 field
  901. foreach ($temp_cw as $p => $cht) {
  902. $cw[] = $this->get255StateCodeword($chr, ($cw_num + $p));
  903. }
  904. }
  905. break;
  906. }
  907. } // end of switch enc
  908. } // end of while
  909. // set last used encoding
  910. $this->last_enc = $enc;
  911. return $cw;
  912. }
  913. /**
  914. * Places "chr+bit" with appropriate wrapping within array[].
  915. * (Annex F - ECC 200 symbol character placement)
  916. * @param $marr (array) Array of symbols.
  917. * @param $nrow (int) Number of rows.
  918. * @param $ncol (int) Number of columns.
  919. * @param $row (int) Row number.
  920. * @param $col (int) Column number.
  921. * @param $chr (int) Char byte.
  922. * @param $bit (int) Bit.
  923. * @return array
  924. * @protected
  925. */
  926. protected function placeModule($marr, $nrow, $ncol, $row, $col, $chr, $bit) {
  927. if ($row < 0) {
  928. $row += $nrow;
  929. $col += (4 - (($nrow + 4) % 8));
  930. }
  931. if ($col < 0) {
  932. $col += $ncol;
  933. $row += (4 - (($ncol + 4) % 8));
  934. }
  935. $marr[(($row * $ncol) + $col)] = ((10 * $chr) + $bit);
  936. return $marr;
  937. }
  938. /**
  939. * Places the 8 bits of a utah-shaped symbol character.
  940. * (Annex F - ECC 200 symbol character placement)
  941. * @param $marr (array) Array of symbols.
  942. * @param $nrow (int) Number of rows.
  943. * @param $ncol (int) Number of columns.
  944. * @param $row (int) Row number.
  945. * @param $col (int) Column number.
  946. * @param $chr (int) Char byte.
  947. * @return array
  948. * @protected
  949. */
  950. protected function placeUtah($marr, $nrow, $ncol, $row, $col, $chr) {
  951. $marr = $this->placeModule($marr, $nrow, $ncol, $row-2, $col-2, $chr, 1);
  952. $marr = $this->placeModule($marr, $nrow, $ncol, $row-2, $col-1, $chr, 2);
  953. $marr = $this->placeModule($marr, $nrow, $ncol, $row-1, $col-2, $chr, 3);
  954. $marr = $this->placeModule($marr, $nrow, $ncol, $row-1, $col-1, $chr, 4);
  955. $marr = $this->placeModule($marr, $nrow, $ncol, $row-1, $col, $chr, 5);
  956. $marr = $this->placeModule($marr, $nrow, $ncol, $row, $col-2, $chr, 6);
  957. $marr = $this->placeModule($marr, $nrow, $ncol, $row, $col-1, $chr, 7);
  958. $marr = $this->placeModule($marr, $nrow, $ncol, $row, $col, $chr, 8);
  959. return $marr;
  960. }
  961. /**
  962. * Places the 8 bits of the first special corner case.
  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 $chr (int) Char byte.
  968. * @return array
  969. * @protected
  970. */
  971. protected function placeCornerA($marr, $nrow, $ncol, $chr) {
  972. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 0, $chr, 1);
  973. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 1, $chr, 2);
  974. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 2, $chr, 3);
  975. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-2, $chr, 4);
  976. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-1, $chr, 5);
  977. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-1, $chr, 6);
  978. $marr = $this->placeModule($marr, $nrow, $ncol, 2, $ncol-1, $chr, 7);
  979. $marr = $this->placeModule($marr, $nrow, $ncol, 3, $ncol-1, $chr, 8);
  980. return $marr;
  981. }
  982. /**
  983. * Places the 8 bits of the second special corner case.
  984. * (Annex F - ECC 200 symbol character placement)
  985. * @param $marr (array) Array of symbols.
  986. * @param $nrow (int) Number of rows.
  987. * @param $ncol (int) Number of columns.
  988. * @param $chr (int) Char byte.
  989. * @return array
  990. * @protected
  991. */
  992. protected function placeCornerB($marr, $nrow, $ncol, $chr) {
  993. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-3, 0, $chr, 1);
  994. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-2, 0, $chr, 2);
  995. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 0, $chr, 3);
  996. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-4, $chr, 4);
  997. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-3, $chr, 5);
  998. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-2, $chr, 6);
  999. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-1, $chr, 7);
  1000. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-1, $chr, 8);
  1001. return $marr;
  1002. }
  1003. /**
  1004. * Places the 8 bits of the third special corner case.
  1005. * (Annex F - ECC 200 symbol character placement)
  1006. * @param $marr (array) Array of symbols.
  1007. * @param $nrow (int) Number of rows.
  1008. * @param $ncol (int) Number of columns.
  1009. * @param $chr (int) Char byte.
  1010. * @return array
  1011. * @protected
  1012. */
  1013. protected function placeCornerC($marr, $nrow, $ncol, $chr) {
  1014. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-3, 0, $chr, 1);
  1015. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-2, 0, $chr, 2);
  1016. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 0, $chr, 3);
  1017. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-2, $chr, 4);
  1018. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-1, $chr, 5);
  1019. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-1, $chr, 6);
  1020. $marr = $this->placeModule($marr, $nrow, $ncol, 2, $ncol-1, $chr, 7);
  1021. $marr = $this->placeModule($marr, $nrow, $ncol, 3, $ncol-1, $chr, 8);
  1022. return $marr;
  1023. }
  1024. /**
  1025. * Places the 8 bits of the fourth special corner case.
  1026. * (Annex F - ECC 200 symbol character placement)
  1027. * @param $marr (array) Array of symbols.
  1028. * @param $nrow (int) Number of rows.
  1029. * @param $ncol (int) Number of columns.
  1030. * @param $chr (int) Char byte.
  1031. * @return array
  1032. * @protected
  1033. */
  1034. protected function placeCornerD($marr, $nrow, $ncol, $chr) {
  1035. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 0, $chr, 1);
  1036. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, $ncol-1, $chr, 2);
  1037. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-3, $chr, 3);
  1038. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-2, $chr, 4);
  1039. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-1, $chr, 5);
  1040. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-3, $chr, 6);
  1041. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-2, $chr, 7);
  1042. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-1, $chr, 8);
  1043. return $marr;
  1044. }
  1045. /**
  1046. * Build a placement map.
  1047. * (Annex F - ECC 200 symbol character placement)
  1048. * @param $nrow (int) Number of rows.
  1049. * @param $ncol (int) Number of columns.
  1050. * @return array
  1051. * @protected
  1052. */
  1053. protected function getPlacementMap($nrow, $ncol) {
  1054. // initialize array with zeros
  1055. $marr = array_fill(0, ($nrow * $ncol), 0);
  1056. // set starting values
  1057. $chr = 1;
  1058. $row = 4;
  1059. $col = 0;
  1060. do {
  1061. // repeatedly first check for one of the special corner cases, then
  1062. if (($row == $nrow) AND ($col == 0)) {
  1063. $marr = $this->placeCornerA($marr, $nrow, $ncol, $chr);
  1064. ++$chr;
  1065. }
  1066. if (($row == ($nrow - 2)) AND ($col == 0) AND ($ncol % 4)) {
  1067. $marr = $this->placeCornerB($marr, $nrow, $ncol, $chr);
  1068. ++$chr;
  1069. }
  1070. if (($row == ($nrow - 2)) AND ($col == 0) AND (($ncol % 8) == 4)) {
  1071. $marr = $this->placeCornerC($marr, $nrow, $ncol, $chr);
  1072. ++$chr;
  1073. }
  1074. if (($row == ($nrow + 4)) AND ($col == 2) AND (!($ncol % 8))) {
  1075. $marr = $this->placeCornerD($marr, $nrow, $ncol, $chr);
  1076. ++$chr;
  1077. }
  1078. // sweep upward diagonally, inserting successive characters,
  1079. do {
  1080. if (($row < $nrow) AND ($col >= 0) AND (!$marr[(($row * $ncol) + $col)])) {
  1081. $marr = $this->placeUtah($marr, $nrow, $ncol, $row, $col, $chr);
  1082. ++$chr;
  1083. }
  1084. $row -= 2;
  1085. $col += 2;
  1086. } while (($row >= 0) AND ($col < $ncol));
  1087. ++$row;
  1088. $col += 3;
  1089. // & then sweep downward diagonally, inserting successive characters,...
  1090. do {
  1091. if (($row >= 0) AND ($col < $ncol) AND (!$marr[(($row * $ncol) + $col)])) {
  1092. $marr = $this->placeUtah($marr, $nrow, $ncol, $row, $col, $chr);
  1093. ++$chr;
  1094. }
  1095. $row += 2;
  1096. $col -= 2;
  1097. } while (($row < $nrow) AND ($col >= 0));
  1098. $row += 3;
  1099. ++$col;
  1100. // ... until the entire array is scanned
  1101. } while (($row < $nrow) OR ($col < $ncol));
  1102. // lastly, if the lower righthand corner is untouched, fill in fixed pattern
  1103. if (!$marr[(($nrow * $ncol) - 1)]) {
  1104. $marr[(($nrow * $ncol) - 1)] = 1;
  1105. $marr[(($nrow * $ncol) - $ncol - 2)] = 1;
  1106. }
  1107. return $marr;
  1108. }
  1109. } // end DataMatrix class
  1110. //============================================================+
  1111. // END OF FILE
  1112. //============================================================+