|
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 = -1; |
|
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, self::M_NULL)); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Returns the data array |
|
150
|
|
|
* |
|
151
|
|
|
* @return array |
|
152
|
|
|
*/ |
|
153
|
|
|
public function matrix():array { |
|
154
|
|
|
return $this->matrix; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return int |
|
159
|
|
|
*/ |
|
160
|
|
|
public function version():int { |
|
161
|
|
|
return $this->version; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return int |
|
166
|
|
|
*/ |
|
167
|
|
|
public function eccLevel():int { |
|
168
|
|
|
return $this->eclevel; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return int |
|
173
|
|
|
*/ |
|
174
|
|
|
public function maskPattern():int { |
|
175
|
|
|
return $this->maskPattern; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Returns the absoulute size of the matrix, including quiet zone (after setting it). |
|
180
|
|
|
* |
|
181
|
|
|
* size = version * 4 + 17 [ + 2 * quietzone size] |
|
182
|
|
|
* |
|
183
|
|
|
* @return int |
|
184
|
|
|
*/ |
|
185
|
|
|
public function size():int{ |
|
186
|
|
|
return $this->moduleCount; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Returns the value of the module at position [$x, $y] |
|
191
|
|
|
* |
|
192
|
|
|
* @param int $x |
|
193
|
|
|
* @param int $y |
|
194
|
|
|
* |
|
195
|
|
|
* @return int |
|
196
|
|
|
*/ |
|
197
|
|
|
public function get(int $x, int $y):int{ |
|
198
|
|
|
return $this->matrix[$y][$x]; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Sets the $M_TYPE value for the module at position [$x, $y] |
|
203
|
|
|
* |
|
204
|
|
|
* true => $M_TYPE << 8 |
|
205
|
|
|
* false => $M_TYPE |
|
206
|
|
|
* |
|
207
|
|
|
* @param int $x |
|
208
|
|
|
* @param int $y |
|
209
|
|
|
* @param int $M_TYPE |
|
210
|
|
|
* @param bool $value |
|
211
|
|
|
* |
|
212
|
|
|
* @return \chillerlan\QRCode\Data\QRMatrix |
|
213
|
|
|
*/ |
|
214
|
|
|
public function set(int $x, int $y, bool $value, int $M_TYPE):QRMatrix{ |
|
215
|
|
|
$this->matrix[$y][$x] = $M_TYPE << ($value ? 8 : 0); |
|
216
|
|
|
|
|
217
|
|
|
return $this; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Checks whether a module is true (dark) or false (light) |
|
222
|
|
|
* |
|
223
|
|
|
* true => $value >> 8 === $M_TYPE |
|
224
|
|
|
* $value >> 8 > 0 |
|
225
|
|
|
* |
|
226
|
|
|
* false => $value === $M_TYPE |
|
227
|
|
|
* $value >> 8 === 0 |
|
228
|
|
|
* |
|
229
|
|
|
* @param int $x |
|
230
|
|
|
* @param int $y |
|
231
|
|
|
* |
|
232
|
|
|
* @return bool |
|
233
|
|
|
*/ |
|
234
|
|
|
public function check(int $x, int $y):bool{ |
|
235
|
|
|
return $this->matrix[$y][$x] >> 8 > 0; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Sets the "dark module", that is always on the same position 1x1px away from the bottom left finder |
|
241
|
|
|
* |
|
242
|
|
|
* @return \chillerlan\QRCode\Data\QRMatrix |
|
243
|
|
|
*/ |
|
244
|
|
|
public function setDarkModule():QRMatrix{ |
|
245
|
|
|
$this->set(8, 4 * $this->version + 9, true, self::M_DARKMODULE); |
|
246
|
|
|
|
|
247
|
|
|
return $this; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Draws the 7x7 finder patterns in the corners top left/right and bottom left |
|
252
|
|
|
* |
|
253
|
|
|
* @return \chillerlan\QRCode\Data\QRMatrix |
|
254
|
|
|
*/ |
|
255
|
|
|
public function setFinderPattern():QRMatrix{ |
|
256
|
|
|
|
|
257
|
|
|
$pos = [ |
|
258
|
|
|
[0, 0], // top left |
|
259
|
|
|
[$this->moduleCount - 7, 0], // bottom left |
|
260
|
|
|
[0, $this->moduleCount - 7], // top right |
|
261
|
|
|
]; |
|
262
|
|
|
|
|
263
|
|
|
foreach($pos as $c){ |
|
264
|
|
|
for($y = 0; $y < 7; $y++){ |
|
265
|
|
|
for($x = 0; $x < 7; $x++){ |
|
266
|
|
|
$this->set( |
|
267
|
|
|
$c[0] + $y, |
|
268
|
|
|
$c[1] + $x, |
|
269
|
|
|
!(($x > 0 && $x < 6 && ($y === 1 || $y === 5)) || ($y > 0 && $y < 6 && ($x === 1 || $x === 5))), |
|
270
|
|
|
self::M_FINDER |
|
271
|
|
|
); |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
return $this; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Draws the separator lines around the finder patterns |
|
281
|
|
|
* |
|
282
|
|
|
* @return \chillerlan\QRCode\Data\QRMatrix |
|
283
|
|
|
*/ |
|
284
|
|
|
public function setSeparators():QRMatrix{ |
|
285
|
|
|
|
|
286
|
|
|
$h = [ |
|
287
|
|
|
[7, 0], |
|
288
|
|
|
[$this->moduleCount - 8, 0], |
|
289
|
|
|
[7, $this->moduleCount - 8], |
|
290
|
|
|
]; |
|
291
|
|
|
|
|
292
|
|
|
$v = [ |
|
293
|
|
|
[7, 7], |
|
294
|
|
|
[$this->moduleCount - 1, 7], |
|
295
|
|
|
[7, $this->moduleCount - 8], |
|
296
|
|
|
]; |
|
297
|
|
|
|
|
298
|
|
|
$t = self::M_SEPARATOR; |
|
299
|
|
|
|
|
300
|
|
|
for($c = 0; $c < 3; $c++){ |
|
301
|
|
|
for($i = 0; $i < 8; $i++){ |
|
302
|
|
|
$this->set($h[$c][0] , $h[$c][1] + $i, false, $t); |
|
303
|
|
|
$this->set($v[$c][0] - $i, $v[$c][1] , false, $t); |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
return $this; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* Draws the 5x5 alignment patterns |
|
313
|
|
|
* |
|
314
|
|
|
* @return \chillerlan\QRCode\Data\QRMatrix |
|
315
|
|
|
*/ |
|
316
|
|
|
public function setAlignmentPattern():QRMatrix{ |
|
317
|
|
|
$pattern = self::alignmentPattern[$this->version]; |
|
318
|
|
|
|
|
319
|
|
|
foreach($pattern as $y){ |
|
320
|
|
|
foreach($pattern as $x){ |
|
321
|
|
|
|
|
322
|
|
|
// skip existing patterns |
|
323
|
|
|
if($this->matrix[$y][$x] !== self::M_NULL){ |
|
324
|
|
|
continue; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
for($ry = -2; $ry <= 2; $ry++){ |
|
328
|
|
|
for($rx = -2; $rx <= 2; $rx++){ |
|
329
|
|
|
$v = ($ry === 0 && $rx === 0) || $ry === 2 || $ry === -2 || $rx === 2 || $rx === -2; |
|
330
|
|
|
|
|
331
|
|
|
$this->set($x + $rx, $y + $ry, $v, self::M_ALIGNMENT); |
|
332
|
|
|
} |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
} |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
return $this; |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Draws the timing pattern (h/v checkered line between the finder patterns) |
|
344
|
|
|
* |
|
345
|
|
|
* @return \chillerlan\QRCode\Data\QRMatrix |
|
346
|
|
|
*/ |
|
347
|
|
|
public function setTimingPattern():QRMatrix{ |
|
348
|
|
|
|
|
349
|
|
|
foreach(range(8, $this->moduleCount - 8 - 1) as $i){ |
|
350
|
|
|
|
|
351
|
|
|
if($this->matrix[6][$i] !== self::M_NULL || $this->matrix[$i][6] !== self::M_NULL){ |
|
352
|
|
|
continue; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
$v = $i % 2 === 0; |
|
356
|
|
|
$t = self::M_TIMING; |
|
357
|
|
|
|
|
358
|
|
|
$this->set($i, 6, $v, $t); // h |
|
359
|
|
|
$this->set(6, $i, $v, $t); // v |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
return $this; |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* Draws the version information, 2x 3x6 pixel |
|
367
|
|
|
* |
|
368
|
|
|
* @param bool|null $test |
|
369
|
|
|
* |
|
370
|
|
|
* @return \chillerlan\QRCode\Data\QRMatrix |
|
371
|
|
|
*/ |
|
372
|
|
|
public function setVersionNumber(bool $test = null):QRMatrix{ |
|
373
|
|
|
$test = $test !== null ? $test : false; |
|
374
|
|
|
|
|
375
|
|
|
$bits = self::versionPattern[$this->version] ?? false; |
|
376
|
|
|
|
|
377
|
|
|
if($bits !== false){ |
|
378
|
|
|
|
|
379
|
|
|
for($i = 0; $i < 18; $i++){ |
|
380
|
|
|
$a = (int)floor($i / 3); |
|
381
|
|
|
$b = $i % 3 + $this->moduleCount - 8 - 3; |
|
382
|
|
|
$v = !$test && (($bits >> $i) & 1) === 1; |
|
383
|
|
|
$t = self::M_VERSION; |
|
384
|
|
|
|
|
385
|
|
|
$this->set($b, $a, $v, $t); // ne |
|
386
|
|
|
$this->set($a, $b, $v, $t); // sw |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
return $this; |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
/** |
|
395
|
|
|
* Draws the format info along the finder patterns |
|
396
|
|
|
* |
|
397
|
|
|
* @param int $maskPattern |
|
398
|
|
|
* @param bool|null $test |
|
399
|
|
|
* |
|
400
|
|
|
* @return \chillerlan\QRCode\Data\QRMatrix |
|
401
|
|
|
*/ |
|
402
|
|
|
public function setFormatInfo(int $maskPattern, bool $test = null):QRMatrix{ |
|
403
|
|
|
$test = $test !== null ? $test : false; |
|
404
|
|
|
$bits = self::formatPattern[QRCode::ECC_MODES[$this->eclevel]][$maskPattern] ?? 0; |
|
405
|
|
|
$t = self::M_FORMAT; |
|
406
|
|
|
|
|
407
|
|
|
for($i = 0; $i < 15; $i++){ |
|
408
|
|
|
$v = !$test && (($bits >> $i) & 1) === 1; |
|
409
|
|
|
|
|
410
|
|
|
if($i < 6){ |
|
411
|
|
|
$this->set(8, $i, $v, $t); |
|
412
|
|
|
} |
|
413
|
|
|
elseif($i < 8){ |
|
414
|
|
|
$this->set(8, $i + 1, $v, $t); |
|
415
|
|
|
} |
|
416
|
|
|
else{ |
|
417
|
|
|
$this->set(8, $this->moduleCount - 15 + $i, $v, $t); |
|
418
|
|
|
} |
|
419
|
|
|
|
|
420
|
|
|
if($i < 8){ |
|
421
|
|
|
$this->set($this->moduleCount - $i - 1, 8, $v, $t); |
|
422
|
|
|
} |
|
423
|
|
|
elseif($i < 9){ |
|
424
|
|
|
$this->set(15 - $i, 8, $v, $t); |
|
425
|
|
|
} |
|
426
|
|
|
else{ |
|
427
|
|
|
$this->set(15 - $i - 1, 8, $v, $t); |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
$this->set(8, $this->moduleCount - 8, !$test, $t); |
|
433
|
|
|
|
|
434
|
|
|
return $this; |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
/** |
|
438
|
|
|
* Draws the "quiet zone" of $size around the matrix |
|
439
|
|
|
* |
|
440
|
|
|
* @param int|null $size |
|
441
|
|
|
* |
|
442
|
|
|
* @return \chillerlan\QRCode\Data\QRMatrix |
|
443
|
|
|
*/ |
|
444
|
|
|
public function setQuietZone(int $size = null):QRMatrix{ |
|
445
|
|
|
|
|
446
|
|
|
if($this->matrix[$this->moduleCount - 1][$this->moduleCount - 1] === self::M_NULL){ |
|
447
|
|
|
throw new QRCodeDataException('use only after writing data'); |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
$size = $size !== null ? max(0, min($size, floor($this->moduleCount / 2))) : 4; |
|
451
|
|
|
$t = self::M_QUIETZONE; |
|
452
|
|
|
|
|
453
|
|
|
for($y = 0; $y < $this->moduleCount; $y++){ |
|
454
|
|
|
for($i = 0; $i < $size; $i++){ |
|
455
|
|
|
array_unshift($this->matrix[$y], $t); |
|
456
|
|
|
array_push($this->matrix[$y], $t); |
|
457
|
|
|
} |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
$this->moduleCount += ($size * 2); |
|
461
|
|
|
$r = array_fill(0, $this->moduleCount, $t); |
|
462
|
|
|
|
|
463
|
|
|
for($i = 0; $i < $size; $i++){ |
|
464
|
|
|
array_unshift($this->matrix, $r); |
|
465
|
|
|
array_push($this->matrix, $r); |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
return $this; |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
/** |
|
472
|
|
|
* Maps the binary $data array from QRDataInterface::maskECC() on the matrix, using $maskPattern |
|
473
|
|
|
* |
|
474
|
|
|
* @see \chillerlan\QRCode\Data\QRDataAbstract::maskECC() |
|
475
|
|
|
* |
|
476
|
|
|
* @param int[] $data |
|
477
|
|
|
* @param int $maskPattern |
|
478
|
|
|
* |
|
479
|
|
|
* @return \chillerlan\QRCode\Data\QRMatrix |
|
480
|
|
|
*/ |
|
481
|
|
|
public function mapData(array $data, int $maskPattern):QRMatrix{ |
|
482
|
|
|
$this->maskPattern = $maskPattern; |
|
483
|
|
|
$byteCount = count($data); |
|
484
|
|
|
$size = $this->moduleCount - 1; |
|
485
|
|
|
|
|
486
|
|
|
for($i = $size, $y = $size, $inc = -1, $byteIndex = 0, $bitIndex = 7; $i > 0; $i -= 2){ |
|
487
|
|
|
|
|
488
|
|
|
if($i === 6){ |
|
489
|
|
|
$i--; |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
|
while(true){ |
|
493
|
|
|
for($c = 0; $c < 2; $c++){ |
|
494
|
|
|
$x = $i - $c; |
|
495
|
|
|
|
|
496
|
|
|
if($this->matrix[$y][$x] === self::M_NULL){ |
|
497
|
|
|
$v = false; |
|
498
|
|
|
|
|
499
|
|
|
if($byteIndex < $byteCount){ |
|
500
|
|
|
$v = (($data[$byteIndex] >> $bitIndex) & 1) === 1; |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
if($this->getMask($x, $y, $maskPattern) === 0){ |
|
504
|
|
|
$v = !$v; |
|
505
|
|
|
} |
|
506
|
|
|
|
|
507
|
|
|
$this->matrix[$y][$x] = self::M_DATA << ($v ? 8 : 0); |
|
508
|
|
|
$bitIndex--; |
|
509
|
|
|
|
|
510
|
|
|
if($bitIndex === -1){ |
|
511
|
|
|
$byteIndex++; |
|
512
|
|
|
$bitIndex = 7; |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
} |
|
516
|
|
|
} |
|
517
|
|
|
|
|
518
|
|
|
$y += $inc; |
|
519
|
|
|
|
|
520
|
|
|
if($y < 0 || $this->moduleCount <= $y){ |
|
521
|
|
|
$y -= $inc; |
|
522
|
|
|
$inc = -$inc; |
|
523
|
|
|
|
|
524
|
|
|
break; |
|
525
|
|
|
} |
|
526
|
|
|
|
|
527
|
|
|
} |
|
528
|
|
|
} |
|
529
|
|
|
|
|
530
|
|
|
return $this; |
|
531
|
|
|
} |
|
532
|
|
|
|
|
533
|
|
|
/** |
|
534
|
|
|
* @see \chillerlan\QRCode\QRMatrix::mapData() |
|
535
|
|
|
* |
|
536
|
|
|
* @internal |
|
537
|
|
|
* |
|
538
|
|
|
* @param int $x |
|
539
|
|
|
* @param int $y |
|
540
|
|
|
* @param int $maskPattern |
|
541
|
|
|
* |
|
542
|
|
|
* @return int |
|
543
|
|
|
*/ |
|
544
|
|
|
protected function getMask(int $x, int $y, int $maskPattern):int { |
|
545
|
|
|
$a = $y + $x; |
|
546
|
|
|
$m = $y * $x; |
|
547
|
|
|
|
|
548
|
|
|
// i hate it so much |
|
549
|
|
|
switch($maskPattern){ |
|
550
|
|
|
case 0: return $a % 2; |
|
|
|
|
|
|
551
|
|
|
case 1: return $y % 2; |
|
|
|
|
|
|
552
|
|
|
case 2: return $x % 3; |
|
|
|
|
|
|
553
|
|
|
case 3: return $a % 3; |
|
|
|
|
|
|
554
|
|
|
case 4: return (floor($y / 2) + floor($x / 3)) % 2; |
|
|
|
|
|
|
555
|
|
|
case 5: return $m % 2 + $m % 3; |
|
|
|
|
|
|
556
|
|
|
case 6: return ($m % 2 + $m % 3) % 2; |
|
|
|
|
|
|
557
|
|
|
case 7: return ($m % 3 + $a % 2) % 2; |
|
|
|
|
|
|
558
|
|
|
} |
|
559
|
|
|
|
|
560
|
|
|
throw new QRCodeDataException('invalid mask pattern'); |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
} |
|
564
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.