1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class QRMatrix |
4
|
|
|
* |
5
|
|
|
* @created 15.11.2017 |
6
|
|
|
* @author Smiley <[email protected]> |
7
|
|
|
* @copyright 2017 Smiley |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace chillerlan\QRCode\Data; |
12
|
|
|
|
13
|
|
|
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, MaskPattern, ReedSolomonEncoder, Version}; |
14
|
|
|
use function array_fill, count, floor, range; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Holds an array representation of the final QR Code that contains numerical values for later output modifications; |
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
|
|
|
class QRMatrix{ |
23
|
|
|
|
24
|
|
|
/** @var int */ |
25
|
|
|
public const IS_DARK = 0b100000000000; |
26
|
|
|
/** @var int */ |
27
|
|
|
public const M_NULL = 0b000000000000; |
28
|
|
|
/** @var int */ |
29
|
|
|
public const M_DARKMODULE = 0b100000000001; |
30
|
|
|
/** @var int */ |
31
|
|
|
public const M_DATA = 0b000000000010; |
32
|
|
|
/** @var int */ |
33
|
|
|
public const M_DATA_DARK = 0b100000000010; |
34
|
|
|
/** @var int */ |
35
|
|
|
public const M_FINDER = 0b000000000100; |
36
|
|
|
/** @var int */ |
37
|
|
|
public const M_FINDER_DARK = 0b100000000100; |
38
|
|
|
/** @var int */ |
39
|
|
|
public const M_SEPARATOR = 0b000000001000; |
40
|
|
|
/** @var int */ |
41
|
|
|
public const M_ALIGNMENT = 0b000000010000; |
42
|
|
|
/** @var int */ |
43
|
|
|
public const M_ALIGNMENT_DARK = 0b100000010000; |
44
|
|
|
/** @var int */ |
45
|
|
|
public const M_TIMING = 0b000000100000; |
46
|
|
|
/** @var int */ |
47
|
|
|
public const M_TIMING_DARK = 0b100000100000; |
48
|
|
|
/** @var int */ |
49
|
|
|
public const M_FORMAT = 0b000001000000; |
50
|
|
|
/** @var int */ |
51
|
|
|
public const M_FORMAT_DARK = 0b100001000000; |
52
|
|
|
/** @var int */ |
53
|
|
|
public const M_VERSION = 0b000010000000; |
54
|
|
|
/** @var int */ |
55
|
|
|
public const M_VERSION_DARK = 0b100010000000; |
56
|
|
|
/** @var int */ |
57
|
|
|
public const M_QUIETZONE = 0b000100000000; |
58
|
|
|
/** @var int */ |
59
|
|
|
public const M_LOGO = 0b001000000000; |
60
|
|
|
/** @var int */ |
61
|
|
|
public const M_FINDER_DOT = 0b110000000000; |
62
|
|
|
/** @var int */ |
63
|
|
|
public const M_TEST = 0b011111111111; |
64
|
|
|
/** @var int */ |
65
|
|
|
public const M_TEST_DARK = 0b111111111111; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Map of flag => coord |
69
|
|
|
* |
70
|
|
|
* @see \chillerlan\QRCode\Data\QRMatrix::checkNeighbours() |
71
|
|
|
* |
72
|
|
|
* @var array |
73
|
|
|
*/ |
74
|
|
|
protected const neighbours = [ |
75
|
|
|
0b00000001 => [-1, -1], |
76
|
|
|
0b00000010 => [ 0, -1], |
77
|
|
|
0b00000100 => [ 1, -1], |
78
|
|
|
0b00001000 => [ 1, 0], |
79
|
|
|
0b00010000 => [ 1, 1], |
80
|
|
|
0b00100000 => [ 0, 1], |
81
|
|
|
0b01000000 => [-1, 1], |
82
|
|
|
0b10000000 => [-1, 0], |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* the matrix version - always set in QRMatrix, may be null in BitMatrix |
87
|
|
|
*/ |
88
|
|
|
protected ?Version $version = null; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* the current ECC level - always set in QRMatrix, may be null in BitMatrix |
92
|
|
|
*/ |
93
|
|
|
protected ?EccLevel $eccLevel = null; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* the mask pattern that was used in the most recent operation, set via: |
97
|
|
|
* |
98
|
|
|
* - QRMatrix::setFormatInfo() |
99
|
|
|
* - QRMatrix::mask() |
100
|
|
|
* - BitMatrix::readFormatInformation() |
101
|
|
|
*/ |
102
|
|
|
protected ?MaskPattern $maskPattern = null; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* the size (side length) of the matrix, including quiet zone (if created) |
106
|
|
|
*/ |
107
|
|
|
protected int $moduleCount; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* the actual matrix data array |
111
|
|
|
* |
112
|
|
|
* @var int[][] |
113
|
|
|
*/ |
114
|
|
|
protected array $matrix; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* QRMatrix constructor. |
118
|
|
|
*/ |
119
|
|
|
public function __construct(Version $version, EccLevel $eccLevel){ |
120
|
|
|
$this->version = $version; |
121
|
|
|
$this->eccLevel = $eccLevel; |
122
|
|
|
$this->moduleCount = $this->version->getDimension(); |
123
|
|
|
$this->matrix = $this->createMatrix($this->moduleCount, $this::M_NULL); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Creates a 2-dimensional array (square) of the given $size |
128
|
|
|
*/ |
129
|
|
|
protected function createMatrix(int $size, int $value):array{ |
130
|
|
|
return array_fill(0, $size, array_fill(0, $size, $value)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* shortcut to initialize the functional patterns |
135
|
|
|
*/ |
136
|
|
|
public function initFunctionalPatterns():self{ |
137
|
|
|
return $this |
138
|
|
|
->setFinderPattern() |
139
|
|
|
->setSeparators() |
140
|
|
|
->setAlignmentPattern() |
141
|
|
|
->setTimingPattern() |
142
|
|
|
->setDarkModule() |
143
|
|
|
->setVersionNumber() |
144
|
|
|
->setFormatInfo() |
145
|
|
|
; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Returns the data matrix, returns a pure boolean representation if $boolean is set to true |
150
|
|
|
* |
151
|
|
|
* @return int[][]|bool[][] |
152
|
|
|
*/ |
153
|
|
|
public function getMatrix(bool $boolean = null):array{ |
154
|
|
|
$matrix = []; |
155
|
|
|
|
156
|
|
|
foreach($this->matrix as $y => $row){ |
157
|
|
|
$matrix[$y] = []; |
158
|
|
|
|
159
|
|
|
foreach($row as $x => $val){ |
160
|
|
|
$matrix[$y][$x] = $boolean === true |
161
|
|
|
? $this->checkType($x, $y, $this::IS_DARK) |
162
|
|
|
: $this->get($x, $y); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $matrix; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @deprecated 5.0.0 use QRMatrix::getMatrix() instead |
171
|
|
|
* @see \chillerlan\QRCode\Data\QRMatrix::getMatrix() |
172
|
|
|
* @codeCoverageIgnore |
173
|
|
|
*/ |
174
|
|
|
public function matrix(bool $boolean = null):array{ |
175
|
|
|
return $this->getMatrix($boolean); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Returns the current version number |
180
|
|
|
*/ |
181
|
|
|
public function getVersion():?Version{ |
182
|
|
|
return $this->version; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @deprecated 5.0.0 use QRMatrix::getVersion() instead |
187
|
|
|
* @see \chillerlan\QRCode\Data\QRMatrix::getVersion() |
188
|
|
|
* @codeCoverageIgnore |
189
|
|
|
*/ |
190
|
|
|
public function version():?Version{ |
191
|
|
|
return $this->getVersion(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Returns the current ECC level |
196
|
|
|
*/ |
197
|
|
|
public function getEccLevel():?EccLevel{ |
198
|
|
|
return $this->eccLevel; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @deprecated 5.0.0 use QRMatrix::getEccLevel() instead |
203
|
|
|
* @see \chillerlan\QRCode\Data\QRMatrix::getEccLevel() |
204
|
|
|
* @codeCoverageIgnore |
205
|
|
|
*/ |
206
|
|
|
public function eccLevel():?EccLevel{ |
207
|
|
|
return $this->getEccLevel(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Returns the current mask pattern |
212
|
|
|
*/ |
213
|
|
|
public function getMaskPattern():?MaskPattern{ |
214
|
|
|
return $this->maskPattern; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @deprecated 5.0.0 use QRMatrix::getMaskPattern() instead |
219
|
|
|
* @see \chillerlan\QRCode\Data\QRMatrix::getMaskPattern() |
220
|
|
|
* @codeCoverageIgnore |
221
|
|
|
*/ |
222
|
|
|
public function maskPattern():?MaskPattern{ |
223
|
|
|
return $this->getMaskPattern(); |
224
|
|
|
} |
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
|
|
|
public function getSize():int{ |
232
|
|
|
return $this->moduleCount; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @deprecated 5.0.0 use QRMatrix::getSize() instead |
237
|
|
|
* @see \chillerlan\QRCode\Data\QRMatrix::getSize() |
238
|
|
|
* @codeCoverageIgnore |
239
|
|
|
*/ |
240
|
|
|
public function size():int{ |
241
|
|
|
return $this->getSize(); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Returns the value of the module at position [$x, $y] or -1 if the coordinate is outside the matrix |
246
|
|
|
*/ |
247
|
|
|
public function get(int $x, int $y):int{ |
248
|
|
|
|
249
|
|
|
if(!isset($this->matrix[$y][$x])){ |
250
|
|
|
return -1; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $this->matrix[$y][$x]; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Sets the $M_TYPE value for the module at position [$x, $y] |
258
|
|
|
* |
259
|
|
|
* true => $M_TYPE | 0x800 |
260
|
|
|
* false => $M_TYPE |
261
|
|
|
*/ |
262
|
|
|
public function set(int $x, int $y, bool $value, int $M_TYPE):self{ |
263
|
|
|
|
264
|
|
|
if(isset($this->matrix[$y][$x])){ |
265
|
|
|
$this->matrix[$y][$x] = (($M_TYPE & ~$this::IS_DARK) | (($value) ? $this::IS_DARK : 0)); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
return $this; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Fills an area of $width * $height, from the given starting point [$startX, $startY] (top left) with $value for $M_TYPE. |
273
|
|
|
*/ |
274
|
|
|
public function setArea(int $startX, int $startY, int $width, int $height, bool $value, int $M_TYPE):self{ |
275
|
|
|
|
276
|
|
|
for($y = $startY; $y < ($startY + $height); $y++){ |
277
|
|
|
for($x = $startX; $x < ($startX + $width); $x++){ |
278
|
|
|
$this->set($x, $y, $value, $M_TYPE); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Flips the value of the module at ($x, $y) |
287
|
|
|
*/ |
288
|
|
|
public function flip(int $x, int $y):self{ |
289
|
|
|
|
290
|
|
|
if(isset($this->matrix[$y][$x])){ |
291
|
|
|
$this->matrix[$y][$x] ^= $this::IS_DARK; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return $this; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Checks whether the module at ($x, $y) is of the given $M_TYPE |
299
|
|
|
* |
300
|
|
|
* true => $value & $M_TYPE === $M_TYPE |
301
|
|
|
*/ |
302
|
|
|
public function checkType(int $x, int $y, int $M_TYPE):bool{ |
303
|
|
|
$val = $this->get($x, $y); |
304
|
|
|
|
305
|
|
|
if($val === -1){ |
306
|
|
|
return false; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return ($val & $M_TYPE) === $M_TYPE; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* checks whether the module at ($x, $y) is in the given array of $M_TYPES, |
314
|
|
|
* returns true if a match is found, otherwise false. |
315
|
|
|
*/ |
316
|
|
|
public function checkTypeIn(int $x, int $y, array $M_TYPES):bool{ |
317
|
|
|
|
318
|
|
|
foreach($M_TYPES as $type){ |
319
|
|
|
if($this->checkType($x, $y, $type)){ |
320
|
|
|
return true; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
return false; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Checks whether the module at ($x, $y) is true (dark) or false (light) |
329
|
|
|
*/ |
330
|
|
|
public function check(int $x, int $y):bool{ |
331
|
|
|
return $this->checkType($x, $y, $this::IS_DARK); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Checks the status of the neighbouring modules for the module at ($x, $y) and returns a bitmask with the results. |
336
|
|
|
* |
337
|
|
|
* The 8 flags of the bitmask represent the status of each of the neighbouring fields, |
338
|
|
|
* starting with the lowest bit for top left, going clockwise: |
339
|
|
|
* |
340
|
|
|
* 1 2 3 |
341
|
|
|
* 8 # 4 |
342
|
|
|
* 7 6 5 |
343
|
|
|
*/ |
344
|
|
|
public function checkNeighbours(int $x, int $y, int $M_TYPE = null):int{ |
345
|
|
|
$bits = 0; |
346
|
|
|
|
347
|
|
|
foreach($this::neighbours as $bit => $coord){ |
348
|
|
|
[$ix, $iy] = $coord; |
349
|
|
|
|
350
|
|
|
$ix += $x; |
351
|
|
|
$iy += $y; |
352
|
|
|
|
353
|
|
|
// $M_TYPE is given, skip if the field is not the same type |
354
|
|
|
if($M_TYPE !== null && !$this->checkType($ix, $iy, $M_TYPE)){ |
355
|
|
|
continue; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
if($this->checkType($ix, $iy, $this::IS_DARK)){ |
359
|
|
|
$bits |= $bit; |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
return $bits; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Sets the "dark module", that is always on the same position 1x1px away from the bottom left finder |
368
|
|
|
* |
369
|
|
|
* 4 * version + 9 or moduleCount - 8 |
370
|
|
|
*/ |
371
|
|
|
public function setDarkModule():self{ |
372
|
|
|
$this->set(8, ($this->moduleCount - 8), true, $this::M_DARKMODULE); |
373
|
|
|
|
374
|
|
|
return $this; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Draws the 7x7 finder patterns in the corners top left/right and bottom left |
379
|
|
|
* |
380
|
|
|
* ISO/IEC 18004:2000 Section 7.3.2 |
381
|
|
|
*/ |
382
|
|
|
public function setFinderPattern():self{ |
383
|
|
|
|
384
|
|
|
$pos = [ |
385
|
|
|
[0, 0], // top left |
386
|
|
|
[($this->moduleCount - 7), 0], // top right |
387
|
|
|
[0, ($this->moduleCount - 7)], // bottom left |
388
|
|
|
]; |
389
|
|
|
|
390
|
|
|
foreach($pos as $c){ |
391
|
|
|
$this |
392
|
|
|
->setArea($c[0], $c[1], 7, 7, true, $this::M_FINDER) |
393
|
|
|
->setArea(($c[0] + 1), ($c[1] + 1), 5, 5, false, $this::M_FINDER) |
394
|
|
|
->setArea(($c[0] + 2), ($c[1] + 2), 3, 3, true, $this::M_FINDER_DOT) |
395
|
|
|
; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
return $this; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Draws the separator lines around the finder patterns |
403
|
|
|
* |
404
|
|
|
* ISO/IEC 18004:2000 Section 7.3.3 |
405
|
|
|
*/ |
406
|
|
|
public function setSeparators():self{ |
407
|
|
|
|
408
|
|
|
$h = [ |
409
|
|
|
[7, 0], |
410
|
|
|
[($this->moduleCount - 8), 0], |
411
|
|
|
[7, ($this->moduleCount - 8)], |
412
|
|
|
]; |
413
|
|
|
|
414
|
|
|
$v = [ |
415
|
|
|
[7, 7], |
416
|
|
|
[($this->moduleCount - 1), 7], |
417
|
|
|
[7, ($this->moduleCount - 8)], |
418
|
|
|
]; |
419
|
|
|
|
420
|
|
|
for($c = 0; $c < 3; $c++){ |
421
|
|
|
for($i = 0; $i < 8; $i++){ |
422
|
|
|
$this->set($h[$c][0] , ($h[$c][1] + $i), false, $this::M_SEPARATOR); |
423
|
|
|
$this->set(($v[$c][0] - $i), $v[$c][1] , false, $this::M_SEPARATOR); |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
return $this; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Draws the 5x5 alignment patterns |
433
|
|
|
* |
434
|
|
|
* ISO/IEC 18004:2000 Section 7.3.5 |
435
|
|
|
*/ |
436
|
|
|
public function setAlignmentPattern():self{ |
437
|
|
|
$alignmentPattern = $this->version->getAlignmentPattern(); |
|
|
|
|
438
|
|
|
|
439
|
|
|
foreach($alignmentPattern as $y){ |
440
|
|
|
foreach($alignmentPattern as $x){ |
441
|
|
|
|
442
|
|
|
// skip existing patterns |
443
|
|
|
if($this->matrix[$y][$x] !== $this::M_NULL){ |
444
|
|
|
continue; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
$this |
448
|
|
|
->setArea(($x - 2), ($y - 2), 5, 5, true, $this::M_ALIGNMENT) |
449
|
|
|
->setArea(($x - 1), ($y - 1), 3, 3, false, $this::M_ALIGNMENT) |
450
|
|
|
->set($x, $y, true, $this::M_ALIGNMENT) |
451
|
|
|
; |
452
|
|
|
|
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
return $this; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* Draws the timing pattern (h/v checkered line between the finder patterns) |
462
|
|
|
* |
463
|
|
|
* ISO/IEC 18004:2000 Section 7.3.4 |
464
|
|
|
*/ |
465
|
|
|
public function setTimingPattern():self{ |
466
|
|
|
|
467
|
|
|
foreach(range(8, ($this->moduleCount - 8 - 1)) as $i){ |
468
|
|
|
|
469
|
|
|
if($this->matrix[6][$i] !== $this::M_NULL || $this->matrix[$i][6] !== $this::M_NULL){ |
470
|
|
|
continue; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
$v = ($i % 2) === 0; |
474
|
|
|
|
475
|
|
|
$this->set($i, 6, $v, $this::M_TIMING); // h |
476
|
|
|
$this->set(6, $i, $v, $this::M_TIMING); // v |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
return $this; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* Draws the version information, 2x 3x6 pixel |
484
|
|
|
* |
485
|
|
|
* ISO/IEC 18004:2000 Section 8.10 |
486
|
|
|
*/ |
487
|
|
|
public function setVersionNumber():self{ |
488
|
|
|
$bits = $this->version->getVersionPattern(); |
489
|
|
|
|
490
|
|
|
if($bits !== null){ |
491
|
|
|
|
492
|
|
|
for($i = 0; $i < 18; $i++){ |
493
|
|
|
$a = (int)($i / 3); |
494
|
|
|
$b = (($i % 3) + ($this->moduleCount - 8 - 3)); |
495
|
|
|
$v = (($bits >> $i) & 1) === 1; |
496
|
|
|
|
497
|
|
|
$this->set($b, $a, $v, $this::M_VERSION); // ne |
498
|
|
|
$this->set($a, $b, $v, $this::M_VERSION); // sw |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
return $this; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Draws the format info along the finder patterns. If no $maskPattern, all format info modules will be set to false. |
508
|
|
|
* |
509
|
|
|
* ISO/IEC 18004:2000 Section 8.9 |
510
|
|
|
*/ |
511
|
|
|
public function setFormatInfo(MaskPattern $maskPattern = null):self{ |
512
|
|
|
$this->maskPattern = $maskPattern; |
513
|
|
|
|
514
|
|
|
$bits = ($this->maskPattern instanceof MaskPattern) |
515
|
|
|
? $this->eccLevel->getformatPattern($this->maskPattern) |
|
|
|
|
516
|
|
|
: 0; // sets all format fields to false (test mode) |
517
|
|
|
|
518
|
|
|
for($i = 0; $i < 15; $i++){ |
519
|
|
|
$v = (($bits >> $i) & 1) === 1; |
520
|
|
|
|
521
|
|
|
if($i < 6){ |
522
|
|
|
$this->set(8, $i, $v, $this::M_FORMAT); |
523
|
|
|
} |
524
|
|
|
elseif($i < 8){ |
525
|
|
|
$this->set(8, ($i + 1), $v, $this::M_FORMAT); |
526
|
|
|
} |
527
|
|
|
else{ |
528
|
|
|
$this->set(8, ($this->moduleCount - 15 + $i), $v, $this::M_FORMAT); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
if($i < 8){ |
532
|
|
|
$this->set(($this->moduleCount - $i - 1), 8, $v, $this::M_FORMAT); |
533
|
|
|
} |
534
|
|
|
elseif($i < 9){ |
535
|
|
|
$this->set(((15 - $i)), 8, $v, $this::M_FORMAT); |
536
|
|
|
} |
537
|
|
|
else{ |
538
|
|
|
$this->set((15 - $i - 1), 8, $v, $this::M_FORMAT); |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
return $this; |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* Draws the "quiet zone" of $size around the matrix |
548
|
|
|
* |
549
|
|
|
* ISO/IEC 18004:2000 Section 7.3.7 |
550
|
|
|
* |
551
|
|
|
* @throws \chillerlan\QRCode\Data\QRCodeDataException |
552
|
|
|
*/ |
553
|
|
|
public function setQuietZone(int $quietZoneSize):self{ |
554
|
|
|
|
555
|
|
|
if($this->matrix[($this->moduleCount - 1)][($this->moduleCount - 1)] === $this::M_NULL){ |
556
|
|
|
throw new QRCodeDataException('use only after writing data'); |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
// create a matrix with the new size |
560
|
|
|
$newSize = ($this->moduleCount + ($quietZoneSize * 2)); |
561
|
|
|
$newMatrix = $this->createMatrix($newSize, $this::M_QUIETZONE); |
562
|
|
|
|
563
|
|
|
// copy over the current matrix |
564
|
|
|
for($y = 0; $y < $this->moduleCount; $y++){ |
565
|
|
|
for($x = 0; $x < $this->moduleCount; $x++){ |
566
|
|
|
$newMatrix[($y + $quietZoneSize)][($x + $quietZoneSize)] = $this->matrix[$y][$x]; |
567
|
|
|
} |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
// set the new values |
571
|
|
|
$this->moduleCount = $newSize; |
572
|
|
|
$this->matrix = $newMatrix; |
573
|
|
|
|
574
|
|
|
return $this; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/** |
578
|
|
|
* Clears a space of $width * $height in order to add a logo or text. |
579
|
|
|
* If no $height is given, the space will be assumed a square of $width. |
580
|
|
|
* |
581
|
|
|
* Additionally, the logo space can be positioned within the QR Code - respecting the main functional patterns - |
582
|
|
|
* using $startX and $startY. If either of these are null, the logo space will be centered in that direction. |
583
|
|
|
* ECC level "H" (30%) is required. |
584
|
|
|
* |
585
|
|
|
* The coordinates of $startX and $startY do not include the quiet zone: |
586
|
|
|
* [0, 0] is always the top left module of the top left finder pattern, negative values go into the quiet zone top and left. |
587
|
|
|
* |
588
|
|
|
* Please note that adding a logo space minimizes the error correction capacity of the QR Code and |
589
|
|
|
* created images may become unreadable, especially when printed with a chance to receive damage. |
590
|
|
|
* Please test thoroughly before using this feature in production. |
591
|
|
|
* |
592
|
|
|
* This method should be called from within an output module (after the matrix has been filled with data). |
593
|
|
|
* Note that there is no restiction on how many times this method could be called on the same matrix instance. |
594
|
|
|
* |
595
|
|
|
* @link https://github.com/chillerlan/php-qrcode/issues/52 |
596
|
|
|
* |
597
|
|
|
* @throws \chillerlan\QRCode\Data\QRCodeDataException |
598
|
|
|
*/ |
599
|
|
|
public function setLogoSpace(int $width, int $height = null, int $startX = null, int $startY = null):self{ |
600
|
|
|
|
601
|
|
|
// for logos, we operate in ECC H (30%) only |
602
|
|
|
if($this->eccLevel->getLevel() !== EccLevel::H){ |
603
|
|
|
throw new QRCodeDataException('ECC level "H" required to add logo space'); |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
if($height === null){ |
607
|
|
|
$height = $width; |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
// if width and height happen to be negative or 0 (default value), just return - nothing to do |
611
|
|
|
if($width <= 0 || $height <= 0){ |
612
|
|
|
return $this; // @codeCoverageIgnore |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
// $this->moduleCount includes the quiet zone (if created), we need the QR size here |
616
|
|
|
$length = $this->version->getDimension(); |
617
|
|
|
|
618
|
|
|
// throw if the size is exceeds the qrcode size |
619
|
|
|
if($width > $length || $height > $length){ |
620
|
|
|
throw new QRCodeDataException('logo dimensions exceed matrix size'); |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
// we need uneven sizes to center the logo space, adjust if needed |
624
|
|
|
if($startX === null && ($width % 2) === 0){ |
625
|
|
|
$width++; |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
if($startY === null && ($height % 2) === 0){ |
629
|
|
|
$height++; |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
// throw if the logo space exceeds the maximum error correction capacity |
633
|
|
|
if(($width * $height) > floor($length * $length * 0.2)){ |
634
|
|
|
throw new QRCodeDataException('logo space exceeds the maximum error correction capacity'); |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
// quiet zone size |
638
|
|
|
$qz = (($this->moduleCount - $length) / 2); |
639
|
|
|
// skip quiet zone and the first 9 rows/columns (finder-, mode-, version- and timing patterns) |
640
|
|
|
$start = ($qz + 9); |
641
|
|
|
// skip quiet zone |
642
|
|
|
$end = ($this->moduleCount - $qz); |
643
|
|
|
|
644
|
|
|
// determine start coordinates |
645
|
|
|
$startX = ((($startX !== null) ? $startX : ($length - $width) / 2) + $qz); |
646
|
|
|
$startY = ((($startY !== null) ? $startY : ($length - $height) / 2) + $qz); |
647
|
|
|
$endX = ($startX + $width); |
648
|
|
|
$endY = ($startY + $height); |
649
|
|
|
|
650
|
|
|
// clear the space |
651
|
|
|
for($y = $startY; $y < $endY; $y++){ |
652
|
|
|
for($x = $startX; $x < $endX; $x++){ |
653
|
|
|
// out of bounds, skip |
654
|
|
|
if($x < $start || $y < $start ||$x >= $end || $y >= $end){ |
655
|
|
|
continue; |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
$this->set($x, $y, false, $this::M_LOGO); |
659
|
|
|
} |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
return $this; |
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
/** |
666
|
|
|
* Maps the interleaved binary $data on the matrix |
667
|
|
|
*/ |
668
|
|
|
public function writeCodewords(BitBuffer $bitBuffer):self{ |
669
|
|
|
$data = (new ReedSolomonEncoder($this->version, $this->eccLevel))->interleaveEcBytes($bitBuffer); |
|
|
|
|
670
|
|
|
$byteCount = count($data); |
671
|
|
|
$iByte = 0; |
672
|
|
|
$iBit = 7; |
673
|
|
|
$direction = true; |
674
|
|
|
|
675
|
|
|
for($i = ($this->moduleCount - 1); $i > 0; $i -= 2){ |
676
|
|
|
|
677
|
|
|
// skip vertical alignment pattern |
678
|
|
|
if($i === 6){ |
679
|
|
|
$i--; |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
for($count = 0; $count < $this->moduleCount; $count++){ |
683
|
|
|
$y = ($direction) ? ($this->moduleCount - 1 - $count) : $count; |
684
|
|
|
|
685
|
|
|
for($col = 0; $col < 2; $col++){ |
686
|
|
|
$x = ($i - $col); |
687
|
|
|
|
688
|
|
|
// skip functional patterns |
689
|
|
|
if($this->get($x, $y) !== $this::M_NULL){ |
690
|
|
|
continue; |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
$v = $iByte < $byteCount && (($data[$iByte] >> $iBit--) & 1) === 1; |
694
|
|
|
|
695
|
|
|
$this->set($x, $y, $v, $this::M_DATA); |
696
|
|
|
|
697
|
|
|
if($iBit === -1){ |
698
|
|
|
$iByte++; |
699
|
|
|
$iBit = 7; |
700
|
|
|
} |
701
|
|
|
} |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
$direction = !$direction; // switch directions |
|
|
|
|
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
return $this; |
708
|
|
|
} |
709
|
|
|
|
710
|
|
|
/** |
711
|
|
|
* Applies/reverses the mask pattern |
712
|
|
|
* |
713
|
|
|
* ISO/IEC 18004:2000 Section 8.8.1 |
714
|
|
|
*/ |
715
|
|
|
public function mask(MaskPattern $maskPattern):self{ |
716
|
|
|
$this->maskPattern = $maskPattern; |
717
|
|
|
$mask = $this->maskPattern->getMask(); |
718
|
|
|
|
719
|
|
|
foreach($this->matrix as $y => $row){ |
720
|
|
|
foreach($row as $x => $val){ |
721
|
|
|
if($mask($x, $y) && ($val & $this::M_DATA) === $this::M_DATA){ |
722
|
|
|
$this->flip($x, $y); |
723
|
|
|
} |
724
|
|
|
} |
725
|
|
|
} |
726
|
|
|
|
727
|
|
|
return $this; |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
} |
731
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.