QRMatrix.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 function array_fill, array_key_exists, array_push, array_unshift, count, floor, in_array, max, min, range;
  15. /**
  16. * @link http://www.thonky.com/qr-code-tutorial/format-version-information
  17. */
  18. class QRMatrix{
  19. public const M_NULL = 0x00;
  20. public const M_DARKMODULE = 0x02;
  21. public const M_DATA = 0x04;
  22. public const M_FINDER = 0x06;
  23. public const M_SEPARATOR = 0x08;
  24. public const M_ALIGNMENT = 0x0a;
  25. public const M_TIMING = 0x0c;
  26. public const M_FORMAT = 0x0e;
  27. public const M_VERSION = 0x10;
  28. public const M_QUIETZONE = 0x12;
  29. public const M_LOGO = 0x14; // @todo
  30. public const M_TEST = 0xff;
  31. /**
  32. * @link http://www.thonky.com/qr-code-tutorial/alignment-pattern-locations
  33. *
  34. * version -> pattern
  35. */
  36. protected const alignmentPattern = [
  37. 1 => [],
  38. 2 => [6, 18],
  39. 3 => [6, 22],
  40. 4 => [6, 26],
  41. 5 => [6, 30],
  42. 6 => [6, 34],
  43. 7 => [6, 22, 38],
  44. 8 => [6, 24, 42],
  45. 9 => [6, 26, 46],
  46. 10 => [6, 28, 50],
  47. 11 => [6, 30, 54],
  48. 12 => [6, 32, 58],
  49. 13 => [6, 34, 62],
  50. 14 => [6, 26, 46, 66],
  51. 15 => [6, 26, 48, 70],
  52. 16 => [6, 26, 50, 74],
  53. 17 => [6, 30, 54, 78],
  54. 18 => [6, 30, 56, 82],
  55. 19 => [6, 30, 58, 86],
  56. 20 => [6, 34, 62, 90],
  57. 21 => [6, 28, 50, 72, 94],
  58. 22 => [6, 26, 50, 74, 98],
  59. 23 => [6, 30, 54, 78, 102],
  60. 24 => [6, 28, 54, 80, 106],
  61. 25 => [6, 32, 58, 84, 110],
  62. 26 => [6, 30, 58, 86, 114],
  63. 27 => [6, 34, 62, 90, 118],
  64. 28 => [6, 26, 50, 74, 98, 122],
  65. 29 => [6, 30, 54, 78, 102, 126],
  66. 30 => [6, 26, 52, 78, 104, 130],
  67. 31 => [6, 30, 56, 82, 108, 134],
  68. 32 => [6, 34, 60, 86, 112, 138],
  69. 33 => [6, 30, 58, 86, 114, 142],
  70. 34 => [6, 34, 62, 90, 118, 146],
  71. 35 => [6, 30, 54, 78, 102, 126, 150],
  72. 36 => [6, 24, 50, 76, 102, 128, 154],
  73. 37 => [6, 28, 54, 80, 106, 132, 158],
  74. 38 => [6, 32, 58, 84, 110, 136, 162],
  75. 39 => [6, 26, 54, 82, 110, 138, 166],
  76. 40 => [6, 30, 58, 86, 114, 142, 170],
  77. ];
  78. /**
  79. * @link http://www.thonky.com/qr-code-tutorial/format-version-tables
  80. *
  81. * no version pattern for QR Codes < 7
  82. */
  83. protected const versionPattern = [
  84. 7 => 0b000111110010010100,
  85. 8 => 0b001000010110111100,
  86. 9 => 0b001001101010011001,
  87. 10 => 0b001010010011010011,
  88. 11 => 0b001011101111110110,
  89. 12 => 0b001100011101100010,
  90. 13 => 0b001101100001000111,
  91. 14 => 0b001110011000001101,
  92. 15 => 0b001111100100101000,
  93. 16 => 0b010000101101111000,
  94. 17 => 0b010001010001011101,
  95. 18 => 0b010010101000010111,
  96. 19 => 0b010011010100110010,
  97. 20 => 0b010100100110100110,
  98. 21 => 0b010101011010000011,
  99. 22 => 0b010110100011001001,
  100. 23 => 0b010111011111101100,
  101. 24 => 0b011000111011000100,
  102. 25 => 0b011001000111100001,
  103. 26 => 0b011010111110101011,
  104. 27 => 0b011011000010001110,
  105. 28 => 0b011100110000011010,
  106. 29 => 0b011101001100111111,
  107. 30 => 0b011110110101110101,
  108. 31 => 0b011111001001010000,
  109. 32 => 0b100000100111010101,
  110. 33 => 0b100001011011110000,
  111. 34 => 0b100010100010111010,
  112. 35 => 0b100011011110011111,
  113. 36 => 0b100100101100001011,
  114. 37 => 0b100101010000101110,
  115. 38 => 0b100110101001100100,
  116. 39 => 0b100111010101000001,
  117. 40 => 0b101000110001101001,
  118. ];
  119. // ECC level -> mask pattern
  120. protected const formatPattern = [
  121. [ // L
  122. 0b111011111000100,
  123. 0b111001011110011,
  124. 0b111110110101010,
  125. 0b111100010011101,
  126. 0b110011000101111,
  127. 0b110001100011000,
  128. 0b110110001000001,
  129. 0b110100101110110,
  130. ],
  131. [ // M
  132. 0b101010000010010,
  133. 0b101000100100101,
  134. 0b101111001111100,
  135. 0b101101101001011,
  136. 0b100010111111001,
  137. 0b100000011001110,
  138. 0b100111110010111,
  139. 0b100101010100000,
  140. ],
  141. [ // Q
  142. 0b011010101011111,
  143. 0b011000001101000,
  144. 0b011111100110001,
  145. 0b011101000000110,
  146. 0b010010010110100,
  147. 0b010000110000011,
  148. 0b010111011011010,
  149. 0b010101111101101,
  150. ],
  151. [ // H
  152. 0b001011010001001,
  153. 0b001001110111110,
  154. 0b001110011100111,
  155. 0b001100111010000,
  156. 0b000011101100010,
  157. 0b000001001010101,
  158. 0b000110100001100,
  159. 0b000100000111011,
  160. ],
  161. ];
  162. /**
  163. * @var int
  164. */
  165. protected $version;
  166. /**
  167. * @var int
  168. */
  169. protected $eclevel;
  170. /**
  171. * @var int
  172. */
  173. protected $maskPattern = QRCode::MASK_PATTERN_AUTO;
  174. /**
  175. * @var int
  176. */
  177. protected $moduleCount;
  178. /**
  179. * @var mixed[]
  180. */
  181. protected $matrix;
  182. /**
  183. * QRMatrix constructor.
  184. *
  185. * @param int $version
  186. * @param int $eclevel
  187. *
  188. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  189. */
  190. public function __construct(int $version, int $eclevel){
  191. if(!in_array($version, range(1, 40), true)){
  192. throw new QRCodeDataException('invalid QR Code version');
  193. }
  194. if(!array_key_exists($eclevel, QRCode::ECC_MODES)){
  195. throw new QRCodeDataException('invalid ecc level');
  196. }
  197. $this->version = $version;
  198. $this->eclevel = $eclevel;
  199. $this->moduleCount = $this->version * 4 + 17;
  200. $this->matrix = array_fill(0, $this->moduleCount, array_fill(0, $this->moduleCount, $this::M_NULL));
  201. }
  202. /**
  203. * @return array
  204. */
  205. public function matrix():array{
  206. return $this->matrix;
  207. }
  208. /**
  209. * @return int
  210. */
  211. public function version():int{
  212. return $this->version;
  213. }
  214. /**
  215. * @return int
  216. */
  217. public function eccLevel():int{
  218. return $this->eclevel;
  219. }
  220. /**
  221. * @return int
  222. */
  223. public function maskPattern():int{
  224. return $this->maskPattern;
  225. }
  226. /**
  227. * Returns the absoulute size of the matrix, including quiet zone (after setting it).
  228. *
  229. * size = version * 4 + 17 [ + 2 * quietzone size]
  230. *
  231. * @return int
  232. */
  233. public function size():int{
  234. return $this->moduleCount;
  235. }
  236. /**
  237. * Returns the value of the module at position [$x, $y]
  238. *
  239. * @param int $x
  240. * @param int $y
  241. *
  242. * @return int
  243. */
  244. public function get(int $x, int $y):int{
  245. return $this->matrix[$y][$x];
  246. }
  247. /**
  248. * Sets the $M_TYPE value for the module at position [$x, $y]
  249. *
  250. * true => $M_TYPE << 8
  251. * false => $M_TYPE
  252. *
  253. * @param int $x
  254. * @param int $y
  255. * @param int $M_TYPE
  256. * @param bool $value
  257. *
  258. * @return \chillerlan\QRCode\Data\QRMatrix
  259. */
  260. public function set(int $x, int $y, bool $value, int $M_TYPE):QRMatrix{
  261. $this->matrix[$y][$x] = $M_TYPE << ($value ? 8 : 0);
  262. return $this;
  263. }
  264. /**
  265. * Checks whether a module is true (dark) or false (light)
  266. *
  267. * true => $value >> 8 === $M_TYPE
  268. * $value >> 8 > 0
  269. *
  270. * false => $value === $M_TYPE
  271. * $value >> 8 === 0
  272. *
  273. * @param int $x
  274. * @param int $y
  275. *
  276. * @return bool
  277. */
  278. public function check(int $x, int $y):bool{
  279. return $this->matrix[$y][$x] >> 8 > 0;
  280. }
  281. /**
  282. * Sets the "dark module", that is always on the same position 1x1px away from the bottom left finder
  283. *
  284. * @return \chillerlan\QRCode\Data\QRMatrix
  285. */
  286. public function setDarkModule():QRMatrix{
  287. $this->set(8, 4 * $this->version + 9, true, $this::M_DARKMODULE);
  288. return $this;
  289. }
  290. /**
  291. * Draws the 7x7 finder patterns in the corners top left/right and bottom left
  292. *
  293. * @return \chillerlan\QRCode\Data\QRMatrix
  294. */
  295. public function setFinderPattern():QRMatrix{
  296. $pos = [
  297. [0, 0], // top left
  298. [$this->moduleCount - 7, 0], // bottom left
  299. [0, $this->moduleCount - 7], // top right
  300. ];
  301. foreach($pos as $c){
  302. for($y = 0; $y < 7; $y++){
  303. for($x = 0; $x < 7; $x++){
  304. $this->set(
  305. $c[0] + $y,
  306. $c[1] + $x,
  307. !(($x > 0 && $x < 6 && ($y === 1 || $y === 5)) || ($y > 0 && $y < 6 && ($x === 1 || $x === 5))),
  308. $this::M_FINDER
  309. );
  310. }
  311. }
  312. }
  313. return $this;
  314. }
  315. /**
  316. * Draws the separator lines around the finder patterns
  317. *
  318. * @return \chillerlan\QRCode\Data\QRMatrix
  319. */
  320. public function setSeparators():QRMatrix{
  321. $h = [
  322. [7, 0],
  323. [$this->moduleCount - 8, 0],
  324. [7, $this->moduleCount - 8],
  325. ];
  326. $v = [
  327. [7, 7],
  328. [$this->moduleCount - 1, 7],
  329. [7, $this->moduleCount - 8],
  330. ];
  331. for($c = 0; $c < 3; $c++){
  332. for($i = 0; $i < 8; $i++){
  333. $this->set($h[$c][0] , $h[$c][1] + $i, false, $this::M_SEPARATOR);
  334. $this->set($v[$c][0] - $i, $v[$c][1] , false, $this::M_SEPARATOR);
  335. }
  336. }
  337. return $this;
  338. }
  339. /**
  340. * Draws the 5x5 alignment patterns
  341. *
  342. * @return \chillerlan\QRCode\Data\QRMatrix
  343. */
  344. public function setAlignmentPattern():QRMatrix{
  345. foreach($this::alignmentPattern[$this->version] as $y){
  346. foreach($this::alignmentPattern[$this->version] as $x){
  347. // skip existing patterns
  348. if($this->matrix[$y][$x] !== $this::M_NULL){
  349. continue;
  350. }
  351. for($ry = -2; $ry <= 2; $ry++){
  352. for($rx = -2; $rx <= 2; $rx++){
  353. $v = ($ry === 0 && $rx === 0) || $ry === 2 || $ry === -2 || $rx === 2 || $rx === -2;
  354. $this->set($x + $rx, $y + $ry, $v, $this::M_ALIGNMENT);
  355. }
  356. }
  357. }
  358. }
  359. return $this;
  360. }
  361. /**
  362. * Draws the timing pattern (h/v checkered line between the finder patterns)
  363. *
  364. * @return \chillerlan\QRCode\Data\QRMatrix
  365. */
  366. public function setTimingPattern():QRMatrix{
  367. foreach(range(8, $this->moduleCount - 8 - 1) as $i){
  368. if($this->matrix[6][$i] !== $this::M_NULL || $this->matrix[$i][6] !== $this::M_NULL){
  369. continue;
  370. }
  371. $v = $i % 2 === 0;
  372. $this->set($i, 6, $v, $this::M_TIMING); // h
  373. $this->set(6, $i, $v, $this::M_TIMING); // v
  374. }
  375. return $this;
  376. }
  377. /**
  378. * Draws the version information, 2x 3x6 pixel
  379. *
  380. * @param bool|null $test
  381. *
  382. * @return \chillerlan\QRCode\Data\QRMatrix
  383. */
  384. public function setVersionNumber(bool $test = null):QRMatrix{
  385. $bits = $this::versionPattern[$this->version] ?? false;
  386. if($bits !== false){
  387. for($i = 0; $i < 18; $i++){
  388. $a = (int)floor($i / 3);
  389. $b = $i % 3 + $this->moduleCount - 8 - 3;
  390. $v = !$test && (($bits >> $i) & 1) === 1;
  391. $this->set($b, $a, $v, $this::M_VERSION); // ne
  392. $this->set($a, $b, $v, $this::M_VERSION); // sw
  393. }
  394. }
  395. return $this;
  396. }
  397. /**
  398. * Draws the format info along the finder patterns
  399. *
  400. * @param int $maskPattern
  401. * @param bool|null $test
  402. *
  403. * @return \chillerlan\QRCode\Data\QRMatrix
  404. */
  405. public function setFormatInfo(int $maskPattern, bool $test = null):QRMatrix{
  406. $bits = $this::formatPattern[QRCode::ECC_MODES[$this->eclevel]][$maskPattern] ?? 0;
  407. for($i = 0; $i < 15; $i++){
  408. $v = !$test && (($bits >> $i) & 1) === 1;
  409. if($i < 6){
  410. $this->set(8, $i, $v, $this::M_FORMAT);
  411. }
  412. elseif($i < 8){
  413. $this->set(8, $i + 1, $v, $this::M_FORMAT);
  414. }
  415. else{
  416. $this->set(8, $this->moduleCount - 15 + $i, $v, $this::M_FORMAT);
  417. }
  418. if($i < 8){
  419. $this->set($this->moduleCount - $i - 1, 8, $v, $this::M_FORMAT);
  420. }
  421. elseif($i < 9){
  422. $this->set(15 - $i, 8, $v, $this::M_FORMAT);
  423. }
  424. else{
  425. $this->set(15 - $i - 1, 8, $v, $this::M_FORMAT);
  426. }
  427. }
  428. $this->set(8, $this->moduleCount - 8, !$test, $this::M_FORMAT);
  429. return $this;
  430. }
  431. /**
  432. * Draws the "quiet zone" of $size around the matrix
  433. *
  434. * @param int|null $size
  435. *
  436. * @return \chillerlan\QRCode\Data\QRMatrix
  437. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  438. */
  439. public function setQuietZone(int $size = null):QRMatrix{
  440. if($this->matrix[$this->moduleCount - 1][$this->moduleCount - 1] === $this::M_NULL){
  441. throw new QRCodeDataException('use only after writing data');
  442. }
  443. $size = $size !== null
  444. ? max(0, min($size, floor($this->moduleCount / 2)))
  445. : 4;
  446. for($y = 0; $y < $this->moduleCount; $y++){
  447. for($i = 0; $i < $size; $i++){
  448. array_unshift($this->matrix[$y], $this::M_QUIETZONE);
  449. array_push($this->matrix[$y], $this::M_QUIETZONE);
  450. }
  451. }
  452. $this->moduleCount += ($size * 2);
  453. $r = array_fill(0, $this->moduleCount, $this::M_QUIETZONE);
  454. for($i = 0; $i < $size; $i++){
  455. array_unshift($this->matrix, $r);
  456. array_push($this->matrix, $r);
  457. }
  458. return $this;
  459. }
  460. /**
  461. * Maps the binary $data array from QRDataInterface::maskECC() on the matrix, using $maskPattern
  462. *
  463. * @see \chillerlan\QRCode\Data\QRDataAbstract::maskECC()
  464. *
  465. * @param int[] $data
  466. * @param int $maskPattern
  467. *
  468. * @return \chillerlan\QRCode\Data\QRMatrix
  469. */
  470. public function mapData(array $data, int $maskPattern):QRMatrix{
  471. $this->maskPattern = $maskPattern;
  472. $byteCount = count($data);
  473. $size = $this->moduleCount - 1;
  474. for($i = $size, $y = $size, $inc = -1, $byteIndex = 0, $bitIndex = 7; $i > 0; $i -= 2){
  475. if($i === 6){
  476. $i--;
  477. }
  478. while(true){
  479. for($c = 0; $c < 2; $c++){
  480. $x = $i - $c;
  481. if($this->matrix[$y][$x] === $this::M_NULL){
  482. $v = false;
  483. if($byteIndex < $byteCount){
  484. $v = (($data[$byteIndex] >> $bitIndex) & 1) === 1;
  485. }
  486. if($this->getMask($x, $y, $maskPattern) === 0){
  487. $v = !$v;
  488. }
  489. $this->matrix[$y][$x] = $this::M_DATA << ($v ? 8 : 0);
  490. $bitIndex--;
  491. if($bitIndex === -1){
  492. $byteIndex++;
  493. $bitIndex = 7;
  494. }
  495. }
  496. }
  497. $y += $inc;
  498. if($y < 0 || $this->moduleCount <= $y){
  499. $y -= $inc;
  500. $inc = -$inc;
  501. break;
  502. }
  503. }
  504. }
  505. return $this;
  506. }
  507. /**
  508. * @see \chillerlan\QRCode\QRMatrix::mapData()
  509. *
  510. * @internal
  511. *
  512. * @param int $x
  513. * @param int $y
  514. * @param int $maskPattern
  515. *
  516. * @return int
  517. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  518. */
  519. protected function getMask(int $x, int $y, int $maskPattern):int{
  520. $a = $y + $x;
  521. $m = $y * $x;
  522. if($maskPattern >= 0 && $maskPattern < 8){
  523. // this is literally the same as the stupid switch...
  524. return [
  525. $a % 2,
  526. $y % 2,
  527. $x % 3,
  528. $a % 3,
  529. (floor($y / 2) + floor($x / 3)) % 2,
  530. $m % 2 + $m % 3,
  531. ($m % 2 + $m % 3) % 2,
  532. ($m % 3 + $a % 2) % 2
  533. ][$maskPattern];
  534. }
  535. throw new QRCodeDataException('invalid mask pattern'); // @codeCoverageIgnore
  536. }
  537. }