PageRenderTime 63ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/cookbook/pmwiki2pdf-v2/html2pdf_v4.03/_tcpdf_5.0.002/qrcode.php

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