PageRenderTime 58ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/includes/phpexcel/PHPExcel/Shared/PDF/qrcode.php

https://bitbucket.org/speedealing/speedealing
PHP | 2879 lines | 1699 code | 209 blank | 971 comment | 296 complexity | 3662dd9c0db109662edd23c537ae5a62 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. //============================================================+
  3. // File name : qrcode.php
  4. // Version : 1.0.006
  5. // Begin : 2010-03-22
  6. // Last Update : 2010-08-30
  7. // Author : Nicola Asuni - Tecnick.com S.r.l - Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  8. // License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
  9. // -------------------------------------------------------------------
  10. // Copyright (C) 2010-2010 Nicola Asuni - Tecnick.com S.r.l.
  11. //
  12. // This file is part of TCPDF software library.
  13. //
  14. // TCPDF is free software: you can redistribute it and/or modify it
  15. // under the terms of the GNU Lesser General Public License as
  16. // published by the Free Software Foundation, either version 3 of the
  17. // License, or (at your option) any later version.
  18. //
  19. // TCPDF is distributed in the hope that it will be useful, but
  20. // WITHOUT ANY WARRANTY; without even the implied warranty of
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. // See the GNU Lesser General Public License for more details.
  23. //
  24. // You should have received a copy of the GNU Lesser General Public License
  25. // along with TCPDF. If not, see <http://www.gnu.org/licenses/>.
  26. //
  27. // See LICENSE.TXT file for more information.
  28. // -------------------------------------------------------------------
  29. //
  30. // DESCRIPTION :
  31. //
  32. // Class to create QR-code arrays for TCPDF class.
  33. // QR Code symbol is a 2D barcode that can be scanned by
  34. // handy terminals such as a mobile phone with CCD.
  35. // The capacity of QR Code is up to 7000 digits or 4000
  36. // characters, and has high robustness.
  37. // This class supports QR Code model 2, described in
  38. // JIS (Japanese Industrial Standards) X0510:2004
  39. // or ISO/IEC 18004.
  40. // Currently the following features are not supported:
  41. // ECI and FNC1 mode, Micro QR Code, QR Code model 1,
  42. // Structured mode.
  43. //
  44. // This class is derived from the following projects:
  45. // ---------------------------------------------------------
  46. // "PHP QR Code encoder"
  47. // License: GNU-LGPLv3
  48. // Copyright (C) 2010 by Dominik Dzienia <deltalab at poczta dot fm>
  49. // http://phpqrcode.sourceforge.net/
  50. // https://sourceforge.net/projects/phpqrcode/
  51. //
  52. // The "PHP QR Code encoder" is based on
  53. // "C libqrencode library" (ver. 3.1.1)
  54. // License: GNU-LGPL 2.1
  55. // Copyright (C) 2006-2010 by Kentaro Fukuchi
  56. // http://megaui.net/fukuchi/works/qrencode/index.en.html
  57. //
  58. // Reed-Solomon code encoder is written by Phil Karn, KA9Q.
  59. // Copyright (C) 2002-2006 Phil Karn, KA9Q
  60. //
  61. // QR Code is registered trademark of DENSO WAVE INCORPORATED
  62. // http://www.denso-wave.com/qrcode/index-e.html
  63. // ---------------------------------------------------------
  64. //============================================================+
  65. /**
  66. * Class to create QR-code arrays for TCPDF class.
  67. * QR Code symbol is a 2D barcode that can be scanned by handy terminals such as a mobile phone with CCD.
  68. * The capacity of QR Code is up to 7000 digits or 4000 characters, and has high robustness.
  69. * This class supports QR Code model 2, described in JIS (Japanese Industrial Standards) X0510:2004 or ISO/IEC 18004.
  70. * Currently the following features are not supported: ECI and FNC1 mode, Micro QR Code, QR Code model 1, Structured mode.
  71. *
  72. * This class is derived from "PHP QR Code encoder" by Dominik Dzienia (http://phpqrcode.sourceforge.net/) based on "libqrencode C library 3.1.1." by Kentaro Fukuchi (http://megaui.net/fukuchi/works/qrencode/index.en.html), contains Reed-Solomon code written by Phil Karn, KA9Q. QR Code is registered trademark of DENSO WAVE INCORPORATED (http://www.denso-wave.com/qrcode/index-e.html).
  73. * Please read comments on this class source file for full copyright and license information.
  74. *
  75. * @package com.tecnick.tcpdf
  76. * @abstract Class for generating QR-code array for TCPDF.
  77. * @author Nicola Asuni
  78. * @copyright 2010-2010 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  79. * @link http://www.tcpdf.org
  80. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  81. * @version 1.0.006
  82. */
  83. // definitions
  84. if (!defined('QRCODEDEFS')) {
  85. /**
  86. * Indicate that definitions for this class are set
  87. */
  88. define('QRCODEDEFS', true);
  89. // -----------------------------------------------------
  90. // Encoding modes (characters which can be encoded in QRcode)
  91. /**
  92. * Encoding mode
  93. */
  94. define('QR_MODE_NL', -1);
  95. /**
  96. * Encoding mode numeric (0-9). 3 characters are encoded to 10bit length. In theory, 7089 characters or less can be stored in a QRcode.
  97. */
  98. define('QR_MODE_NM', 0);
  99. /**
  100. * Encoding mode alphanumeric (0-9A-Z $%*+-./:) 45characters. 2 characters are encoded to 11bit length. In theory, 4296 characters or less can be stored in a QRcode.
  101. */
  102. define('QR_MODE_AN', 1);
  103. /**
  104. * Encoding mode 8bit byte data. In theory, 2953 characters or less can be stored in a QRcode.
  105. */
  106. define('QR_MODE_8B', 2);
  107. /**
  108. * Encoding mode KANJI. A KANJI character (multibyte character) is encoded to 13bit length. In theory, 1817 characters or less can be stored in a QRcode.
  109. */
  110. define('QR_MODE_KJ', 3);
  111. /**
  112. * Encoding mode STRUCTURED (currently unsupported)
  113. */
  114. define('QR_MODE_ST', 4);
  115. // -----------------------------------------------------
  116. // Levels of error correction.
  117. // QRcode has a function of an error correcting for miss reading that white is black.
  118. // Error correcting is defined in 4 level as below.
  119. /**
  120. * Error correction level L : About 7% or less errors can be corrected.
  121. */
  122. define('QR_ECLEVEL_L', 0);
  123. /**
  124. * Error correction level M : About 15% or less errors can be corrected.
  125. */
  126. define('QR_ECLEVEL_M', 1);
  127. /**
  128. * Error correction level Q : About 25% or less errors can be corrected.
  129. */
  130. define('QR_ECLEVEL_Q', 2);
  131. /**
  132. * Error correction level H : About 30% or less errors can be corrected.
  133. */
  134. define('QR_ECLEVEL_H', 3);
  135. // -----------------------------------------------------
  136. // Version. Size of QRcode is defined as version.
  137. // Version is from 1 to 40.
  138. // Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases.
  139. // So version 40 is 177*177 matrix.
  140. /**
  141. * Maximum QR Code version.
  142. */
  143. define('QRSPEC_VERSION_MAX', 40);
  144. /**
  145. * Maximum matrix size for maximum version (version 40 is 177*177 matrix).
  146. */
  147. define('QRSPEC_WIDTH_MAX', 177);
  148. // -----------------------------------------------------
  149. /**
  150. * Matrix index to get width from $capacity array.
  151. */
  152. define('QRCAP_WIDTH', 0);
  153. /**
  154. * Matrix index to get number of words from $capacity array.
  155. */
  156. define('QRCAP_WORDS', 1);
  157. /**
  158. * Matrix index to get remainder from $capacity array.
  159. */
  160. define('QRCAP_REMINDER', 2);
  161. /**
  162. * Matrix index to get error correction level from $capacity array.
  163. */
  164. define('QRCAP_EC', 3);
  165. // -----------------------------------------------------
  166. // Structure (currently usupported)
  167. /**
  168. * Number of header bits for structured mode
  169. */
  170. define('STRUCTURE_HEADER_BITS', 20);
  171. /**
  172. * Max number of symbols for structured mode
  173. */
  174. define('MAX_STRUCTURED_SYMBOLS', 16);
  175. // -----------------------------------------------------
  176. // Masks
  177. /**
  178. * Down point base value for case 1 mask pattern (concatenation of same color in a line or a column)
  179. */
  180. define('N1', 3);
  181. /**
  182. * Down point base value for case 2 mask pattern (module block of same color)
  183. */
  184. define('N2', 3);
  185. /**
  186. * Down point base value for case 3 mask pattern (1:1:3:1:1(dark:bright:dark:bright:dark)pattern in a line or a column)
  187. */
  188. define('N3', 40);
  189. /**
  190. * Down point base value for case 4 mask pattern (ration of dark modules in whole)
  191. */
  192. define('N4', 10);
  193. // -----------------------------------------------------
  194. // Optimization settings
  195. /**
  196. * if true, estimates best mask (spec. default, but extremally slow; set to false to significant performance boost but (propably) worst quality code
  197. */
  198. define('QR_FIND_BEST_MASK', true);
  199. /**
  200. * if false, checks all masks available, otherwise value tells count of masks need to be checked, mask id are got randomly
  201. */
  202. define('QR_FIND_FROM_RANDOM', 2);
  203. /**
  204. * when QR_FIND_BEST_MASK === false
  205. */
  206. define('QR_DEFAULT_MASK', 2);
  207. // -----------------------------------------------------
  208. } // end of definitions
  209. // #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
  210. if (!class_exists('QRcode', false)) {
  211. // for compatibility with PHP4
  212. if (!function_exists('str_split')) {
  213. /**
  214. * Convert a string to an array (needed for PHP4 compatibility)
  215. * @param string $string The input string.
  216. * @param int $split_length Maximum length of the chunk.
  217. * @return If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length. FALSE is returned if split_length is less than 1. If the split_length length exceeds the length of string , the entire string is returned as the first (and only) array element.
  218. */
  219. function str_split($string, $split_length=1) {
  220. if ((strlen($string) > $split_length) OR (!$split_length)) {
  221. do {
  222. $c = strlen($string);
  223. $parts[] = substr($string, 0, $split_length);
  224. $string = substr($string, $split_length);
  225. } while ($string !== false);
  226. } else {
  227. $parts = array($string);
  228. }
  229. return $parts;
  230. }
  231. }
  232. // #####################################################
  233. /**
  234. * Class to create QR-code arrays for TCPDF class.
  235. * QR Code symbol is a 2D barcode that can be scanned by handy terminals such as a mobile phone with CCD.
  236. * The capacity of QR Code is up to 7000 digits or 4000 characters, and has high robustness.
  237. * This class supports QR Code model 2, described in JIS (Japanese Industrial Standards) X0510:2004 or ISO/IEC 18004.
  238. * Currently the following features are not supported: ECI and FNC1 mode, Micro QR Code, QR Code model 1, Structured mode.
  239. *
  240. * This class is derived from "PHP QR Code encoder" by Dominik Dzienia (http://phpqrcode.sourceforge.net/) based on "libqrencode C library 3.1.1." by Kentaro Fukuchi (http://megaui.net/fukuchi/works/qrencode/index.en.html), contains Reed-Solomon code written by Phil Karn, KA9Q. QR Code is registered trademark of DENSO WAVE INCORPORATED (http://www.denso-wave.com/qrcode/index-e.html).
  241. * Please read comments on this class source file for full copyright and license information.
  242. *
  243. * @name QRcode
  244. * @package com.tecnick.tcpdf
  245. * @abstract Class for generating QR-code array for TCPDF.
  246. * @author Nicola Asuni
  247. * @copyright 2010-2010 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  248. * @link http://www.tcpdf.org
  249. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  250. * @version 1.0.006
  251. */
  252. class QRcode {
  253. /**
  254. * @var barcode array to be returned which is readable by TCPDF
  255. * @access protected
  256. */
  257. protected $barcode_array = array();
  258. /**
  259. * @var QR code version. Size of QRcode is defined as version. Version is from 1 to 40. Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases. So version 40 is 177*177 matrix.
  260. * @access protected
  261. */
  262. protected $version = 0;
  263. /**
  264. * @var Levels of error correction. See definitions for possible values.
  265. * @access protected
  266. */
  267. protected $level = QR_ECLEVEL_L;
  268. /**
  269. * @var Encoding mode
  270. * @access protected
  271. */
  272. protected $hint = QR_MODE_8B;
  273. /**
  274. * @var if true the input string will be converted to uppercase
  275. * @access protected
  276. */
  277. protected $casesensitive = true;
  278. /**
  279. * @var structured QR code (not supported yet)
  280. * @access protected
  281. */
  282. protected $structured = 0;
  283. /**
  284. * @var mask data
  285. * @access protected
  286. */
  287. protected $data;
  288. // FrameFiller
  289. /**
  290. * @var width
  291. * @access protected
  292. */
  293. protected $width;
  294. /**
  295. * @var frame
  296. * @access protected
  297. */
  298. protected $frame;
  299. /**
  300. * @var X position of bit
  301. * @access protected
  302. */
  303. protected $x;
  304. /**
  305. * @var Y position of bit
  306. * @access protected
  307. */
  308. protected $y;
  309. /**
  310. * @var direction
  311. * @access protected
  312. */
  313. protected $dir;
  314. /**
  315. * @var single bit
  316. * @access protected
  317. */
  318. protected $bit;
  319. // ---- QRrawcode ----
  320. /**
  321. * @var data code
  322. * @access protected
  323. */
  324. protected $datacode = array();
  325. /**
  326. * @var error correction code
  327. * @access protected
  328. */
  329. protected $ecccode = array();
  330. /**
  331. * @var blocks
  332. * @access protected
  333. */
  334. protected $blocks;
  335. /**
  336. * @var Reed-Solomon blocks
  337. * @access protected
  338. */
  339. protected $rsblocks = array(); //of RSblock
  340. /**
  341. * @var counter
  342. * @access protected
  343. */
  344. protected $count;
  345. /**
  346. * @var data length
  347. * @access protected
  348. */
  349. protected $dataLength;
  350. /**
  351. * @var error correction length
  352. * @access protected
  353. */
  354. protected $eccLength;
  355. /**
  356. * @var b1
  357. * @access protected
  358. */
  359. protected $b1;
  360. // ---- QRmask ----
  361. /**
  362. * @var run length
  363. * @access protected
  364. */
  365. protected $runLength = array();
  366. // ---- QRsplit ----
  367. /**
  368. * @var input data string
  369. * @access protected
  370. */
  371. protected $dataStr = '';
  372. /**
  373. * @var input items
  374. * @access protected
  375. */
  376. protected $items;
  377. // Reed-Solomon items
  378. /**
  379. * @var Reed-Solomon items
  380. * @access protected
  381. */
  382. protected $rsitems = array();
  383. /**
  384. * @var array of frames
  385. * @access protected
  386. */
  387. protected $frames = array();
  388. /**
  389. * @var alphabet-numeric convesion table
  390. * @access protected
  391. */
  392. protected $anTable = array(
  393. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //
  394. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //
  395. 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, //
  396. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, //
  397. -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, //
  398. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, //
  399. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //
  400. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 //
  401. );
  402. /**
  403. * @var array Table of the capacity of symbols
  404. * See Table 1 (pp.13) and Table 12-16 (pp.30-36), JIS X0510:2004.
  405. * @access protected
  406. */
  407. protected $capacity = array(
  408. array( 0, 0, 0, array( 0, 0, 0, 0)), //
  409. array( 21, 26, 0, array( 7, 10, 13, 17)), // 1
  410. array( 25, 44, 7, array( 10, 16, 22, 28)), //
  411. array( 29, 70, 7, array( 15, 26, 36, 44)), //
  412. array( 33, 100, 7, array( 20, 36, 52, 64)), //
  413. array( 37, 134, 7, array( 26, 48, 72, 88)), // 5
  414. array( 41, 172, 7, array( 36, 64, 96, 112)), //
  415. array( 45, 196, 0, array( 40, 72, 108, 130)), //
  416. array( 49, 242, 0, array( 48, 88, 132, 156)), //
  417. array( 53, 292, 0, array( 60, 110, 160, 192)), //
  418. array( 57, 346, 0, array( 72, 130, 192, 224)), // 10
  419. array( 61, 404, 0, array( 80, 150, 224, 264)), //
  420. array( 65, 466, 0, array( 96, 176, 260, 308)), //
  421. array( 69, 532, 0, array( 104, 198, 288, 352)), //
  422. array( 73, 581, 3, array( 120, 216, 320, 384)), //
  423. array( 77, 655, 3, array( 132, 240, 360, 432)), // 15
  424. array( 81, 733, 3, array( 144, 280, 408, 480)), //
  425. array( 85, 815, 3, array( 168, 308, 448, 532)), //
  426. array( 89, 901, 3, array( 180, 338, 504, 588)), //
  427. array( 93, 991, 3, array( 196, 364, 546, 650)), //
  428. array( 97, 1085, 3, array( 224, 416, 600, 700)), // 20
  429. array(101, 1156, 4, array( 224, 442, 644, 750)), //
  430. array(105, 1258, 4, array( 252, 476, 690, 816)), //
  431. array(109, 1364, 4, array( 270, 504, 750, 900)), //
  432. array(113, 1474, 4, array( 300, 560, 810, 960)), //
  433. array(117, 1588, 4, array( 312, 588, 870, 1050)), // 25
  434. array(121, 1706, 4, array( 336, 644, 952, 1110)), //
  435. array(125, 1828, 4, array( 360, 700, 1020, 1200)), //
  436. array(129, 1921, 3, array( 390, 728, 1050, 1260)), //
  437. array(133, 2051, 3, array( 420, 784, 1140, 1350)), //
  438. array(137, 2185, 3, array( 450, 812, 1200, 1440)), // 30
  439. array(141, 2323, 3, array( 480, 868, 1290, 1530)), //
  440. array(145, 2465, 3, array( 510, 924, 1350, 1620)), //
  441. array(149, 2611, 3, array( 540, 980, 1440, 1710)), //
  442. array(153, 2761, 3, array( 570, 1036, 1530, 1800)), //
  443. array(157, 2876, 0, array( 570, 1064, 1590, 1890)), // 35
  444. array(161, 3034, 0, array( 600, 1120, 1680, 1980)), //
  445. array(165, 3196, 0, array( 630, 1204, 1770, 2100)), //
  446. array(169, 3362, 0, array( 660, 1260, 1860, 2220)), //
  447. array(173, 3532, 0, array( 720, 1316, 1950, 2310)), //
  448. array(177, 3706, 0, array( 750, 1372, 2040, 2430)) // 40
  449. );
  450. /**
  451. * @var array Length indicator
  452. * @access protected
  453. */
  454. protected $lengthTableBits = array(
  455. array(10, 12, 14),
  456. array( 9, 11, 13),
  457. array( 8, 16, 16),
  458. array( 8, 10, 12)
  459. );
  460. /**
  461. * @var array Table of the error correction code (Reed-Solomon block)
  462. * See Table 12-16 (pp.30-36), JIS X0510:2004.
  463. * @access protected
  464. */
  465. protected $eccTable = array(
  466. array(array( 0, 0), array( 0, 0), array( 0, 0), array( 0, 0)), //
  467. array(array( 1, 0), array( 1, 0), array( 1, 0), array( 1, 0)), // 1
  468. array(array( 1, 0), array( 1, 0), array( 1, 0), array( 1, 0)), //
  469. array(array( 1, 0), array( 1, 0), array( 2, 0), array( 2, 0)), //
  470. array(array( 1, 0), array( 2, 0), array( 2, 0), array( 4, 0)), //
  471. array(array( 1, 0), array( 2, 0), array( 2, 2), array( 2, 2)), // 5
  472. array(array( 2, 0), array( 4, 0), array( 4, 0), array( 4, 0)), //
  473. array(array( 2, 0), array( 4, 0), array( 2, 4), array( 4, 1)), //
  474. array(array( 2, 0), array( 2, 2), array( 4, 2), array( 4, 2)), //
  475. array(array( 2, 0), array( 3, 2), array( 4, 4), array( 4, 4)), //
  476. array(array( 2, 2), array( 4, 1), array( 6, 2), array( 6, 2)), // 10
  477. array(array( 4, 0), array( 1, 4), array( 4, 4), array( 3, 8)), //
  478. array(array( 2, 2), array( 6, 2), array( 4, 6), array( 7, 4)), //
  479. array(array( 4, 0), array( 8, 1), array( 8, 4), array(12, 4)), //
  480. array(array( 3, 1), array( 4, 5), array(11, 5), array(11, 5)), //
  481. array(array( 5, 1), array( 5, 5), array( 5, 7), array(11, 7)), // 15
  482. array(array( 5, 1), array( 7, 3), array(15, 2), array( 3, 13)), //
  483. array(array( 1, 5), array(10, 1), array( 1, 15), array( 2, 17)), //
  484. array(array( 5, 1), array( 9, 4), array(17, 1), array( 2, 19)), //
  485. array(array( 3, 4), array( 3, 11), array(17, 4), array( 9, 16)), //
  486. array(array( 3, 5), array( 3, 13), array(15, 5), array(15, 10)), // 20
  487. array(array( 4, 4), array(17, 0), array(17, 6), array(19, 6)), //
  488. array(array( 2, 7), array(17, 0), array( 7, 16), array(34, 0)), //
  489. array(array( 4, 5), array( 4, 14), array(11, 14), array(16, 14)), //
  490. array(array( 6, 4), array( 6, 14), array(11, 16), array(30, 2)), //
  491. array(array( 8, 4), array( 8, 13), array( 7, 22), array(22, 13)), // 25
  492. array(array(10, 2), array(19, 4), array(28, 6), array(33, 4)), //
  493. array(array( 8, 4), array(22, 3), array( 8, 26), array(12, 28)), //
  494. array(array( 3, 10), array( 3, 23), array( 4, 31), array(11, 31)), //
  495. array(array( 7, 7), array(21, 7), array( 1, 37), array(19, 26)), //
  496. array(array( 5, 10), array(19, 10), array(15, 25), array(23, 25)), // 30
  497. array(array(13, 3), array( 2, 29), array(42, 1), array(23, 28)), //
  498. array(array(17, 0), array(10, 23), array(10, 35), array(19, 35)), //
  499. array(array(17, 1), array(14, 21), array(29, 19), array(11, 46)), //
  500. array(array(13, 6), array(14, 23), array(44, 7), array(59, 1)), //
  501. array(array(12, 7), array(12, 26), array(39, 14), array(22, 41)), // 35
  502. array(array( 6, 14), array( 6, 34), array(46, 10), array( 2, 64)), //
  503. array(array(17, 4), array(29, 14), array(49, 10), array(24, 46)), //
  504. array(array( 4, 18), array(13, 32), array(48, 14), array(42, 32)), //
  505. array(array(20, 4), array(40, 7), array(43, 22), array(10, 67)), //
  506. array(array(19, 6), array(18, 31), array(34, 34), array(20, 61)) // 40
  507. );
  508. /**
  509. * @var array Positions of alignment patterns.
  510. * This array includes only the second and the third position of the alignment patterns. Rest of them can be calculated from the distance between them.
  511. * See Table 1 in Appendix E (pp.71) of JIS X0510:2004.
  512. * @access protected
  513. */
  514. protected $alignmentPattern = array(
  515. array( 0, 0),
  516. array( 0, 0), array(18, 0), array(22, 0), array(26, 0), array(30, 0), // 1- 5
  517. array(34, 0), array(22, 38), array(24, 42), array(26, 46), array(28, 50), // 6-10
  518. array(30, 54), array(32, 58), array(34, 62), array(26, 46), array(26, 48), // 11-15
  519. array(26, 50), array(30, 54), array(30, 56), array(30, 58), array(34, 62), // 16-20
  520. array(28, 50), array(26, 50), array(30, 54), array(28, 54), array(32, 58), // 21-25
  521. array(30, 58), array(34, 62), array(26, 50), array(30, 54), array(26, 52), // 26-30
  522. array(30, 56), array(34, 60), array(30, 58), array(34, 62), array(30, 54), // 31-35
  523. array(24, 50), array(28, 54), array(32, 58), array(26, 54), array(30, 58) // 35-40
  524. );
  525. /**
  526. * @var array Version information pattern (BCH coded).
  527. * See Table 1 in Appendix D (pp.68) of JIS X0510:2004.
  528. * size: [QRSPEC_VERSION_MAX - 6]
  529. * @access protected
  530. */
  531. protected $versionPattern = array(
  532. 0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d, //
  533. 0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9, //
  534. 0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75, //
  535. 0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64, //
  536. 0x27541, 0x28c69
  537. );
  538. /**
  539. * @var array Format information
  540. * @access protected
  541. */
  542. protected $formatInfo = array(
  543. array(0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976), //
  544. array(0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0), //
  545. array(0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed), //
  546. array(0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b) //
  547. );
  548. // -------------------------------------------------
  549. // -------------------------------------------------
  550. /**
  551. * This is the class constructor.
  552. * Creates a QRcode object
  553. * @param string $code code to represent using QRcode
  554. * @param string $eclevel error level: <ul><li>L : About 7% or less errors can be corrected.</li><li>M : About 15% or less errors can be corrected.</li><li>Q : About 25% or less errors can be corrected.</li><li>H : About 30% or less errors can be corrected.</li></ul>
  555. * @access public
  556. * @since 1.0.000
  557. */
  558. public function __construct($code, $eclevel = 'L') {
  559. $barcode_array = array();
  560. if ((is_null($code)) OR ($code == '\0') OR ($code == '')) {
  561. return false;
  562. }
  563. // set error correction level
  564. $this->level = array_search($eclevel, array('L', 'M', 'Q', 'H'));
  565. if ($this->level === false) {
  566. $this->level = QR_ECLEVEL_L;
  567. }
  568. if (($this->hint != QR_MODE_8B) AND ($this->hint != QR_MODE_KJ)) {
  569. return false;
  570. }
  571. if (($this->version < 0) OR ($this->version > QRSPEC_VERSION_MAX)) {
  572. return false;
  573. }
  574. $this->items = array();
  575. $this->encodeString($code);
  576. if (is_null($this->data)) {
  577. return false;
  578. }
  579. $qrTab = $this->binarize($this->data);
  580. $size = count($qrTab);
  581. $barcode_array['num_rows'] = $size;
  582. $barcode_array['num_cols'] = $size;
  583. $barcode_array['bcode'] = array();
  584. foreach ($qrTab as $line) {
  585. $arrAdd = array();
  586. foreach (str_split($line) as $char) {
  587. $arrAdd[] = ($char=='1')?1:0;
  588. }
  589. $barcode_array['bcode'][] = $arrAdd;
  590. }
  591. $this->barcode_array = $barcode_array;
  592. }
  593. /**
  594. * Returns a barcode array which is readable by TCPDF
  595. * @return array barcode array readable by TCPDF;
  596. * @access public
  597. */
  598. public function getBarcodeArray() {
  599. return $this->barcode_array;
  600. }
  601. /**
  602. * Convert the frame in binary form
  603. * @param array $frame array to binarize
  604. * @return array frame in binary form
  605. */
  606. protected function binarize($frame) {
  607. $len = count($frame);
  608. // the frame is square (width = height)
  609. foreach ($frame as &$frameLine) {
  610. for ($i=0; $i<$len; $i++) {
  611. $frameLine[$i] = (ord($frameLine[$i])&1)?'1':'0';
  612. }
  613. }
  614. return $frame;
  615. }
  616. /**
  617. * Encode the input string to QR code
  618. * @param string $string input string to encode
  619. */
  620. protected function encodeString($string) {
  621. $this->dataStr = $string;
  622. if (!$this->casesensitive) {
  623. $this->toUpper();
  624. }
  625. $ret = $this->splitString();
  626. if ($ret < 0) {
  627. return NULL;
  628. }
  629. $this->encodeMask(-1);
  630. }
  631. /**
  632. * Encode mask
  633. * @param int $mask masking mode
  634. */
  635. protected function encodeMask($mask) {
  636. $spec = array(0, 0, 0, 0, 0);
  637. $this->datacode = $this->getByteStream($this->items);
  638. if (is_null($this->datacode)) {
  639. return NULL;
  640. }
  641. $spec = $this->getEccSpec($this->version, $this->level, $spec);
  642. $this->b1 = $this->rsBlockNum1($spec);
  643. $this->dataLength = $this->rsDataLength($spec);
  644. $this->eccLength = $this->rsEccLength($spec);
  645. $this->ecccode = array_fill(0, $this->eccLength, 0);
  646. $this->blocks = $this->rsBlockNum($spec);
  647. $ret = $this->init($spec);
  648. if ($ret < 0) {
  649. return NULL;
  650. }
  651. $this->count = 0;
  652. $this->width = $this->getWidth($this->version);
  653. $this->frame = $this->newFrame($this->version);
  654. $this->x = $this->width - 1;
  655. $this->y = $this->width - 1;
  656. $this->dir = -1;
  657. $this->bit = -1;
  658. // inteleaved data and ecc codes
  659. for ($i=0; $i < ($this->dataLength + $this->eccLength); $i++) {
  660. $code = $this->getCode();
  661. $bit = 0x80;
  662. for ($j=0; $j<8; $j++) {
  663. $addr = $this->getNextPosition();
  664. $this->setFrameAt($addr, 0x02 | (($bit & $code) != 0));
  665. $bit = $bit >> 1;
  666. }
  667. }
  668. // remainder bits
  669. $j = $this->getRemainder($this->version);
  670. for ($i=0; $i<$j; $i++) {
  671. $addr = $this->getNextPosition();
  672. $this->setFrameAt($addr, 0x02);
  673. }
  674. // masking
  675. $this->runLength = array_fill(0, QRSPEC_WIDTH_MAX + 1, 0);
  676. if ($mask < 0) {
  677. if (QR_FIND_BEST_MASK) {
  678. $masked = $this->mask($this->width, $this->frame, $this->level);
  679. } else {
  680. $masked = $this->makeMask($this->width, $this->frame, (intval(QR_DEFAULT_MASK) % 8), $this->level);
  681. }
  682. } else {
  683. $masked = $this->makeMask($this->width, $this->frame, $mask, $this->level);
  684. }
  685. if ($masked == NULL) {
  686. return NULL;
  687. }
  688. $this->data = $masked;
  689. }
  690. // - - - - - - - - - - - - - - - - - - - - - - - - -
  691. // FrameFiller
  692. /**
  693. * Set frame value at specified position
  694. * @param array $at x,y position
  695. * @param int $val value of the character to set
  696. */
  697. protected function setFrameAt($at, $val) {
  698. $this->frame[$at['y']][$at['x']] = chr($val);
  699. }
  700. /**
  701. * Get frame value at specified position
  702. * @param array $at x,y position
  703. * @return value at specified position
  704. */
  705. protected function getFrameAt($at) {
  706. return ord($this->frame[$at['y']][$at['x']]);
  707. }
  708. /**
  709. * Return the next frame position
  710. * @return array of x,y coordinates
  711. */
  712. protected function getNextPosition() {
  713. do {
  714. if ($this->bit == -1) {
  715. $this->bit = 0;
  716. return array('x'=>$this->x, 'y'=>$this->y);
  717. }
  718. $x = $this->x;
  719. $y = $this->y;
  720. $w = $this->width;
  721. if ($this->bit == 0) {
  722. $x--;
  723. $this->bit++;
  724. } else {
  725. $x++;
  726. $y += $this->dir;
  727. $this->bit--;
  728. }
  729. if ($this->dir < 0) {
  730. if ($y < 0) {
  731. $y = 0;
  732. $x -= 2;
  733. $this->dir = 1;
  734. if ($x == 6) {
  735. $x--;
  736. $y = 9;
  737. }
  738. }
  739. } else {
  740. if ($y == $w) {
  741. $y = $w - 1;
  742. $x -= 2;
  743. $this->dir = -1;
  744. if ($x == 6) {
  745. $x--;
  746. $y -= 8;
  747. }
  748. }
  749. }
  750. if (($x < 0) OR ($y < 0)) {
  751. return NULL;
  752. }
  753. $this->x = $x;
  754. $this->y = $y;
  755. } while(ord($this->frame[$y][$x]) & 0x80);
  756. return array('x'=>$x, 'y'=>$y);
  757. }
  758. // - - - - - - - - - - - - - - - - - - - - - - - - -
  759. // QRrawcode
  760. /**
  761. * Initialize code.
  762. * @param array $spec array of ECC specification
  763. * @return 0 in case of success, -1 in case of error
  764. */
  765. protected function init($spec) {
  766. $dl = $this->rsDataCodes1($spec);
  767. $el = $this->rsEccCodes1($spec);
  768. $rs = $this->init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
  769. $blockNo = 0;
  770. $dataPos = 0;
  771. $eccPos = 0;
  772. $endfor = $this->rsBlockNum1($spec);
  773. for ($i=0; $i < $endfor; ++$i) {
  774. $ecc = array_slice($this->ecccode, $eccPos);
  775. $this->rsblocks[$blockNo] = array();
  776. $this->rsblocks[$blockNo]['dataLength'] = $dl;
  777. $this->rsblocks[$blockNo]['data'] = array_slice($this->datacode, $dataPos);
  778. $this->rsblocks[$blockNo]['eccLength'] = $el;
  779. $ecc = $this->encode_rs_char($rs, $this->rsblocks[$blockNo]['data'], $ecc);
  780. $this->rsblocks[$blockNo]['ecc'] = $ecc;
  781. $this->ecccode = array_merge(array_slice($this->ecccode,0, $eccPos), $ecc);
  782. $dataPos += $dl;
  783. $eccPos += $el;
  784. $blockNo++;
  785. }
  786. if ($this->rsBlockNum2($spec) == 0) {
  787. return 0;
  788. }
  789. $dl = $this->rsDataCodes2($spec);
  790. $el = $this->rsEccCodes2($spec);
  791. $rs = $this->init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
  792. if ($rs == NULL) {
  793. return -1;
  794. }
  795. $endfor = $this->rsBlockNum2($spec);
  796. for ($i=0; $i < $endfor; ++$i) {
  797. $ecc = array_slice($this->ecccode, $eccPos);
  798. $this->rsblocks[$blockNo] = array();
  799. $this->rsblocks[$blockNo]['dataLength'] = $dl;
  800. $this->rsblocks[$blockNo]['data'] = array_slice($this->datacode, $dataPos);
  801. $this->rsblocks[$blockNo]['eccLength'] = $el;
  802. $ecc = $this->encode_rs_char($rs, $this->rsblocks[$blockNo]['data'], $ecc);
  803. $this->rsblocks[$blockNo]['ecc'] = $ecc;
  804. $this->ecccode = array_merge(array_slice($this->ecccode, 0, $eccPos), $ecc);
  805. $dataPos += $dl;
  806. $eccPos += $el;
  807. $blockNo++;
  808. }
  809. return 0;
  810. }
  811. /**
  812. * Return Reed-Solomon block code.
  813. * @return array rsblocks
  814. */
  815. protected function getCode() {
  816. if ($this->count < $this->dataLength) {
  817. $row = $this->count % $this->blocks;
  818. $col = $this->count / $this->blocks;
  819. if ($col >= $this->rsblocks[0]['dataLength']) {
  820. $row += $this->b1;
  821. }
  822. $ret = $this->rsblocks[$row]['data'][$col];
  823. } elseif ($this->count < $this->dataLength + $this->eccLength) {
  824. $row = ($this->count - $this->dataLength) % $this->blocks;
  825. $col = ($this->count - $this->dataLength) / $this->blocks;
  826. $ret = $this->rsblocks[$row]['ecc'][$col];
  827. } else {
  828. return 0;
  829. }
  830. $this->count++;
  831. return $ret;
  832. }
  833. // - - - - - - - - - - - - - - - - - - - - - - - - -
  834. // QRmask
  835. /**
  836. * Write Format Information on frame and returns the number of black bits
  837. * @param int $width frame width
  838. * @param array $frame frame
  839. * @param array $mask masking mode
  840. * @param int $level error correction level
  841. * @return int blacks
  842. */
  843. protected function writeFormatInformation($width, &$frame, $mask, $level) {
  844. $blacks = 0;
  845. $format = $this->getFormatInfo($mask, $level);
  846. for ($i=0; $i<8; ++$i) {
  847. if ($format & 1) {
  848. $blacks += 2;
  849. $v = 0x85;
  850. } else {
  851. $v = 0x84;
  852. }
  853. $frame[8][$width - 1 - $i] = chr($v);
  854. if ($i < 6) {
  855. $frame[$i][8] = chr($v);
  856. } else {
  857. $frame[$i + 1][8] = chr($v);
  858. }
  859. $format = $format >> 1;
  860. }
  861. for ($i=0; $i<7; ++$i) {
  862. if ($format & 1) {
  863. $blacks += 2;
  864. $v = 0x85;
  865. } else {
  866. $v = 0x84;
  867. }
  868. $frame[$width - 7 + $i][8] = chr($v);
  869. if ($i == 0) {
  870. $frame[8][7] = chr($v);
  871. } else {
  872. $frame[8][6 - $i] = chr($v);
  873. }
  874. $format = $format >> 1;
  875. }
  876. return $blacks;
  877. }
  878. /**
  879. * mask0
  880. * @param int $x X position
  881. * @param int $y Y position
  882. * @return int mask
  883. */
  884. protected function mask0($x, $y) {
  885. return ($x + $y) & 1;
  886. }
  887. /**
  888. * mask1
  889. * @param int $x X position
  890. * @param int $y Y position
  891. * @return int mask
  892. */
  893. protected function mask1($x, $y) {
  894. return ($y & 1);
  895. }
  896. /**
  897. * mask2
  898. * @param int $x X position
  899. * @param int $y Y position
  900. * @return int mask
  901. */
  902. protected function mask2($x, $y) {
  903. return ($x % 3);
  904. }
  905. /**
  906. * mask3
  907. * @param int $x X position
  908. * @param int $y Y position
  909. * @return int mask
  910. */
  911. protected function mask3($x, $y) {
  912. return ($x + $y) % 3;
  913. }
  914. /**
  915. * mask4
  916. * @param int $x X position
  917. * @param int $y Y position
  918. * @return int mask
  919. */
  920. protected function mask4($x, $y) {
  921. return (((int)($y / 2)) + ((int)($x / 3))) & 1;
  922. }
  923. /**
  924. * mask5
  925. * @param int $x X position
  926. * @param int $y Y position
  927. * @return int mask
  928. */
  929. protected function mask5($x, $y) {
  930. return (($x * $y) & 1) + ($x * $y) % 3;
  931. }
  932. /**
  933. * mask6
  934. * @param int $x X position
  935. * @param int $y Y position
  936. * @return int mask
  937. */
  938. protected function mask6($x, $y) {
  939. return ((($x * $y) & 1) + ($x * $y) % 3) & 1;
  940. }
  941. /**
  942. * mask7
  943. * @param int $x X position
  944. * @param int $y Y position
  945. * @return int mask
  946. */
  947. protected function mask7($x, $y) {
  948. return ((($x * $y) % 3) + (($x + $y) & 1)) & 1;
  949. }
  950. /**
  951. * Return bitmask
  952. * @param int $maskNo mask number
  953. * @param int $width width
  954. * @param array $frame frame
  955. * @return array bitmask
  956. */
  957. protected function generateMaskNo($maskNo, $width, $frame) {
  958. $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
  959. for ($y=0; $y<$width; ++$y) {
  960. for ($x=0; $x<$width; ++$x) {
  961. if (ord($frame[$y][$x]) & 0x80) {
  962. $bitMask[$y][$x] = 0;
  963. } else {
  964. $maskFunc = call_user_func(array($this, 'mask'.$maskNo), $x, $y);
  965. $bitMask[$y][$x] = ($maskFunc == 0)?1:0;
  966. }
  967. }
  968. }
  969. return $bitMask;
  970. }
  971. /**
  972. * makeMaskNo
  973. * @param int $maskNo
  974. * @param int $width
  975. * @param int $s
  976. * @param int $d
  977. * @param boolean $maskGenOnly
  978. * @return int b
  979. */
  980. protected function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly=false) {
  981. $b = 0;
  982. $bitMask = array();
  983. $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
  984. if ($maskGenOnly) {
  985. return;
  986. }
  987. $d = $s;
  988. for ($y=0; $y<$width; ++$y) {
  989. for ($x=0; $x<$width; ++$x) {
  990. if ($bitMask[$y][$x] == 1) {
  991. $d[$y][$x] = chr(ord($s[$y][$x]) ^ (int)$bitMask[$y][$x]);
  992. }
  993. $b += (int)(ord($d[$y][$x]) & 1);
  994. }
  995. }
  996. return $b;
  997. }
  998. /**
  999. * makeMask
  1000. * @param int $width
  1001. * @param array $frame
  1002. * @param int $maskNo
  1003. * @param int $level
  1004. * @return array mask
  1005. */
  1006. protected function makeMask($width, $frame, $maskNo, $level) {
  1007. $masked = array_fill(0, $width, str_repeat("\0", $width));
  1008. $this->makeMaskNo($maskNo, $width, $frame, $masked);
  1009. $this->writeFormatInformation($width, $masked, $maskNo, $level);
  1010. return $masked;
  1011. }
  1012. /**
  1013. * calcN1N3
  1014. * @param int $length
  1015. * @return int demerit
  1016. */
  1017. protected function calcN1N3($length) {
  1018. $demerit = 0;
  1019. for ($i=0; $i<$length; ++$i) {
  1020. if ($this->runLength[$i] >= 5) {
  1021. $demerit += (N1 + ($this->runLength[$i] - 5));
  1022. }
  1023. if ($i & 1) {
  1024. if (($i >= 3) AND ($i < ($length-2)) AND ($this->runLength[$i] % 3 == 0)) {
  1025. $fact = (int)($this->runLength[$i] / 3);
  1026. if (($this->runLength[$i-2] == $fact)
  1027. AND ($this->runLength[$i-1] == $fact)
  1028. AND ($this->runLength[$i+1] == $fact)
  1029. AND ($this->runLength[$i+2] == $fact)) {
  1030. if (($this->runLength[$i-3] < 0) OR ($this->runLength[$i-3] >= (4 * $fact))) {
  1031. $demerit += N3;
  1032. } elseif ((($i+3) >= $length) OR ($this->runLength[$i+3] >= (4 * $fact))) {
  1033. $demerit += N3;
  1034. }
  1035. }
  1036. }
  1037. }
  1038. }
  1039. return $demerit;
  1040. }
  1041. /**
  1042. * evaluateSymbol
  1043. * @param int $width
  1044. * @param array $frame
  1045. * @return int demerit
  1046. */
  1047. protected function evaluateSymbol($width, $frame) {
  1048. $head = 0;
  1049. $demerit = 0;
  1050. for ($y=0; $y<$width; ++$y) {
  1051. $head = 0;
  1052. $this->runLength[0] = 1;
  1053. $frameY = $frame[$y];
  1054. if ($y > 0) {
  1055. $frameYM = $frame[$y-1];
  1056. }
  1057. for ($x=0; $x<$width; ++$x) {
  1058. if (($x > 0) AND ($y > 0)) {
  1059. $b22 = ord($frameY[$x]) & ord($frameY[$x-1]) & ord($frameYM[$x]) & ord($frameYM[$x-1]);
  1060. $w22 = ord($frameY[$x]) | ord($frameY[$x-1]) | ord($frameYM[$x]) | ord($frameYM[$x-1]);
  1061. if (($b22 | ($w22 ^ 1)) & 1) {
  1062. $demerit += N2;
  1063. }
  1064. }
  1065. if (($x == 0) AND (ord($frameY[$x]) & 1)) {
  1066. $this->runLength[0] = -1;
  1067. $head = 1;
  1068. $this->runLength[$head] = 1;
  1069. } elseif ($x > 0) {
  1070. if ((ord($frameY[$x]) ^ ord($frameY[$x-1])) & 1) {
  1071. $head++;
  1072. $this->runLength[$head] = 1;
  1073. } else {
  1074. $this->runLength[$head]++;
  1075. }
  1076. }
  1077. }
  1078. $demerit += $this->calcN1N3($head+1);
  1079. }
  1080. for ($x=0; $x<$width; ++$x) {
  1081. $head = 0;
  1082. $this->runLength[0] = 1;
  1083. for ($y=0; $y<$width; ++$y) {
  1084. if (($y == 0) AND (ord($frame[$y][$x]) & 1)) {
  1085. $this->runLength[0] = -1;
  1086. $head = 1;
  1087. $this->runLength[$head] = 1;
  1088. } elseif ($y > 0) {
  1089. if ((ord($frame[$y][$x]) ^ ord($frame[$y-1][$x])) & 1) {
  1090. $head++;
  1091. $this->runLength[$head] = 1;
  1092. } else {
  1093. $this->runLength[$head]++;
  1094. }
  1095. }
  1096. }
  1097. $demerit += $this->calcN1N3($head+1);
  1098. }
  1099. return $demerit;
  1100. }
  1101. /**
  1102. * mask
  1103. * @param int $width
  1104. * @param array $frame
  1105. * @param int $level
  1106. * @return array best mask
  1107. */
  1108. protected function mask($width, $frame, $level) {
  1109. $minDemerit = PHP_INT_MAX;
  1110. $bestMaskNum = 0;
  1111. $bestMask = array();
  1112. $checked_masks = array(0, 1, 2, 3, 4, 5, 6, 7);
  1113. if (QR_FIND_FROM_RANDOM !== false) {
  1114. $howManuOut = 8 - (QR_FIND_FROM_RANDOM % 9);
  1115. for ($i = 0; $i < $howManuOut; ++$i) {
  1116. $remPos = rand (0, count($checked_masks)-1);
  1117. unset($checked_masks[$remPos]);
  1118. $checked_masks = array_values($checked_masks);
  1119. }
  1120. }
  1121. $bestMask = $frame;
  1122. foreach ($checked_masks as $i) {
  1123. $mask = array_fill(0, $width, str_repeat("\0", $width));
  1124. $demerit = 0;
  1125. $blacks = 0;
  1126. $blacks = $this->makeMaskNo($i, $width, $frame, $mask);
  1127. $blacks += $this->writeFormatInformation($width, $mask, $i, $level);
  1128. $blacks = (int)(100 * $blacks / ($width * $width));
  1129. $demerit = (int)((int)(abs($blacks - 50) / 5) * N4);
  1130. $demerit += $this->evaluateSymbol($width, $mask);
  1131. if ($demerit < $minDemerit) {
  1132. $minDemerit = $demerit;
  1133. $bestMask = $mask;
  1134. $bestMaskNum = $i;
  1135. }
  1136. }
  1137. return $bestMask;
  1138. }
  1139. // - - - - - - - - - - - - - - - - - - - - - - - - -
  1140. // QRsplit
  1141. /**
  1142. * Return true if the character at specified position is a number
  1143. * @param string $str string
  1144. * @param int $pos characted position
  1145. * @return boolean true of false
  1146. */
  1147. protected function isdigitat($str, $pos) {
  1148. if ($pos >= strlen($str)) {
  1149. return false;
  1150. }
  1151. return ((ord($str[$pos]) >= ord('0'))&&(ord($str[$pos]) <= ord('9')));
  1152. }
  1153. /**
  1154. * Return true if the character at specified position is an alphanumeric character
  1155. * @param string $str string
  1156. * @param int $pos characted position
  1157. * @return boolean true of false
  1158. */
  1159. protected function isalnumat($str, $pos) {
  1160. if ($pos >= strlen($str)) {
  1161. return false;
  1162. }
  1163. return ($this->lookAnTable(ord($str[$pos])) >= 0);
  1164. }
  1165. /**
  1166. * identifyMode
  1167. * @param int $pos
  1168. * @return int mode
  1169. */
  1170. protected function identifyMode($pos) {
  1171. if ($pos >= strlen($this->dataStr)) {
  1172. return QR_MODE_NL;
  1173. }
  1174. $c = $this->dataStr[$pos];
  1175. if ($this->isdigitat($this->dataStr, $pos)) {
  1176. return QR_MODE_NM;
  1177. } elseif ($this->isalnumat($this->dataStr, $pos)) {
  1178. return QR_MODE_AN;
  1179. } elseif ($this->hint == QR_MODE_KJ) {
  1180. if ($pos+1 < strlen($this->dataStr)) {
  1181. $d = $this->dataStr[$pos+1];
  1182. $word = (ord($c) << 8) | ord($d);
  1183. if (($word >= 0x8140 && $word <= 0x9ffc) OR ($word >= 0xe040 && $word <= 0xebbf)) {
  1184. return QR_MODE_KJ;
  1185. }
  1186. }
  1187. }
  1188. return QR_MODE_8B;
  1189. }
  1190. /**
  1191. * eatNum
  1192. * @return int run
  1193. */
  1194. protected function eatNum() {
  1195. $ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
  1196. $p = 0;
  1197. while($this->isdigitat($this->dataStr, $p)) {
  1198. $p++;
  1199. }
  1200. $run = $p;
  1201. $mode = $this->identifyMode($p);
  1202. if ($mode == QR_MODE_8B) {
  1203. $dif = $this->estimateBitsModeNum($run) + 4 + $ln
  1204. + $this->estimateBitsMode8(1) // + 4 + l8
  1205. - $this->estimateBitsMode8($run + 1); // - 4 - l8
  1206. if ($dif > 0) {
  1207. return $this->eat8();
  1208. }
  1209. }
  1210. if ($mode == QR_MODE_AN) {
  1211. $dif = $this->estimateBitsModeNum($run) + 4 + $ln
  1212. + $this->estimateBitsModeAn(1) // + 4 + la
  1213. - $this->estimateBitsModeAn($run + 1);// - 4 - la
  1214. if ($dif > 0) {
  1215. return $this->eatAn();
  1216. }
  1217. }
  1218. $this->items = $this->appendNewInputItem($this->items, QR_MODE_NM, $run, str_split($this->dataStr));
  1219. return $run;
  1220. }
  1221. /**
  1222. * eatAn
  1223. * @return int run
  1224. */
  1225. protected function eatAn() {
  1226. $la = $this->lengthIndicator(QR_MODE_AN, $this->version);
  1227. $ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
  1228. $p = 0;
  1229. while($this->isalnumat($this->dataStr, $p)) {
  1230. if ($this->isdigitat($this->dataStr, $p)) {
  1231. $q = $p;
  1232. while($this->isdigitat($this->dataStr, $q)) {
  1233. $q++;
  1234. }
  1235. $dif = $this->estimateBitsModeAn($p) // + 4 + la
  1236. + $this->estimateBitsModeNum($q - $p) + 4 + $ln
  1237. - $this->estimateBitsModeAn($q); // - 4 - la
  1238. if ($dif < 0) {
  1239. break;
  1240. } else {
  1241. $p = $q;
  1242. }
  1243. } else {
  1244. $p++;
  1245. }
  1246. }
  1247. $run = $p;
  1248. if (!$this->isalnumat($this->dataStr, $p)) {
  1249. $dif = $this->estimateBitsModeAn($run) + 4 + $la
  1250. + $this->estimateBitsMode8(1) // + 4 + l8
  1251. - $this->estimateBitsMode8($run + 1); // - 4 - l8
  1252. if ($dif > 0) {
  1253. return $this->eat8();
  1254. }
  1255. }
  1256. $this->items = $this->appendNewInputItem($this->items, QR_MODE_AN, $run, str_split($this->dataStr));
  1257. return $run;
  1258. }
  1259. /**
  1260. * eatKanji
  1261. * @return int run
  1262. */
  1263. protected function eatKanji() {
  1264. $p = 0;
  1265. while($this->identifyMode($p) == QR_MODE_KJ) {
  1266. $p += 2;
  1267. }
  1268. $this->items = $this->appendNewInputItem($this->items, QR_MODE_KJ, $p, str_split($this->dataStr));
  1269. return $run;
  1270. }
  1271. /**
  1272. * eat8
  1273. * @return int run
  1274. */
  1275. protected function eat8() {
  1276. $la = $this->lengthIndicator(QR_MODE_AN, $this->version);
  1277. $ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
  1278. $p = 1;
  1279. $dataStrLen = strlen($this->dataStr);
  1280. while($p < $dataStrLen) {
  1281. $mode = $this->identifyMode($p);
  1282. if ($mode == QR_MODE_KJ) {
  1283. break;
  1284. }
  1285. if ($mode == QR_MODE_NM) {
  1286. $q = $p;
  1287. while($this->isdigitat($this->dataStr, $q)) {
  1288. $q++;
  1289. }
  1290. $dif = $this->estimateBitsMode8($p) // + 4 + l8
  1291. + $this->estimateBitsModeNum($q - $p) + 4 + $ln
  1292. - $this->estimateBitsMode8($q); // - 4 - l8
  1293. if ($dif < 0) {
  1294. break;
  1295. } else {
  1296. $p = $q;
  1297. }
  1298. } elseif ($mode == QR_MODE_AN) {
  1299. $q = $p;
  1300. while($this->isalnumat($this->dataStr, $q)) {
  1301. $q++;
  1302. }
  1303. $dif = $this->estimateBitsMode8($p) // + 4 + l8
  1304. + $this->estimateBitsModeAn($q - $p) + 4 + $la
  1305. - $this->estimateBitsMode8($q); // - 4 - l8
  1306. if ($dif < 0) {
  1307. break;
  1308. } else {
  1309. $p = $q;
  1310. }
  1311. } else {
  1312. $p++;
  1313. }
  1314. }
  1315. $run = $p;
  1316. $this->items = $this->appendNewInputItem($this->items, QR_MODE_8B, $run, str_split($this->dataStr));
  1317. return $run;
  1318. }
  1319. /**
  1320. * splitString
  1321. */
  1322. protected function splitString() {
  1323. while (strlen($this->dataStr) > 0) {
  1324. if ($this->dataStr == '') {
  1325. return 0;
  1326. }
  1327. $mode = $this->identifyMode(0);
  1328. switch ($mode) {
  1329. case QR_MODE_NM: {
  1330. $length = $this->eatNum();
  1331. break;
  1332. }
  1333. case QR_MODE_AN: {
  1334. $length = $this->eatAn();
  1335. break;
  1336. }
  1337. case QR_MODE_KJ: {
  1338. if ($hint == QR_MODE_KJ) {
  1339. $length = $this->eatKanji();
  1340. } else {
  1341. $length = $this->eat8();
  1342. }
  1343. break;
  1344. }
  1345. default: {
  1346. $length = $this->eat8();
  1347. break;
  1348. }
  1349. }
  1350. if ($length == 0) {
  1351. return 0;
  1352. }
  1353. if ($length < 0) {
  1354. return -1;
  1355. }
  1356. $this->dataStr = substr($this->dataStr, $length);
  1357. }
  1358. }
  1359. /**
  1360. * toUpper
  1361. */
  1362. protected function toUpper() {
  1363. $stringLen = strlen($this->dataStr);
  1364. $p = 0;
  1365. while ($p < $stringLen) {
  1366. $mode = $this->identifyMode(substr($this->dataStr, $p), $this->hint);
  1367. if ($mode == QR_MODE_KJ) {
  1368. $p += 2;
  1369. } else {
  1370. if ((ord($this->dataStr[$p]) >= ord('a')) AND (ord($this->dataStr[$p]) <= ord('z'))) {
  1371. $this->dataStr[$p] = chr(ord($this->dataStr[$p]) - 32);
  1372. }
  1373. $p++;
  1374. }
  1375. }
  1376. return $this->dataStr;
  1377. }
  1378. // - - - - - - - - - - - - - - - - - - - - - - - - -
  1379. // QRinputItem
  1380. /**
  1381. * newInputItem
  1382. * @param int $mode
  1383. * @param int $size
  1384. * @param array $data
  1385. * @param array $bstream
  1386. * @return array input item
  1387. */
  1388. protected function newInputItem($mode, $size, $data, $bstream=null) {
  1389. $setData = array_slice($data, 0, $size);
  1390. if (count($setData) < $size) {
  1391. $setData = array_merge($setData, array_fill(0, ($size - count($setData)), 0));
  1392. }
  1393. if (!$this->check($mode, $size, $setData)) {
  1394. return NULL;
  1395. }
  1396. $inputitem = array();
  1397. $inputitem['mode'] = $mode;
  1398. $inputitem['size'] = $size;
  1399. $inputitem['data'] = $setData;
  1400. $inputitem['bstream'] = $bstream;
  1401. return $inputitem;
  1402. }
  1403. /**
  1404. * encodeModeNum
  1405. * @param array $inputitem
  1406. * @param int $version
  1407. * @return array input item
  1408. */
  1409. protected function encodeModeNum($inputitem, $version) {
  1410. $words = (int)($inputitem['size'] / 3);
  1411. $inputitem['bstream'] = array();
  1412. $val = 0x1;
  1413. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val);
  1414. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_NM, $version), $inputitem['size']);
  1415. for ($i=0; $i < $words; ++$i) {
  1416. $val = (ord($inputitem['data'][$i*3 ]) - ord('0')) * 100;
  1417. $val += (ord($inputitem['data'][$i*3+1]) - ord('0')) * 10;
  1418. $val += (ord($inputitem['data'][$i*3+2]) - ord('0'));
  1419. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 10, $val);
  1420. }
  1421. if ($inputitem['size'] - $words * 3 == 1) {
  1422. $val = ord($inputitem['data'][$words*3]) - ord('0');
  1423. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val);
  1424. } elseif (($inputitem['size'] - ($words * 3)) == 2) {
  1425. $val = (ord($inputitem['data'][$words*3 ]) - ord('0')) * 10;
  1426. $val += (ord($inputitem['data'][$words*3+1]) - ord('0'));
  1427. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 7, $val);
  1428. }
  1429. return $inputitem;
  1430. }
  1431. /**
  1432. * encodeModeAn
  1433. * @param array $inputitem
  1434. * @param int $version
  1435. * @return array input item
  1436. */
  1437. protected function encodeModeAn($inputitem, $version) {
  1438. $words = (int)($inputitem['size'] / 2);
  1439. $inputitem['bstream'] = array();
  1440. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x02);
  1441. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_AN, $version), $inputitem['size']);
  1442. for ($i=0; $i < $words; ++$i) {
  1443. $val = (int)$this->lookAnTable(ord($inputitem['data'][$i*2 ])) * 45;
  1444. $val += (int)$this->lookAnTable(ord($inputitem['data'][$i*2+1]));
  1445. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 11, $val);
  1446. }
  1447. if ($inputitem['size'] & 1) {
  1448. $val = $this->lookAnTable(ord($inputitem['data'][($words * 2)]));
  1449. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 6, $val);
  1450. }
  1451. return $inputitem;
  1452. }
  1453. /**
  1454. * encodeMode8
  1455. * @param array $inputitem
  1456. * @param int $version
  1457. * @return array input item
  1458. */
  1459. protected function encodeMode8($inputitem, $version) {
  1460. $inputitem['bstream'] = array();
  1461. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x4);
  1462. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_8B, $version), $inputitem['size']);
  1463. for ($i=0; $i < $inputitem['size']; ++$i) {
  1464. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, ord($inputitem['data'][$i]));
  1465. }
  1466. return $inputitem;
  1467. }
  1468. /**
  1469. * encodeModeKanji
  1470. * @param array $inputitem
  1471. * @param int $version
  1472. * @return array input item
  1473. */
  1474. protected function encodeModeKanji($inputitem, $version) {
  1475. $inputitem['bstream'] = array();
  1476. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x8);
  1477. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_KJ, $version), (int)($inputitem['size'] / 2));
  1478. for ($i=0; $i<$inputitem['size']; $i+=2) {
  1479. $val = (ord($inputitem['data'][$i]) << 8) | ord($inputitem['data'][$i+1]);
  1480. if ($val <= 0x9ffc) {
  1481. $val -= 0x8140;
  1482. } else {
  1483. $val -= 0xc140;
  1484. }
  1485. $h = ($val >> 8) * 0xc0;
  1486. $val = ($val & 0xff) + $h;
  1487. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 13, $val);
  1488. }
  1489. return $inputitem;
  1490. }
  1491. /**
  1492. * encodeModeStructure
  1493. * @param array $inputitem
  1494. * @return array input item
  1495. */
  1496. protected function encodeModeStructure($inputitem) {
  1497. $inputitem['bstream'] = array();
  1498. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x03);
  1499. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, ord($inputitem['data'][1]) - 1);
  1500. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, ord($inputitem['data'][0]) - 1);
  1501. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, ord($inputitem['data'][2]));
  1502. return $inputitem;
  1503. }
  1504. /**
  1505. * encodeBitStream
  1506. * @param array $inputitem
  1507. * @param int $version
  1508. * @return array input item
  1509. */
  1510. protected function encodeBitStream($inputitem, $version) {
  1511. $inputitem['bstream']

Large files files are truncated, but you can click here to view the full file