PageRenderTime 67ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/shared/code/qrcode.php

https://bitbucket.org/biadmin/tcexam
PHP | 2865 lines | 1688 code | 209 blank | 968 comment | 292 complexity | b57b0525d53b08c1843e739c0c51f351 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0
  1. <?php
  2. //============================================================+
  3. // File name : qrcode.php
  4. // Version : 1.0.004
  5. // Begin : 2010-03-22
  6. // Last Update : 2010-06-03
  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.004
  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.004
  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. $qrTab = $this->binarize($this->data);
  577. $size = count($qrTab);
  578. $barcode_array['num_rows'] = $size;
  579. $barcode_array['num_cols'] = $size;
  580. $barcode_array['bcode'] = array();
  581. foreach ($qrTab as $line) {
  582. $arrAdd = array();
  583. foreach (str_split($line) as $char) {
  584. $arrAdd[] = ($char=='1')?1:0;
  585. }
  586. $barcode_array['bcode'][] = $arrAdd;
  587. }
  588. $this->barcode_array = $barcode_array;
  589. }
  590. /**
  591. * Returns a barcode array which is readable by TCPDF
  592. * @return array barcode array readable by TCPDF;
  593. * @access public
  594. */
  595. public function getBarcodeArray() {
  596. return $this->barcode_array;
  597. }
  598. /**
  599. * Convert the frame in binary form
  600. * @param array $frame array to binarize
  601. * @return array frame in binary form
  602. */
  603. protected function binarize($frame) {
  604. $len = count($frame);
  605. // the frame is square (width = height)
  606. foreach ($frame as &$frameLine) {
  607. for ($i=0; $i<$len; $i++) {
  608. $frameLine[$i] = (ord($frameLine[$i])&1)?'1':'0';
  609. }
  610. }
  611. return $frame;
  612. }
  613. /**
  614. * Encode the input string to QR code
  615. * @param string $string input string to encode
  616. */
  617. protected function encodeString($string) {
  618. $this->dataStr = $string;
  619. if (!$this->casesensitive) {
  620. $this->toUpper();
  621. }
  622. $ret = $this->splitString();
  623. if ($ret < 0) {
  624. return NULL;
  625. }
  626. $this->encodeMask(-1);
  627. }
  628. /**
  629. * Encode mask
  630. * @param int $mask masking mode
  631. */
  632. protected function encodeMask($mask) {
  633. $spec = array(0, 0, 0, 0, 0);
  634. $this->datacode = $this->getByteStream($this->items);
  635. if (is_null($this->datacode)) {
  636. return NULL;
  637. }
  638. $spec = $this->getEccSpec($this->version, $this->level, $spec);
  639. $this->b1 = $this->rsBlockNum1($spec);
  640. $this->dataLength = $this->rsDataLength($spec);
  641. $this->eccLength = $this->rsEccLength($spec);
  642. $this->ecccode = array_fill(0, $this->eccLength, 0);
  643. $this->blocks = $this->rsBlockNum($spec);
  644. $ret = $this->init($spec);
  645. if ($ret < 0) {
  646. return NULL;
  647. }
  648. $this->count = 0;
  649. $this->width = $this->getWidth($this->version);
  650. $this->frame = $this->newFrame($this->version);
  651. $this->x = $this->width - 1;
  652. $this->y = $this->width - 1;
  653. $this->dir = -1;
  654. $this->bit = -1;
  655. // inteleaved data and ecc codes
  656. for ($i=0; $i < ($this->dataLength + $this->eccLength); $i++) {
  657. $code = $this->getCode();
  658. $bit = 0x80;
  659. for ($j=0; $j<8; $j++) {
  660. $addr = $this->getNextPosition();
  661. $this->setFrameAt($addr, 0x02 | (($bit & $code) != 0));
  662. $bit = $bit >> 1;
  663. }
  664. }
  665. // remainder bits
  666. $j = $this->getRemainder($this->version);
  667. for ($i=0; $i<$j; $i++) {
  668. $addr = $this->getNextPosition();
  669. $this->setFrameAt($addr, 0x02);
  670. }
  671. // masking
  672. $this->runLength = array_fill(0, QRSPEC_WIDTH_MAX + 1, 0);
  673. if ($mask < 0) {
  674. if (QR_FIND_BEST_MASK) {
  675. $masked = $this->mask($this->width, $this->frame, $this->level);
  676. } else {
  677. $masked = $this->makeMask($this->width, $this->frame, (intval(QR_DEFAULT_MASK) % 8), $this->level);
  678. }
  679. } else {
  680. $masked = $this->makeMask($this->width, $this->frame, $mask, $this->level);
  681. }
  682. if ($masked == NULL) {
  683. return NULL;
  684. }
  685. $this->data = $masked;
  686. }
  687. // - - - - - - - - - - - - - - - - - - - - - - - - -
  688. // FrameFiller
  689. /**
  690. * Set frame value at specified position
  691. * @param array $at x,y position
  692. * @param int $val value of the character to set
  693. */
  694. protected function setFrameAt($at, $val) {
  695. $this->frame[$at['y']][$at['x']] = chr($val);
  696. }
  697. /**
  698. * Get frame value at specified position
  699. * @param array $at x,y position
  700. * @return value at specified position
  701. */
  702. protected function getFrameAt($at) {
  703. return ord($this->frame[$at['y']][$at['x']]);
  704. }
  705. /**
  706. * Return the next frame position
  707. * @return array of x,y coordinates
  708. */
  709. protected function getNextPosition() {
  710. do {
  711. if ($this->bit == -1) {
  712. $this->bit = 0;
  713. return array('x'=>$this->x, 'y'=>$this->y);
  714. }
  715. $x = $this->x;
  716. $y = $this->y;
  717. $w = $this->width;
  718. if ($this->bit == 0) {
  719. $x--;
  720. $this->bit++;
  721. } else {
  722. $x++;
  723. $y += $this->dir;
  724. $this->bit--;
  725. }
  726. if ($this->dir < 0) {
  727. if ($y < 0) {
  728. $y = 0;
  729. $x -= 2;
  730. $this->dir = 1;
  731. if ($x == 6) {
  732. $x--;
  733. $y = 9;
  734. }
  735. }
  736. } else {
  737. if ($y == $w) {
  738. $y = $w - 1;
  739. $x -= 2;
  740. $this->dir = -1;
  741. if ($x == 6) {
  742. $x--;
  743. $y -= 8;
  744. }
  745. }
  746. }
  747. if (($x < 0) OR ($y < 0)) {
  748. return NULL;
  749. }
  750. $this->x = $x;
  751. $this->y = $y;
  752. } while(ord($this->frame[$y][$x]) & 0x80);
  753. return array('x'=>$x, 'y'=>$y);
  754. }
  755. // - - - - - - - - - - - - - - - - - - - - - - - - -
  756. // QRrawcode
  757. /**
  758. * Initialize code.
  759. * @param array $spec array of ECC specification
  760. * @return 0 in case of success, -1 in case of error
  761. */
  762. protected function init($spec) {
  763. $dl = $this->rsDataCodes1($spec);
  764. $el = $this->rsEccCodes1($spec);
  765. $rs = $this->init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
  766. $blockNo = 0;
  767. $dataPos = 0;
  768. $eccPos = 0;
  769. $endfor = $this->rsBlockNum1($spec);
  770. for ($i=0; $i < $endfor; ++$i) {
  771. $ecc = array_slice($this->ecccode, $eccPos);
  772. $this->rsblocks[$blockNo] = array();
  773. $this->rsblocks[$blockNo]['dataLength'] = $dl;
  774. $this->rsblocks[$blockNo]['data'] = array_slice($this->datacode, $dataPos);
  775. $this->rsblocks[$blockNo]['eccLength'] = $el;
  776. $ecc = $this->encode_rs_char($rs, $this->rsblocks[$blockNo]['data'], $ecc);
  777. $this->rsblocks[$blockNo]['ecc'] = $ecc;
  778. $this->ecccode = array_merge(array_slice($this->ecccode,0, $eccPos), $ecc);
  779. $dataPos += $dl;
  780. $eccPos += $el;
  781. $blockNo++;
  782. }
  783. if ($this->rsBlockNum2($spec) == 0) {
  784. return 0;
  785. }
  786. $dl = $this->rsDataCodes2($spec);
  787. $el = $this->rsEccCodes2($spec);
  788. $rs = $this->init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
  789. if ($rs == NULL) {
  790. return -1;
  791. }
  792. $endfor = $this->rsBlockNum2($spec);
  793. for ($i=0; $i < $endfor; ++$i) {
  794. $ecc = array_slice($this->ecccode, $eccPos);
  795. $this->rsblocks[$blockNo] = array();
  796. $this->rsblocks[$blockNo]['dataLength'] = $dl;
  797. $this->rsblocks[$blockNo]['data'] = array_slice($this->datacode, $dataPos);
  798. $this->rsblocks[$blockNo]['eccLength'] = $el;
  799. $ecc = $this->encode_rs_char($rs, $this->rsblocks[$blockNo]['data'], $ecc);
  800. $this->rsblocks[$blockNo]['ecc'] = $ecc;
  801. $this->ecccode = array_merge(array_slice($this->ecccode, 0, $eccPos), $ecc);
  802. $dataPos += $dl;
  803. $eccPos += $el;
  804. $blockNo++;
  805. }
  806. return 0;
  807. }
  808. /**
  809. * Return Reed-Solomon block code.
  810. * @return array rsblocks
  811. */
  812. protected function getCode() {
  813. if ($this->count < $this->dataLength) {
  814. $row = $this->count % $this->blocks;
  815. $col = $this->count / $this->blocks;
  816. if ($col >= $this->rsblocks[0]['dataLength']) {
  817. $row += $this->b1;
  818. }
  819. $ret = $this->rsblocks[$row]['data'][$col];
  820. } elseif ($this->count < $this->dataLength + $this->eccLength) {
  821. $row = ($this->count - $this->dataLength) % $this->blocks;
  822. $col = ($this->count - $this->dataLength) / $this->blocks;
  823. $ret = $this->rsblocks[$row]['ecc'][$col];
  824. } else {
  825. return 0;
  826. }
  827. $this->count++;
  828. return $ret;
  829. }
  830. // - - - - - - - - - - - - - - - - - - - - - - - - -
  831. // QRmask
  832. /**
  833. * Write Format Information on frame and returns the number of black bits
  834. * @param int $width frame width
  835. * @param array $frame frame
  836. * @param array $mask masking mode
  837. * @param int $level error correction level
  838. * @return int blacks
  839. */
  840. protected function writeFormatInformation($width, &$frame, $mask, $level) {
  841. $blacks = 0;
  842. $format = $this->getFormatInfo($mask, $level);
  843. for ($i=0; $i<8; ++$i) {
  844. if ($format & 1) {
  845. $blacks += 2;
  846. $v = 0x85;
  847. } else {
  848. $v = 0x84;
  849. }
  850. $frame[8][$width - 1 - $i] = chr($v);
  851. if ($i < 6) {
  852. $frame[$i][8] = chr($v);
  853. } else {
  854. $frame[$i + 1][8] = chr($v);
  855. }
  856. $format = $format >> 1;
  857. }
  858. for ($i=0; $i<7; ++$i) {
  859. if ($format & 1) {
  860. $blacks += 2;
  861. $v = 0x85;
  862. } else {
  863. $v = 0x84;
  864. }
  865. $frame[$width - 7 + $i][8] = chr($v);
  866. if ($i == 0) {
  867. $frame[8][7] = chr($v);
  868. } else {
  869. $frame[8][6 - $i] = chr($v);
  870. }
  871. $format = $format >> 1;
  872. }
  873. return $blacks;
  874. }
  875. /**
  876. * mask0
  877. * @param int $x X position
  878. * @param int $y Y position
  879. * @return int mask
  880. */
  881. protected function mask0($x, $y) {
  882. return ($x + $y) & 1;
  883. }
  884. /**
  885. * mask1
  886. * @param int $x X position
  887. * @param int $y Y position
  888. * @return int mask
  889. */
  890. protected function mask1($x, $y) {
  891. return ($y & 1);
  892. }
  893. /**
  894. * mask2
  895. * @param int $x X position
  896. * @param int $y Y position
  897. * @return int mask
  898. */
  899. protected function mask2($x, $y) {
  900. return ($x % 3);
  901. }
  902. /**
  903. * mask3
  904. * @param int $x X position
  905. * @param int $y Y position
  906. * @return int mask
  907. */
  908. protected function mask3($x, $y) {
  909. return ($x + $y) % 3;
  910. }
  911. /**
  912. * mask4
  913. * @param int $x X position
  914. * @param int $y Y position
  915. * @return int mask
  916. */
  917. protected function mask4($x, $y) {
  918. return (((int)($y / 2)) + ((int)($x / 3))) & 1;
  919. }
  920. /**
  921. * mask5
  922. * @param int $x X position
  923. * @param int $y Y position
  924. * @return int mask
  925. */
  926. protected function mask5($x, $y) {
  927. return (($x * $y) & 1) + ($x * $y) % 3;
  928. }
  929. /**
  930. * mask6
  931. * @param int $x X position
  932. * @param int $y Y position
  933. * @return int mask
  934. */
  935. protected function mask6($x, $y) {
  936. return ((($x * $y) & 1) + ($x * $y) % 3) & 1;
  937. }
  938. /**
  939. * mask7
  940. * @param int $x X position
  941. * @param int $y Y position
  942. * @return int mask
  943. */
  944. protected function mask7($x, $y) {
  945. return ((($x * $y) % 3) + (($x + $y) & 1)) & 1;
  946. }
  947. /**
  948. * Return bitmask
  949. * @param int $maskNo mask number
  950. * @param int $width width
  951. * @param array $frame frame
  952. * @return array bitmask
  953. */
  954. protected function generateMaskNo($maskNo, $width, $frame) {
  955. $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
  956. for ($y=0; $y<$width; ++$y) {
  957. for ($x=0; $x<$width; ++$x) {
  958. if (ord($frame[$y][$x]) & 0x80) {
  959. $bitMask[$y][$x] = 0;
  960. } else {
  961. $maskFunc = call_user_func(array($this, 'mask'.$maskNo), $x, $y);
  962. $bitMask[$y][$x] = ($maskFunc == 0)?1:0;
  963. }
  964. }
  965. }
  966. return $bitMask;
  967. }
  968. /**
  969. * makeMaskNo
  970. * @param int $maskNo
  971. * @param int $width
  972. * @param int $s
  973. * @param int $d
  974. * @param boolean $maskGenOnly
  975. * @return int b
  976. */
  977. protected function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly=false) {
  978. $b = 0;
  979. $bitMask = array();
  980. $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
  981. if ($maskGenOnly) {
  982. return;
  983. }
  984. $d = $s;
  985. for ($y=0; $y<$width; ++$y) {
  986. for ($x=0; $x<$width; ++$x) {
  987. if ($bitMask[$y][$x] == 1) {
  988. $d[$y][$x] = chr(ord($s[$y][$x]) ^ (int)$bitMask[$y][$x]);
  989. }
  990. $b += (int)(ord($d[$y][$x]) & 1);
  991. }
  992. }
  993. return $b;
  994. }
  995. /**
  996. * makeMask
  997. * @param int $width
  998. * @param array $frame
  999. * @param int $maskNo
  1000. * @param int $level
  1001. * @return array mask
  1002. */
  1003. protected function makeMask($width, $frame, $maskNo, $level) {
  1004. $masked = array_fill(0, $width, str_repeat("\0", $width));
  1005. $this->makeMaskNo($maskNo, $width, $frame, $masked);
  1006. $this->writeFormatInformation($width, $masked, $maskNo, $level);
  1007. return $masked;
  1008. }
  1009. /**
  1010. * calcN1N3
  1011. * @param int $length
  1012. * @return int demerit
  1013. */
  1014. protected function calcN1N3($length) {
  1015. $demerit = 0;
  1016. for ($i=0; $i<$length; ++$i) {
  1017. if ($this->runLength[$i] >= 5) {
  1018. $demerit += (N1 + ($this->runLength[$i] - 5));
  1019. }
  1020. if ($i & 1) {
  1021. if (($i >= 3) AND ($i < ($length-2)) AND ($this->runLength[$i] % 3 == 0)) {
  1022. $fact = (int)($this->runLength[$i] / 3);
  1023. if (($this->runLength[$i-2] == $fact)
  1024. AND ($this->runLength[$i-1] == $fact)
  1025. AND ($this->runLength[$i+1] == $fact)
  1026. AND ($this->runLength[$i+2] == $fact)) {
  1027. if (($this->runLength[$i-3] < 0) OR ($this->runLength[$i-3] >= (4 * $fact))) {
  1028. $demerit += N3;
  1029. } elseif ((($i+3) >= $length) OR ($this->runLength[$i+3] >= (4 * $fact))) {
  1030. $demerit += N3;
  1031. }
  1032. }
  1033. }
  1034. }
  1035. }
  1036. return $demerit;
  1037. }
  1038. /**
  1039. * evaluateSymbol
  1040. * @param int $width
  1041. * @param array $frame
  1042. * @return int demerit
  1043. */
  1044. protected function evaluateSymbol($width, $frame) {
  1045. $head = 0;
  1046. $demerit = 0;
  1047. for ($y=0; $y<$width; ++$y) {
  1048. $head = 0;
  1049. $this->runLength[0] = 1;
  1050. $frameY = $frame[$y];
  1051. if ($y > 0) {
  1052. $frameYM = $frame[$y-1];
  1053. }
  1054. for ($x=0; $x<$width; ++$x) {
  1055. if (($x > 0) AND ($y > 0)) {
  1056. $b22 = ord($frameY[$x]) & ord($frameY[$x-1]) & ord($frameYM[$x]) & ord($frameYM[$x-1]);
  1057. $w22 = ord($frameY[$x]) | ord($frameY[$x-1]) | ord($frameYM[$x]) | ord($frameYM[$x-1]);
  1058. if (($b22 | ($w22 ^ 1)) & 1) {
  1059. $demerit += N2;
  1060. }
  1061. }
  1062. if (($x == 0) AND (ord($frameY[$x]) & 1)) {
  1063. $this->runLength[0] = -1;
  1064. $head = 1;
  1065. $this->runLength[$head] = 1;
  1066. } elseif ($x > 0) {
  1067. if ((ord($frameY[$x]) ^ ord($frameY[$x-1])) & 1) {
  1068. $head++;
  1069. $this->runLength[$head] = 1;
  1070. } else {
  1071. $this->runLength[$head]++;
  1072. }
  1073. }
  1074. }
  1075. $demerit += $this->calcN1N3($head+1);
  1076. }
  1077. for ($x=0; $x<$width; ++$x) {
  1078. $head = 0;
  1079. $this->runLength[0] = 1;
  1080. for ($y=0; $y<$width; ++$y) {
  1081. if (($y == 0) AND (ord($frame[$y][$x]) & 1)) {
  1082. $this->runLength[0] = -1;
  1083. $head = 1;
  1084. $this->runLength[$head] = 1;
  1085. } elseif ($y > 0) {
  1086. if ((ord($frame[$y][$x]) ^ ord($frame[$y-1][$x])) & 1) {
  1087. $head++;
  1088. $this->runLength[$head] = 1;
  1089. } else {
  1090. $this->runLength[$head]++;
  1091. }
  1092. }
  1093. }
  1094. $demerit += $this->calcN1N3($head+1);
  1095. }
  1096. return $demerit;
  1097. }
  1098. /**
  1099. * mask
  1100. * @param int $width
  1101. * @param array $frame
  1102. * @param int $level
  1103. * @return array best mask
  1104. */
  1105. protected function mask($width, $frame, $level) {
  1106. $minDemerit = PHP_INT_MAX;
  1107. $bestMaskNum = 0;
  1108. $bestMask = array();
  1109. $checked_masks = array(0, 1, 2, 3, 4, 5, 6, 7);
  1110. if (QR_FIND_FROM_RANDOM !== false) {
  1111. $howManuOut = 8 - (QR_FIND_FROM_RANDOM % 9);
  1112. for ($i = 0; $i < $howManuOut; ++$i) {
  1113. $remPos = rand (0, count($checked_masks)-1);
  1114. unset($checked_masks[$remPos]);
  1115. $checked_masks = array_values($checked_masks);
  1116. }
  1117. }
  1118. $bestMask = $frame;
  1119. foreach ($checked_masks as $i) {
  1120. $mask = array_fill(0, $width, str_repeat("\0", $width));
  1121. $demerit = 0;
  1122. $blacks = 0;
  1123. $blacks = $this->makeMaskNo($i, $width, $frame, $mask);
  1124. $blacks += $this->writeFormatInformation($width, $mask, $i, $level);
  1125. $blacks = (int)(100 * $blacks / ($width * $width));
  1126. $demerit = (int)((int)(abs($blacks - 50) / 5) * N4);
  1127. $demerit += $this->evaluateSymbol($width, $mask);
  1128. if ($demerit < $minDemerit) {
  1129. $minDemerit = $demerit;
  1130. $bestMask = $mask;
  1131. $bestMaskNum = $i;
  1132. }
  1133. }
  1134. return $bestMask;
  1135. }
  1136. // - - - - - - - - - - - - - - - - - - - - - - - - -
  1137. // QRsplit
  1138. /**
  1139. * Return true if the character at specified position is a number
  1140. * @param string $str string
  1141. * @param int $pos characted position
  1142. * @return boolean true of false
  1143. */
  1144. protected function isdigitat($str, $pos) {
  1145. if ($pos >= strlen($str)) {
  1146. return false;
  1147. }
  1148. return ((ord($str[$pos]) >= ord('0'))&&(ord($str[$pos]) <= ord('9')));
  1149. }
  1150. /**
  1151. * Return true if the character at specified position is an alphanumeric character
  1152. * @param string $str string
  1153. * @param int $pos characted position
  1154. * @return boolean true of false
  1155. */
  1156. protected function isalnumat($str, $pos) {
  1157. if ($pos >= strlen($str)) {
  1158. return false;
  1159. }
  1160. return ($this->lookAnTable(ord($str[$pos])) >= 0);
  1161. }
  1162. /**
  1163. * identifyMode
  1164. * @param int $pos
  1165. * @return int mode
  1166. */
  1167. protected function identifyMode($pos) {
  1168. if ($pos >= strlen($this->dataStr)) {
  1169. return QR_MODE_NL;
  1170. }
  1171. $c = $this->dataStr[$pos];
  1172. if ($this->isdigitat($this->dataStr, $pos)) {
  1173. return QR_MODE_NM;
  1174. } elseif ($this->isalnumat($this->dataStr, $pos)) {
  1175. return QR_MODE_AN;
  1176. } elseif ($this->hint == QR_MODE_KJ) {
  1177. if ($pos+1 < strlen($this->dataStr)) {
  1178. $d = $this->dataStr[$pos+1];
  1179. $word = (ord($c) << 8) | ord($d);
  1180. if (($word >= 0x8140 && $word <= 0x9ffc) OR ($word >= 0xe040 && $word <= 0xebbf)) {
  1181. return QR_MODE_KJ;
  1182. }
  1183. }
  1184. }
  1185. return QR_MODE_8B;
  1186. }
  1187. /**
  1188. * eatNum
  1189. * @return int run
  1190. */
  1191. protected function eatNum() {
  1192. $ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
  1193. $p = 0;
  1194. while($this->isdigitat($this->dataStr, $p)) {
  1195. $p++;
  1196. }
  1197. $run = $p;
  1198. $mode = $this->identifyMode($p);
  1199. if ($mode == QR_MODE_8B) {
  1200. $dif = $this->estimateBitsModeNum($run) + 4 + $ln
  1201. + $this->estimateBitsMode8(1) // + 4 + l8
  1202. - $this->estimateBitsMode8($run + 1); // - 4 - l8
  1203. if ($dif > 0) {
  1204. return $this->eat8();
  1205. }
  1206. }
  1207. if ($mode == QR_MODE_AN) {
  1208. $dif = $this->estimateBitsModeNum($run) + 4 + $ln
  1209. + $this->estimateBitsModeAn(1) // + 4 + la
  1210. - $this->estimateBitsModeAn($run + 1);// - 4 - la
  1211. if ($dif > 0) {
  1212. return $this->eatAn();
  1213. }
  1214. }
  1215. $this->items = $this->appendNewInputItem($this->items, QR_MODE_NM, $run, str_split($this->dataStr));
  1216. return $run;
  1217. }
  1218. /**
  1219. * eatAn
  1220. * @return int run
  1221. */
  1222. protected function eatAn() {
  1223. $la = $this->lengthIndicator(QR_MODE_AN, $this->version);
  1224. $ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
  1225. $p = 0;
  1226. while($this->isalnumat($this->dataStr, $p)) {
  1227. if ($this->isdigitat($this->dataStr, $p)) {
  1228. $q = $p;
  1229. while($this->isdigitat($this->dataStr, $q)) {
  1230. $q++;
  1231. }
  1232. $dif = $this->estimateBitsModeAn($p) // + 4 + la
  1233. + $this->estimateBitsModeNum($q - $p) + 4 + $ln
  1234. - $this->estimateBitsModeAn($q); // - 4 - la
  1235. if ($dif < 0) {
  1236. break;
  1237. } else {
  1238. $p = $q;
  1239. }
  1240. } else {
  1241. $p++;
  1242. }
  1243. }
  1244. $run = $p;
  1245. if (!$this->isalnumat($this->dataStr, $p)) {
  1246. $dif = $this->estimateBitsModeAn($run) + 4 + $la
  1247. + $this->estimateBitsMode8(1) // + 4 + l8
  1248. - $this->estimateBitsMode8($run + 1); // - 4 - l8
  1249. if ($dif > 0) {
  1250. return $this->eat8();
  1251. }
  1252. }
  1253. $this->items = $this->appendNewInputItem($this->items, QR_MODE_AN, $run, str_split($this->dataStr));
  1254. return $run;
  1255. }
  1256. /**
  1257. * eatKanji
  1258. * @return int run
  1259. */
  1260. protected function eatKanji() {
  1261. $p = 0;
  1262. while($this->identifyMode($p) == QR_MODE_KJ) {
  1263. $p += 2;
  1264. }
  1265. $this->items = $this->appendNewInputItem($this->items, QR_MODE_KJ, $p, str_split($this->dataStr));
  1266. return $run;
  1267. }
  1268. /**
  1269. * eat8
  1270. * @return int run
  1271. */
  1272. protected function eat8() {
  1273. $la = $this->lengthIndicator(QR_MODE_AN, $this->version);
  1274. $ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
  1275. $p = 1;
  1276. $dataStrLen = strlen($this->dataStr);
  1277. while($p < $dataStrLen) {
  1278. $mode = $this->identifyMode($p);
  1279. if ($mode == QR_MODE_KJ) {
  1280. break;
  1281. }
  1282. if ($mode == QR_MODE_NM) {
  1283. $q = $p;
  1284. while($this->isdigitat($this->dataStr, $q)) {
  1285. $q++;
  1286. }
  1287. $dif = $this->estimateBitsMode8($p) // + 4 + l8
  1288. + $this->estimateBitsModeNum($q - $p) + 4 + $ln
  1289. - $this->estimateBitsMode8($q); // - 4 - l8
  1290. if ($dif < 0) {
  1291. break;
  1292. } else {
  1293. $p = $q;
  1294. }
  1295. } elseif ($mode == QR_MODE_AN) {
  1296. $q = $p;
  1297. while($this->isalnumat($this->dataStr, $q)) {
  1298. $q++;
  1299. }
  1300. $dif = $this->estimateBitsMode8($p) // + 4 + l8
  1301. + $this->estimateBitsModeAn($q - $p) + 4 + $la
  1302. - $this->estimateBitsMode8($q); // - 4 - l8
  1303. if ($dif < 0) {
  1304. break;
  1305. } else {
  1306. $p = $q;
  1307. }
  1308. } else {
  1309. $p++;
  1310. }
  1311. }
  1312. $run = $p;
  1313. $this->items = $this->appendNewInputItem($this->items, QR_MODE_8B, $run, str_split($this->dataStr));
  1314. return $run;
  1315. }
  1316. /**
  1317. * splitString
  1318. */
  1319. protected function splitString() {
  1320. while (strlen($this->dataStr) > 0) {
  1321. if ($this->dataStr == '') {
  1322. return 0;
  1323. }
  1324. $mode = $this->identifyMode(0);
  1325. switch ($mode) {
  1326. case QR_MODE_NM: {
  1327. $length = $this->eatNum();
  1328. break;
  1329. }
  1330. case QR_MODE_AN: {
  1331. $length = $this->eatAn();
  1332. break;
  1333. }
  1334. case QR_MODE_KJ: {
  1335. if ($hint == QR_MODE_KJ) {
  1336. $length = $this->eatKanji();
  1337. } else {
  1338. $length = $this->eat8();
  1339. }
  1340. break;
  1341. }
  1342. default: {
  1343. $length = $this->eat8();
  1344. break;
  1345. }
  1346. }
  1347. if ($length == 0) {
  1348. return 0;
  1349. }
  1350. if ($length < 0) {
  1351. return -1;
  1352. }
  1353. $this->dataStr = substr($this->dataStr, $length);
  1354. }
  1355. }
  1356. /**
  1357. * toUpper
  1358. */
  1359. protected function toUpper() {
  1360. $stringLen = strlen($this->dataStr);
  1361. $p = 0;
  1362. while ($p < $stringLen) {
  1363. $mode = $this->identifyMode(substr($this->dataStr, $p), $this->hint);
  1364. if ($mode == QR_MODE_KJ) {
  1365. $p += 2;
  1366. } else {
  1367. if ((ord($this->dataStr[$p]) >= ord('a')) AND (ord($this->dataStr[$p]) <= ord('z'))) {
  1368. $this->dataStr[$p] = chr(ord($this->dataStr[$p]) - 32);
  1369. }
  1370. $p++;
  1371. }
  1372. }
  1373. return $this->dataStr;
  1374. }
  1375. // - - - - - - - - - - - - - - - - - - - - - - - - -
  1376. // QRinputItem
  1377. /**
  1378. * newInputItem
  1379. * @param int $mode
  1380. * @param int $size
  1381. * @param array $data
  1382. * @param array $bstream
  1383. * @return array input item
  1384. */
  1385. protected function newInputItem($mode, $size, $data, $bstream=null) {
  1386. $setData = array_slice($data, 0, $size);
  1387. if (count($setData) < $size) {
  1388. $setData = array_merge($setData, array_fill(0, ($size - count($setData)), 0));
  1389. }
  1390. if (!$this->check($mode, $size, $setData)) {
  1391. return NULL;
  1392. }
  1393. $inputitem = array();
  1394. $inputitem['mode'] = $mode;
  1395. $inputitem['size'] = $size;
  1396. $inputitem['data'] = $setData;
  1397. $inputitem['bstream'] = $bstream;
  1398. return $inputitem;
  1399. }
  1400. /**
  1401. * encodeModeNum
  1402. * @param array $inputitem
  1403. * @param int $version
  1404. * @return array input item
  1405. */
  1406. protected function encodeModeNum($inputitem, $version) {
  1407. $words = (int)($inputitem['size'] / 3);
  1408. $inputitem['bstream'] = array();
  1409. $val = 0x1;
  1410. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val);
  1411. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_NM, $version), $inputitem['size']);
  1412. for ($i=0; $i < $words; ++$i) {
  1413. $val = (ord($inputitem['data'][$i*3 ]) - ord('0')) * 100;
  1414. $val += (ord($inputitem['data'][$i*3+1]) - ord('0')) * 10;
  1415. $val += (ord($inputitem['data'][$i*3+2]) - ord('0'));
  1416. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 10, $val);
  1417. }
  1418. if ($inputitem['size'] - $words * 3 == 1) {
  1419. $val = ord($inputitem['data'][$words*3]) - ord('0');
  1420. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val);
  1421. } elseif (($inputitem['size'] - ($words * 3)) == 2) {
  1422. $val = (ord($inputitem['data'][$words*3 ]) - ord('0')) * 10;
  1423. $val += (ord($inputitem['data'][$words*3+1]) - ord('0'));
  1424. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 7, $val);
  1425. }
  1426. return $inputitem;
  1427. }
  1428. /**
  1429. * encodeModeAn
  1430. * @param array $inputitem
  1431. * @param int $version
  1432. * @return array input item
  1433. */
  1434. protected function encodeModeAn($inputitem, $version) {
  1435. $words = (int)($inputitem['size'] / 2);
  1436. $inputitem['bstream'] = array();
  1437. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x02);
  1438. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_AN, $version), $inputitem['size']);
  1439. for ($i=0; $i < $words; ++$i) {
  1440. $val = (int)$this->lookAnTable(ord($inputitem['data'][$i*2 ])) * 45;
  1441. $val += (int)$this->lookAnTable(ord($inputitem['data'][$i*2+1]));
  1442. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 11, $val);
  1443. }
  1444. if ($inputitem['size'] & 1) {
  1445. $val = $this->lookAnTable(ord($inputitem['data'][($words * 2)]));
  1446. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 6, $val);
  1447. }
  1448. return $inputitem;
  1449. }
  1450. /**
  1451. * encodeMode8
  1452. * @param array $inputitem
  1453. * @param int $version
  1454. * @return array input item
  1455. */
  1456. protected function encodeMode8($inputitem, $version) {
  1457. $inputitem['bstream'] = array();
  1458. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x4);
  1459. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_8B, $version), $inputitem['size']);
  1460. for ($i=0; $i < $inputitem['size']; ++$i) {
  1461. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, ord($inputitem['data'][$i]));
  1462. }
  1463. return $inputitem;
  1464. }
  1465. /**
  1466. * encodeModeKanji
  1467. * @param array $inputitem
  1468. * @param int $version
  1469. * @return array input item
  1470. */
  1471. protected function encodeModeKanji($inputitem, $version) {
  1472. $inputitem['bstream'] = array();
  1473. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x8);
  1474. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_KJ, $version), (int)($inputitem['size'] / 2));
  1475. for ($i=0; $i<$inputitem['size']; $i+=2) {
  1476. $val = (ord($inputitem['data'][$i]) << 8) | ord($inputitem['data'][$i+1]);
  1477. if ($val <= 0x9ffc) {
  1478. $val -= 0x8140;
  1479. } else {
  1480. $val -= 0xc140;
  1481. }
  1482. $h = ($val >> 8) * 0xc0;
  1483. $val = ($val & 0xff) + $h;
  1484. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 13, $val);
  1485. }
  1486. return $inputitem;
  1487. }
  1488. /**
  1489. * encodeModeStructure
  1490. * @param array $inputitem
  1491. * @return array input item
  1492. */
  1493. protected function encodeModeStructure($inputitem) {
  1494. $inputitem['bstream'] = array();
  1495. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x03);
  1496. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, ord($inputitem['data'][1]) - 1);
  1497. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, ord($inputitem['data'][0]) - 1);
  1498. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, ord($inputitem['data'][2]));
  1499. return $inputitem;
  1500. }
  1501. /**
  1502. * encodeBitStream
  1503. * @param array $inputitem
  1504. * @param int $version
  1505. * @return array input item
  1506. */
  1507. protected function encodeBitStream($inputitem, $version) {
  1508. $inputitem['bstream'] = array();
  1509. $words = $this->maximumWords($inputitem['mode'], $version);
  1510. if ($inputitem['size'] > $words) {
  1511. $st1 = $this->newInputItem($inputitem['mode'], $words, $inputitem['data']);
  1512. $st2 = $this->newInputItem($inputitem['mode'], $inputitem['size'] - $words, array_slice($inputitem['data'], $words));
  1513. $st1 = $this->encodeBitStream($st1, $version);
  1514. $st2 = $this->encodeBitStream($st2, $version);
  1515. $inputitem['bstream'] = array();
  1516. $inputitem['bstream'] = $this->appendBitstream($inputitem['bstream'], $st1['bstream']);
  1517. $inputitem['bstream'] = $this->appendBitstream($inputitem['bstream'], $st2['bstream']);
  1518. } else {
  1519. switch($inputitem['mode']) {
  1520. case QR_MODE_NM: {
  1521. $inputitem = $this->encodeModeNum($inputitem, $version);
  1522. break;
  1523. }
  1524. case QR_MODE_AN: {
  1525. $inputitem = $this->encodeModeAn($inputitem, $version);
  1526. break;
  1527. }
  1528. case QR_MODE_8B: {
  1529. $inputitem = $this->encodeMode8($inputitem, $version);
  1530. break;
  1531. }
  1532. case QR_MODE_KJ: {
  1533. $inputitem = $this->encodeModeKanji($inputitem, $version);
  1534. break;
  1535. }
  1536. case QR_MODE_ST: {
  1537. $inputitem = $this->encodeModeStructure($inputitem);
  1538. break;
  1539. }
  1540. default: {
  1541. break;
  1542. }
  1543. }
  1544. }
  1545. return $inputitem;
  1546. }
  1547. // - - - - - - - - - - - - - - - - - - - - - - - - -
  1548. // QRinput
  1549. /**
  1550. * Append data to an input object.
  1551. * The data is copied and appended to the input object.
  1552. * @param array items input items
  1553. * @param int $mode encoding mode.
  1554. * @param int $size size of data (byte).
  1555. * @param array $data array of input data.
  1556. * @return items
  1557. *
  1558. */
  1559. protected function appendNewInputItem($items, $mode, $size, $data) {
  1560. $items[] = $this->newInputItem($mode, $size, $data);
  1561. return $items;
  1562. }
  1563. /**
  1564. * insertStructuredAppendHeader
  1565. * @param array $items
  1566. * @param int $size
  1567. * @param int $index
  1568. * @param int $parity
  1569. * @return array items
  1570. */
  1571. protected function insertStructuredAppendHeader($items, $size, $index, $parity) {
  1572. if ($size > MAX_STRUCTURED_SYMBOLS) {
  1573. return -1;
  1574. }
  1575. if (($index <= 0) OR ($index > MAX_STRUCTURED_SYMBOLS)) {
  1576. return -1;
  1577. }
  1578. $buf = array($size, $index, $parity);
  1579. $entry = $this->newInputItem(QR_MODE_ST, 3, buf);
  1580. array_unshift($items, $entry);
  1581. return $items;
  1582. }
  1583. /**
  1584. * calcParity
  1585. * @param array $items
  1586. * @return int parity
  1587. */
  1588. protected function calcParity($items) {
  1589. $parity = 0;
  1590. foreach ($items as $item) {
  1591. if ($item['mode'] != QR_MODE_ST) {
  1592. for ($i=$item['size']-1; $i>=0; --$i) {
  1593. $parity ^= $item['data'][$i];
  1594. }
  1595. }
  1596. }
  1597. return $parity;
  1598. }
  1599. /**
  1600. * checkModeNum
  1601. * @param int $size
  1602. * @param array $data
  1603. * @return boolean true or false
  1604. */
  1605. protected function checkModeNum($size, $data) {
  1606. for ($i=0; $i<$size; ++$i) {
  1607. if ((ord($data[$i]) < ord('0')) OR (ord($data[$i]) > ord('9'))){
  1608. return false;
  1609. }
  1610. }
  1611. return true;
  1612. }
  1613. /**
  1614. * estimateBitsModeNum
  1615. * @param int $size
  1616. * @return int number of bits
  1617. */
  1618. protected function estimateBitsModeNum($size) {
  1619. $w = (int)$size / 3;
  1620. $bits = $w * 10;
  1621. switch($size - $w * 3) {
  1622. case 1: {
  1623. $bits += 4;
  1624. break;
  1625. }
  1626. case 2: {
  1627. $bits += 7;
  1628. break;
  1629. }
  1630. default: {
  1631. break;
  1632. }
  1633. }
  1634. return $bits;
  1635. }
  1636. /**
  1637. * Look up the alphabet-numeric convesion table (see JIS X0510:2004, pp.19).
  1638. * @param int $c character value
  1639. * @return value
  1640. */
  1641. protected function lookAnTable($c) {
  1642. return (($c > 127)?-1:$this->anTable[$c]);
  1643. }
  1644. /**
  1645. * checkModeAn
  1646. * @param int $size
  1647. * @param array $data
  1648. * @return boolean true or false
  1649. */
  1650. protected function checkModeAn($size, $data) {
  1651. for ($i=0; $i<$size; ++$i) {
  1652. if ($this->lookAnTable(ord($data[$i])) == -1) {
  1653. return false;
  1654. }
  1655. }
  1656. return true;
  1657. }
  1658. /**
  1659. * estimateBitsModeAn
  1660. * @param int $size
  1661. * @return int number of bits
  1662. */
  1663. protected function estimateBitsModeAn($size) {
  1664. $w = (int)($size / 2);
  1665. $bits = $w * 11;
  1666. if ($size & 1) {
  1667. $bits += 6;
  1668. }
  1669. return $bits;
  1670. }
  1671. /**
  1672. * estimateBitsMode8
  1673. * @param int $size
  1674. * @return int number of bits
  1675. */
  1676. protected function estimateBitsMode8($size) {
  1677. return $size * 8;
  1678. }
  1679. /**
  1680. * estimateBitsModeKanji
  1681. * @param int $size
  1682. * @return int number of bits
  1683. */
  1684. protected function estimateBitsModeKanji($size) {
  1685. return (int)(($size / 2) * 13);
  1686. }
  1687. /**
  1688. * checkModeKanji
  1689. * @param int $size
  1690. * @param array $data
  1691. * @return boolean true or false
  1692. */
  1693. protected function checkModeKanji($size, $data) {
  1694. if ($size & 1) {
  1695. return false;
  1696. }
  1697. for ($i=0; $i<$size; $i+=2) {
  1698. $val = (ord($data[$i]) << 8) | ord($data[$i+1]);
  1699. if (($val < 0x8140) OR (($val > 0x9ffc) AND ($val < 0xe040)) OR ($val > 0xebbf)) {
  1700. return false;
  1701. }
  1702. }
  1703. return true;
  1704. }
  1705. /**
  1706. * Validate the input data.
  1707. * @param int $mode encoding mode.
  1708. * @param int $size size of data (byte).
  1709. * @param array data data to validate
  1710. * @return boolean true in case of valid data, false otherwise
  1711. */
  1712. protected function check($mode, $size, $data) {
  1713. if ($size <= 0) {
  1714. return false;
  1715. }
  1716. switch($mode) {
  1717. case QR_MODE_NM: {
  1718. return $this->checkModeNum($size, $data);
  1719. }
  1720. case QR_MODE_AN: {
  1721. return $this->checkModeAn($size, $data);
  1722. }
  1723. case QR_MODE_KJ: {
  1724. return $this->checkModeKanji($size, $data);
  1725. }
  1726. case QR_MODE_8B: {
  1727. return true;
  1728. }
  1729. case QR_MODE_ST: {
  1730. return true;
  1731. }
  1732. default: {
  1733. break;
  1734. }
  1735. }
  1736. return false;
  1737. }
  1738. /**
  1739. * estimateBitStreamSize
  1740. * @param array $items
  1741. * @param int $version
  1742. * @return int bits
  1743. */
  1744. protected function estimateBitStreamSize($items, $version) {
  1745. $bits = 0;
  1746. if ($version == 0) {
  1747. $version = 1;
  1748. }
  1749. foreach ($items as $item) {
  1750. switch($item['mode']) {
  1751. case QR_MODE_NM: {
  1752. $bits = $this->estimateBitsModeNum($item['size']);
  1753. break;
  1754. }
  1755. case QR_MODE_AN: {
  1756. $bits = $this->estimateBitsModeAn($item['size']);
  1757. break;
  1758. }
  1759. case QR_MODE_8B: {
  1760. $bits = $this->estimateBitsMode8($item['size']);
  1761. break;
  1762. }
  1763. case QR_MODE_KJ: {
  1764. $bits = $this->estimateBitsModeKanji($item['size']);
  1765. break;
  1766. }
  1767. case QR_MODE_ST: {
  1768. return STRUCTURE_HEADER_BITS;
  1769. }
  1770. default: {
  1771. return 0;
  1772. }
  1773. }
  1774. $l = $this->lengthIndicator($item['mode'], $version);
  1775. $m = 1 << $l;
  1776. $num = (int)(($item['size'] + $m - 1) / $m);
  1777. $bits += $num * (4 + $l);
  1778. }
  1779. return $bits;
  1780. }
  1781. /**
  1782. * estimateVersion
  1783. * @param array $items
  1784. * @return int version
  1785. */
  1786. protected function estimateVersion($items) {
  1787. $version = 0;
  1788. $prev = 0;
  1789. do {
  1790. $prev = $version;
  1791. $bits = $this->estimateBitStreamSize($items, $prev);
  1792. $version = $this->getMinimumVersion((int)(($bits + 7) / 8), $this->level);
  1793. if ($version < 0) {
  1794. return -1;
  1795. }
  1796. } while ($version > $prev);
  1797. return $version;
  1798. }
  1799. /**
  1800. * lengthOfCode
  1801. * @param int $mode
  1802. * @param int $version
  1803. * @param int $bits
  1804. * @return int size
  1805. */
  1806. protected function lengthOfCode($mode, $version, $bits) {
  1807. $payload = $bits - 4 - $this->lengthIndicator($mode, $version);
  1808. switch($mode) {
  1809. case QR_MODE_NM: {
  1810. $chunks = (int)($payload / 10);
  1811. $remain = $payload - $chunks * 10;
  1812. $size = $chunks * 3;
  1813. if ($remain >= 7) {
  1814. $size += 2;
  1815. } elseif ($remain >= 4) {
  1816. $size += 1;
  1817. }
  1818. break;
  1819. }
  1820. case QR_MODE_AN: {
  1821. $chunks = (int)($payload / 11);
  1822. $remain = $payload - $chunks * 11;
  1823. $size = $chunks * 2;
  1824. if ($remain >= 6) {
  1825. ++$size;
  1826. }
  1827. break;
  1828. }
  1829. case QR_MODE_8B: {
  1830. $size = (int)($payload / 8);
  1831. break;
  1832. }
  1833. case QR_MODE_KJ: {
  1834. $size = (int)(($payload / 13) * 2);
  1835. break;
  1836. }
  1837. case QR_MODE_ST: {
  1838. $size = (int)($payload / 8);
  1839. break;
  1840. }
  1841. default: {
  1842. $size = 0;
  1843. break;
  1844. }
  1845. }
  1846. $maxsize = $this->maximumWords($mode, $version);
  1847. if ($size < 0) {
  1848. $size = 0;
  1849. }
  1850. if ($size > $maxsize) {
  1851. $size = $maxsize;
  1852. }
  1853. return $size;
  1854. }
  1855. /**
  1856. * createBitStream
  1857. * @param array $items
  1858. * @return array of items and total bits
  1859. */
  1860. protected function createBitStream($items) {
  1861. $total = 0;
  1862. foreach ($items as $key => $item) {
  1863. $items[$key] = $this->encodeBitStream($item, $this->version);
  1864. $bits = count($items[$key]['bstream']);
  1865. $total += $bits;
  1866. }
  1867. return array($items, $total);
  1868. }
  1869. /**
  1870. * convertData
  1871. * @param array $items
  1872. * @return array items
  1873. */
  1874. protected function convertData($items) {
  1875. $ver = $this->estimateVersion($items);
  1876. if ($ver > $this->version) {
  1877. $this->version = $ver;
  1878. }
  1879. for (;;) {
  1880. $cbs = $this->createBitStream($items);
  1881. $items = $cbs[0];
  1882. $bits = $cbs[1];
  1883. if ($bits < 0) {
  1884. return -1;
  1885. }
  1886. $ver = $this->getMinimumVersion((int)(($bits + 7) / 8), $this->level);
  1887. if ($ver < 0) {
  1888. return -1;
  1889. } elseif ($ver > $this->version) {
  1890. $this->version = $ver;
  1891. } else {
  1892. break;
  1893. }
  1894. }
  1895. return $items;
  1896. }
  1897. /**
  1898. * Append Padding Bit to bitstream
  1899. * @param array $bstream
  1900. * @return array bitstream
  1901. */
  1902. protected function appendPaddingBit($bstream) {
  1903. $bits = count($bstream);
  1904. $maxwords = $this->getDataLength($this->version, $this->level);
  1905. $maxbits = $maxwords * 8;
  1906. if ($maxbits == $bits) {
  1907. return 0;
  1908. }
  1909. if ($maxbits - $bits < 5) {
  1910. return $this->appendNum($bstream, $maxbits - $bits, 0);
  1911. }
  1912. $bits += 4;
  1913. $words = (int)(($bits + 7) / 8);
  1914. $padding = array();
  1915. $padding = $this->appendNum($padding, $words * 8 - $bits + 4, 0);
  1916. $padlen = $maxwords - $words;
  1917. if ($padlen > 0) {
  1918. $padbuf = array();
  1919. for ($i=0; $i<$padlen; ++$i) {
  1920. $padbuf[$i] = ($i&1)?0x11:0xec;
  1921. }
  1922. $padding = $this->appendBytes($padding, $padlen, $padbuf);
  1923. }
  1924. return $this->appendBitstream($bstream, $padding);
  1925. }
  1926. /**
  1927. * mergeBitStream
  1928. * @param array $bstream
  1929. * @return array bitstream
  1930. */
  1931. protected function mergeBitStream($items) {
  1932. $items = $this->convertData($items);
  1933. $bstream = array();
  1934. foreach ($items as $item) {
  1935. $bstream = $this->appendBitstream($bstream, $item['bstream']);
  1936. }
  1937. return $bstream;
  1938. }
  1939. /**
  1940. * Returns a stream of bits.
  1941. * @param int $items
  1942. * @return array padded merged byte stream
  1943. */
  1944. protected function getBitStream($items) {
  1945. $bstream = $this->mergeBitStream($items);
  1946. return $this->appendPaddingBit($bstream);
  1947. }
  1948. /**
  1949. * Pack all bit streams padding bits into a byte array.
  1950. * @param int $items
  1951. * @return array padded merged byte stream
  1952. */
  1953. protected function getByteStream($items) {
  1954. $bstream = $this->getBitStream($items);
  1955. return $this->bitstreamToByte($bstream);
  1956. }
  1957. // - - - - - - - - - - - - - - - - - - - - - - - - -
  1958. // QRbitstream
  1959. /**
  1960. * Return an array with zeros
  1961. * @param int $setLength array size
  1962. * @return array
  1963. */
  1964. protected function allocate($setLength) {
  1965. return array_fill(0, $setLength, 0);
  1966. }
  1967. /**
  1968. * Return new bitstream from number
  1969. * @param int $bits number of bits
  1970. * @param int $num number
  1971. * @return array bitstream
  1972. */
  1973. protected function newFromNum($bits, $num) {
  1974. $bstream = $this->allocate($bits);
  1975. $mask = 1 << ($bits - 1);
  1976. for ($i=0; $i<$bits; ++$i) {
  1977. if ($num & $mask) {
  1978. $bstream[$i] = 1;
  1979. } else {
  1980. $bstream[$i] = 0;
  1981. }
  1982. $mask = $mask >> 1;
  1983. }
  1984. return $bstream;
  1985. }
  1986. /**
  1987. * Return new bitstream from bytes
  1988. * @param int $size size
  1989. * @param array $data bytes
  1990. * @return array bitstream
  1991. */
  1992. protected function newFromBytes($size, $data) {
  1993. $bstream = $this->allocate($size * 8);
  1994. $p=0;
  1995. for ($i=0; $i<$size; ++$i) {
  1996. $mask = 0x80;
  1997. for ($j=0; $j<8; ++$j) {
  1998. if ($data[$i] & $mask) {
  1999. $bstream[$p] = 1;
  2000. } else {
  2001. $bstream[$p] = 0;
  2002. }
  2003. $p++;
  2004. $mask = $mask >> 1;
  2005. }
  2006. }
  2007. return $bstream;
  2008. }
  2009. /**
  2010. * Append one bitstream to another
  2011. * @param array $bitstream original bitstream
  2012. * @param array $append bitstream to append
  2013. * @return array bitstream
  2014. */
  2015. protected function appendBitstream($bitstream, $append) {
  2016. if ((!is_array($append)) OR (count($append) == 0)) {
  2017. return $bitstream;
  2018. }
  2019. if (count($bitstream) == 0) {
  2020. return $append;
  2021. }
  2022. return array_values(array_merge($bitstream, $append));
  2023. }
  2024. /**
  2025. * Append one bitstream created from number to another
  2026. * @param array $bitstream original bitstream
  2027. * @param int $bits number of bits
  2028. * @param int $num number
  2029. * @return array bitstream
  2030. */
  2031. protected function appendNum($bitstream, $bits, $num) {
  2032. if ($bits == 0) {
  2033. return 0;
  2034. }
  2035. $b = $this->newFromNum($bits, $num);
  2036. return $this->appendBitstream($bitstream, $b);
  2037. }
  2038. /**
  2039. * Append one bitstream created from bytes to another
  2040. * @param array $bitstream original bitstream
  2041. * @param int $size size
  2042. * @param array $data bytes
  2043. * @return array bitstream
  2044. */
  2045. protected function appendBytes($bitstream, $size, $data) {
  2046. if ($size == 0) {
  2047. return 0;
  2048. }
  2049. $b = $this->newFromBytes($size, $data);
  2050. return $this->appendBitstream($bitstream, $b);
  2051. }
  2052. /**
  2053. * Convert bitstream to bytes
  2054. * @param array $bitstream original bitstream
  2055. * @return array of bytes
  2056. */
  2057. protected function bitstreamToByte($bstream) {
  2058. $size = count($bstream);
  2059. if ($size == 0) {
  2060. return array();
  2061. }
  2062. $data = array_fill(0, (int)(($size + 7) / 8), 0);
  2063. $bytes = (int)($size / 8);
  2064. $p = 0;
  2065. for ($i=0; $i<$bytes; $i++) {
  2066. $v = 0;
  2067. for ($j=0; $j<8; $j++) {
  2068. $v = $v << 1;
  2069. $v |= $bstream[$p];
  2070. $p++;
  2071. }
  2072. $data[$i] = $v;
  2073. }
  2074. if ($size & 7) {
  2075. $v = 0;
  2076. for ($j=0; $j<($size & 7); $j++) {
  2077. $v = $v << 1;
  2078. $v |= $bstream[$p];
  2079. $p++;
  2080. }
  2081. $data[$bytes] = $v;
  2082. }
  2083. return $data;
  2084. }
  2085. // - - - - - - - - - - - - - - - - - - - - - - - - -
  2086. // QRspec
  2087. /**
  2088. * Replace a value on the array at the specified position
  2089. * @param array $srctab
  2090. * @param int $x X position
  2091. * @param int $y Y position
  2092. * @param string $repl value to replace
  2093. * @param int $replLen length of the repl string
  2094. * @return array srctab
  2095. */
  2096. protected function qrstrset($srctab, $x, $y, $repl, $replLen=false) {
  2097. $srctab[$y] = substr_replace($srctab[$y], ($replLen !== false)?substr($repl,0,$replLen):$repl, $x, ($replLen !== false)?$replLen:strlen($repl));
  2098. return $srctab;
  2099. }
  2100. /**
  2101. * Return maximum data code length (bytes) for the version.
  2102. * @param int $version version
  2103. * @param int $level error correction level
  2104. * @return int maximum size (bytes)
  2105. */
  2106. protected function getDataLength($version, $level) {
  2107. return $this->capacity[$version][QRCAP_WORDS] - $this->capacity[$version][QRCAP_EC][$level];
  2108. }
  2109. /**
  2110. * Return maximum error correction code length (bytes) for the version.
  2111. * @param int $version version
  2112. * @param int $level error correction level
  2113. * @return int ECC size (bytes)
  2114. */
  2115. protected function getECCLength($version, $level){
  2116. return $this->capacity[$version][QRCAP_EC][$level];
  2117. }
  2118. /**
  2119. * Return the width of the symbol for the version.
  2120. * @param int $version version
  2121. * @return int width
  2122. */
  2123. protected function getWidth($version) {
  2124. return $this->capacity[$version][QRCAP_WIDTH];
  2125. }
  2126. /**
  2127. * Return the numer of remainder bits.
  2128. * @param int $version version
  2129. * @return int number of remainder bits
  2130. */
  2131. protected function getRemainder($version) {
  2132. return $this->capacity[$version][QRCAP_REMINDER];
  2133. }
  2134. /**
  2135. * Return a version number that satisfies the input code length.
  2136. * @param int $size input code length (byte)
  2137. * @param int $level error correction level
  2138. * @return int version number
  2139. */
  2140. protected function getMinimumVersion($size, $level) {
  2141. for ($i=1; $i <= QRSPEC_VERSION_MAX; ++$i) {
  2142. $words = $this->capacity[$i][QRCAP_WORDS] - $this->capacity[$i][QRCAP_EC][$level];
  2143. if ($words >= $size) {
  2144. return $i;
  2145. }
  2146. }
  2147. return -1;
  2148. }
  2149. /**
  2150. * Return the size of length indicator for the mode and version.
  2151. * @param int $mode encoding mode
  2152. * @param int $version version
  2153. * @return int the size of the appropriate length indicator (bits).
  2154. */
  2155. protected function lengthIndicator($mode, $version) {
  2156. if ($mode == QR_MODE_ST) {
  2157. return 0;
  2158. }
  2159. if ($version <= 9) {
  2160. $l = 0;
  2161. } elseif ($version <= 26) {
  2162. $l = 1;
  2163. } else {
  2164. $l = 2;
  2165. }
  2166. return $this->lengthTableBits[$mode][$l];
  2167. }
  2168. /**
  2169. * Return the maximum length for the mode and version.
  2170. * @param int $mode encoding mode
  2171. * @param int $version version
  2172. * @return int the maximum length (bytes)
  2173. */
  2174. protected function maximumWords($mode, $version) {
  2175. if ($mode == QR_MODE_ST) {
  2176. return 3;
  2177. }
  2178. if ($version <= 9) {
  2179. $l = 0;
  2180. } else if ($version <= 26) {
  2181. $l = 1;
  2182. } else {
  2183. $l = 2;
  2184. }
  2185. $bits = $this->lengthTableBits[$mode][$l];
  2186. $words = (1 << $bits) - 1;
  2187. if ($mode == QR_MODE_KJ) {
  2188. $words *= 2; // the number of bytes is required
  2189. }
  2190. return $words;
  2191. }
  2192. /**
  2193. * Return an array of ECC specification.
  2194. * @param int $version version
  2195. * @param int $level error correction level
  2196. * @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}
  2197. * @return array spec
  2198. */
  2199. protected function getEccSpec($version, $level, $spec) {
  2200. if (count($spec) < 5) {
  2201. $spec = array(0, 0, 0, 0, 0);
  2202. }
  2203. $b1 = $this->eccTable[$version][$level][0];
  2204. $b2 = $this->eccTable[$version][$level][1];
  2205. $data = $this->getDataLength($version, $level);
  2206. $ecc = $this->getECCLength($version, $level);
  2207. if ($b2 == 0) {
  2208. $spec[0] = $b1;
  2209. $spec[1] = (int)($data / $b1);
  2210. $spec[2] = (int)($ecc / $b1);
  2211. $spec[3] = 0;
  2212. $spec[4] = 0;
  2213. } else {
  2214. $spec[0] = $b1;
  2215. $spec[1] = (int)($data / ($b1 + $b2));
  2216. $spec[2] = (int)($ecc / ($b1 + $b2));
  2217. $spec[3] = $b2;
  2218. $spec[4] = $spec[1] + 1;
  2219. }
  2220. return $spec;
  2221. }
  2222. /**
  2223. * Put an alignment marker.
  2224. * @param array $frame frame
  2225. * @param int $width width
  2226. * @param int $ox X center coordinate of the pattern
  2227. * @param int $oy Y center coordinate of the pattern
  2228. * @return array frame
  2229. */
  2230. protected function putAlignmentMarker($frame, $ox, $oy) {
  2231. $finder = array(
  2232. "\xa1\xa1\xa1\xa1\xa1",
  2233. "\xa1\xa0\xa0\xa0\xa1",
  2234. "\xa1\xa0\xa1\xa0\xa1",
  2235. "\xa1\xa0\xa0\xa0\xa1",
  2236. "\xa1\xa1\xa1\xa1\xa1"
  2237. );
  2238. $yStart = $oy - 2;
  2239. $xStart = $ox - 2;
  2240. for ($y=0; $y < 5; $y++) {
  2241. $frame = $this->qrstrset($frame, $xStart, $yStart+$y, $finder[$y]);
  2242. }
  2243. return $frame;
  2244. }
  2245. /**
  2246. * Put an alignment pattern.
  2247. * @param int $version version
  2248. * @param array $fram frame
  2249. * @param int $width width
  2250. * @return array frame
  2251. */
  2252. protected function putAlignmentPattern($version, $frame, $width) {
  2253. if ($version < 2) {
  2254. return $frame;
  2255. }
  2256. $d = $this->alignmentPattern[$version][1] - $this->alignmentPattern[$version][0];
  2257. if ($d < 0) {
  2258. $w = 2;
  2259. } else {
  2260. $w = (int)(($width - $this->alignmentPattern[$version][0]) / $d + 2);
  2261. }
  2262. if ($w * $w - 3 == 1) {
  2263. $x = $this->alignmentPattern[$version][0];
  2264. $y = $this->alignmentPattern[$version][0];
  2265. $frame = $this->putAlignmentMarker($frame, $x, $y);
  2266. return $frame;
  2267. }
  2268. $cx = $this->alignmentPattern[$version][0];
  2269. $wo = $w - 1;
  2270. for ($x=1; $x < $wo; ++$x) {
  2271. $frame = $this->putAlignmentMarker($frame, 6, $cx);
  2272. $frame = $this->putAlignmentMarker($frame, $cx, 6);
  2273. $cx += $d;
  2274. }
  2275. $cy = $this->alignmentPattern[$version][0];
  2276. for ($y=0; $y < $wo; ++$y) {
  2277. $cx = $this->alignmentPattern[$version][0];
  2278. for ($x=0; $x < $wo; ++$x) {
  2279. $frame = $this->putAlignmentMarker($frame, $cx, $cy);
  2280. $cx += $d;
  2281. }
  2282. $cy += $d;
  2283. }
  2284. return $frame;
  2285. }
  2286. /**
  2287. * Return BCH encoded version information pattern that is used for the symbol of version 7 or greater. Use lower 18 bits.
  2288. * @param int $version version
  2289. * @return BCH encoded version information pattern
  2290. */
  2291. protected function getVersionPattern($version) {
  2292. if (($version < 7) OR ($version > QRSPEC_VERSION_MAX)) {
  2293. return 0;
  2294. }
  2295. return $this->versionPattern[($version - 7)];
  2296. }
  2297. /**
  2298. * Return BCH encoded format information pattern.
  2299. * @param array $mask
  2300. * @param int $level error correction level
  2301. * @return BCH encoded format information pattern
  2302. */
  2303. protected function getFormatInfo($mask, $level) {
  2304. if (($mask < 0) OR ($mask > 7)) {
  2305. return 0;
  2306. }
  2307. if (($level < 0) OR ($level > 3)) {
  2308. return 0;
  2309. }
  2310. return $this->formatInfo[$level][$mask];
  2311. }
  2312. /**
  2313. * Put a finder pattern.
  2314. * @param array $frame frame
  2315. * @param int $width width
  2316. * @param int $ox X center coordinate of the pattern
  2317. * @param int $oy Y center coordinate of the pattern
  2318. * @return array frame
  2319. */
  2320. protected function putFinderPattern($frame, $ox, $oy) {
  2321. $finder = array(
  2322. "\xc1\xc1\xc1\xc1\xc1\xc1\xc1",
  2323. "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
  2324. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  2325. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  2326. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  2327. "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
  2328. "\xc1\xc1\xc1\xc1\xc1\xc1\xc1"
  2329. );
  2330. for ($y=0; $y < 7; $y++) {
  2331. $frame = $this->qrstrset($frame, $ox, ($oy + $y), $finder[$y]);
  2332. }
  2333. return $frame;
  2334. }
  2335. /**
  2336. * Return a copy of initialized frame.
  2337. * @param int $version version
  2338. * @return Array of unsigned char.
  2339. */
  2340. protected function createFrame($version) {
  2341. $width = $this->capacity[$version][QRCAP_WIDTH];
  2342. $frameLine = str_repeat ("\0", $width);
  2343. $frame = array_fill(0, $width, $frameLine);
  2344. // Finder pattern
  2345. $frame = $this->putFinderPattern($frame, 0, 0);
  2346. $frame = $this->putFinderPattern($frame, $width - 7, 0);
  2347. $frame = $this->putFinderPattern($frame, 0, $width - 7);
  2348. // Separator
  2349. $yOffset = $width - 7;
  2350. for ($y=0; $y < 7; ++$y) {
  2351. $frame[$y][7] = "\xc0";
  2352. $frame[$y][$width - 8] = "\xc0";
  2353. $frame[$yOffset][7] = "\xc0";
  2354. ++$yOffset;
  2355. }
  2356. $setPattern = str_repeat("\xc0", 8);
  2357. $frame = $this->qrstrset($frame, 0, 7, $setPattern);
  2358. $frame = $this->qrstrset($frame, $width-8, 7, $setPattern);
  2359. $frame = $this->qrstrset($frame, 0, $width - 8, $setPattern);
  2360. // Format info
  2361. $setPattern = str_repeat("\x84", 9);
  2362. $frame = $this->qrstrset($frame, 0, 8, $setPattern);
  2363. $frame = $this->qrstrset($frame, $width - 8, 8, $setPattern, 8);
  2364. $yOffset = $width - 8;
  2365. for ($y=0; $y < 8; ++$y,++$yOffset) {
  2366. $frame[$y][8] = "\x84";
  2367. $frame[$yOffset][8] = "\x84";
  2368. }
  2369. // Timing pattern
  2370. $wo = $width - 15;
  2371. for ($i=1; $i < $wo; ++$i) {
  2372. $frame[6][7+$i] = chr(0x90 | ($i & 1));
  2373. $frame[7+$i][6] = chr(0x90 | ($i & 1));
  2374. }
  2375. // Alignment pattern
  2376. $frame = $this->putAlignmentPattern($version, $frame, $width);
  2377. // Version information
  2378. if ($version >= 7) {
  2379. $vinf = $this->getVersionPattern($version);
  2380. $v = $vinf;
  2381. for ($x=0; $x<6; ++$x) {
  2382. for ($y=0; $y<3; ++$y) {
  2383. $frame[($width - 11)+$y][$x] = chr(0x88 | ($v & 1));
  2384. $v = $v >> 1;
  2385. }
  2386. }
  2387. $v = $vinf;
  2388. for ($y=0; $y<6; ++$y) {
  2389. for ($x=0; $x<3; ++$x) {
  2390. $frame[$y][$x+($width - 11)] = chr(0x88 | ($v & 1));
  2391. $v = $v >> 1;
  2392. }
  2393. }
  2394. }
  2395. // and a little bit...
  2396. $frame[$width - 8][8] = "\x81";
  2397. return $frame;
  2398. }
  2399. /**
  2400. * Set new frame for the specified version.
  2401. * @param int $version version
  2402. * @return Array of unsigned char.
  2403. */
  2404. protected function newFrame($version) {
  2405. if (($version < 1) OR ($version > QRSPEC_VERSION_MAX)) {
  2406. return NULL;
  2407. }
  2408. if (!isset($this->frames[$version])) {
  2409. $this->frames[$version] = $this->createFrame($version);
  2410. }
  2411. if (is_null($this->frames[$version])) {
  2412. return NULL;
  2413. }
  2414. return $this->frames[$version];
  2415. }
  2416. /**
  2417. * Return block number 0
  2418. * @param array $spec
  2419. * @return int value
  2420. */
  2421. protected function rsBlockNum($spec) {
  2422. return ($spec[0] + $spec[3]);
  2423. }
  2424. /**
  2425. * Return block number 1
  2426. * @param array $spec
  2427. * @return int value
  2428. */
  2429. protected function rsBlockNum1($spec) {
  2430. return $spec[0];
  2431. }
  2432. /**
  2433. * Return data codes 1
  2434. * @param array $spec
  2435. * @return int value
  2436. */
  2437. protected function rsDataCodes1($spec) {
  2438. return $spec[1];
  2439. }
  2440. /**
  2441. * Return ecc codes 1
  2442. * @param array $spec
  2443. * @return int value
  2444. */
  2445. protected function rsEccCodes1($spec) {
  2446. return $spec[2];
  2447. }
  2448. /**
  2449. * Return block number 2
  2450. * @param array $spec
  2451. * @return int value
  2452. */
  2453. protected function rsBlockNum2($spec) {
  2454. return $spec[3];
  2455. }
  2456. /**
  2457. * Return data codes 2
  2458. * @param array $spec
  2459. * @return int value
  2460. */
  2461. protected function rsDataCodes2($spec) {
  2462. return $spec[4];
  2463. }
  2464. /**
  2465. * Return ecc codes 2
  2466. * @param array $spec
  2467. * @return int value
  2468. */
  2469. protected function rsEccCodes2($spec) {
  2470. return $spec[2];
  2471. }
  2472. /**
  2473. * Return data length
  2474. * @param array $spec
  2475. * @return int value
  2476. */
  2477. protected function rsDataLength($spec) {
  2478. return ($spec[0] * $spec[1]) + ($spec[3] * $spec[4]);
  2479. }
  2480. /**
  2481. * Return ecc length
  2482. * @param array $spec
  2483. * @return int value
  2484. */
  2485. protected function rsEccLength($spec) {
  2486. return ($spec[0] + $spec[3]) * $spec[2];
  2487. }
  2488. // - - - - - - - - - - - - - - - - - - - - - - - - -
  2489. // QRrs
  2490. /**
  2491. * Initialize a Reed-Solomon codec and add it to existing rsitems
  2492. * @param int $symsize symbol size, bits
  2493. * @param int $gfpoly Field generator polynomial coefficients
  2494. * @param int $fcr first root of RS code generator polynomial, index form
  2495. * @param int $prim primitive element to generate polynomial roots
  2496. * @param int $nroots RS code generator polynomial degree (number of roots)
  2497. * @param int $pad padding bytes at front of shortened block
  2498. * @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>.
  2499. */
  2500. protected function init_rs($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) {
  2501. foreach ($this->rsitems as $rs) {
  2502. if (($rs['pad'] != $pad) OR ($rs['nroots'] != $nroots) OR ($rs['mm'] != $symsize)
  2503. OR ($rs['gfpoly'] != $gfpoly) OR ($rs['fcr'] != $fcr) OR ($rs['prim'] != $prim)) {
  2504. continue;
  2505. }
  2506. return $rs;
  2507. }
  2508. $rs = $this->init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad);
  2509. array_unshift($this->rsitems, $rs);
  2510. return $rs;
  2511. }
  2512. // - - - - - - - - - - - - - - - - - - - - - - - - -
  2513. // QRrsItem
  2514. /**
  2515. * modnn
  2516. * @param array RS values
  2517. * @param int $x X position
  2518. * @return int X osition
  2519. */
  2520. protected function modnn($rs, $x) {
  2521. while ($x >= $rs['nn']) {
  2522. $x -= $rs['nn'];
  2523. $x = ($x >> $rs['mm']) + ($x & $rs['nn']);
  2524. }
  2525. return $x;
  2526. }
  2527. /**
  2528. * Initialize a Reed-Solomon codec and returns an array of values.
  2529. * @param int $symsize symbol size, bits
  2530. * @param int $gfpoly Field generator polynomial coefficients
  2531. * @param int $fcr first root of RS code generator polynomial, index form
  2532. * @param int $prim primitive element to generate polynomial roots
  2533. * @param int $nroots RS code generator polynomial degree (number of roots)
  2534. * @param int $pad padding bytes at front of shortened block
  2535. * @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>.
  2536. */
  2537. protected function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) {
  2538. // Based on Reed solomon encoder by Phil Karn, KA9Q (GNU-LGPLv2)
  2539. $rs = null;
  2540. // Check parameter ranges
  2541. if (($symsize < 0) OR ($symsize > 8)) {
  2542. return $rs;
  2543. }
  2544. if (($fcr < 0) OR ($fcr >= (1<<$symsize))) {
  2545. return $rs;
  2546. }
  2547. if (($prim <= 0) OR ($prim >= (1<<$symsize))) {
  2548. return $rs;
  2549. }
  2550. if (($nroots < 0) OR ($nroots >= (1<<$symsize))) {
  2551. return $rs;
  2552. }
  2553. if (($pad < 0) OR ($pad >= ((1<<$symsize) -1 - $nroots))) {
  2554. return $rs;
  2555. }
  2556. $rs = array();
  2557. $rs['mm'] = $symsize;
  2558. $rs['nn'] = (1 << $symsize) - 1;
  2559. $rs['pad'] = $pad;
  2560. $rs['alpha_to'] = array_fill(0, ($rs['nn'] + 1), 0);
  2561. $rs['index_of'] = array_fill(0, ($rs['nn'] + 1), 0);
  2562. // PHP style macro replacement ;)
  2563. $NN =& $rs['nn'];
  2564. $A0 =& $NN;
  2565. // Generate Galois field lookup tables
  2566. $rs['index_of'][0] = $A0; // log(zero) = -inf
  2567. $rs['alpha_to'][$A0] = 0; // alpha**-inf = 0
  2568. $sr = 1;
  2569. for ($i=0; $i<$rs['nn']; ++$i) {
  2570. $rs['index_of'][$sr] = $i;
  2571. $rs['alpha_to'][$i] = $sr;
  2572. $sr <<= 1;
  2573. if ($sr & (1 << $symsize)) {
  2574. $sr ^= $gfpoly;
  2575. }
  2576. $sr &= $rs['nn'];
  2577. }
  2578. if ($sr != 1) {
  2579. // field generator polynomial is not primitive!
  2580. return NULL;
  2581. }
  2582. // Form RS code generator polynomial from its roots
  2583. $rs['genpoly'] = array_fill(0, ($nroots + 1), 0);
  2584. $rs['fcr'] = $fcr;
  2585. $rs['prim'] = $prim;
  2586. $rs['nroots'] = $nroots;
  2587. $rs['gfpoly'] = $gfpoly;
  2588. // Find prim-th root of 1, used in decoding
  2589. for ($iprim=1; ($iprim % $prim) != 0; $iprim += $rs['nn']) {
  2590. ; // intentional empty-body loop!
  2591. }
  2592. $rs['iprim'] = (int)($iprim / $prim);
  2593. $rs['genpoly'][0] = 1;
  2594. for ($i = 0,$root=$fcr*$prim; $i < $nroots; $i++, $root += $prim) {
  2595. $rs['genpoly'][$i+1] = 1;
  2596. // Multiply rs->genpoly[] by @**(root + x)
  2597. for ($j = $i; $j > 0; --$j) {
  2598. if ($rs['genpoly'][$j] != 0) {
  2599. $rs['genpoly'][$j] = $rs['genpoly'][$j-1] ^ $rs['alpha_to'][$this->modnn($rs, $rs['index_of'][$rs['genpoly'][$j]] + $root)];
  2600. } else {
  2601. $rs['genpoly'][$j] = $rs['genpoly'][$j-1];
  2602. }
  2603. }
  2604. // rs->genpoly[0] can never be zero
  2605. $rs['genpoly'][0] = $rs['alpha_to'][$this->modnn($rs, $rs['index_of'][$rs['genpoly'][0]] + $root)];
  2606. }
  2607. // convert rs->genpoly[] to index form for quicker encoding
  2608. for ($i = 0; $i <= $nroots; ++$i) {
  2609. $rs['genpoly'][$i] = $rs['index_of'][$rs['genpoly'][$i]];
  2610. }
  2611. return $rs;
  2612. }
  2613. /**
  2614. * Encode a Reed-Solomon codec and returns the parity array
  2615. * @param array $rs RS values
  2616. * @param array $data data
  2617. * @param array $parity parity
  2618. * @return parity array
  2619. */
  2620. protected function encode_rs_char($rs, $data, $parity) {
  2621. $MM =& $rs['mm']; // bits per symbol
  2622. $NN =& $rs['nn']; // the total number of symbols in a RS block
  2623. $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
  2624. $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
  2625. $GENPOLY =& $rs['genpoly']; // an array of NROOTS+1 elements containing the generator polynomial in index form
  2626. $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
  2627. $FCR =& $rs['fcr']; // first consecutive root, index form
  2628. $PRIM =& $rs['prim']; // primitive element, index form
  2629. $IPRIM =& $rs['iprim']; // prim-th root of 1, index form
  2630. $PAD =& $rs['pad']; // the number of pad symbols in a block
  2631. $A0 =& $NN;
  2632. $parity = array_fill(0, $NROOTS, 0);
  2633. for ($i=0; $i < ($NN - $NROOTS - $PAD); $i++) {
  2634. $feedback = $INDEX_OF[$data[$i] ^ $parity[0]];
  2635. if ($feedback != $A0) {
  2636. // feedback term is non-zero
  2637. // This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
  2638. // always be for the polynomials constructed by init_rs()
  2639. $feedback = $this->modnn($rs, $NN - $GENPOLY[$NROOTS] + $feedback);
  2640. for ($j=1; $j < $NROOTS; ++$j) {
  2641. $parity[$j] ^= $ALPHA_TO[$this->modnn($rs, $feedback + $GENPOLY[($NROOTS - $j)])];
  2642. }
  2643. }
  2644. // Shift
  2645. array_shift($parity);
  2646. if ($feedback != $A0) {
  2647. array_push($parity, $ALPHA_TO[$this->modnn($rs, $feedback + $GENPOLY[0])]);
  2648. } else {
  2649. array_push($parity, 0);
  2650. }
  2651. }
  2652. return $parity;
  2653. }
  2654. } // end QRcode class
  2655. } // END OF "class_exists QRcode"
  2656. ?>