QRMatrix.php 13 KB

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