QRMatrix.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. <?php
  2. /**
  3. * Class QRMatrix
  4. *
  5. * @filesource QRMatrix.php
  6. * @created 15.11.2017
  7. * @package chillerlan\QRCode\Data
  8. * @author Smiley <smiley@chillerlan.net>
  9. * @copyright 2017 Smiley
  10. * @license MIT
  11. */
  12. namespace chillerlan\QRCode\Data;
  13. use chillerlan\QRCode\QRCode;
  14. use Closure;
  15. use function array_fill, array_key_exists, array_push, array_unshift, count, floor, in_array, max, min, range;
  16. /**
  17. * Holds a numerical representation of the final QR Code;
  18. * maps the ECC coded binary data and applies the mask pattern
  19. *
  20. * @see http://www.thonky.com/qr-code-tutorial/format-version-information
  21. */
  22. final class QRMatrix{
  23. /*
  24. * special values
  25. */
  26. /** @var int */
  27. public const M_NULL = 0x00;
  28. /** @var int */
  29. public const M_LOGO = 0x14;
  30. /** @var int */
  31. public const M_LOGO_DARK = self::M_LOGO << 8;
  32. /*
  33. * light values
  34. */
  35. /** @var int */
  36. public const M_DATA = 0x04;
  37. /** @var int */
  38. public const M_FINDER = 0x06;
  39. /** @var int */
  40. public const M_SEPARATOR = 0x08;
  41. /** @var int */
  42. public const M_ALIGNMENT = 0x0a;
  43. /** @var int */
  44. public const M_TIMING = 0x0c;
  45. /** @var int */
  46. public const M_FORMAT = 0x0e;
  47. /** @var int */
  48. public const M_VERSION = 0x10;
  49. /** @var int */
  50. public const M_QUIETZONE = 0x12;
  51. /*
  52. * dark values
  53. */
  54. /** @var int */
  55. public const M_DARKMODULE = self::M_DARKMODULE_LIGHT << 8;
  56. /** @var int */
  57. public const M_DATA_DARK = self::M_DATA << 8;
  58. /** @var int */
  59. public const M_FINDER_DARK = self::M_FINDER << 8;
  60. /** @var int */
  61. public const M_ALIGNMENT_DARK = self::M_ALIGNMENT << 8;
  62. /** @var int */
  63. public const M_TIMING_DARK = self::M_TIMING << 8;
  64. /** @var int */
  65. public const M_FORMAT_DARK = self::M_FORMAT << 8;
  66. /** @var int */
  67. public const M_VERSION_DARK = self::M_VERSION << 8;
  68. /** @var int */
  69. public const M_FINDER_DOT = self::M_FINDER_DOT_LIGHT << 8;
  70. /*
  71. * values used for reversed reflectance
  72. */
  73. /** @var int */
  74. public const M_DARKMODULE_LIGHT = 0x02;
  75. /** @var int */
  76. public const M_FINDER_DOT_LIGHT = 0x16;
  77. /** @var int */
  78. public const M_SEPARATOR_DARK = self::M_SEPARATOR << 8;
  79. /** @var int */
  80. public const M_QUIETZONE_DARK = self::M_QUIETZONE << 8;
  81. /**
  82. * ISO/IEC 18004:2000 Annex E, Table E.1 - Row/column coordinates of center module of Alignment Patterns
  83. *
  84. * version -> pattern
  85. *
  86. * @var int[][]
  87. */
  88. protected const alignmentPattern = [
  89. 1 => [],
  90. 2 => [6, 18],
  91. 3 => [6, 22],
  92. 4 => [6, 26],
  93. 5 => [6, 30],
  94. 6 => [6, 34],
  95. 7 => [6, 22, 38],
  96. 8 => [6, 24, 42],
  97. 9 => [6, 26, 46],
  98. 10 => [6, 28, 50],
  99. 11 => [6, 30, 54],
  100. 12 => [6, 32, 58],
  101. 13 => [6, 34, 62],
  102. 14 => [6, 26, 46, 66],
  103. 15 => [6, 26, 48, 70],
  104. 16 => [6, 26, 50, 74],
  105. 17 => [6, 30, 54, 78],
  106. 18 => [6, 30, 56, 82],
  107. 19 => [6, 30, 58, 86],
  108. 20 => [6, 34, 62, 90],
  109. 21 => [6, 28, 50, 72, 94],
  110. 22 => [6, 26, 50, 74, 98],
  111. 23 => [6, 30, 54, 78, 102],
  112. 24 => [6, 28, 54, 80, 106],
  113. 25 => [6, 32, 58, 84, 110],
  114. 26 => [6, 30, 58, 86, 114],
  115. 27 => [6, 34, 62, 90, 118],
  116. 28 => [6, 26, 50, 74, 98, 122],
  117. 29 => [6, 30, 54, 78, 102, 126],
  118. 30 => [6, 26, 52, 78, 104, 130],
  119. 31 => [6, 30, 56, 82, 108, 134],
  120. 32 => [6, 34, 60, 86, 112, 138],
  121. 33 => [6, 30, 58, 86, 114, 142],
  122. 34 => [6, 34, 62, 90, 118, 146],
  123. 35 => [6, 30, 54, 78, 102, 126, 150],
  124. 36 => [6, 24, 50, 76, 102, 128, 154],
  125. 37 => [6, 28, 54, 80, 106, 132, 158],
  126. 38 => [6, 32, 58, 84, 110, 136, 162],
  127. 39 => [6, 26, 54, 82, 110, 138, 166],
  128. 40 => [6, 30, 58, 86, 114, 142, 170],
  129. ];
  130. /**
  131. * ISO/IEC 18004:2000 Annex D, Table D.1 - Version information bit stream for each version
  132. *
  133. * no version pattern for QR Codes < 7
  134. *
  135. * @var int[]
  136. */
  137. protected const versionPattern = [
  138. 7 => 0b000111110010010100,
  139. 8 => 0b001000010110111100,
  140. 9 => 0b001001101010011001,
  141. 10 => 0b001010010011010011,
  142. 11 => 0b001011101111110110,
  143. 12 => 0b001100011101100010,
  144. 13 => 0b001101100001000111,
  145. 14 => 0b001110011000001101,
  146. 15 => 0b001111100100101000,
  147. 16 => 0b010000101101111000,
  148. 17 => 0b010001010001011101,
  149. 18 => 0b010010101000010111,
  150. 19 => 0b010011010100110010,
  151. 20 => 0b010100100110100110,
  152. 21 => 0b010101011010000011,
  153. 22 => 0b010110100011001001,
  154. 23 => 0b010111011111101100,
  155. 24 => 0b011000111011000100,
  156. 25 => 0b011001000111100001,
  157. 26 => 0b011010111110101011,
  158. 27 => 0b011011000010001110,
  159. 28 => 0b011100110000011010,
  160. 29 => 0b011101001100111111,
  161. 30 => 0b011110110101110101,
  162. 31 => 0b011111001001010000,
  163. 32 => 0b100000100111010101,
  164. 33 => 0b100001011011110000,
  165. 34 => 0b100010100010111010,
  166. 35 => 0b100011011110011111,
  167. 36 => 0b100100101100001011,
  168. 37 => 0b100101010000101110,
  169. 38 => 0b100110101001100100,
  170. 39 => 0b100111010101000001,
  171. 40 => 0b101000110001101001,
  172. ];
  173. /**
  174. * ISO/IEC 18004:2000 Section 8.9 - Format Information
  175. *
  176. * ECC level -> mask pattern
  177. *
  178. * @var int[][]
  179. */
  180. protected const formatPattern = [
  181. [ // L
  182. 0b111011111000100,
  183. 0b111001011110011,
  184. 0b111110110101010,
  185. 0b111100010011101,
  186. 0b110011000101111,
  187. 0b110001100011000,
  188. 0b110110001000001,
  189. 0b110100101110110,
  190. ],
  191. [ // M
  192. 0b101010000010010,
  193. 0b101000100100101,
  194. 0b101111001111100,
  195. 0b101101101001011,
  196. 0b100010111111001,
  197. 0b100000011001110,
  198. 0b100111110010111,
  199. 0b100101010100000,
  200. ],
  201. [ // Q
  202. 0b011010101011111,
  203. 0b011000001101000,
  204. 0b011111100110001,
  205. 0b011101000000110,
  206. 0b010010010110100,
  207. 0b010000110000011,
  208. 0b010111011011010,
  209. 0b010101111101101,
  210. ],
  211. [ // H
  212. 0b001011010001001,
  213. 0b001001110111110,
  214. 0b001110011100111,
  215. 0b001100111010000,
  216. 0b000011101100010,
  217. 0b000001001010101,
  218. 0b000110100001100,
  219. 0b000100000111011,
  220. ],
  221. ];
  222. /**
  223. * the current QR Code version number
  224. */
  225. protected int $version;
  226. /**
  227. * the current ECC level
  228. */
  229. protected int $eclevel;
  230. /**
  231. * the used mask pattern, set via QRMatrix::mapData()
  232. */
  233. protected int $maskPattern = QRCode::MASK_PATTERN_AUTO;
  234. /**
  235. * the size (side length) of the matrix
  236. */
  237. protected int $moduleCount;
  238. /**
  239. * the actual matrix data array
  240. *
  241. * @var int[][]
  242. */
  243. protected array $matrix;
  244. /**
  245. * QRMatrix constructor.
  246. *
  247. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  248. */
  249. public function __construct(int $version, int $eclevel){
  250. if(!in_array($version, range(1, 40), true)){
  251. throw new QRCodeDataException('invalid QR Code version');
  252. }
  253. if(!array_key_exists($eclevel, QRCode::ECC_MODES)){
  254. throw new QRCodeDataException('invalid ecc level');
  255. }
  256. $this->version = $version;
  257. $this->eclevel = $eclevel;
  258. $this->moduleCount = $this->version * 4 + 17;
  259. $this->matrix = array_fill(0, $this->moduleCount, array_fill(0, $this->moduleCount, $this::M_NULL));
  260. }
  261. /**
  262. * shortcut to initialize the matrix
  263. */
  264. public function init(int $maskPattern, bool $test = null):QRMatrix{
  265. return $this
  266. ->setFinderPattern()
  267. ->setSeparators()
  268. ->setAlignmentPattern()
  269. ->setTimingPattern()
  270. ->setVersionNumber($test)
  271. ->setFormatInfo($maskPattern, $test)
  272. ->setDarkModule()
  273. ;
  274. }
  275. /**
  276. * Returns the data matrix, returns a pure boolean representation if $boolean is set to true
  277. *
  278. * @return int[][]|bool[][]
  279. */
  280. public function matrix(bool $boolean = false):array{
  281. if(!$boolean){
  282. return $this->matrix;
  283. }
  284. $matrix = [];
  285. foreach($this->matrix as $y => $row){
  286. $matrix[$y] = [];
  287. foreach($row as $x => $val){
  288. $matrix[$y][$x] = ($val >> 8) > 0;
  289. }
  290. }
  291. return $matrix;
  292. }
  293. /**
  294. * Returns the current version number
  295. */
  296. public function version():int{
  297. return $this->version;
  298. }
  299. /**
  300. * Returns the current ECC level
  301. */
  302. public function eccLevel():int{
  303. return $this->eclevel;
  304. }
  305. /**
  306. * Returns the current mask pattern
  307. */
  308. public function maskPattern():int{
  309. return $this->maskPattern;
  310. }
  311. /**
  312. * Returns the absoulute size of the matrix, including quiet zone (after setting it).
  313. *
  314. * size = version * 4 + 17 [ + 2 * quietzone size]
  315. */
  316. public function size():int{
  317. return $this->moduleCount;
  318. }
  319. /**
  320. * Returns the value of the module at position [$x, $y]
  321. */
  322. public function get(int $x, int $y):int{
  323. return $this->matrix[$y][$x];
  324. }
  325. /**
  326. * Sets the $M_TYPE value for the module at position [$x, $y]
  327. *
  328. * true => $M_TYPE << 8
  329. * false => $M_TYPE
  330. */
  331. public function set(int $x, int $y, bool $value, int $M_TYPE):QRMatrix{
  332. $this->matrix[$y][$x] = $M_TYPE << ($value ? 8 : 0);
  333. return $this;
  334. }
  335. /**
  336. * Checks whether a module is true (dark) or false (light)
  337. *
  338. * true => $value >> 8 === $M_TYPE
  339. * $value >> 8 > 0
  340. *
  341. * false => $value === $M_TYPE
  342. * $value >> 8 === 0
  343. */
  344. public function check(int $x, int $y):bool{
  345. return ($this->matrix[$y][$x] >> 8) > 0;
  346. }
  347. /**
  348. * Sets the "dark module", that is always on the same position 1x1px away from the bottom left finder
  349. */
  350. public function setDarkModule():QRMatrix{
  351. $this->set(8, 4 * $this->version + 9, true, $this::M_DARKMODULE_LIGHT);
  352. return $this;
  353. }
  354. /**
  355. * Draws the 7x7 finder patterns in the corners top left/right and bottom left
  356. *
  357. * ISO/IEC 18004:2000 Section 7.3.2
  358. */
  359. public function setFinderPattern():QRMatrix{
  360. $pos = [
  361. [0, 0], // top left
  362. [$this->moduleCount - 7, 0], // top right
  363. [0, $this->moduleCount - 7], // bottom left
  364. ];
  365. foreach($pos as $c){
  366. for($y = 0; $y < 7; $y++){
  367. for($x = 0; $x < 7; $x++){
  368. // outer (dark) 7*7 square
  369. if($x === 0 || $x === 6 || $y === 0 || $y === 6){
  370. $this->set($c[0] + $y, $c[1] + $x, true, $this::M_FINDER);
  371. }
  372. // inner (light) 5*5 square
  373. elseif($x === 1 || $x === 5 || $y === 1 || $y === 5){
  374. $this->set($c[0] + $y, $c[1] + $x, false, $this::M_FINDER);
  375. }
  376. // 3*3 dot
  377. else{
  378. $this->set($c[0] + $y, $c[1] + $x, true, $this::M_FINDER_DOT_LIGHT);
  379. }
  380. }
  381. }
  382. }
  383. return $this;
  384. }
  385. /**
  386. * Draws the separator lines around the finder patterns
  387. *
  388. * ISO/IEC 18004:2000 Section 7.3.3
  389. */
  390. public function setSeparators():QRMatrix{
  391. $h = [
  392. [7, 0],
  393. [$this->moduleCount - 8, 0],
  394. [7, $this->moduleCount - 8],
  395. ];
  396. $v = [
  397. [7, 7],
  398. [$this->moduleCount - 1, 7],
  399. [7, $this->moduleCount - 8],
  400. ];
  401. for($c = 0; $c < 3; $c++){
  402. for($i = 0; $i < 8; $i++){
  403. $this->set($h[$c][0] , $h[$c][1] + $i, false, $this::M_SEPARATOR);
  404. $this->set($v[$c][0] - $i, $v[$c][1] , false, $this::M_SEPARATOR);
  405. }
  406. }
  407. return $this;
  408. }
  409. /**
  410. * Draws the 5x5 alignment patterns
  411. *
  412. * ISO/IEC 18004:2000 Section 7.3.5
  413. */
  414. public function setAlignmentPattern():QRMatrix{
  415. foreach($this::alignmentPattern[$this->version] as $y){
  416. foreach($this::alignmentPattern[$this->version] as $x){
  417. // skip existing patterns
  418. if($this->matrix[$y][$x] !== $this::M_NULL){
  419. continue;
  420. }
  421. for($ry = -2; $ry <= 2; $ry++){
  422. for($rx = -2; $rx <= 2; $rx++){
  423. $v = ($ry === 0 && $rx === 0) || $ry === 2 || $ry === -2 || $rx === 2 || $rx === -2;
  424. $this->set($x + $rx, $y + $ry, $v, $this::M_ALIGNMENT);
  425. }
  426. }
  427. }
  428. }
  429. return $this;
  430. }
  431. /**
  432. * Draws the timing pattern (h/v checkered line between the finder patterns)
  433. *
  434. * ISO/IEC 18004:2000 Section 7.3.4
  435. */
  436. public function setTimingPattern():QRMatrix{
  437. foreach(range(8, $this->moduleCount - 8 - 1) as $i){
  438. if($this->matrix[6][$i] !== $this::M_NULL || $this->matrix[$i][6] !== $this::M_NULL){
  439. continue;
  440. }
  441. $v = $i % 2 === 0;
  442. $this->set($i, 6, $v, $this::M_TIMING); // h
  443. $this->set(6, $i, $v, $this::M_TIMING); // v
  444. }
  445. return $this;
  446. }
  447. /**
  448. * Draws the version information, 2x 3x6 pixel
  449. *
  450. * ISO/IEC 18004:2000 Section 8.10
  451. */
  452. public function setVersionNumber(bool $test = null):QRMatrix{
  453. $bits = $this::versionPattern[$this->version] ?? false;
  454. if($bits !== false){
  455. for($i = 0; $i < 18; $i++){
  456. $a = (int)floor($i / 3);
  457. $b = $i % 3 + $this->moduleCount - 8 - 3;
  458. $v = !$test && (($bits >> $i) & 1) === 1;
  459. $this->set($b, $a, $v, $this::M_VERSION); // ne
  460. $this->set($a, $b, $v, $this::M_VERSION); // sw
  461. }
  462. }
  463. return $this;
  464. }
  465. /**
  466. * Draws the format info along the finder patterns
  467. *
  468. * ISO/IEC 18004:2000 Section 8.9
  469. */
  470. public function setFormatInfo(int $maskPattern, bool $test = null):QRMatrix{
  471. $bits = $this::formatPattern[QRCode::ECC_MODES[$this->eclevel]][$maskPattern] ?? 0;
  472. for($i = 0; $i < 15; $i++){
  473. $v = !$test && (($bits >> $i) & 1) === 1;
  474. if($i < 6){
  475. $this->set(8, $i, $v, $this::M_FORMAT);
  476. }
  477. elseif($i < 8){
  478. $this->set(8, $i + 1, $v, $this::M_FORMAT);
  479. }
  480. else{
  481. $this->set(8, $this->moduleCount - 15 + $i, $v, $this::M_FORMAT);
  482. }
  483. if($i < 8){
  484. $this->set($this->moduleCount - $i - 1, 8, $v, $this::M_FORMAT);
  485. }
  486. elseif($i < 9){
  487. $this->set(15 - $i, 8, $v, $this::M_FORMAT);
  488. }
  489. else{
  490. $this->set(15 - $i - 1, 8, $v, $this::M_FORMAT);
  491. }
  492. }
  493. $this->set(8, $this->moduleCount - 8, !$test, $this::M_FORMAT);
  494. return $this;
  495. }
  496. /**
  497. * Draws the "quiet zone" of $size around the matrix
  498. *
  499. * ISO/IEC 18004:2000 Section 7.3.7
  500. *
  501. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  502. */
  503. public function setQuietZone(int $size = null):QRMatrix{
  504. if($this->matrix[$this->moduleCount - 1][$this->moduleCount - 1] === $this::M_NULL){
  505. throw new QRCodeDataException('use only after writing data');
  506. }
  507. $size = $size !== null
  508. ? max(0, min($size, floor($this->moduleCount / 2)))
  509. : 4;
  510. for($y = 0; $y < $this->moduleCount; $y++){
  511. for($i = 0; $i < $size; $i++){
  512. array_unshift($this->matrix[$y], $this::M_QUIETZONE);
  513. array_push($this->matrix[$y], $this::M_QUIETZONE);
  514. }
  515. }
  516. $this->moduleCount += ($size * 2);
  517. $r = array_fill(0, $this->moduleCount, $this::M_QUIETZONE);
  518. for($i = 0; $i < $size; $i++){
  519. array_unshift($this->matrix, $r);
  520. array_push($this->matrix, $r);
  521. }
  522. return $this;
  523. }
  524. /**
  525. * Clears a space of $width * $height in order to add a logo or text.
  526. *
  527. * Additionally, the logo space can be positioned within the QR Code - respecting the main functional patterns -
  528. * using $startX and $startY. If either of these are null, the logo space will be centered in that direction.
  529. * ECC level "H" (30%) is required.
  530. *
  531. * Please note that adding a logo space minimizes the error correction capacity of the QR Code and
  532. * created images may become unreadable, especially when printed with a chance to receive damage.
  533. * Please test thoroughly before using this feature in production.
  534. *
  535. * This method should be called from within an output module (after the matrix has been filled with data).
  536. * Note that there is no restiction on how many times this method could be called on the same matrix instance.
  537. *
  538. * @link https://github.com/chillerlan/php-qrcode/issues/52
  539. *
  540. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  541. */
  542. public function setLogoSpace(int $width, int $height, int $startX = null, int $startY = null):QRMatrix{
  543. // for logos we operate in ECC H (30%) only
  544. if($this->eclevel !== QRCode::ECC_H){
  545. throw new QRCodeDataException('ECC level "H" required to add logo space');
  546. }
  547. // we need uneven sizes to center the logo space, adjust if needed
  548. if($startX === null && ($width % 2) === 0){
  549. $width++;
  550. }
  551. if($startY === null && ($height % 2) === 0){
  552. $height++;
  553. }
  554. // $this->moduleCount includes the quiet zone (if created), we need the QR size here
  555. $length = $this->version * 4 + 17;
  556. // throw if the logo space exceeds the maximum error correction capacity
  557. if($width * $height > floor($length * $length * 0.2)){
  558. throw new QRCodeDataException('logo space exceeds the maximum error correction capacity');
  559. }
  560. // quiet zone size
  561. $qz = ($this->moduleCount - $length) / 2;
  562. // skip quiet zone and the first 9 rows/columns (finder-, mode-, version- and timing patterns)
  563. $start = $qz + 9;
  564. // skip quiet zone
  565. $end = $this->moduleCount - $qz;
  566. // determine start coordinates
  567. $startX = ($startX !== null ? $startX : ($length - $width) / 2) + $qz;
  568. $startY = ($startY !== null ? $startY : ($length - $height) / 2) + $qz;
  569. // clear the space
  570. for($y = 0; $y < $this->moduleCount; $y++){
  571. for($x = 0; $x < $this->moduleCount; $x++){
  572. // out of bounds, skip
  573. if($x < $start || $y < $start ||$x >= $end || $y >= $end){
  574. continue;
  575. }
  576. // a match
  577. if($x >= $startX && $x < ($startX + $width) && $y >= $startY && $y < ($startY + $height)){
  578. $this->set($x, $y, false, $this::M_LOGO);
  579. }
  580. }
  581. }
  582. return $this;
  583. }
  584. /**
  585. * Maps the binary $data array from QRDataInterface::maskECC() on the matrix,
  586. * masking the data using $maskPattern (ISO/IEC 18004:2000 Section 8.8)
  587. *
  588. * @see \chillerlan\QRCode\Data\QRDataAbstract::maskECC()
  589. *
  590. * @param int[] $data
  591. * @param int $maskPattern
  592. *
  593. * @return \chillerlan\QRCode\Data\QRMatrix
  594. */
  595. public function mapData(array $data, int $maskPattern):QRMatrix{
  596. $this->maskPattern = $maskPattern;
  597. $byteCount = count($data);
  598. $y = $this->moduleCount - 1;
  599. $inc = -1;
  600. $byteIndex = 0;
  601. $bitIndex = 7;
  602. $mask = $this->getMask($this->maskPattern);
  603. for($i = $y; $i > 0; $i -= 2){
  604. if($i === 6){
  605. $i--;
  606. }
  607. while(true){
  608. for($c = 0; $c < 2; $c++){
  609. $x = $i - $c;
  610. if($this->matrix[$y][$x] === $this::M_NULL){
  611. $v = false;
  612. if($byteIndex < $byteCount){
  613. $v = (($data[$byteIndex] >> $bitIndex) & 1) === 1;
  614. }
  615. if($mask($x, $y) === 0){
  616. $v = !$v;
  617. }
  618. $this->matrix[$y][$x] = $this::M_DATA << ($v ? 8 : 0);
  619. $bitIndex--;
  620. if($bitIndex === -1){
  621. $byteIndex++;
  622. $bitIndex = 7;
  623. }
  624. }
  625. }
  626. $y += $inc;
  627. if($y < 0 || $this->moduleCount <= $y){
  628. $y -= $inc;
  629. $inc = -$inc;
  630. break;
  631. }
  632. }
  633. }
  634. return $this;
  635. }
  636. /**
  637. * ISO/IEC 18004:2000 Section 8.8.1
  638. *
  639. * Note that some versions of the QR code standard have had errors in the section about mask patterns.
  640. * The information below has been corrected. (https://www.thonky.com/qr-code-tutorial/mask-patterns)
  641. *
  642. * @see \chillerlan\QRCode\QRMatrix::mapData()
  643. *
  644. * @internal
  645. *
  646. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  647. */
  648. protected function getMask(int $maskPattern):Closure{
  649. if((0b111 & $maskPattern) !== $maskPattern){
  650. throw new QRCodeDataException('invalid mask pattern'); // @codeCoverageIgnore
  651. }
  652. return [
  653. 0b000 => fn($x, $y):int => ($x + $y) % 2,
  654. 0b001 => fn($x, $y):int => $y % 2,
  655. 0b010 => fn($x, $y):int => $x % 3,
  656. 0b011 => fn($x, $y):int => ($x + $y) % 3,
  657. 0b100 => fn($x, $y):int => ((int)($y / 2) + (int)($x / 3)) % 2,
  658. 0b101 => fn($x, $y):int => (($x * $y) % 2) + (($x * $y) % 3),
  659. 0b110 => fn($x, $y):int => ((($x * $y) % 2) + (($x * $y) % 3)) % 2,
  660. 0b111 => fn($x, $y):int => ((($x * $y) % 3) + (($x + $y) % 2)) % 2,
  661. ][$maskPattern];
  662. }
  663. }