Skip to content

Commit 7fd6cfc

Browse files
committed
🚿
1 parent 564ef12 commit 7fd6cfc

File tree

6 files changed

+293
-93
lines changed

6 files changed

+293
-93
lines changed

src/Common/MaskPattern.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,15 @@ export default class MaskPattern{
101101
let $size = $QRMatrix.getSize();
102102

103103
for(let $pattern of PATTERNS){
104-
let $mp = new MaskPattern($pattern);
105-
let $matrix = PHPJS.clone($QRMatrix).setFormatInfo($mp).mask($mp).getMatrix(true);
106104
let $penalty = 0;
105+
let $mp = new MaskPattern($pattern);
106+
let $matrix = PHPJS.clone($QRMatrix);
107+
// because js is fucking dumb, it can't even properly clone THAT ONE FUCKING ARRAY WITHOUT LEAVING REFERENCES
108+
$matrix._matrix = structuredClone($QRMatrix._matrix);
109+
let $m = $matrix.setFormatInfo($mp).mask($mp).getMatrix(true);
107110

108111
for(let $level = 1; $level <= 4; $level++){
109-
$penalty += this['testRule' + $level]($matrix, $size, $size);
112+
$penalty += this['testRule' + $level]($m, $size, $size);
110113
}
111114

112115
$penalties[$pattern] = PHPJS.intval($penalty);

src/Data/QRMatrix.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,14 @@ export default class QRMatrix{
200200
set($x, $y, $value, $M_TYPE){
201201

202202
if(PHPJS.isset(() => this._matrix[$y][$x])){
203-
this._matrix[$y][$x] = $M_TYPE | ($value ? IS_DARK : 0);
203+
// we don't know whether the input is dark, so we remove the dark bit
204+
$M_TYPE &= ~IS_DARK
205+
206+
if($value === true){
207+
$M_TYPE |= IS_DARK;
208+
}
209+
210+
this._matrix[$y][$x] = $M_TYPE;
204211
}
205212

206213
return this;
@@ -259,11 +266,11 @@ export default class QRMatrix{
259266
*/
260267
checkType($x, $y, $M_TYPE){
261268

262-
if(!PHPJS.isset(() => this._matrix[$y][$x])){
263-
return false;
269+
if(PHPJS.isset(() => this._matrix[$y][$x])){
270+
return (this._matrix[$y][$x] & $M_TYPE) === $M_TYPE;
264271
}
265272

266-
return (this._matrix[$y][$x] & $M_TYPE) === $M_TYPE;
273+
return false;
267274
}
268275

269276
/**
@@ -299,7 +306,12 @@ export default class QRMatrix{
299306
* @returns {boolean}
300307
*/
301308
check($x, $y){
302-
return this.checkType($x, $y, IS_DARK);
309+
310+
if(PHPJS.isset(() => this._matrix[$y][$x])){
311+
return this.isDark(this._matrix[$y][$x]);
312+
}
313+
314+
return false;
303315
}
304316

305317
/**
@@ -585,6 +597,17 @@ export default class QRMatrix{
585597
return this;
586598
}
587599

600+
/**
601+
* Rotates the matrix by 90 degrees clock wise
602+
*
603+
* @link https://stackoverflow.com/a/58668351
604+
*/
605+
rotate90(){
606+
this._matrix = this._matrix[0].map((val, index) => this._matrix.map(row => row[index]).reverse())
607+
608+
return this;
609+
}
610+
588611
/**
589612
* Inverts the values of the whole matrix
590613
*
@@ -630,18 +653,18 @@ export default class QRMatrix{
630653
* @link https://github.com/chillerlan/php-qrcode/issues/52
631654
*
632655
* @param {number|int} $width
633-
* @param {number|int} $height
656+
* @param {number|int|null} $height
634657
* @param {number|int|null} $startX
635658
* @param {number|int|null} $startY
636659
*
637660
* @returns {QRMatrix}
638661
* @throws {QRCodeDataException}
639662
*/
640-
setLogoSpace($width, $height, $startX = null, $startY = null){
663+
setLogoSpace($width, $height = null, $startX = null, $startY = null){
641664
$height ??= $width;
642665

643666
// if width and height happen to be negative or 0 (default value), just return - nothing to do
644-
if($width === 0 || $height === 0){
667+
if($width <= 0 || $height <= 0){
645668
return this;
646669
}
647670

@@ -654,8 +677,8 @@ export default class QRMatrix{
654677
let $dimension = this._version.getDimension();
655678

656679
// throw if the size is negative or exceeds the qrcode size
657-
if($width < 0 || $height < 0 || $width > $dimension || $height > $dimension){
658-
throw new QRCodeDataException('invalid logo dimensions');
680+
if($width > $dimension || $height > $dimension){
681+
throw new QRCodeDataException('logo dimensions exceed matrix size');
659682
}
660683

661684
// we need uneven sizes to center the logo space, adjust if needed
@@ -765,7 +788,7 @@ export default class QRMatrix{
765788

766789
for(let $y = 0; $y < this.moduleCount; $y++){
767790
for(let $x = 0; $x < this.moduleCount; $x++){
768-
if($mask($x, $y) && (this._matrix[$y][$x] & M_DATA) === M_DATA){
791+
if((this._matrix[$y][$x] & M_DATA) === M_DATA && $mask($x, $y)){
769792
this.flip($x, $y);
770793
}
771794
}

src/QRCode.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,9 @@ export default class QRCode{
127127
addMatrixModifications($QRMatrix){
128128

129129
if(this.options.addLogoSpace){
130-
let $logoSpaceWidth = this.options.logoSpaceWidth;
131-
let $logoSpaceHeight = this.options.logoSpaceHeight;
132-
133130
// check whether one of the dimensions was omitted
134-
if($logoSpaceWidth === null || $logoSpaceHeight === null){
135-
$logoSpaceWidth = ($logoSpaceWidth ?? $logoSpaceHeight ?? 0);
136-
$logoSpaceHeight = null;
137-
}
131+
let $logoSpaceWidth = (this.options.logoSpaceWidth ?? this.options.logoSpaceHeight ?? 0);
132+
let $logoSpaceHeight = (this.options.logoSpaceHeight ?? $logoSpaceWidth);
138133

139134
$QRMatrix.setLogoSpace(
140135
$logoSpaceWidth,

src/QROptions.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,18 @@ export default class QROptions{
267267
/**
268268
* width of the logo space
269269
*
270-
* @type {number|int}
270+
* @type {number|int|null}
271271
* @protected
272272
*/
273-
_logoSpaceWidth = 0;
273+
_logoSpaceWidth = null;
274274

275275
/**
276276
* height of the logo space
277277
*
278-
* @type {number|int}
278+
* @type {number|int|null}
279279
* @protected
280280
*/
281-
_logoSpaceHeight = 0;
281+
_logoSpaceHeight = null;
282282

283283
/**
284284
* optional horizontal start position of the logo space (top left corner)

test/Data/QRDataMode.test.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
*/
77

88
import {
9-
AlphaNum, Byte, /* Kanji, */Numeric, PATTERN_000, PATTERN_001, PATTERN_010, PATTERN_011, PATTERN_100,
10-
PATTERN_101, PATTERN_110, PATTERN_111, QRData, QRDataModeInterface, QRMatrix, QROptions,
9+
AlphaNum, Byte, Numeric, QRData, QRDataModeInterface, QRMatrix, QROptions,
1110
} from '../../src/index.js';
1211

1312
import {beforeEach, suite, test} from 'mocha';
@@ -34,7 +33,6 @@ suite('QRDataModeTest', function(){
3433
let datamodeProvider = [
3534
{$fqn: AlphaNum, desc: 'AlphaNum'},
3635
{$fqn: Byte, desc: 'Byte'},
37-
// {$fqn: Kanji, desc: 'Kanji'},
3836
{$fqn: Numeric, desc: 'Numeric'},
3937
];
4038

@@ -77,17 +75,6 @@ suite('QRDataModeTest', function(){
7775
],
7876
}[desc];
7977

80-
// the 8 mask patterns to iterate over
81-
let maskPatternProvider = [
82-
{$maskPattern: PATTERN_000, pattern: 'PATTERN_000'},
83-
{$maskPattern: PATTERN_001, pattern: 'PATTERN_001'},
84-
{$maskPattern: PATTERN_010, pattern: 'PATTERN_010'},
85-
{$maskPattern: PATTERN_011, pattern: 'PATTERN_011'},
86-
{$maskPattern: PATTERN_100, pattern: 'PATTERN_100'},
87-
{$maskPattern: PATTERN_101, pattern: 'PATTERN_101'},
88-
{$maskPattern: PATTERN_110, pattern: 'PATTERN_110'},
89-
{$maskPattern: PATTERN_111, pattern: 'PATTERN_111'},
90-
];
9178

9279
suite(desc, function(){
9380

0 commit comments

Comments
 (0)