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