Completed
Push — master ( 7599f8...fd679d )
by smiley
03:18
created

QRMatrix::setSeparators()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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