datamatrix.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. <?php
  2. //============================================================+
  3. // File name : datamatrix.php
  4. // Version : 1.0.001
  5. // Begin : 2010-06-07
  6. // Last Update : 2011-09-14
  7. // Author : Nicola Asuni - Tecnick.com S.r.l - Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  8. // License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
  9. // -------------------------------------------------------------------
  10. // Copyright (C) 2010-2011 Nicola Asuni - Tecnick.com S.r.l.
  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.001
  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.001
  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 charactes 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->getPlacemetMap($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. break;
  773. }
  774. }
  775. } while (($p > 0) AND ($epos < $data_lenght));
  776. // process last data (if any)
  777. if ($p > 0) {
  778. // get remaining number of data symbols
  779. $cwr = ($this->getMaxDataCodewords($cw_num + 2) - $cw_num);
  780. if (($cwr == 1) AND ($p == 1)) {
  781. // d. If one symbol character remains and one C40 value (data character) remains to be encoded
  782. $c1 = array_shift($temp_cw);
  783. --$p;
  784. $cw[] = ($c1 + 1);
  785. ++$cw_num;
  786. } elseif (($cwr == 2) AND ($p == 1)) {
  787. // c. If two symbol characters remain and only one C40 value (data character) remains to be encoded
  788. $c1 = array_shift($temp_cw);
  789. --$p;
  790. $cw[] = 254;
  791. $cw[] = ($c1 + 1);
  792. $cw_num += 2;
  793. } elseif (($cwr == 2) AND ($p == 2)) {
  794. // b. If two symbol characters remain and two C40 values remain to be encoded
  795. $c1 = array_shift($temp_cw);
  796. $c2 = array_shift($temp_cw);
  797. $p -= 2;
  798. $tmp = ((1600 * $c1) + (40 * $c2) + 1);
  799. $cw[] = ($tmp >> 8);
  800. $cw[] = ($tmp % 256);
  801. $cw_num += 2;
  802. } else {
  803. // switch to ASCII encoding
  804. $enc = ENC_ASCII;
  805. $cw[] = $this->getSwitchEncodingCodeword($enc);
  806. ++$cw_num;
  807. }
  808. }
  809. break;
  810. }
  811. case ENC_EDF: { // F. While in EDIFACT (EDF) encodation
  812. // initialize temporary array with 0 lenght
  813. $temp_cw = array();
  814. $epos = $pos;
  815. $field_lenght = 0;
  816. while ($epos < $data_lenght) {
  817. // 2. process the next character in EDIFACT encodation.
  818. $chr = ord($data{($epos)});
  819. ++$epos;
  820. $temp_cw[] = $chr;
  821. ++$field_lenght;
  822. if (($field_lenght == 4) OR ($epos == $data_lenght)) {
  823. if ($field_lenght < 4) {
  824. // set unlatch character
  825. $temp_cw[] = 0x1f;
  826. ++$field_lenght;
  827. $enc = ENC_ASCII;
  828. // fill empty characters
  829. for ($i = $field_lenght; $i < 4; ++$i) {
  830. $temp_cw[] = 0;
  831. }
  832. }
  833. // encodes four data characters in three codewords
  834. $cw[] = (($temp_cw[0] & 0x3F) << 2) + (($temp_cw[1] & 0x30) >> 4);
  835. $cw[] = (($temp_cw[1] & 0x0F) << 4) + (($temp_cw[2] & 0x3C) >> 2);
  836. $cw[] = (($temp_cw[2] & 0x03) << 6) + ($temp_cw[3] & 0x3F);
  837. $cw_num += 3;
  838. $temp_cw = array();
  839. $pos = $epos;
  840. $field_lenght = 0;
  841. }
  842. // 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.
  843. if ($field_lenght == 0) {
  844. // get remaining number of data symbols
  845. $cwr = ($this->getMaxDataCodewords($cw_num + 2) - $cw_num);
  846. if ($cwr < 3) {
  847. // return to ascii without unlatch
  848. $enc = ENC_ASCII;
  849. break; // exit from EDIFACT mode
  850. } else {
  851. $newenc = $this->lookAheadTest($data, $pos, $enc);
  852. if ($newenc != $enc) {
  853. // 1. If the look-ahead test (starting at step J) indicates another mode, switch to that mode.
  854. $enc = $newenc;
  855. $cw[] = $this->getSwitchEncodingCodeword($enc);
  856. ++$cw_num;
  857. break; // exit from EDIFACT mode
  858. }
  859. }
  860. }
  861. }
  862. break;
  863. }
  864. case ENC_BASE256: { // G. While in Base 256 (B256) encodation
  865. // initialize temporary array with 0 lenght
  866. $temp_cw = array();
  867. $field_lenght = 0;
  868. while (($pos < $data_lenght) AND ($field_lenght <= 1555)) {
  869. $newenc = $this->lookAheadTest($data, $pos, $enc);
  870. if ($newenc != $enc) {
  871. // 1. If the look-ahead test (starting at step J) indicates another mode, switch to that mode.
  872. $enc = $newenc;
  873. $cw[] = $this->getSwitchEncodingCodeword($enc);
  874. ++$cw_num;
  875. break; // exit from B256 mode
  876. } else {
  877. // 2. Otherwise, process the next character in Base 256 encodation.
  878. $chr = ord($data{($pos)});
  879. ++$pos;
  880. $temp_cw[] = $chr;
  881. ++$field_lenght;
  882. }
  883. }
  884. // set field lenght
  885. if ($field_lenght <= 249) {
  886. $cw[] = $field_lenght;
  887. ++$cw_num;
  888. } else {
  889. $cw[] = (floor($field_lenght / 250) + 249);
  890. $cw[] = ($field_lenght % 250);
  891. $cw_num += 2;
  892. }
  893. if (!empty($temp_cw)) {
  894. // add B256 field
  895. foreach ($temp_cw as $p => $cht) {
  896. $cw[] = $this->get255StateCodeword($chr, ($cw_num + $p));
  897. }
  898. }
  899. break;
  900. }
  901. } // end of switch enc
  902. } // end of while
  903. // set last used encoding
  904. $this->last_enc = $enc;
  905. return $cw;
  906. }
  907. /**
  908. * Places "chr+bit" with appropriate wrapping within array[].
  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 $row (int) Row number.
  914. * @param $col (int) Column number.
  915. * @param $chr (int) Char byte.
  916. * @param $bit (int) Bit.
  917. * @return array
  918. * @protected
  919. */
  920. protected function placeModule($marr, $nrow, $ncol, $row, $col, $chr, $bit) {
  921. if ($row < 0) {
  922. $row += $nrow;
  923. $col += (4 - (($nrow + 4) % 8));
  924. }
  925. if ($col < 0) {
  926. $col += $ncol;
  927. $row += (4 - (($ncol + 4) % 8));
  928. }
  929. $marr[(($row * $ncol) + $col)] = ((10 * $chr) + $bit);
  930. return $marr;
  931. }
  932. /**
  933. * Places the 8 bits of a utah-shaped symbol character.
  934. * (Annex F - ECC 200 symbol character placement)
  935. * @param $marr array Array of symbols.
  936. * @param $nrow (int) Number of rows.
  937. * @param $ncol (int) Number of columns.
  938. * @param $row (int) Row number.
  939. * @param $col (int) Column number.
  940. * @param $chr (int) Char byte.
  941. * @return array
  942. * @protected
  943. */
  944. protected function placeUtah($marr, $nrow, $ncol, $row, $col, $chr) {
  945. $marr = $this->placeModule($marr, $nrow, $ncol, $row-2, $col-2, $chr, 1);
  946. $marr = $this->placeModule($marr, $nrow, $ncol, $row-2, $col-1, $chr, 2);
  947. $marr = $this->placeModule($marr, $nrow, $ncol, $row-1, $col-2, $chr, 3);
  948. $marr = $this->placeModule($marr, $nrow, $ncol, $row-1, $col-1, $chr, 4);
  949. $marr = $this->placeModule($marr, $nrow, $ncol, $row-1, $col, $chr, 5);
  950. $marr = $this->placeModule($marr, $nrow, $ncol, $row, $col-2, $chr, 6);
  951. $marr = $this->placeModule($marr, $nrow, $ncol, $row, $col-1, $chr, 7);
  952. $marr = $this->placeModule($marr, $nrow, $ncol, $row, $col, $chr, 8);
  953. return $marr;
  954. }
  955. /**
  956. * Places the 8 bits of the first special corner case.
  957. * (Annex F - ECC 200 symbol character placement)
  958. * @param $marr array Array of symbols.
  959. * @param $nrow (int) Number of rows.
  960. * @param $ncol (int) Number of columns.
  961. * @param $chr (int) Char byte.
  962. * @return array
  963. * @protected
  964. */
  965. protected function placeCornerA($marr, $nrow, $ncol, $chr) {
  966. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 0, $chr, 1);
  967. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 1, $chr, 2);
  968. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 2, $chr, 3);
  969. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-2, $chr, 4);
  970. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-1, $chr, 5);
  971. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-1, $chr, 6);
  972. $marr = $this->placeModule($marr, $nrow, $ncol, 2, $ncol-1, $chr, 7);
  973. $marr = $this->placeModule($marr, $nrow, $ncol, 3, $ncol-1, $chr, 8);
  974. return $marr;
  975. }
  976. /**
  977. * Places the 8 bits of the second special corner case.
  978. * (Annex F - ECC 200 symbol character placement)
  979. * @param $marr array Array of symbols.
  980. * @param $nrow (int) Number of rows.
  981. * @param $ncol (int) Number of columns.
  982. * @param $chr (int) Char byte.
  983. * @return array
  984. * @protected
  985. */
  986. protected function placeCornerB($marr, $nrow, $ncol, $chr) {
  987. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-3, 0, $chr, 1);
  988. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-2, 0, $chr, 2);
  989. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 0, $chr, 3);
  990. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-4, $chr, 4);
  991. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-3, $chr, 5);
  992. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-2, $chr, 6);
  993. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-1, $chr, 7);
  994. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-1, $chr, 8);
  995. return $marr;
  996. }
  997. /**
  998. * Places the 8 bits of the third special corner case.
  999. * (Annex F - ECC 200 symbol character placement)
  1000. * @param $marr array Array of symbols.
  1001. * @param $nrow (int) Number of rows.
  1002. * @param $ncol (int) Number of columns.
  1003. * @param $chr (int) Char byte.
  1004. * @return array
  1005. * @protected
  1006. */
  1007. protected function placeCornerC($marr, $nrow, $ncol, $chr) {
  1008. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-3, 0, $chr, 1);
  1009. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-2, 0, $chr, 2);
  1010. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 0, $chr, 3);
  1011. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-2, $chr, 4);
  1012. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-1, $chr, 5);
  1013. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-1, $chr, 6);
  1014. $marr = $this->placeModule($marr, $nrow, $ncol, 2, $ncol-1, $chr, 7);
  1015. $marr = $this->placeModule($marr, $nrow, $ncol, 3, $ncol-1, $chr, 8);
  1016. return $marr;
  1017. }
  1018. /**
  1019. * Places the 8 bits of the fourth special corner case.
  1020. * (Annex F - ECC 200 symbol character placement)
  1021. * @param $marr array Array of symbols.
  1022. * @param $nrow (int) Number of rows.
  1023. * @param $ncol (int) Number of columns.
  1024. * @param $chr (int) Char byte.
  1025. * @return array
  1026. * @protected
  1027. */
  1028. protected function placeCornerD($marr, $nrow, $ncol, $chr) {
  1029. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, 0, $chr, 1);
  1030. $marr = $this->placeModule($marr, $nrow, $ncol, $nrow-1, $ncol-1, $chr, 2);
  1031. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-3, $chr, 3);
  1032. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-2, $chr, 4);
  1033. $marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol-1, $chr, 5);
  1034. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-3, $chr, 6);
  1035. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-2, $chr, 7);
  1036. $marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol-1, $chr, 8);
  1037. return $marr;
  1038. }
  1039. /**
  1040. * Build a placement map.
  1041. * (Annex F - ECC 200 symbol character placement)
  1042. * @param $nrow (int) Number of rows.
  1043. * @param $ncol (int) Number of columns.
  1044. * @return array
  1045. * @protected
  1046. */
  1047. protected function getPlacemetMap($nrow, $ncol) {
  1048. // initialize array with zeros
  1049. $marr = array_fill(0, ($nrow * $ncol), 0);
  1050. // set starting values
  1051. $chr = 1;
  1052. $row = 4;
  1053. $col = 0;
  1054. do {
  1055. // repeatedly first check for one of the special corner cases, then
  1056. if (($row == $nrow) AND ($col == 0)) {
  1057. $marr = $this->placeCornerA($marr, $nrow, $ncol, $chr);
  1058. ++$chr;
  1059. }
  1060. if (($row == ($nrow - 2)) AND ($col == 0) AND ($ncol % 4)) {
  1061. $marr = $this->placeCornerB($marr, $nrow, $ncol, $chr);
  1062. ++$chr;
  1063. }
  1064. if (($row == ($nrow - 2)) AND ($col == 0) AND (($ncol % 8) == 4)) {
  1065. $marr = $this->placeCornerC($marr, $nrow, $ncol, $chr);
  1066. ++$chr;
  1067. }
  1068. if (($row == ($nrow + 4)) AND ($col == 2) AND (!($ncol % 8))) {
  1069. $marr = $this->placeCornerD($marr, $nrow, $ncol, $chr);
  1070. ++$chr;
  1071. }
  1072. // sweep upward diagonally, inserting successive characters,
  1073. do {
  1074. if (($row < $nrow) AND ($col >= 0) AND (!$marr[(($row * $ncol) + $col)])) {
  1075. $marr = $this->placeUtah($marr, $nrow, $ncol, $row, $col, $chr);
  1076. ++$chr;
  1077. }
  1078. $row -= 2;
  1079. $col += 2;
  1080. } while (($row >= 0) AND ($col < $ncol));
  1081. ++$row;
  1082. $col += 3;
  1083. // & then sweep downward diagonally, inserting successive characters,...
  1084. do {
  1085. if (($row >= 0) AND ($col < $ncol) AND (!$marr[(($row * $ncol) + $col)])) {
  1086. $marr = $this->placeUtah($marr, $nrow, $ncol, $row, $col, $chr);
  1087. ++$chr;
  1088. }
  1089. $row += 2;
  1090. $col -= 2;
  1091. } while (($row < $nrow) AND ($col >= 0));
  1092. $row += 3;
  1093. ++$col;
  1094. // ... until the entire array is scanned
  1095. } while (($row < $nrow) OR ($col < $ncol));
  1096. // lastly, if the lower righthand corner is untouched, fill in fixed pattern
  1097. if (!$marr[(($nrow * $ncol) - 1)]) {
  1098. $marr[(($nrow * $ncol) - 1)] = 1;
  1099. $marr[(($nrow * $ncol) - $ncol - 2)] = 1;
  1100. }
  1101. return $marr;
  1102. }
  1103. } // end DataMatrix class
  1104. //============================================================+
  1105. // END OF FILE
  1106. //============================================================+