PageRenderTime 76ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/PHPExcel/Shared/PDF/qrcode.php

https://bitbucket.org/tedbundyjr/phpexcel
PHP | 2879 lines | 1699 code | 209 blank | 971 comment | 296 complexity | 3662dd9c0db109662edd23c537ae5a62 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. //============================================================+
  3. // File name : qrcode.php
  4. // Version : 1.0.006
  5. // Begin : 2010-03-22
  6. // Last Update : 2010-08-30
  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-2010 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 QR-code arrays for TCPDF class.
  33. // QR Code symbol is a 2D barcode that can be scanned by
  34. // handy terminals such as a mobile phone with CCD.
  35. // The capacity of QR Code is up to 7000 digits or 4000
  36. // characters, and has high robustness.
  37. // This class supports QR Code model 2, described in
  38. // JIS (Japanese Industrial Standards) X0510:2004
  39. // or ISO/IEC 18004.
  40. // Currently the following features are not supported:
  41. // ECI and FNC1 mode, Micro QR Code, QR Code model 1,
  42. // Structured mode.
  43. //
  44. // This class is derived from the following projects:
  45. // ---------------------------------------------------------
  46. // "PHP QR Code encoder"
  47. // License: GNU-LGPLv3
  48. // Copyright (C) 2010 by Dominik Dzienia <deltalab at poczta dot fm>
  49. // http://phpqrcode.sourceforge.net/
  50. // https://sourceforge.net/projects/phpqrcode/
  51. //
  52. // The "PHP QR Code encoder" is based on
  53. // "C libqrencode library" (ver. 3.1.1)
  54. // License: GNU-LGPL 2.1
  55. // Copyright (C) 2006-2010 by Kentaro Fukuchi
  56. // http://megaui.net/fukuchi/works/qrencode/index.en.html
  57. //
  58. // Reed-Solomon code encoder is written by Phil Karn, KA9Q.
  59. // Copyright (C) 2002-2006 Phil Karn, KA9Q
  60. //
  61. // QR Code is registered trademark of DENSO WAVE INCORPORATED
  62. // http://www.denso-wave.com/qrcode/index-e.html
  63. // ---------------------------------------------------------
  64. //============================================================+
  65. /**
  66. * Class to create QR-code arrays for TCPDF class.
  67. * QR Code symbol is a 2D barcode that can be scanned by handy terminals such as a mobile phone with CCD.
  68. * The capacity of QR Code is up to 7000 digits or 4000 characters, and has high robustness.
  69. * This class supports QR Code model 2, described in JIS (Japanese Industrial Standards) X0510:2004 or ISO/IEC 18004.
  70. * Currently the following features are not supported: ECI and FNC1 mode, Micro QR Code, QR Code model 1, Structured mode.
  71. *
  72. * This class is derived from "PHP QR Code encoder" by Dominik Dzienia (http://phpqrcode.sourceforge.net/) based on "libqrencode C library 3.1.1." by Kentaro Fukuchi (http://megaui.net/fukuchi/works/qrencode/index.en.html), contains Reed-Solomon code written by Phil Karn, KA9Q. QR Code is registered trademark of DENSO WAVE INCORPORATED (http://www.denso-wave.com/qrcode/index-e.html).
  73. * Please read comments on this class source file for full copyright and license information.
  74. *
  75. * @package com.tecnick.tcpdf
  76. * @abstract Class for generating QR-code array for TCPDF.
  77. * @author Nicola Asuni
  78. * @copyright 2010-2010 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  79. * @link http://www.tcpdf.org
  80. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  81. * @version 1.0.006
  82. */
  83. // definitions
  84. if (!defined('QRCODEDEFS')) {
  85. /**
  86. * Indicate that definitions for this class are set
  87. */
  88. define('QRCODEDEFS', true);
  89. // -----------------------------------------------------
  90. // Encoding modes (characters which can be encoded in QRcode)
  91. /**
  92. * Encoding mode
  93. */
  94. define('QR_MODE_NL', -1);
  95. /**
  96. * Encoding mode numeric (0-9). 3 characters are encoded to 10bit length. In theory, 7089 characters or less can be stored in a QRcode.
  97. */
  98. define('QR_MODE_NM', 0);
  99. /**
  100. * Encoding mode alphanumeric (0-9A-Z $%*+-./:) 45characters. 2 characters are encoded to 11bit length. In theory, 4296 characters or less can be stored in a QRcode.
  101. */
  102. define('QR_MODE_AN', 1);
  103. /**
  104. * Encoding mode 8bit byte data. In theory, 2953 characters or less can be stored in a QRcode.
  105. */
  106. define('QR_MODE_8B', 2);
  107. /**
  108. * Encoding mode KANJI. A KANJI character (multibyte character) is encoded to 13bit length. In theory, 1817 characters or less can be stored in a QRcode.
  109. */
  110. define('QR_MODE_KJ', 3);
  111. /**
  112. * Encoding mode STRUCTURED (currently unsupported)
  113. */
  114. define('QR_MODE_ST', 4);
  115. // -----------------------------------------------------
  116. // Levels of error correction.
  117. // QRcode has a function of an error correcting for miss reading that white is black.
  118. // Error correcting is defined in 4 level as below.
  119. /**
  120. * Error correction level L : About 7% or less errors can be corrected.
  121. */
  122. define('QR_ECLEVEL_L', 0);
  123. /**
  124. * Error correction level M : About 15% or less errors can be corrected.
  125. */
  126. define('QR_ECLEVEL_M', 1);
  127. /**
  128. * Error correction level Q : About 25% or less errors can be corrected.
  129. */
  130. define('QR_ECLEVEL_Q', 2);
  131. /**
  132. * Error correction level H : About 30% or less errors can be corrected.
  133. */
  134. define('QR_ECLEVEL_H', 3);
  135. // -----------------------------------------------------
  136. // Version. Size of QRcode is defined as version.
  137. // Version is from 1 to 40.
  138. // Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases.
  139. // So version 40 is 177*177 matrix.
  140. /**
  141. * Maximum QR Code version.
  142. */
  143. define('QRSPEC_VERSION_MAX', 40);
  144. /**
  145. * Maximum matrix size for maximum version (version 40 is 177*177 matrix).
  146. */
  147. define('QRSPEC_WIDTH_MAX', 177);
  148. // -----------------------------------------------------
  149. /**
  150. * Matrix index to get width from $capacity array.
  151. */
  152. define('QRCAP_WIDTH', 0);
  153. /**
  154. * Matrix index to get number of words from $capacity array.
  155. */
  156. define('QRCAP_WORDS', 1);
  157. /**
  158. * Matrix index to get remainder from $capacity array.
  159. */
  160. define('QRCAP_REMINDER', 2);
  161. /**
  162. * Matrix index to get error correction level from $capacity array.
  163. */
  164. define('QRCAP_EC', 3);
  165. // -----------------------------------------------------
  166. // Structure (currently usupported)
  167. /**
  168. * Number of header bits for structured mode
  169. */
  170. define('STRUCTURE_HEADER_BITS', 20);
  171. /**
  172. * Max number of symbols for structured mode
  173. */
  174. define('MAX_STRUCTURED_SYMBOLS', 16);
  175. // -----------------------------------------------------
  176. // Masks
  177. /**
  178. * Down point base value for case 1 mask pattern (concatenation of same color in a line or a column)
  179. */
  180. define('N1', 3);
  181. /**
  182. * Down point base value for case 2 mask pattern (module block of same color)
  183. */
  184. define('N2', 3);
  185. /**
  186. * Down point base value for case 3 mask pattern (1:1:3:1:1(dark:bright:dark:bright:dark)pattern in a line or a column)
  187. */
  188. define('N3', 40);
  189. /**
  190. * Down point base value for case 4 mask pattern (ration of dark modules in whole)
  191. */
  192. define('N4', 10);
  193. // -----------------------------------------------------
  194. // Optimization settings
  195. /**
  196. * if true, estimates best mask (spec. default, but extremally slow; set to false to significant performance boost but (propably) worst quality code
  197. */
  198. define('QR_FIND_BEST_MASK', true);
  199. /**
  200. * if false, checks all masks available, otherwise value tells count of masks need to be checked, mask id are got randomly
  201. */
  202. define('QR_FIND_FROM_RANDOM', 2);
  203. /**
  204. * when QR_FIND_BEST_MASK === false
  205. */
  206. define('QR_DEFAULT_MASK', 2);
  207. // -----------------------------------------------------
  208. } // end of definitions
  209. // #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
  210. if (!class_exists('QRcode', false)) {
  211. // for compatibility with PHP4
  212. if (!function_exists('str_split')) {
  213. /**
  214. * Convert a string to an array (needed for PHP4 compatibility)
  215. * @param string $string The input string.
  216. * @param int $split_length Maximum length of the chunk.
  217. * @return If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length. FALSE is returned if split_length is less than 1. If the split_length length exceeds the length of string , the entire string is returned as the first (and only) array element.
  218. */
  219. function str_split($string, $split_length=1) {
  220. if ((strlen($string) > $split_length) OR (!$split_length)) {
  221. do {
  222. $c = strlen($string);
  223. $parts[] = substr($string, 0, $split_length);
  224. $string = substr($string, $split_length);
  225. } while ($string !== false);
  226. } else {
  227. $parts = array($string);
  228. }
  229. return $parts;
  230. }
  231. }
  232. // #####################################################
  233. /**
  234. * Class to create QR-code arrays for TCPDF class.
  235. * QR Code symbol is a 2D barcode that can be scanned by handy terminals such as a mobile phone with CCD.
  236. * The capacity of QR Code is up to 7000 digits or 4000 characters, and has high robustness.
  237. * This class supports QR Code model 2, described in JIS (Japanese Industrial Standards) X0510:2004 or ISO/IEC 18004.
  238. * Currently the following features are not supported: ECI and FNC1 mode, Micro QR Code, QR Code model 1, Structured mode.
  239. *
  240. * This class is derived from "PHP QR Code encoder" by Dominik Dzienia (http://phpqrcode.sourceforge.net/) based on "libqrencode C library 3.1.1." by Kentaro Fukuchi (http://megaui.net/fukuchi/works/qrencode/index.en.html), contains Reed-Solomon code written by Phil Karn, KA9Q. QR Code is registered trademark of DENSO WAVE INCORPORATED (http://www.denso-wave.com/qrcode/index-e.html).
  241. * Please read comments on this class source file for full copyright and license information.
  242. *
  243. * @name QRcode
  244. * @package com.tecnick.tcpdf
  245. * @abstract Class for generating QR-code array for TCPDF.
  246. * @author Nicola Asuni
  247. * @copyright 2010-2010 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  248. * @link http://www.tcpdf.org
  249. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  250. * @version 1.0.006
  251. */
  252. class QRcode {
  253. /**
  254. * @var barcode array to be returned which is readable by TCPDF
  255. * @access protected
  256. */
  257. protected $barcode_array = array();
  258. /**
  259. * @var QR code version. Size of QRcode is defined as version. Version is from 1 to 40. Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases. So version 40 is 177*177 matrix.
  260. * @access protected
  261. */
  262. protected $version = 0;
  263. /**
  264. * @var Levels of error correction. See definitions for possible values.
  265. * @access protected
  266. */
  267. protected $level = QR_ECLEVEL_L;
  268. /**
  269. * @var Encoding mode
  270. * @access protected
  271. */
  272. protected $hint = QR_MODE_8B;
  273. /**
  274. * @var if true the input string will be converted to uppercase
  275. * @access protected
  276. */
  277. protected $casesensitive = true;
  278. /**
  279. * @var structured QR code (not supported yet)
  280. * @access protected
  281. */
  282. protected $structured = 0;
  283. /**
  284. * @var mask data
  285. * @access protected
  286. */
  287. protected $data;
  288. // FrameFiller
  289. /**
  290. * @var width
  291. * @access protected
  292. */
  293. protected $width;
  294. /**
  295. * @var frame
  296. * @access protected
  297. */
  298. protected $frame;
  299. /**
  300. * @var X position of bit
  301. * @access protected
  302. */
  303. protected $x;
  304. /**
  305. * @var Y position of bit
  306. * @access protected
  307. */
  308. protected $y;
  309. /**
  310. * @var direction
  311. * @access protected
  312. */
  313. protected $dir;
  314. /**
  315. * @var single bit
  316. * @access protected
  317. */
  318. protected $bit;
  319. // ---- QRrawcode ----
  320. /**
  321. * @var data code
  322. * @access protected
  323. */
  324. protected $datacode = array();
  325. /**
  326. * @var error correction code
  327. * @access protected
  328. */
  329. protected $ecccode = array();
  330. /**
  331. * @var blocks
  332. * @access protected
  333. */
  334. protected $blocks;
  335. /**
  336. * @var Reed-Solomon blocks
  337. * @access protected
  338. */
  339. protected $rsblocks = array(); //of RSblock
  340. /**
  341. * @var counter
  342. * @access protected
  343. */
  344. protected $count;
  345. /**
  346. * @var data length
  347. * @access protected
  348. */
  349. protected $dataLength;
  350. /**
  351. * @var error correction length
  352. * @access protected
  353. */
  354. protected $eccLength;
  355. /**
  356. * @var b1
  357. * @access protected
  358. */
  359. protected $b1;
  360. // ---- QRmask ----
  361. /**
  362. * @var run length
  363. * @access protected
  364. */
  365. protected $runLength = array();
  366. // ---- QRsplit ----
  367. /**
  368. * @var input data string
  369. * @access protected
  370. */
  371. protected $dataStr = '';
  372. /**
  373. * @var input items
  374. * @access protected
  375. */
  376. protected $items;
  377. // Reed-Solomon items
  378. /**
  379. * @var Reed-Solomon items
  380. * @access protected
  381. */
  382. protected $rsitems = array();
  383. /**
  384. * @var array of frames
  385. * @access protected
  386. */
  387. protected $frames = array();
  388. /**
  389. * @var alphabet-numeric convesion table
  390. * @access protected
  391. */
  392. protected $anTable = array(
  393. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //
  394. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //
  395. 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, //
  396. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, //
  397. -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, //
  398. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, //
  399. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //
  400. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 //
  401. );
  402. /**
  403. * @var array Table of the capacity of symbols
  404. * See Table 1 (pp.13) and Table 12-16 (pp.30-36), JIS X0510:2004.
  405. * @access protected
  406. */
  407. protected $capacity = array(
  408. array( 0, 0, 0, array( 0, 0, 0, 0)), //
  409. array( 21, 26, 0, array( 7, 10, 13, 17)), // 1
  410. array( 25, 44, 7, array( 10, 16, 22, 28)), //
  411. array( 29, 70, 7, array( 15, 26, 36, 44)), //
  412. array( 33, 100, 7, array( 20, 36, 52, 64)), //
  413. array( 37, 134, 7, array( 26, 48, 72, 88)), // 5
  414. array( 41, 172, 7, array( 36, 64, 96, 112)), //
  415. array( 45, 196, 0, array( 40, 72, 108, 130)), //
  416. array( 49, 242, 0, array( 48, 88, 132, 156)), //
  417. array( 53, 292, 0, array( 60, 110, 160, 192)), //
  418. array( 57, 346, 0, array( 72, 130, 192, 224)), // 10
  419. array( 61, 404, 0, array( 80, 150, 224, 264)), //
  420. array( 65, 466, 0, array( 96, 176, 260, 308)), //
  421. array( 69, 532, 0, array( 104, 198, 288, 352)), //
  422. array( 73, 581, 3, array( 120, 216, 320, 384)), //
  423. array( 77, 655, 3, array( 132, 240, 360, 432)), // 15
  424. array( 81, 733, 3, array( 144, 280, 408, 480)), //
  425. array( 85, 815, 3, array( 168, 308, 448, 532)), //
  426. array( 89, 901, 3, array( 180, 338, 504, 588)), //
  427. array( 93, 991, 3, array( 196, 364, 546, 650)), //
  428. array( 97, 1085, 3, array( 224, 416, 600, 700)), // 20
  429. array(101, 1156, 4, array( 224, 442, 644, 750)), //
  430. array(105, 1258, 4, array( 252, 476, 690, 816)), //
  431. array(109, 1364, 4, array( 270, 504, 750, 900)), //
  432. array(113, 1474, 4, array( 300, 560, 810, 960)), //
  433. array(117, 1588, 4, array( 312, 588, 870, 1050)), // 25
  434. array(121, 1706, 4, array( 336, 644, 952, 1110)), //
  435. array(125, 1828, 4, array( 360, 700, 1020, 1200)), //
  436. array(129, 1921, 3, array( 390, 728, 1050, 1260)), //
  437. array(133, 2051, 3, array( 420, 784, 1140, 1350)), //
  438. array(137, 2185, 3, array( 450, 812, 1200, 1440)), // 30
  439. array(141, 2323, 3, array( 480, 868, 1290, 1530)), //
  440. array(145, 2465, 3, array( 510, 924, 1350, 1620)), //
  441. array(149, 2611, 3, array( 540, 980, 1440, 1710)), //
  442. array(153, 2761, 3, array( 570, 1036, 1530, 1800)), //
  443. array(157, 2876, 0, array( 570, 1064, 1590, 1890)), // 35
  444. array(161, 3034, 0, array( 600, 1120, 1680, 1980)), //
  445. array(165, 3196, 0, array( 630, 1204, 1770, 2100)), //
  446. array(169, 3362, 0, array( 660, 1260, 1860, 2220)), //
  447. array(173, 3532, 0, array( 720, 1316, 1950, 2310)), //
  448. array(177, 3706, 0, array( 750, 1372, 2040, 2430)) // 40
  449. );
  450. /**
  451. * @var array Length indicator
  452. * @access protected
  453. */
  454. protected $lengthTableBits = array(
  455. array(10, 12, 14),
  456. array( 9, 11, 13),
  457. array( 8, 16, 16),
  458. array( 8, 10, 12)
  459. );
  460. /**
  461. * @var array Table of the error correction code (Reed-Solomon block)
  462. * See Table 12-16 (pp.30-36), JIS X0510:2004.
  463. * @access protected
  464. */
  465. protected $eccTable = array(
  466. array(array( 0, 0), array( 0, 0), array( 0, 0), array( 0, 0)), //
  467. array(array( 1, 0), array( 1, 0), array( 1, 0), array( 1, 0)), // 1
  468. array(array( 1, 0), array( 1, 0), array( 1, 0), array( 1, 0)), //
  469. array(array( 1, 0), array( 1, 0), array( 2, 0), array( 2, 0)), //
  470. array(array( 1, 0), array( 2, 0), array( 2, 0), array( 4, 0)), //
  471. array(array( 1, 0), array( 2, 0), array( 2, 2), array( 2, 2)), // 5
  472. array(array( 2, 0), array( 4, 0), array( 4, 0), array( 4, 0)), //
  473. array(array( 2, 0), array( 4, 0), array( 2, 4), array( 4, 1)), //
  474. array(array( 2, 0), array( 2, 2), array( 4, 2), array( 4, 2)), //
  475. array(array( 2, 0), array( 3, 2), array( 4, 4), array( 4, 4)), //
  476. array(array( 2, 2), array( 4, 1), array( 6, 2), array( 6, 2)), // 10
  477. array(array( 4, 0), array( 1, 4), array( 4, 4), array( 3, 8)), //
  478. array(array( 2, 2), array( 6, 2), array( 4, 6), array( 7, 4)), //
  479. array(array( 4, 0), array( 8, 1), array( 8, 4), array(12, 4)), //
  480. array(array( 3, 1), array( 4, 5), array(11, 5), array(11, 5)), //
  481. array(array( 5, 1), array( 5, 5), array( 5, 7), array(11, 7)), // 15
  482. array(array( 5, 1), array( 7, 3), array(15, 2), array( 3, 13)), //
  483. array(array( 1, 5), array(10, 1), array( 1, 15), array( 2, 17)), //
  484. array(array( 5, 1), array( 9, 4), array(17, 1), array( 2, 19)), //
  485. array(array( 3, 4), array( 3, 11), array(17, 4), array( 9, 16)), //
  486. array(array( 3, 5), array( 3, 13), array(15, 5), array(15, 10)), // 20
  487. array(array( 4, 4), array(17, 0), array(17, 6), array(19, 6)), //
  488. array(array( 2, 7), array(17, 0), array( 7, 16), array(34, 0)), //
  489. array(array( 4, 5), array( 4, 14), array(11, 14), array(16, 14)), //
  490. array(array( 6, 4), array( 6, 14), array(11, 16), array(30, 2)), //
  491. array(array( 8, 4), array( 8, 13), array( 7, 22), array(22, 13)), // 25
  492. array(array(10, 2), array(19, 4), array(28, 6), array(33, 4)), //
  493. array(array( 8, 4), array(22, 3), array( 8, 26), array(12, 28)), //
  494. array(array( 3, 10), array( 3, 23), array( 4, 31), array(11, 31)), //
  495. array(array( 7, 7), array(21, 7), array( 1, 37), array(19, 26)), //
  496. array(array( 5, 10), array(19, 10), array(15, 25), array(23, 25)), // 30
  497. array(array(13, 3), array( 2, 29), array(42, 1), array(23, 28)), //
  498. array(array(17, 0), array(10, 23), array(10, 35), array(19, 35)), //
  499. array(array(17, 1), array(14, 21), array(29, 19), array(11, 46)), //
  500. array(array(13, 6), array(14, 23), array(44, 7), array(59, 1)), //
  501. array(array(12, 7), array(12, 26), array(39, 14), array(22, 41)), // 35
  502. array(array( 6, 14), array( 6, 34), array(46, 10), array( 2, 64)), //
  503. array(array(17, 4), array(29, 14), array(49, 10), array(24, 46)), //
  504. array(array( 4, 18), array(13, 32), array(48, 14), array(42, 32)), //
  505. array(array(20, 4), array(40, 7), array(43, 22), array(10, 67)), //
  506. array(array(19, 6), array(18, 31), array(34, 34), array(20, 61)) // 40
  507. );
  508. /**
  509. * @var array Positions of alignment patterns.
  510. * This array includes only the second and the third position of the alignment patterns. Rest of them can be calculated from the distance between them.
  511. * See Table 1 in Appendix E (pp.71) of JIS X0510:2004.
  512. * @access protected
  513. */
  514. protected $alignmentPattern = array(
  515. array( 0, 0),
  516. array( 0, 0), array(18, 0), array(22, 0), array(26, 0), array(30, 0), // 1- 5
  517. array(34, 0), array(22, 38), array(24, 42), array(26, 46), array(28, 50), // 6-10
  518. array(30, 54), array(32, 58), array(34, 62), array(26, 46), array(26, 48), // 11-15
  519. array(26, 50), array(30, 54), array(30, 56), array(30, 58), array(34, 62), // 16-20
  520. array(28, 50), array(26, 50), array(30, 54), array(28, 54), array(32, 58), // 21-25
  521. array(30, 58), array(34, 62), array(26, 50), array(30, 54), array(26, 52), // 26-30
  522. array(30, 56), array(34, 60), array(30, 58), array(34, 62), array(30, 54), // 31-35
  523. array(24, 50), array(28, 54), array(32, 58), array(26, 54), array(30, 58) // 35-40
  524. );
  525. /**
  526. * @var array Version information pattern (BCH coded).
  527. * See Table 1 in Appendix D (pp.68) of JIS X0510:2004.
  528. * size: [QRSPEC_VERSION_MAX - 6]
  529. * @access protected
  530. */
  531. protected $versionPattern = array(
  532. 0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d, //
  533. 0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9, //
  534. 0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75, //
  535. 0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64, //
  536. 0x27541, 0x28c69
  537. );
  538. /**
  539. * @var array Format information
  540. * @access protected
  541. */
  542. protected $formatInfo = array(
  543. array(0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976), //
  544. array(0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0), //
  545. array(0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed), //
  546. array(0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b) //
  547. );
  548. // -------------------------------------------------
  549. // -------------------------------------------------
  550. /**
  551. * This is the class constructor.
  552. * Creates a QRcode object
  553. * @param string $code code to represent using QRcode
  554. * @param string $eclevel error level: <ul><li>L : About 7% or less errors can be corrected.</li><li>M : About 15% or less errors can be corrected.</li><li>Q : About 25% or less errors can be corrected.</li><li>H : About 30% or less errors can be corrected.</li></ul>
  555. * @access public
  556. * @since 1.0.000
  557. */
  558. public function __construct($code, $eclevel = 'L') {
  559. $barcode_array = array();
  560. if ((is_null($code)) OR ($code == '\0') OR ($code == '')) {
  561. return false;
  562. }
  563. // set error correction level
  564. $this->level = array_search($eclevel, array('L', 'M', 'Q', 'H'));
  565. if ($this->level === false) {
  566. $this->level = QR_ECLEVEL_L;
  567. }
  568. if (($this->hint != QR_MODE_8B) AND ($this->hint != QR_MODE_KJ)) {
  569. return false;
  570. }
  571. if (($this->version < 0) OR ($this->version > QRSPEC_VERSION_MAX)) {
  572. return false;
  573. }
  574. $this->items = array();
  575. $this->encodeString($code);
  576. if (is_null($this->data)) {
  577. return false;
  578. }
  579. $qrTab = $this->binarize($this->data);
  580. $size = count($qrTab);
  581. $barcode_array['num_rows'] = $size;
  582. $barcode_array['num_cols'] = $size;
  583. $barcode_array['bcode'] = array();
  584. foreach ($qrTab as $line) {
  585. $arrAdd = array();
  586. foreach (str_split($line) as $char) {
  587. $arrAdd[] = ($char=='1')?1:0;
  588. }
  589. $barcode_array['bcode'][] = $arrAdd;
  590. }
  591. $this->barcode_array = $barcode_array;
  592. }
  593. /**
  594. * Returns a barcode array which is readable by TCPDF
  595. * @return array barcode array readable by TCPDF;
  596. * @access public
  597. */
  598. public function getBarcodeArray() {
  599. return $this->barcode_array;
  600. }
  601. /**
  602. * Convert the frame in binary form
  603. * @param array $frame array to binarize
  604. * @return array frame in binary form
  605. */
  606. protected function binarize($frame) {
  607. $len = count($frame);
  608. // the frame is square (width = height)
  609. foreach ($frame as &$frameLine) {
  610. for ($i=0; $i<$len; $i++) {
  611. $frameLine[$i] = (ord($frameLine[$i])&1)?'1':'0';
  612. }
  613. }
  614. return $frame;
  615. }
  616. /**
  617. * Encode the input string to QR code
  618. * @param string $string input string to encode
  619. */
  620. protected function encodeString($string) {
  621. $this->dataStr = $string;
  622. if (!$this->casesensitive) {
  623. $this->toUpper();
  624. }
  625. $ret = $this->splitString();
  626. if ($ret < 0) {
  627. return NULL;
  628. }
  629. $this->encodeMask(-1);
  630. }
  631. /**
  632. * Encode mask
  633. * @param int $mask masking mode
  634. */
  635. protected function encodeMask($mask) {
  636. $spec = array(0, 0, 0, 0, 0);
  637. $this->datacode = $this->getByteStream($this->items);
  638. if (is_null($this->datacode)) {
  639. return NULL;
  640. }
  641. $spec = $this->getEccSpec($this->version, $this->level, $spec);
  642. $this->b1 = $this->rsBlockNum1($spec);
  643. $this->dataLength = $this->rsDataLength($spec);
  644. $this->eccLength = $this->rsEccLength($spec);
  645. $this->ecccode = array_fill(0, $this->eccLength, 0);
  646. $this->blocks = $this->rsBlockNum($spec);
  647. $ret = $this->init($spec);
  648. if ($ret < 0) {
  649. return NULL;
  650. }
  651. $this->count = 0;
  652. $this->width = $this->getWidth($this->version);
  653. $this->frame = $this->newFrame($this->version);
  654. $this->x = $this->width - 1;
  655. $this->y = $this->width - 1;
  656. $this->dir = -1;
  657. $this->bit = -1;
  658. // inteleaved data and ecc codes
  659. for ($i=0; $i < ($this->dataLength + $this->eccLength); $i++) {
  660. $code = $this->getCode();
  661. $bit = 0x80;
  662. for ($j=0; $j<8; $j++) {
  663. $addr = $this->getNextPosition();
  664. $this->setFrameAt($addr, 0x02 | (($bit & $code) != 0));
  665. $bit = $bit >> 1;
  666. }
  667. }
  668. // remainder bits
  669. $j = $this->getRemainder($this->version);
  670. for ($i=0; $i<$j; $i++) {
  671. $addr = $this->getNextPosition();
  672. $this->setFrameAt($addr, 0x02);
  673. }
  674. // masking
  675. $this->runLength = array_fill(0, QRSPEC_WIDTH_MAX + 1, 0);
  676. if ($mask < 0) {
  677. if (QR_FIND_BEST_MASK) {
  678. $masked = $this->mask($this->width, $this->frame, $this->level);
  679. } else {
  680. $masked = $this->makeMask($this->width, $this->frame, (intval(QR_DEFAULT_MASK) % 8), $this->level);
  681. }
  682. } else {
  683. $masked = $this->makeMask($this->width, $this->frame, $mask, $this->level);
  684. }
  685. if ($masked == NULL) {
  686. return NULL;
  687. }
  688. $this->data = $masked;
  689. }
  690. // - - - - - - - - - - - - - - - - - - - - - - - - -
  691. // FrameFiller
  692. /**
  693. * Set frame value at specified position
  694. * @param array $at x,y position
  695. * @param int $val value of the character to set
  696. */
  697. protected function setFrameAt($at, $val) {
  698. $this->frame[$at['y']][$at['x']] = chr($val);
  699. }
  700. /**
  701. * Get frame value at specified position
  702. * @param array $at x,y position
  703. * @return value at specified position
  704. */
  705. protected function getFrameAt($at) {
  706. return ord($this->frame[$at['y']][$at['x']]);
  707. }
  708. /**
  709. * Return the next frame position
  710. * @return array of x,y coordinates
  711. */
  712. protected function getNextPosition() {
  713. do {
  714. if ($this->bit == -1) {
  715. $this->bit = 0;
  716. return array('x'=>$this->x, 'y'=>$this->y);
  717. }
  718. $x = $this->x;
  719. $y = $this->y;
  720. $w = $this->width;
  721. if ($this->bit == 0) {
  722. $x--;
  723. $this->bit++;
  724. } else {
  725. $x++;
  726. $y += $this->dir;
  727. $this->bit--;
  728. }
  729. if ($this->dir < 0) {
  730. if ($y < 0) {
  731. $y = 0;
  732. $x -= 2;
  733. $this->dir = 1;
  734. if ($x == 6) {
  735. $x--;
  736. $y = 9;
  737. }
  738. }
  739. } else {
  740. if ($y == $w) {
  741. $y = $w - 1;
  742. $x -= 2;
  743. $this->dir = -1;
  744. if ($x == 6) {
  745. $x--;
  746. $y -= 8;
  747. }
  748. }
  749. }
  750. if (($x < 0) OR ($y < 0)) {
  751. return NULL;
  752. }
  753. $this->x = $x;
  754. $this->y = $y;
  755. } while(ord($this->frame[$y][$x]) & 0x80);
  756. return array('x'=>$x, 'y'=>$y);
  757. }
  758. // - - - - - - - - - - - - - - - - - - - - - - - - -
  759. // QRrawcode
  760. /**
  761. * Initialize code.
  762. * @param array $spec array of ECC specification
  763. * @return 0 in case of success, -1 in case of error
  764. */
  765. protected function init($spec) {
  766. $dl = $this->rsDataCodes1($spec);
  767. $el = $this->rsEccCodes1($spec);
  768. $rs = $this->init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
  769. $blockNo = 0;
  770. $dataPos = 0;
  771. $eccPos = 0;
  772. $endfor = $this->rsBlockNum1($spec);
  773. for ($i=0; $i < $endfor; ++$i) {
  774. $ecc = array_slice($this->ecccode, $eccPos);
  775. $this->rsblocks[$blockNo] = array();
  776. $this->rsblocks[$blockNo]['dataLength'] = $dl;
  777. $this->rsblocks[$blockNo]['data'] = array_slice($this->datacode, $dataPos);
  778. $this->rsblocks[$blockNo]['eccLength'] = $el;
  779. $ecc = $this->encode_rs_char($rs, $this->rsblocks[$blockNo]['data'], $ecc);
  780. $this->rsblocks[$blockNo]['ecc'] = $ecc;
  781. $this->ecccode = array_merge(array_slice($this->ecccode,0, $eccPos), $ecc);
  782. $dataPos += $dl;
  783. $eccPos += $el;
  784. $blockNo++;
  785. }
  786. if ($this->rsBlockNum2($spec) == 0) {
  787. return 0;
  788. }
  789. $dl = $this->rsDataCodes2($spec);
  790. $el = $this->rsEccCodes2($spec);
  791. $rs = $this->init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
  792. if ($rs == NULL) {
  793. return -1;
  794. }
  795. $endfor = $this->rsBlockNum2($spec);
  796. for ($i=0; $i < $endfor; ++$i) {
  797. $ecc = array_slice($this->ecccode, $eccPos);
  798. $this->rsblocks[$blockNo] = array();
  799. $this->rsblocks[$blockNo]['dataLength'] = $dl;
  800. $this->rsblocks[$blockNo]['data'] = array_slice($this->datacode, $dataPos);
  801. $this->rsblocks[$blockNo]['eccLength'] = $el;
  802. $ecc = $this->encode_rs_char($rs, $this->rsblocks[$blockNo]['data'], $ecc);
  803. $this->rsblocks[$blockNo]['ecc'] = $ecc;
  804. $this->ecccode = array_merge(array_slice($this->ecccode, 0, $eccPos), $ecc);
  805. $dataPos += $dl;
  806. $eccPos += $el;
  807. $blockNo++;
  808. }
  809. return 0;
  810. }
  811. /**
  812. * Return Reed-Solomon block code.
  813. * @return array rsblocks
  814. */
  815. protected function getCode() {
  816. if ($this->count < $this->dataLength) {
  817. $row = $this->count % $this->blocks;
  818. $col = $this->count / $this->blocks;
  819. if ($col >= $this->rsblocks[0]['dataLength']) {
  820. $row += $this->b1;
  821. }
  822. $ret = $this->rsblocks[$row]['data'][$col];
  823. } elseif ($this->count < $this->dataLength + $this->eccLength) {
  824. $row = ($this->count - $this->dataLength) % $this->blocks;
  825. $col = ($this->count - $this->dataLength) / $this->blocks;
  826. $ret = $this->rsblocks[$row]['ecc'][$col];
  827. } else {
  828. return 0;
  829. }
  830. $this->count++;
  831. return $ret;
  832. }
  833. // - - - - - - - - - - - - - - - - - - - - - - - - -
  834. // QRmask
  835. /**
  836. * Write Format Information on frame and returns the number of black bits
  837. * @param int $width frame width
  838. * @param array $frame frame
  839. * @param array $mask masking mode
  840. * @param int $level error correction level
  841. * @return int blacks
  842. */
  843. protected function writeFormatInformation($width, &$frame, $mask, $level) {
  844. $blacks = 0;
  845. $format = $this->getFormatInfo($mask, $level);
  846. for ($i=0; $i<8; ++$i) {
  847. if ($format & 1) {
  848. $blacks += 2;
  849. $v = 0x85;
  850. } else {
  851. $v = 0x84;
  852. }
  853. $frame[8][$width - 1 - $i] = chr($v);
  854. if ($i < 6) {
  855. $frame[$i][8] = chr($v);
  856. } else {
  857. $frame[$i + 1][8] = chr($v);
  858. }
  859. $format = $format >> 1;
  860. }
  861. for ($i=0; $i<7; ++$i) {
  862. if ($format & 1) {
  863. $blacks += 2;
  864. $v = 0x85;
  865. } else {
  866. $v = 0x84;
  867. }
  868. $frame[$width - 7 + $i][8] = chr($v);
  869. if ($i == 0) {
  870. $frame[8][7] = chr($v);
  871. } else {
  872. $frame[8][6 - $i] = chr($v);
  873. }
  874. $format = $format >> 1;
  875. }
  876. return $blacks;
  877. }
  878. /**
  879. * mask0
  880. * @param int $x X position
  881. * @param int $y Y position
  882. * @return int mask
  883. */
  884. protected function mask0($x, $y) {
  885. return ($x + $y) & 1;
  886. }
  887. /**
  888. * mask1
  889. * @param int $x X position
  890. * @param int $y Y position
  891. * @return int mask
  892. */
  893. protected function mask1($x, $y) {
  894. return ($y & 1);
  895. }
  896. /**
  897. * mask2
  898. * @param int $x X position
  899. * @param int $y Y position
  900. * @return int mask
  901. */
  902. protected function mask2($x, $y) {
  903. return ($x % 3);
  904. }
  905. /**
  906. * mask3
  907. * @param int $x X position
  908. * @param int $y Y position
  909. * @return int mask
  910. */
  911. protected function mask3($x, $y) {
  912. return ($x + $y) % 3;
  913. }
  914. /**
  915. * mask4
  916. * @param int $x X position
  917. * @param int $y Y position
  918. * @return int mask
  919. */
  920. protected function mask4($x, $y) {
  921. return (((int)($y / 2)) + ((int)($x / 3))) & 1;
  922. }
  923. /**
  924. * mask5
  925. * @param int $x X position
  926. * @param int $y Y position
  927. * @return int mask
  928. */
  929. protected function mask5($x, $y) {
  930. return (($x * $y) & 1) + ($x * $y) % 3;
  931. }
  932. /**
  933. * mask6
  934. * @param int $x X position
  935. * @param int $y Y position
  936. * @return int mask
  937. */
  938. protected function mask6($x, $y) {
  939. return ((($x * $y) & 1) + ($x * $y) % 3) & 1;
  940. }
  941. /**
  942. * mask7
  943. * @param int $x X position
  944. * @param int $y Y position
  945. * @return int mask
  946. */
  947. protected function mask7($x, $y) {
  948. return ((($x * $y) % 3) + (($x + $y) & 1)) & 1;
  949. }
  950. /**
  951. * Return bitmask
  952. * @param int $maskNo mask number
  953. * @param int $width width
  954. * @param array $frame frame
  955. * @return array bitmask
  956. */
  957. protected function generateMaskNo($maskNo, $width, $frame) {
  958. $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
  959. for ($y=0; $y<$width; ++$y) {
  960. for ($x=0; $x<$width; ++$x) {
  961. if (ord($frame[$y][$x]) & 0x80) {
  962. $bitMask[$y][$x] = 0;
  963. } else {
  964. $maskFunc = call_user_func(array($this, 'mask'.$maskNo), $x, $y);
  965. $bitMask[$y][$x] = ($maskFunc == 0)?1:0;
  966. }
  967. }
  968. }
  969. return $bitMask;
  970. }
  971. /**
  972. * makeMaskNo
  973. * @param int $maskNo
  974. * @param int $width
  975. * @param int $s
  976. * @param int $d
  977. * @param boolean $maskGenOnly
  978. * @return int b
  979. */
  980. protected function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly=false) {
  981. $b = 0;
  982. $bitMask = array();
  983. $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
  984. if ($maskGenOnly) {
  985. return;
  986. }
  987. $d = $s;
  988. for ($y=0; $y<$width; ++$y) {
  989. for ($x=0; $x<$width; ++$x) {
  990. if ($bitMask[$y][$x] == 1) {
  991. $d[$y][$x] = chr(ord($s[$y][$x]) ^ (int)$bitMask[$y][$x]);
  992. }
  993. $b += (int)(ord($d[$y][$x]) & 1);
  994. }
  995. }
  996. return $b;
  997. }
  998. /**
  999. * makeMask
  1000. * @param int $width
  1001. * @param array $frame
  1002. * @param int $maskNo
  1003. * @param int $level
  1004. * @return array mask
  1005. */
  1006. protected function makeMask($width, $frame, $maskNo, $level) {
  1007. $masked = array_fill(0, $width, str_repeat("\0", $width));
  1008. $this->makeMaskNo($maskNo, $width, $frame, $masked);
  1009. $this->writeFormatInformation($width, $masked, $maskNo, $level);
  1010. return $masked;
  1011. }
  1012. /**
  1013. * calcN1N3
  1014. * @param int $length
  1015. * @return int demerit
  1016. */
  1017. protected function calcN1N3($length) {
  1018. $demerit = 0;
  1019. for ($i=0; $i<$length; ++$i) {
  1020. if ($this->runLength[$i] >= 5) {
  1021. $demerit += (N1 + ($this->runLength[$i] - 5));
  1022. }
  1023. if ($i & 1) {
  1024. if (($i >= 3) AND ($i < ($length-2)) AND ($this->runLength[$i] % 3 == 0)) {
  1025. $fact = (int)($this->runLength[$i] / 3);
  1026. if (($this->runLength[$i-2] == $fact)
  1027. AND ($this->runLength[$i-1] == $fact)
  1028. AND ($this->runLength[$i+1] == $fact)
  1029. AND ($this->runLength[$i+2] == $fact)) {
  1030. if (($this->runLength[$i-3] < 0) OR ($this->runLength[$i-3] >= (4 * $fact))) {
  1031. $demerit += N3;
  1032. } elseif ((($i+3) >= $length) OR ($this->runLength[$i+3] >= (4 * $fact))) {
  1033. $demerit += N3;
  1034. }
  1035. }
  1036. }
  1037. }
  1038. }
  1039. return $demerit;
  1040. }
  1041. /**
  1042. * evaluateSymbol
  1043. * @param int $width
  1044. * @param array $frame
  1045. * @return int demerit
  1046. */
  1047. protected function evaluateSymbol($width, $frame) {
  1048. $head = 0;
  1049. $demerit = 0;
  1050. for ($y=0; $y<$width; ++$y) {
  1051. $head = 0;
  1052. $this->runLength[0] = 1;
  1053. $frameY = $frame[$y];
  1054. if ($y > 0) {
  1055. $frameYM = $frame[$y-1];
  1056. }
  1057. for ($x=0; $x<$width; ++$x) {
  1058. if (($x > 0) AND ($y > 0)) {
  1059. $b22 = ord($frameY[$x]) & ord($frameY[$x-1]) & ord($frameYM[$x]) & ord($frameYM[$x-1]);
  1060. $w22 = ord($frameY[$x]) | ord($frameY[$x-1]) | ord($frameYM[$x]) | ord($frameYM[$x-1]);
  1061. if (($b22 | ($w22 ^ 1)) & 1) {
  1062. $demerit += N2;
  1063. }
  1064. }
  1065. if (($x == 0) AND (ord($frameY[$x]) & 1)) {
  1066. $this->runLength[0] = -1;
  1067. $head = 1;
  1068. $this->runLength[$head] = 1;
  1069. } elseif ($x > 0) {
  1070. if ((ord($frameY[$x]) ^ ord($frameY[$x-1])) & 1) {
  1071. $head++;
  1072. $this->runLength[$head] = 1;
  1073. } else {
  1074. $this->runLength[$head]++;
  1075. }
  1076. }
  1077. }
  1078. $demerit += $this->calcN1N3($head+1);
  1079. }
  1080. for ($x=0; $x<$width; ++$x) {
  1081. $head = 0;
  1082. $this->runLength[0] = 1;
  1083. for ($y=0; $y<$width; ++$y) {
  1084. if (($y == 0) AND (ord($frame[$y][$x]) & 1)) {
  1085. $this->runLength[0] = -1;
  1086. $head = 1;
  1087. $this->runLength[$head] = 1;
  1088. } elseif ($y > 0) {
  1089. if ((ord($frame[$y][$x]) ^ ord($frame[$y-1][$x])) & 1) {
  1090. $head++;
  1091. $this->runLength[$head] = 1;
  1092. } else {
  1093. $this->runLength[$head]++;
  1094. }
  1095. }
  1096. }
  1097. $demerit += $this->calcN1N3($head+1);
  1098. }
  1099. return $demerit;
  1100. }
  1101. /**
  1102. * mask
  1103. * @param int $width
  1104. * @param array $frame
  1105. * @param int $level
  1106. * @return array best mask
  1107. */
  1108. protected function mask($width, $frame, $level) {
  1109. $minDemerit = PHP_INT_MAX;
  1110. $bestMaskNum = 0;
  1111. $bestMask = array();
  1112. $checked_masks = array(0, 1, 2, 3, 4, 5, 6, 7);
  1113. if (QR_FIND_FROM_RANDOM !== false) {
  1114. $howManuOut = 8 - (QR_FIND_FROM_RANDOM % 9);
  1115. for ($i = 0; $i < $howManuOut; ++$i) {
  1116. $remPos = rand (0, count($checked_masks)-1);
  1117. unset($checked_masks[$remPos]);
  1118. $checked_masks = array_values($checked_masks);
  1119. }
  1120. }
  1121. $bestMask = $frame;
  1122. foreach ($checked_masks as $i) {
  1123. $mask = array_fill(0, $width, str_repeat("\0", $width));
  1124. $demerit = 0;
  1125. $blacks = 0;
  1126. $blacks = $this->makeMaskNo($i, $width, $frame, $mask);
  1127. $blacks += $this->writeFormatInformation($width, $mask, $i, $level);
  1128. $blacks = (int)(100 * $blacks / ($width * $width));
  1129. $demerit = (int)((int)(abs($blacks - 50) / 5) * N4);
  1130. $demerit += $this->evaluateSymbol($width, $mask);
  1131. if ($demerit < $minDemerit) {
  1132. $minDemerit = $demerit;
  1133. $bestMask = $mask;
  1134. $bestMaskNum = $i;
  1135. }
  1136. }
  1137. return $bestMask;
  1138. }
  1139. // - - - - - - - - - - - - - - - - - - - - - - - - -
  1140. // QRsplit
  1141. /**
  1142. * Return true if the character at specified position is a number
  1143. * @param string $str string
  1144. * @param int $pos characted position
  1145. * @return boolean true of false
  1146. */
  1147. protected function isdigitat($str, $pos) {
  1148. if ($pos >= strlen($str)) {
  1149. return false;
  1150. }
  1151. return ((ord($str[$pos]) >= ord('0'))&&(ord($str[$pos]) <= ord('9')));
  1152. }
  1153. /**
  1154. * Return true if the character at specified position is an alphanumeric character
  1155. * @param string $str string
  1156. * @param int $pos characted position
  1157. * @return boolean true of false
  1158. */
  1159. protected function isalnumat($str, $pos) {
  1160. if ($pos >= strlen($str)) {
  1161. return false;
  1162. }
  1163. return ($this->lookAnTable(ord($str[$pos])) >= 0);
  1164. }
  1165. /**
  1166. * identifyMode
  1167. * @param int $pos
  1168. * @return int mode
  1169. */
  1170. protected function identifyMode($pos) {
  1171. if ($pos >= strlen($this->dataStr)) {
  1172. return QR_MODE_NL;
  1173. }
  1174. $c = $this->dataStr[$pos];
  1175. if ($this->isdigitat($this->dataStr, $pos)) {
  1176. return QR_MODE_NM;
  1177. } elseif ($this->isalnumat($this->dataStr, $pos)) {
  1178. return QR_MODE_AN;
  1179. } elseif ($this->hint == QR_MODE_KJ) {
  1180. if ($pos+1 < strlen($this->dataStr)) {
  1181. $d = $this->dataStr[$pos+1];
  1182. $word = (ord($c) << 8) | ord($d);
  1183. if (($word >= 0x8140 && $word <= 0x9ffc) OR ($word >= 0xe040 && $word <= 0xebbf)) {
  1184. return QR_MODE_KJ;
  1185. }
  1186. }
  1187. }
  1188. return QR_MODE_8B;
  1189. }
  1190. /**
  1191. * eatNum
  1192. * @return int run
  1193. */
  1194. protected function eatNum() {
  1195. $ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
  1196. $p = 0;
  1197. while($this->isdigitat($this->dataStr, $p)) {
  1198. $p++;
  1199. }
  1200. $run = $p;
  1201. $mode = $this->identifyMode($p);
  1202. if ($mode == QR_MODE_8B) {
  1203. $dif = $this->estimateBitsModeNum($run) + 4 + $ln
  1204. + $this->estimateBitsMode8(1) // + 4 + l8
  1205. - $this->estimateBitsMode8($run + 1); // - 4 - l8
  1206. if ($dif > 0) {
  1207. return $this->eat8();
  1208. }
  1209. }
  1210. if ($mode == QR_MODE_AN) {
  1211. $dif = $this->estimateBitsModeNum($run) + 4 + $ln
  1212. + $this->estimateBitsModeAn(1) // + 4 + la
  1213. - $this->estimateBitsModeAn($run + 1);// - 4 - la
  1214. if ($dif > 0) {
  1215. return $this->eatAn();
  1216. }
  1217. }
  1218. $this->items = $this->appendNewInputItem($this->items, QR_MODE_NM, $run, str_split($this->dataStr));
  1219. return $run;
  1220. }
  1221. /**
  1222. * eatAn
  1223. * @return int run
  1224. */
  1225. protected function eatAn() {
  1226. $la = $this->lengthIndicator(QR_MODE_AN, $this->version);
  1227. $ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
  1228. $p = 0;
  1229. while($this->isalnumat($this->dataStr, $p)) {
  1230. if ($this->isdigitat($this->dataStr, $p)) {
  1231. $q = $p;
  1232. while($this->isdigitat($this->dataStr, $q)) {
  1233. $q++;
  1234. }
  1235. $dif = $this->estimateBitsModeAn($p) // + 4 + la
  1236. + $this->estimateBitsModeNum($q - $p) + 4 + $ln
  1237. - $this->estimateBitsModeAn($q); // - 4 - la
  1238. if ($dif < 0) {
  1239. break;
  1240. } else {
  1241. $p = $q;
  1242. }
  1243. } else {
  1244. $p++;
  1245. }
  1246. }
  1247. $run = $p;
  1248. if (!$this->isalnumat($this->dataStr, $p)) {
  1249. $dif = $this->estimateBitsModeAn($run) + 4 + $la
  1250. + $this->estimateBitsMode8(1) // + 4 + l8
  1251. - $this->estimateBitsMode8($run + 1); // - 4 - l8
  1252. if ($dif > 0) {
  1253. return $this->eat8();
  1254. }
  1255. }
  1256. $this->items = $this->appendNewInputItem($this->items, QR_MODE_AN, $run, str_split($this->dataStr));
  1257. return $run;
  1258. }
  1259. /**
  1260. * eatKanji
  1261. * @return int run
  1262. */
  1263. protected function eatKanji() {
  1264. $p = 0;
  1265. while($this->identifyMode($p) == QR_MODE_KJ) {
  1266. $p += 2;
  1267. }
  1268. $this->items = $this->appendNewInputItem($this->items, QR_MODE_KJ, $p, str_split($this->dataStr));
  1269. return $run;
  1270. }
  1271. /**
  1272. * eat8
  1273. * @return int run
  1274. */
  1275. protected function eat8() {
  1276. $la = $this->lengthIndicator(QR_MODE_AN, $this->version);
  1277. $ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
  1278. $p = 1;
  1279. $dataStrLen = strlen($this->dataStr);
  1280. while($p < $dataStrLen) {
  1281. $mode = $this->identifyMode($p);
  1282. if ($mode == QR_MODE_KJ) {
  1283. break;
  1284. }
  1285. if ($mode == QR_MODE_NM) {
  1286. $q = $p;
  1287. while($this->isdigitat($this->dataStr, $q)) {
  1288. $q++;
  1289. }
  1290. $dif = $this->estimateBitsMode8($p) // + 4 + l8
  1291. + $this->estimateBitsModeNum($q - $p) + 4 + $ln
  1292. - $this->estimateBitsMode8($q); // - 4 - l8
  1293. if ($dif < 0) {
  1294. break;
  1295. } else {
  1296. $p = $q;
  1297. }
  1298. } elseif ($mode == QR_MODE_AN) {
  1299. $q = $p;
  1300. while($this->isalnumat($this->dataStr, $q)) {
  1301. $q++;
  1302. }
  1303. $dif = $this->estimateBitsMode8($p) // + 4 + l8
  1304. + $this->estimateBitsModeAn($q - $p) + 4 + $la
  1305. - $this->estimateBitsMode8($q); // - 4 - l8
  1306. if ($dif < 0) {
  1307. break;
  1308. } else {
  1309. $p = $q;
  1310. }
  1311. } else {
  1312. $p++;
  1313. }
  1314. }
  1315. $run = $p;
  1316. $this->items = $this->appendNewInputItem($this->items, QR_MODE_8B, $run, str_split($this->dataStr));
  1317. return $run;
  1318. }
  1319. /**
  1320. * splitString
  1321. */
  1322. protected function splitString() {
  1323. while (strlen($this->dataStr) > 0) {
  1324. if ($this->dataStr == '') {
  1325. return 0;
  1326. }
  1327. $mode = $this->identifyMode(0);
  1328. switch ($mode) {
  1329. case QR_MODE_NM: {
  1330. $length = $this->eatNum();
  1331. break;
  1332. }
  1333. case QR_MODE_AN: {
  1334. $length = $this->eatAn();
  1335. break;
  1336. }
  1337. case QR_MODE_KJ: {
  1338. if ($hint == QR_MODE_KJ) {
  1339. $length = $this->eatKanji();
  1340. } else {
  1341. $length = $this->eat8();
  1342. }
  1343. break;
  1344. }
  1345. default: {
  1346. $length = $this->eat8();
  1347. break;
  1348. }
  1349. }
  1350. if ($length == 0) {
  1351. return 0;
  1352. }
  1353. if ($length < 0) {
  1354. return -1;
  1355. }
  1356. $this->dataStr = substr($this->dataStr, $length);
  1357. }
  1358. }
  1359. /**
  1360. * toUpper
  1361. */
  1362. protected function toUpper() {
  1363. $stringLen = strlen($this->dataStr);
  1364. $p = 0;
  1365. while ($p < $stringLen) {
  1366. $mode = $this->identifyMode(substr($this->dataStr, $p), $this->hint);
  1367. if ($mode == QR_MODE_KJ) {
  1368. $p += 2;
  1369. } else {
  1370. if ((ord($this->dataStr[$p]) >= ord('a')) AND (ord($this->dataStr[$p]) <= ord('z'))) {
  1371. $this->dataStr[$p] = chr(ord($this->dataStr[$p]) - 32);
  1372. }
  1373. $p++;
  1374. }
  1375. }
  1376. return $this->dataStr;
  1377. }
  1378. // - - - - - - - - - - - - - - - - - - - - - - - - -
  1379. // QRinputItem
  1380. /**
  1381. * newInputItem
  1382. * @param int $mode
  1383. * @param int $size
  1384. * @param array $data
  1385. * @param array $bstream
  1386. * @return array input item
  1387. */
  1388. protected function newInputItem($mode, $size, $data, $bstream=null) {
  1389. $setData = array_slice($data, 0, $size);
  1390. if (count($setData) < $size) {
  1391. $setData = array_merge($setData, array_fill(0, ($size - count($setData)), 0));
  1392. }
  1393. if (!$this->check($mode, $size, $setData)) {
  1394. return NULL;
  1395. }
  1396. $inputitem = array();
  1397. $inputitem['mode'] = $mode;
  1398. $inputitem['size'] = $size;
  1399. $inputitem['data'] = $setData;
  1400. $inputitem['bstream'] = $bstream;
  1401. return $inputitem;
  1402. }
  1403. /**
  1404. * encodeModeNum
  1405. * @param array $inputitem
  1406. * @param int $version
  1407. * @return array input item
  1408. */
  1409. protected function encodeModeNum($inputitem, $version) {
  1410. $words = (int)($inputitem['size'] / 3);
  1411. $inputitem['bstream'] = array();
  1412. $val = 0x1;
  1413. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val);
  1414. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_NM, $version), $inputitem['size']);
  1415. for ($i=0; $i < $words; ++$i) {
  1416. $val = (ord($inputitem['data'][$i*3 ]) - ord('0')) * 100;
  1417. $val += (ord($inputitem['data'][$i*3+1]) - ord('0')) * 10;
  1418. $val += (ord($inputitem['data'][$i*3+2]) - ord('0'));
  1419. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 10, $val);
  1420. }
  1421. if ($inputitem['size'] - $words * 3 == 1) {
  1422. $val = ord($inputitem['data'][$words*3]) - ord('0');
  1423. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val);
  1424. } elseif (($inputitem['size'] - ($words * 3)) == 2) {
  1425. $val = (ord($inputitem['data'][$words*3 ]) - ord('0')) * 10;
  1426. $val += (ord($inputitem['data'][$words*3+1]) - ord('0'));
  1427. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 7, $val);
  1428. }
  1429. return $inputitem;
  1430. }
  1431. /**
  1432. * encodeModeAn
  1433. * @param array $inputitem
  1434. * @param int $version
  1435. * @return array input item
  1436. */
  1437. protected function encodeModeAn($inputitem, $version) {
  1438. $words = (int)($inputitem['size'] / 2);
  1439. $inputitem['bstream'] = array();
  1440. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x02);
  1441. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_AN, $version), $inputitem['size']);
  1442. for ($i=0; $i < $words; ++$i) {
  1443. $val = (int)$this->lookAnTable(ord($inputitem['data'][$i*2 ])) * 45;
  1444. $val += (int)$this->lookAnTable(ord($inputitem['data'][$i*2+1]));
  1445. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 11, $val);
  1446. }
  1447. if ($inputitem['size'] & 1) {
  1448. $val = $this->lookAnTable(ord($inputitem['data'][($words * 2)]));
  1449. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 6, $val);
  1450. }
  1451. return $inputitem;
  1452. }
  1453. /**
  1454. * encodeMode8
  1455. * @param array $inputitem
  1456. * @param int $version
  1457. * @return array input item
  1458. */
  1459. protected function encodeMode8($inputitem, $version) {
  1460. $inputitem['bstream'] = array();
  1461. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x4);
  1462. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_8B, $version), $inputitem['size']);
  1463. for ($i=0; $i < $inputitem['size']; ++$i) {
  1464. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, ord($inputitem['data'][$i]));
  1465. }
  1466. return $inputitem;
  1467. }
  1468. /**
  1469. * encodeModeKanji
  1470. * @param array $inputitem
  1471. * @param int $version
  1472. * @return array input item
  1473. */
  1474. protected function encodeModeKanji($inputitem, $version) {
  1475. $inputitem['bstream'] = array();
  1476. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x8);
  1477. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_KJ, $version), (int)($inputitem['size'] / 2));
  1478. for ($i=0; $i<$inputitem['size']; $i+=2) {
  1479. $val = (ord($inputitem['data'][$i]) << 8) | ord($inputitem['data'][$i+1]);
  1480. if ($val <= 0x9ffc) {
  1481. $val -= 0x8140;
  1482. } else {
  1483. $val -= 0xc140;
  1484. }
  1485. $h = ($val >> 8) * 0xc0;
  1486. $val = ($val & 0xff) + $h;
  1487. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 13, $val);
  1488. }
  1489. return $inputitem;
  1490. }
  1491. /**
  1492. * encodeModeStructure
  1493. * @param array $inputitem
  1494. * @return array input item
  1495. */
  1496. protected function encodeModeStructure($inputitem) {
  1497. $inputitem['bstream'] = array();
  1498. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x03);
  1499. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, ord($inputitem['data'][1]) - 1);
  1500. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, ord($inputitem['data'][0]) - 1);
  1501. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, ord($inputitem['data'][2]));
  1502. return $inputitem;
  1503. }
  1504. /**
  1505. * encodeBitStream
  1506. * @param array $inputitem
  1507. * @param int $version
  1508. * @return array input item
  1509. */
  1510. protected function encodeBitStream($inputitem, $version) {
  1511. $inputitem['bstream'] = array();
  1512. $words = $this->maximumWords($inputitem['mode'], $version);
  1513. if ($inputitem['size'] > $words) {
  1514. $st1 = $this->newInputItem($inputitem['mode'], $words, $inputitem['data']);
  1515. $st2 = $this->newInputItem($inputitem['mode'], $inputitem['size'] - $words, array_slice($inputitem['data'], $words));
  1516. $st1 = $this->encodeBitStream($st1, $version);
  1517. $st2 = $this->encodeBitStream($st2, $version);
  1518. $inputitem['bstream'] = array();
  1519. $inputitem['bstream'] = $this->appendBitstream($inputitem['bstream'], $st1['bstream']);
  1520. $inputitem['bstream'] = $this->appendBitstream($inputitem['bstream'], $st2['bstream']);
  1521. } else {
  1522. switch($inputitem['mode']) {
  1523. case QR_MODE_NM: {
  1524. $inputitem = $this->encodeModeNum($inputitem, $version);
  1525. break;
  1526. }
  1527. case QR_MODE_AN: {
  1528. $inputitem = $this->encodeModeAn($inputitem, $version);
  1529. break;
  1530. }
  1531. case QR_MODE_8B: {
  1532. $inputitem = $this->encodeMode8($inputitem, $version);
  1533. break;
  1534. }
  1535. case QR_MODE_KJ: {
  1536. $inputitem = $this->encodeModeKanji($inputitem, $version);
  1537. break;
  1538. }
  1539. case QR_MODE_ST: {
  1540. $inputitem = $this->encodeModeStructure($inputitem);
  1541. break;
  1542. }
  1543. default: {
  1544. break;
  1545. }
  1546. }
  1547. }
  1548. return $inputitem;
  1549. }
  1550. // - - - - - - - - - - - - - - - - - - - - - - - - -
  1551. // QRinput
  1552. /**
  1553. * Append data to an input object.
  1554. * The data is copied and appended to the input object.
  1555. * @param array items input items
  1556. * @param int $mode encoding mode.
  1557. * @param int $size size of data (byte).
  1558. * @param array $data array of input data.
  1559. * @return items
  1560. *
  1561. */
  1562. protected function appendNewInputItem($items, $mode, $size, $data) {
  1563. $items[] = $this->newInputItem($mode, $size, $data);
  1564. return $items;
  1565. }
  1566. /**
  1567. * insertStructuredAppendHeader
  1568. * @param array $items
  1569. * @param int $size
  1570. * @param int $index
  1571. * @param int $parity
  1572. * @return array items
  1573. */
  1574. protected function insertStructuredAppendHeader($items, $size, $index, $parity) {
  1575. if ($size > MAX_STRUCTURED_SYMBOLS) {
  1576. return -1;
  1577. }
  1578. if (($index <= 0) OR ($index > MAX_STRUCTURED_SYMBOLS)) {
  1579. return -1;
  1580. }
  1581. $buf = array($size, $index, $parity);
  1582. $entry = $this->newInputItem(QR_MODE_ST, 3, buf);
  1583. array_unshift($items, $entry);
  1584. return $items;
  1585. }
  1586. /**
  1587. * calcParity
  1588. * @param array $items
  1589. * @return int parity
  1590. */
  1591. protected function calcParity($items) {
  1592. $parity = 0;
  1593. foreach ($items as $item) {
  1594. if ($item['mode'] != QR_MODE_ST) {
  1595. for ($i=$item['size']-1; $i>=0; --$i) {
  1596. $parity ^= $item['data'][$i];
  1597. }
  1598. }
  1599. }
  1600. return $parity;
  1601. }
  1602. /**
  1603. * checkModeNum
  1604. * @param int $size
  1605. * @param array $data
  1606. * @return boolean true or false
  1607. */
  1608. protected function checkModeNum($size, $data) {
  1609. for ($i=0; $i<$size; ++$i) {
  1610. if ((ord($data[$i]) < ord('0')) OR (ord($data[$i]) > ord('9'))){
  1611. return false;
  1612. }
  1613. }
  1614. return true;
  1615. }
  1616. /**
  1617. * estimateBitsModeNum
  1618. * @param int $size
  1619. * @return int number of bits
  1620. */
  1621. protected function estimateBitsModeNum($size) {
  1622. $w = (int)$size / 3;
  1623. $bits = $w * 10;
  1624. switch($size - $w * 3) {
  1625. case 1: {
  1626. $bits += 4;
  1627. break;
  1628. }
  1629. case 2: {
  1630. $bits += 7;
  1631. break;
  1632. }
  1633. default: {
  1634. break;
  1635. }
  1636. }
  1637. return $bits;
  1638. }
  1639. /**
  1640. * Look up the alphabet-numeric convesion table (see JIS X0510:2004, pp.19).
  1641. * @param int $c character value
  1642. * @return value
  1643. */
  1644. protected function lookAnTable($c) {
  1645. return (($c > 127)?-1:$this->anTable[$c]);
  1646. }
  1647. /**
  1648. * checkModeAn
  1649. * @param int $size
  1650. * @param array $data
  1651. * @return boolean true or false
  1652. */
  1653. protected function checkModeAn($size, $data) {
  1654. for ($i=0; $i<$size; ++$i) {
  1655. if ($this->lookAnTable(ord($data[$i])) == -1) {
  1656. return false;
  1657. }
  1658. }
  1659. return true;
  1660. }
  1661. /**
  1662. * estimateBitsModeAn
  1663. * @param int $size
  1664. * @return int number of bits
  1665. */
  1666. protected function estimateBitsModeAn($size) {
  1667. $w = (int)($size / 2);
  1668. $bits = $w * 11;
  1669. if ($size & 1) {
  1670. $bits += 6;
  1671. }
  1672. return $bits;
  1673. }
  1674. /**
  1675. * estimateBitsMode8
  1676. * @param int $size
  1677. * @return int number of bits
  1678. */
  1679. protected function estimateBitsMode8($size) {
  1680. return $size * 8;
  1681. }
  1682. /**
  1683. * estimateBitsModeKanji
  1684. * @param int $size
  1685. * @return int number of bits
  1686. */
  1687. protected function estimateBitsModeKanji($size) {
  1688. return (int)(($size / 2) * 13);
  1689. }
  1690. /**
  1691. * checkModeKanji
  1692. * @param int $size
  1693. * @param array $data
  1694. * @return boolean true or false
  1695. */
  1696. protected function checkModeKanji($size, $data) {
  1697. if ($size & 1) {
  1698. return false;
  1699. }
  1700. for ($i=0; $i<$size; $i+=2) {
  1701. $val = (ord($data[$i]) << 8) | ord($data[$i+1]);
  1702. if (($val < 0x8140) OR (($val > 0x9ffc) AND ($val < 0xe040)) OR ($val > 0xebbf)) {
  1703. return false;
  1704. }
  1705. }
  1706. return true;
  1707. }
  1708. /**
  1709. * Validate the input data.
  1710. * @param int $mode encoding mode.
  1711. * @param int $size size of data (byte).
  1712. * @param array data data to validate
  1713. * @return boolean true in case of valid data, false otherwise
  1714. */
  1715. protected function check($mode, $size, $data) {
  1716. if ($size <= 0) {
  1717. return false;
  1718. }
  1719. switch($mode) {
  1720. case QR_MODE_NM: {
  1721. return $this->checkModeNum($size, $data);
  1722. }
  1723. case QR_MODE_AN: {
  1724. return $this->checkModeAn($size, $data);
  1725. }
  1726. case QR_MODE_KJ: {
  1727. return $this->checkModeKanji($size, $data);
  1728. }
  1729. case QR_MODE_8B: {
  1730. return true;
  1731. }
  1732. case QR_MODE_ST: {
  1733. return true;
  1734. }
  1735. default: {
  1736. break;
  1737. }
  1738. }
  1739. return false;
  1740. }
  1741. /**
  1742. * estimateBitStreamSize
  1743. * @param array $items
  1744. * @param int $version
  1745. * @return int bits
  1746. */
  1747. protected function estimateBitStreamSize($items, $version) {
  1748. $bits = 0;
  1749. if ($version == 0) {
  1750. $version = 1;
  1751. }
  1752. foreach ($items as $item) {
  1753. switch($item['mode']) {
  1754. case QR_MODE_NM: {
  1755. $bits = $this->estimateBitsModeNum($item['size']);
  1756. break;
  1757. }
  1758. case QR_MODE_AN: {
  1759. $bits = $this->estimateBitsModeAn($item['size']);
  1760. break;
  1761. }
  1762. case QR_MODE_8B: {
  1763. $bits = $this->estimateBitsMode8($item['size']);
  1764. break;
  1765. }
  1766. case QR_MODE_KJ: {
  1767. $bits = $this->estimateBitsModeKanji($item['size']);
  1768. break;
  1769. }
  1770. case QR_MODE_ST: {
  1771. return STRUCTURE_HEADER_BITS;
  1772. }
  1773. default: {
  1774. return 0;
  1775. }
  1776. }
  1777. $l = $this->lengthIndicator($item['mode'], $version);
  1778. $m = 1 << $l;
  1779. $num = (int)(($item['size'] + $m - 1) / $m);
  1780. $bits += $num * (4 + $l);
  1781. }
  1782. return $bits;
  1783. }
  1784. /**
  1785. * estimateVersion
  1786. * @param array $items
  1787. * @return int version
  1788. */
  1789. protected function estimateVersion($items) {
  1790. $version = 0;
  1791. $prev = 0;
  1792. do {
  1793. $prev = $version;
  1794. $bits = $this->estimateBitStreamSize($items, $prev);
  1795. $version = $this->getMinimumVersion((int)(($bits + 7) / 8), $this->level);
  1796. if ($version < 0) {
  1797. return -1;
  1798. }
  1799. } while ($version > $prev);
  1800. return $version;
  1801. }
  1802. /**
  1803. * lengthOfCode
  1804. * @param int $mode
  1805. * @param int $version
  1806. * @param int $bits
  1807. * @return int size
  1808. */
  1809. protected function lengthOfCode($mode, $version, $bits) {
  1810. $payload = $bits - 4 - $this->lengthIndicator($mode, $version);
  1811. switch($mode) {
  1812. case QR_MODE_NM: {
  1813. $chunks = (int)($payload / 10);
  1814. $remain = $payload - $chunks * 10;
  1815. $size = $chunks * 3;
  1816. if ($remain >= 7) {
  1817. $size += 2;
  1818. } elseif ($remain >= 4) {
  1819. $size += 1;
  1820. }
  1821. break;
  1822. }
  1823. case QR_MODE_AN: {
  1824. $chunks = (int)($payload / 11);
  1825. $remain = $payload - $chunks * 11;
  1826. $size = $chunks * 2;
  1827. if ($remain >= 6) {
  1828. ++$size;
  1829. }
  1830. break;
  1831. }
  1832. case QR_MODE_8B: {
  1833. $size = (int)($payload / 8);
  1834. break;
  1835. }
  1836. case QR_MODE_KJ: {
  1837. $size = (int)(($payload / 13) * 2);
  1838. break;
  1839. }
  1840. case QR_MODE_ST: {
  1841. $size = (int)($payload / 8);
  1842. break;
  1843. }
  1844. default: {
  1845. $size = 0;
  1846. break;
  1847. }
  1848. }
  1849. $maxsize = $this->maximumWords($mode, $version);
  1850. if ($size < 0) {
  1851. $size = 0;
  1852. }
  1853. if ($size > $maxsize) {
  1854. $size = $maxsize;
  1855. }
  1856. return $size;
  1857. }
  1858. /**
  1859. * createBitStream
  1860. * @param array $items
  1861. * @return array of items and total bits
  1862. */
  1863. protected function createBitStream($items) {
  1864. $total = 0;
  1865. foreach ($items as $key => $item) {
  1866. $items[$key] = $this->encodeBitStream($item, $this->version);
  1867. $bits = count($items[$key]['bstream']);
  1868. $total += $bits;
  1869. }
  1870. return array($items, $total);
  1871. }
  1872. /**
  1873. * convertData
  1874. * @param array $items
  1875. * @return array items
  1876. */
  1877. protected function convertData($items) {
  1878. $ver = $this->estimateVersion($items);
  1879. if ($ver > $this->version) {
  1880. $this->version = $ver;
  1881. }
  1882. for (;;) {
  1883. $cbs = $this->createBitStream($items);
  1884. $items = $cbs[0];
  1885. $bits = $cbs[1];
  1886. if ($bits < 0) {
  1887. return -1;
  1888. }
  1889. $ver = $this->getMinimumVersion((int)(($bits + 7) / 8), $this->level);
  1890. if ($ver < 0) {
  1891. return -1;
  1892. } elseif ($ver > $this->version) {
  1893. $this->version = $ver;
  1894. } else {
  1895. break;
  1896. }
  1897. }
  1898. return $items;
  1899. }
  1900. /**
  1901. * Append Padding Bit to bitstream
  1902. * @param array $bstream
  1903. * @return array bitstream
  1904. */
  1905. protected function appendPaddingBit($bstream) {
  1906. if (is_null($bstream)) {
  1907. return null;
  1908. }
  1909. $bits = count($bstream);
  1910. $maxwords = $this->getDataLength($this->version, $this->level);
  1911. $maxbits = $maxwords * 8;
  1912. if ($maxbits == $bits) {
  1913. return 0;
  1914. }
  1915. if ($maxbits - $bits < 5) {
  1916. return $this->appendNum($bstream, $maxbits - $bits, 0);
  1917. }
  1918. $bits += 4;
  1919. $words = (int)(($bits + 7) / 8);
  1920. $padding = array();
  1921. $padding = $this->appendNum($padding, $words * 8 - $bits + 4, 0);
  1922. $padlen = $maxwords - $words;
  1923. if ($padlen > 0) {
  1924. $padbuf = array();
  1925. for ($i=0; $i<$padlen; ++$i) {
  1926. $padbuf[$i] = ($i&1)?0x11:0xec;
  1927. }
  1928. $padding = $this->appendBytes($padding, $padlen, $padbuf);
  1929. }
  1930. return $this->appendBitstream($bstream, $padding);
  1931. }
  1932. /**
  1933. * mergeBitStream
  1934. * @param array $bstream
  1935. * @return array bitstream
  1936. */
  1937. protected function mergeBitStream($items) {
  1938. $items = $this->convertData($items);
  1939. if (!is_array($items)) {
  1940. return null;
  1941. }
  1942. $bstream = array();
  1943. foreach ($items as $item) {
  1944. $bstream = $this->appendBitstream($bstream, $item['bstream']);
  1945. }
  1946. return $bstream;
  1947. }
  1948. /**
  1949. * Returns a stream of bits.
  1950. * @param int $items
  1951. * @return array padded merged byte stream
  1952. */
  1953. protected function getBitStream($items) {
  1954. $bstream = $this->mergeBitStream($items);
  1955. return $this->appendPaddingBit($bstream);
  1956. }
  1957. /**
  1958. * Pack all bit streams padding bits into a byte array.
  1959. * @param int $items
  1960. * @return array padded merged byte stream
  1961. */
  1962. protected function getByteStream($items) {
  1963. $bstream = $this->getBitStream($items);
  1964. return $this->bitstreamToByte($bstream);
  1965. }
  1966. // - - - - - - - - - - - - - - - - - - - - - - - - -
  1967. // QRbitstream
  1968. /**
  1969. * Return an array with zeros
  1970. * @param int $setLength array size
  1971. * @return array
  1972. */
  1973. protected function allocate($setLength) {
  1974. return array_fill(0, $setLength, 0);
  1975. }
  1976. /**
  1977. * Return new bitstream from number
  1978. * @param int $bits number of bits
  1979. * @param int $num number
  1980. * @return array bitstream
  1981. */
  1982. protected function newFromNum($bits, $num) {
  1983. $bstream = $this->allocate($bits);
  1984. $mask = 1 << ($bits - 1);
  1985. for ($i=0; $i<$bits; ++$i) {
  1986. if ($num & $mask) {
  1987. $bstream[$i] = 1;
  1988. } else {
  1989. $bstream[$i] = 0;
  1990. }
  1991. $mask = $mask >> 1;
  1992. }
  1993. return $bstream;
  1994. }
  1995. /**
  1996. * Return new bitstream from bytes
  1997. * @param int $size size
  1998. * @param array $data bytes
  1999. * @return array bitstream
  2000. */
  2001. protected function newFromBytes($size, $data) {
  2002. $bstream = $this->allocate($size * 8);
  2003. $p=0;
  2004. for ($i=0; $i<$size; ++$i) {
  2005. $mask = 0x80;
  2006. for ($j=0; $j<8; ++$j) {
  2007. if ($data[$i] & $mask) {
  2008. $bstream[$p] = 1;
  2009. } else {
  2010. $bstream[$p] = 0;
  2011. }
  2012. $p++;
  2013. $mask = $mask >> 1;
  2014. }
  2015. }
  2016. return $bstream;
  2017. }
  2018. /**
  2019. * Append one bitstream to another
  2020. * @param array $bitstream original bitstream
  2021. * @param array $append bitstream to append
  2022. * @return array bitstream
  2023. */
  2024. protected function appendBitstream($bitstream, $append) {
  2025. if ((!is_array($append)) OR (count($append) == 0)) {
  2026. return $bitstream;
  2027. }
  2028. if (count($bitstream) == 0) {
  2029. return $append;
  2030. }
  2031. return array_values(array_merge($bitstream, $append));
  2032. }
  2033. /**
  2034. * Append one bitstream created from number to another
  2035. * @param array $bitstream original bitstream
  2036. * @param int $bits number of bits
  2037. * @param int $num number
  2038. * @return array bitstream
  2039. */
  2040. protected function appendNum($bitstream, $bits, $num) {
  2041. if ($bits == 0) {
  2042. return 0;
  2043. }
  2044. $b = $this->newFromNum($bits, $num);
  2045. return $this->appendBitstream($bitstream, $b);
  2046. }
  2047. /**
  2048. * Append one bitstream created from bytes to another
  2049. * @param array $bitstream original bitstream
  2050. * @param int $size size
  2051. * @param array $data bytes
  2052. * @return array bitstream
  2053. */
  2054. protected function appendBytes($bitstream, $size, $data) {
  2055. if ($size == 0) {
  2056. return 0;
  2057. }
  2058. $b = $this->newFromBytes($size, $data);
  2059. return $this->appendBitstream($bitstream, $b);
  2060. }
  2061. /**
  2062. * Convert bitstream to bytes
  2063. * @param array $bitstream original bitstream
  2064. * @return array of bytes
  2065. */
  2066. protected function bitstreamToByte($bstream) {
  2067. if (is_null($bstream)) {
  2068. return null;
  2069. }
  2070. $size = count($bstream);
  2071. if ($size == 0) {
  2072. return array();
  2073. }
  2074. $data = array_fill(0, (int)(($size + 7) / 8), 0);
  2075. $bytes = (int)($size / 8);
  2076. $p = 0;
  2077. for ($i=0; $i<$bytes; $i++) {
  2078. $v = 0;
  2079. for ($j=0; $j<8; $j++) {
  2080. $v = $v << 1;
  2081. $v |= $bstream[$p];
  2082. $p++;
  2083. }
  2084. $data[$i] = $v;
  2085. }
  2086. if ($size & 7) {
  2087. $v = 0;
  2088. for ($j=0; $j<($size & 7); $j++) {
  2089. $v = $v << 1;
  2090. $v |= $bstream[$p];
  2091. $p++;
  2092. }
  2093. $data[$bytes] = $v;
  2094. }
  2095. return $data;
  2096. }
  2097. // - - - - - - - - - - - - - - - - - - - - - - - - -
  2098. // QRspec
  2099. /**
  2100. * Replace a value on the array at the specified position
  2101. * @param array $srctab
  2102. * @param int $x X position
  2103. * @param int $y Y position
  2104. * @param string $repl value to replace
  2105. * @param int $replLen length of the repl string
  2106. * @return array srctab
  2107. */
  2108. protected function qrstrset($srctab, $x, $y, $repl, $replLen=false) {
  2109. $srctab[$y] = substr_replace($srctab[$y], ($replLen !== false)?substr($repl,0,$replLen):$repl, $x, ($replLen !== false)?$replLen:strlen($repl));
  2110. return $srctab;
  2111. }
  2112. /**
  2113. * Return maximum data code length (bytes) for the version.
  2114. * @param int $version version
  2115. * @param int $level error correction level
  2116. * @return int maximum size (bytes)
  2117. */
  2118. protected function getDataLength($version, $level) {
  2119. return $this->capacity[$version][QRCAP_WORDS] - $this->capacity[$version][QRCAP_EC][$level];
  2120. }
  2121. /**
  2122. * Return maximum error correction code length (bytes) for the version.
  2123. * @param int $version version
  2124. * @param int $level error correction level
  2125. * @return int ECC size (bytes)
  2126. */
  2127. protected function getECCLength($version, $level){
  2128. return $this->capacity[$version][QRCAP_EC][$level];
  2129. }
  2130. /**
  2131. * Return the width of the symbol for the version.
  2132. * @param int $version version
  2133. * @return int width
  2134. */
  2135. protected function getWidth($version) {
  2136. return $this->capacity[$version][QRCAP_WIDTH];
  2137. }
  2138. /**
  2139. * Return the numer of remainder bits.
  2140. * @param int $version version
  2141. * @return int number of remainder bits
  2142. */
  2143. protected function getRemainder($version) {
  2144. return $this->capacity[$version][QRCAP_REMINDER];
  2145. }
  2146. /**
  2147. * Return a version number that satisfies the input code length.
  2148. * @param int $size input code length (byte)
  2149. * @param int $level error correction level
  2150. * @return int version number
  2151. */
  2152. protected function getMinimumVersion($size, $level) {
  2153. for ($i=1; $i <= QRSPEC_VERSION_MAX; ++$i) {
  2154. $words = $this->capacity[$i][QRCAP_WORDS] - $this->capacity[$i][QRCAP_EC][$level];
  2155. if ($words >= $size) {
  2156. return $i;
  2157. }
  2158. }
  2159. return -1;
  2160. }
  2161. /**
  2162. * Return the size of length indicator for the mode and version.
  2163. * @param int $mode encoding mode
  2164. * @param int $version version
  2165. * @return int the size of the appropriate length indicator (bits).
  2166. */
  2167. protected function lengthIndicator($mode, $version) {
  2168. if ($mode == QR_MODE_ST) {
  2169. return 0;
  2170. }
  2171. if ($version <= 9) {
  2172. $l = 0;
  2173. } elseif ($version <= 26) {
  2174. $l = 1;
  2175. } else {
  2176. $l = 2;
  2177. }
  2178. return $this->lengthTableBits[$mode][$l];
  2179. }
  2180. /**
  2181. * Return the maximum length for the mode and version.
  2182. * @param int $mode encoding mode
  2183. * @param int $version version
  2184. * @return int the maximum length (bytes)
  2185. */
  2186. protected function maximumWords($mode, $version) {
  2187. if ($mode == QR_MODE_ST) {
  2188. return 3;
  2189. }
  2190. if ($version <= 9) {
  2191. $l = 0;
  2192. } else if ($version <= 26) {
  2193. $l = 1;
  2194. } else {
  2195. $l = 2;
  2196. }
  2197. $bits = $this->lengthTableBits[$mode][$l];
  2198. $words = (1 << $bits) - 1;
  2199. if ($mode == QR_MODE_KJ) {
  2200. $words *= 2; // the number of bytes is required
  2201. }
  2202. return $words;
  2203. }
  2204. /**
  2205. * Return an array of ECC specification.
  2206. * @param int $version version
  2207. * @param int $level error correction level
  2208. * @param array $spec an array of ECC specification contains as following: {# of type1 blocks, # of data code, # of ecc code, # of type2 blocks, # of data code}
  2209. * @return array spec
  2210. */
  2211. protected function getEccSpec($version, $level, $spec) {
  2212. if (count($spec) < 5) {
  2213. $spec = array(0, 0, 0, 0, 0);
  2214. }
  2215. $b1 = $this->eccTable[$version][$level][0];
  2216. $b2 = $this->eccTable[$version][$level][1];
  2217. $data = $this->getDataLength($version, $level);
  2218. $ecc = $this->getECCLength($version, $level);
  2219. if ($b2 == 0) {
  2220. $spec[0] = $b1;
  2221. $spec[1] = (int)($data / $b1);
  2222. $spec[2] = (int)($ecc / $b1);
  2223. $spec[3] = 0;
  2224. $spec[4] = 0;
  2225. } else {
  2226. $spec[0] = $b1;
  2227. $spec[1] = (int)($data / ($b1 + $b2));
  2228. $spec[2] = (int)($ecc / ($b1 + $b2));
  2229. $spec[3] = $b2;
  2230. $spec[4] = $spec[1] + 1;
  2231. }
  2232. return $spec;
  2233. }
  2234. /**
  2235. * Put an alignment marker.
  2236. * @param array $frame frame
  2237. * @param int $width width
  2238. * @param int $ox X center coordinate of the pattern
  2239. * @param int $oy Y center coordinate of the pattern
  2240. * @return array frame
  2241. */
  2242. protected function putAlignmentMarker($frame, $ox, $oy) {
  2243. $finder = array(
  2244. "\xa1\xa1\xa1\xa1\xa1",
  2245. "\xa1\xa0\xa0\xa0\xa1",
  2246. "\xa1\xa0\xa1\xa0\xa1",
  2247. "\xa1\xa0\xa0\xa0\xa1",
  2248. "\xa1\xa1\xa1\xa1\xa1"
  2249. );
  2250. $yStart = $oy - 2;
  2251. $xStart = $ox - 2;
  2252. for ($y=0; $y < 5; $y++) {
  2253. $frame = $this->qrstrset($frame, $xStart, $yStart+$y, $finder[$y]);
  2254. }
  2255. return $frame;
  2256. }
  2257. /**
  2258. * Put an alignment pattern.
  2259. * @param int $version version
  2260. * @param array $fram frame
  2261. * @param int $width width
  2262. * @return array frame
  2263. */
  2264. protected function putAlignmentPattern($version, $frame, $width) {
  2265. if ($version < 2) {
  2266. return $frame;
  2267. }
  2268. $d = $this->alignmentPattern[$version][1] - $this->alignmentPattern[$version][0];
  2269. if ($d < 0) {
  2270. $w = 2;
  2271. } else {
  2272. $w = (int)(($width - $this->alignmentPattern[$version][0]) / $d + 2);
  2273. }
  2274. if ($w * $w - 3 == 1) {
  2275. $x = $this->alignmentPattern[$version][0];
  2276. $y = $this->alignmentPattern[$version][0];
  2277. $frame = $this->putAlignmentMarker($frame, $x, $y);
  2278. return $frame;
  2279. }
  2280. $cx = $this->alignmentPattern[$version][0];
  2281. $wo = $w - 1;
  2282. for ($x=1; $x < $wo; ++$x) {
  2283. $frame = $this->putAlignmentMarker($frame, 6, $cx);
  2284. $frame = $this->putAlignmentMarker($frame, $cx, 6);
  2285. $cx += $d;
  2286. }
  2287. $cy = $this->alignmentPattern[$version][0];
  2288. for ($y=0; $y < $wo; ++$y) {
  2289. $cx = $this->alignmentPattern[$version][0];
  2290. for ($x=0; $x < $wo; ++$x) {
  2291. $frame = $this->putAlignmentMarker($frame, $cx, $cy);
  2292. $cx += $d;
  2293. }
  2294. $cy += $d;
  2295. }
  2296. return $frame;
  2297. }
  2298. /**
  2299. * Return BCH encoded version information pattern that is used for the symbol of version 7 or greater. Use lower 18 bits.
  2300. * @param int $version version
  2301. * @return BCH encoded version information pattern
  2302. */
  2303. protected function getVersionPattern($version) {
  2304. if (($version < 7) OR ($version > QRSPEC_VERSION_MAX)) {
  2305. return 0;
  2306. }
  2307. return $this->versionPattern[($version - 7)];
  2308. }
  2309. /**
  2310. * Return BCH encoded format information pattern.
  2311. * @param array $mask
  2312. * @param int $level error correction level
  2313. * @return BCH encoded format information pattern
  2314. */
  2315. protected function getFormatInfo($mask, $level) {
  2316. if (($mask < 0) OR ($mask > 7)) {
  2317. return 0;
  2318. }
  2319. if (($level < 0) OR ($level > 3)) {
  2320. return 0;
  2321. }
  2322. return $this->formatInfo[$level][$mask];
  2323. }
  2324. /**
  2325. * Put a finder pattern.
  2326. * @param array $frame frame
  2327. * @param int $width width
  2328. * @param int $ox X center coordinate of the pattern
  2329. * @param int $oy Y center coordinate of the pattern
  2330. * @return array frame
  2331. */
  2332. protected function putFinderPattern($frame, $ox, $oy) {
  2333. $finder = array(
  2334. "\xc1\xc1\xc1\xc1\xc1\xc1\xc1",
  2335. "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
  2336. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  2337. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  2338. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  2339. "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
  2340. "\xc1\xc1\xc1\xc1\xc1\xc1\xc1"
  2341. );
  2342. for ($y=0; $y < 7; $y++) {
  2343. $frame = $this->qrstrset($frame, $ox, ($oy + $y), $finder[$y]);
  2344. }
  2345. return $frame;
  2346. }
  2347. /**
  2348. * Return a copy of initialized frame.
  2349. * @param int $version version
  2350. * @return Array of unsigned char.
  2351. */
  2352. protected function createFrame($version) {
  2353. $width = $this->capacity[$version][QRCAP_WIDTH];
  2354. $frameLine = str_repeat ("\0", $width);
  2355. $frame = array_fill(0, $width, $frameLine);
  2356. // Finder pattern
  2357. $frame = $this->putFinderPattern($frame, 0, 0);
  2358. $frame = $this->putFinderPattern($frame, $width - 7, 0);
  2359. $frame = $this->putFinderPattern($frame, 0, $width - 7);
  2360. // Separator
  2361. $yOffset = $width - 7;
  2362. for ($y=0; $y < 7; ++$y) {
  2363. $frame[$y][7] = "\xc0";
  2364. $frame[$y][$width - 8] = "\xc0";
  2365. $frame[$yOffset][7] = "\xc0";
  2366. ++$yOffset;
  2367. }
  2368. $setPattern = str_repeat("\xc0", 8);
  2369. $frame = $this->qrstrset($frame, 0, 7, $setPattern);
  2370. $frame = $this->qrstrset($frame, $width-8, 7, $setPattern);
  2371. $frame = $this->qrstrset($frame, 0, $width - 8, $setPattern);
  2372. // Format info
  2373. $setPattern = str_repeat("\x84", 9);
  2374. $frame = $this->qrstrset($frame, 0, 8, $setPattern);
  2375. $frame = $this->qrstrset($frame, $width - 8, 8, $setPattern, 8);
  2376. $yOffset = $width - 8;
  2377. for ($y=0; $y < 8; ++$y,++$yOffset) {
  2378. $frame[$y][8] = "\x84";
  2379. $frame[$yOffset][8] = "\x84";
  2380. }
  2381. // Timing pattern
  2382. $wo = $width - 15;
  2383. for ($i=1; $i < $wo; ++$i) {
  2384. $frame[6][7+$i] = chr(0x90 | ($i & 1));
  2385. $frame[7+$i][6] = chr(0x90 | ($i & 1));
  2386. }
  2387. // Alignment pattern
  2388. $frame = $this->putAlignmentPattern($version, $frame, $width);
  2389. // Version information
  2390. if ($version >= 7) {
  2391. $vinf = $this->getVersionPattern($version);
  2392. $v = $vinf;
  2393. for ($x=0; $x<6; ++$x) {
  2394. for ($y=0; $y<3; ++$y) {
  2395. $frame[($width - 11)+$y][$x] = chr(0x88 | ($v & 1));
  2396. $v = $v >> 1;
  2397. }
  2398. }
  2399. $v = $vinf;
  2400. for ($y=0; $y<6; ++$y) {
  2401. for ($x=0; $x<3; ++$x) {
  2402. $frame[$y][$x+($width - 11)] = chr(0x88 | ($v & 1));
  2403. $v = $v >> 1;
  2404. }
  2405. }
  2406. }
  2407. // and a little bit...
  2408. $frame[$width - 8][8] = "\x81";
  2409. return $frame;
  2410. }
  2411. /**
  2412. * Set new frame for the specified version.
  2413. * @param int $version version
  2414. * @return Array of unsigned char.
  2415. */
  2416. protected function newFrame($version) {
  2417. if (($version < 1) OR ($version > QRSPEC_VERSION_MAX)) {
  2418. return NULL;
  2419. }
  2420. if (!isset($this->frames[$version])) {
  2421. $this->frames[$version] = $this->createFrame($version);
  2422. }
  2423. if (is_null($this->frames[$version])) {
  2424. return NULL;
  2425. }
  2426. return $this->frames[$version];
  2427. }
  2428. /**
  2429. * Return block number 0
  2430. * @param array $spec
  2431. * @return int value
  2432. */
  2433. protected function rsBlockNum($spec) {
  2434. return ($spec[0] + $spec[3]);
  2435. }
  2436. /**
  2437. * Return block number 1
  2438. * @param array $spec
  2439. * @return int value
  2440. */
  2441. protected function rsBlockNum1($spec) {
  2442. return $spec[0];
  2443. }
  2444. /**
  2445. * Return data codes 1
  2446. * @param array $spec
  2447. * @return int value
  2448. */
  2449. protected function rsDataCodes1($spec) {
  2450. return $spec[1];
  2451. }
  2452. /**
  2453. * Return ecc codes 1
  2454. * @param array $spec
  2455. * @return int value
  2456. */
  2457. protected function rsEccCodes1($spec) {
  2458. return $spec[2];
  2459. }
  2460. /**
  2461. * Return block number 2
  2462. * @param array $spec
  2463. * @return int value
  2464. */
  2465. protected function rsBlockNum2($spec) {
  2466. return $spec[3];
  2467. }
  2468. /**
  2469. * Return data codes 2
  2470. * @param array $spec
  2471. * @return int value
  2472. */
  2473. protected function rsDataCodes2($spec) {
  2474. return $spec[4];
  2475. }
  2476. /**
  2477. * Return ecc codes 2
  2478. * @param array $spec
  2479. * @return int value
  2480. */
  2481. protected function rsEccCodes2($spec) {
  2482. return $spec[2];
  2483. }
  2484. /**
  2485. * Return data length
  2486. * @param array $spec
  2487. * @return int value
  2488. */
  2489. protected function rsDataLength($spec) {
  2490. return ($spec[0] * $spec[1]) + ($spec[3] * $spec[4]);
  2491. }
  2492. /**
  2493. * Return ecc length
  2494. * @param array $spec
  2495. * @return int value
  2496. */
  2497. protected function rsEccLength($spec) {
  2498. return ($spec[0] + $spec[3]) * $spec[2];
  2499. }
  2500. // - - - - - - - - - - - - - - - - - - - - - - - - -
  2501. // QRrs
  2502. /**
  2503. * Initialize a Reed-Solomon codec and add it to existing rsitems
  2504. * @param int $symsize symbol size, bits
  2505. * @param int $gfpoly Field generator polynomial coefficients
  2506. * @param int $fcr first root of RS code generator polynomial, index form
  2507. * @param int $prim primitive element to generate polynomial roots
  2508. * @param int $nroots RS code generator polynomial degree (number of roots)
  2509. * @param int $pad padding bytes at front of shortened block
  2510. * @return array Array of RS values:<ul><li>mm = Bits per symbol;</li><li>nn = Symbols per block;</li><li>alpha_to = log lookup table array;</li><li>index_of = Antilog lookup table array;</li><li>genpoly = Generator polynomial array;</li><li>nroots = Number of generator;</li><li>roots = number of parity symbols;</li><li>fcr = First consecutive root, index form;</li><li>prim = Primitive element, index form;</li><li>iprim = prim-th root of 1, index form;</li><li>pad = Padding bytes in shortened block;</li><li>gfpoly</ul>.
  2511. */
  2512. protected function init_rs($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) {
  2513. foreach ($this->rsitems as $rs) {
  2514. if (($rs['pad'] != $pad) OR ($rs['nroots'] != $nroots) OR ($rs['mm'] != $symsize)
  2515. OR ($rs['gfpoly'] != $gfpoly) OR ($rs['fcr'] != $fcr) OR ($rs['prim'] != $prim)) {
  2516. continue;
  2517. }
  2518. return $rs;
  2519. }
  2520. $rs = $this->init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad);
  2521. array_unshift($this->rsitems, $rs);
  2522. return $rs;
  2523. }
  2524. // - - - - - - - - - - - - - - - - - - - - - - - - -
  2525. // QRrsItem
  2526. /**
  2527. * modnn
  2528. * @param array RS values
  2529. * @param int $x X position
  2530. * @return int X osition
  2531. */
  2532. protected function modnn($rs, $x) {
  2533. while ($x >= $rs['nn']) {
  2534. $x -= $rs['nn'];
  2535. $x = ($x >> $rs['mm']) + ($x & $rs['nn']);
  2536. }
  2537. return $x;
  2538. }
  2539. /**
  2540. * Initialize a Reed-Solomon codec and returns an array of values.
  2541. * @param int $symsize symbol size, bits
  2542. * @param int $gfpoly Field generator polynomial coefficients
  2543. * @param int $fcr first root of RS code generator polynomial, index form
  2544. * @param int $prim primitive element to generate polynomial roots
  2545. * @param int $nroots RS code generator polynomial degree (number of roots)
  2546. * @param int $pad padding bytes at front of shortened block
  2547. * @return array Array of RS values:<ul><li>mm = Bits per symbol;</li><li>nn = Symbols per block;</li><li>alpha_to = log lookup table array;</li><li>index_of = Antilog lookup table array;</li><li>genpoly = Generator polynomial array;</li><li>nroots = Number of generator;</li><li>roots = number of parity symbols;</li><li>fcr = First consecutive root, index form;</li><li>prim = Primitive element, index form;</li><li>iprim = prim-th root of 1, index form;</li><li>pad = Padding bytes in shortened block;</li><li>gfpoly</ul>.
  2548. */
  2549. protected function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) {
  2550. // Based on Reed solomon encoder by Phil Karn, KA9Q (GNU-LGPLv2)
  2551. $rs = null;
  2552. // Check parameter ranges
  2553. if (($symsize < 0) OR ($symsize > 8)) {
  2554. return $rs;
  2555. }
  2556. if (($fcr < 0) OR ($fcr >= (1<<$symsize))) {
  2557. return $rs;
  2558. }
  2559. if (($prim <= 0) OR ($prim >= (1<<$symsize))) {
  2560. return $rs;
  2561. }
  2562. if (($nroots < 0) OR ($nroots >= (1<<$symsize))) {
  2563. return $rs;
  2564. }
  2565. if (($pad < 0) OR ($pad >= ((1<<$symsize) -1 - $nroots))) {
  2566. return $rs;
  2567. }
  2568. $rs = array();
  2569. $rs['mm'] = $symsize;
  2570. $rs['nn'] = (1 << $symsize) - 1;
  2571. $rs['pad'] = $pad;
  2572. $rs['alpha_to'] = array_fill(0, ($rs['nn'] + 1), 0);
  2573. $rs['index_of'] = array_fill(0, ($rs['nn'] + 1), 0);
  2574. // PHP style macro replacement ;)
  2575. $NN =& $rs['nn'];
  2576. $A0 =& $NN;
  2577. // Generate Galois field lookup tables
  2578. $rs['index_of'][0] = $A0; // log(zero) = -inf
  2579. $rs['alpha_to'][$A0] = 0; // alpha**-inf = 0
  2580. $sr = 1;
  2581. for ($i=0; $i<$rs['nn']; ++$i) {
  2582. $rs['index_of'][$sr] = $i;
  2583. $rs['alpha_to'][$i] = $sr;
  2584. $sr <<= 1;
  2585. if ($sr & (1 << $symsize)) {
  2586. $sr ^= $gfpoly;
  2587. }
  2588. $sr &= $rs['nn'];
  2589. }
  2590. if ($sr != 1) {
  2591. // field generator polynomial is not primitive!
  2592. return NULL;
  2593. }
  2594. // Form RS code generator polynomial from its roots
  2595. $rs['genpoly'] = array_fill(0, ($nroots + 1), 0);
  2596. $rs['fcr'] = $fcr;
  2597. $rs['prim'] = $prim;
  2598. $rs['nroots'] = $nroots;
  2599. $rs['gfpoly'] = $gfpoly;
  2600. // Find prim-th root of 1, used in decoding
  2601. for ($iprim=1; ($iprim % $prim) != 0; $iprim += $rs['nn']) {
  2602. ; // intentional empty-body loop!
  2603. }
  2604. $rs['iprim'] = (int)($iprim / $prim);
  2605. $rs['genpoly'][0] = 1;
  2606. for ($i = 0,$root=$fcr*$prim; $i < $nroots; $i++, $root += $prim) {
  2607. $rs['genpoly'][$i+1] = 1;
  2608. // Multiply rs->genpoly[] by @**(root + x)
  2609. for ($j = $i; $j > 0; --$j) {
  2610. if ($rs['genpoly'][$j] != 0) {
  2611. $rs['genpoly'][$j] = $rs['genpoly'][$j-1] ^ $rs['alpha_to'][$this->modnn($rs, $rs['index_of'][$rs['genpoly'][$j]] + $root)];
  2612. } else {
  2613. $rs['genpoly'][$j] = $rs['genpoly'][$j-1];
  2614. }
  2615. }
  2616. // rs->genpoly[0] can never be zero
  2617. $rs['genpoly'][0] = $rs['alpha_to'][$this->modnn($rs, $rs['index_of'][$rs['genpoly'][0]] + $root)];
  2618. }
  2619. // convert rs->genpoly[] to index form for quicker encoding
  2620. for ($i = 0; $i <= $nroots; ++$i) {
  2621. $rs['genpoly'][$i] = $rs['index_of'][$rs['genpoly'][$i]];
  2622. }
  2623. return $rs;
  2624. }
  2625. /**
  2626. * Encode a Reed-Solomon codec and returns the parity array
  2627. * @param array $rs RS values
  2628. * @param array $data data
  2629. * @param array $parity parity
  2630. * @return parity array
  2631. */
  2632. protected function encode_rs_char($rs, $data, $parity) {
  2633. $MM =& $rs['mm']; // bits per symbol
  2634. $NN =& $rs['nn']; // the total number of symbols in a RS block
  2635. $ALPHA_TO =& $rs['alpha_to']; // the address of an array of NN elements to convert Galois field elements in index (log) form to polynomial form
  2636. $INDEX_OF =& $rs['index_of']; // the address of an array of NN elements to convert Galois field elements in polynomial form to index (log) form
  2637. $GENPOLY =& $rs['genpoly']; // an array of NROOTS+1 elements containing the generator polynomial in index form
  2638. $NROOTS =& $rs['nroots']; // the number of roots in the RS code generator polynomial, which is the same as the number of parity symbols in a block
  2639. $FCR =& $rs['fcr']; // first consecutive root, index form
  2640. $PRIM =& $rs['prim']; // primitive element, index form
  2641. $IPRIM =& $rs['iprim']; // prim-th root of 1, index form
  2642. $PAD =& $rs['pad']; // the number of pad symbols in a block
  2643. $A0 =& $NN;
  2644. $parity = array_fill(0, $NROOTS, 0);
  2645. for ($i=0; $i < ($NN - $NROOTS - $PAD); $i++) {
  2646. $feedback = $INDEX_OF[$data[$i] ^ $parity[0]];
  2647. if ($feedback != $A0) {
  2648. // feedback term is non-zero
  2649. // This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
  2650. // always be for the polynomials constructed by init_rs()
  2651. $feedback = $this->modnn($rs, $NN - $GENPOLY[$NROOTS] + $feedback);
  2652. for ($j=1; $j < $NROOTS; ++$j) {
  2653. $parity[$j] ^= $ALPHA_TO[$this->modnn($rs, $feedback + $GENPOLY[($NROOTS - $j)])];
  2654. }
  2655. }
  2656. // Shift
  2657. array_shift($parity);
  2658. if ($feedback != $A0) {
  2659. array_push($parity, $ALPHA_TO[$this->modnn($rs, $feedback + $GENPOLY[0])]);
  2660. } else {
  2661. array_push($parity, 0);
  2662. }
  2663. }
  2664. return $parity;
  2665. }
  2666. } // end QRcode class
  2667. } // END OF "class_exists QRcode"
  2668. //============================================================+
  2669. // END OF FILE
  2670. //============================================================+