QRMatrix.php 17 KB

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