Skip to content

Commit

Permalink
🚿
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Dec 19, 2023
1 parent 564ef12 commit 7fd6cfc
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 93 deletions.
9 changes: 6 additions & 3 deletions src/Common/MaskPattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,15 @@ export default class MaskPattern{
let $size = $QRMatrix.getSize();

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

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

$penalties[$pattern] = PHPJS.intval($penalty);
Expand Down
45 changes: 34 additions & 11 deletions src/Data/QRMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,14 @@ export default class QRMatrix{
set($x, $y, $value, $M_TYPE){

if(PHPJS.isset(() => this._matrix[$y][$x])){
this._matrix[$y][$x] = $M_TYPE | ($value ? IS_DARK : 0);
// we don't know whether the input is dark, so we remove the dark bit
$M_TYPE &= ~IS_DARK

if($value === true){
$M_TYPE |= IS_DARK;
}

this._matrix[$y][$x] = $M_TYPE;
}

return this;
Expand Down Expand Up @@ -259,11 +266,11 @@ export default class QRMatrix{
*/
checkType($x, $y, $M_TYPE){

if(!PHPJS.isset(() => this._matrix[$y][$x])){
return false;
if(PHPJS.isset(() => this._matrix[$y][$x])){
return (this._matrix[$y][$x] & $M_TYPE) === $M_TYPE;
}

return (this._matrix[$y][$x] & $M_TYPE) === $M_TYPE;
return false;
}

/**
Expand Down Expand Up @@ -299,7 +306,12 @@ export default class QRMatrix{
* @returns {boolean}
*/
check($x, $y){
return this.checkType($x, $y, IS_DARK);

if(PHPJS.isset(() => this._matrix[$y][$x])){
return this.isDark(this._matrix[$y][$x]);
}

return false;
}

/**
Expand Down Expand Up @@ -585,6 +597,17 @@ export default class QRMatrix{
return this;
}

/**
* Rotates the matrix by 90 degrees clock wise
*
* @link https://stackoverflow.com/a/58668351
*/
rotate90(){
this._matrix = this._matrix[0].map((val, index) => this._matrix.map(row => row[index]).reverse())

return this;
}

/**
* Inverts the values of the whole matrix
*
Expand Down Expand Up @@ -630,18 +653,18 @@ export default class QRMatrix{
* @link https://github.com/chillerlan/php-qrcode/issues/52
*
* @param {number|int} $width
* @param {number|int} $height
* @param {number|int|null} $height
* @param {number|int|null} $startX
* @param {number|int|null} $startY
*
* @returns {QRMatrix}
* @throws {QRCodeDataException}
*/
setLogoSpace($width, $height, $startX = null, $startY = null){
setLogoSpace($width, $height = null, $startX = null, $startY = null){
$height ??= $width;

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

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

// throw if the size is negative or exceeds the qrcode size
if($width < 0 || $height < 0 || $width > $dimension || $height > $dimension){
throw new QRCodeDataException('invalid logo dimensions');
if($width > $dimension || $height > $dimension){
throw new QRCodeDataException('logo dimensions exceed matrix size');
}

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

for(let $y = 0; $y < this.moduleCount; $y++){
for(let $x = 0; $x < this.moduleCount; $x++){
if($mask($x, $y) && (this._matrix[$y][$x] & M_DATA) === M_DATA){
if((this._matrix[$y][$x] & M_DATA) === M_DATA && $mask($x, $y)){
this.flip($x, $y);
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/QRCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,9 @@ export default class QRCode{
addMatrixModifications($QRMatrix){

if(this.options.addLogoSpace){
let $logoSpaceWidth = this.options.logoSpaceWidth;
let $logoSpaceHeight = this.options.logoSpaceHeight;

// check whether one of the dimensions was omitted
if($logoSpaceWidth === null || $logoSpaceHeight === null){
$logoSpaceWidth = ($logoSpaceWidth ?? $logoSpaceHeight ?? 0);
$logoSpaceHeight = null;
}
let $logoSpaceWidth = (this.options.logoSpaceWidth ?? this.options.logoSpaceHeight ?? 0);
let $logoSpaceHeight = (this.options.logoSpaceHeight ?? $logoSpaceWidth);

$QRMatrix.setLogoSpace(
$logoSpaceWidth,
Expand Down
8 changes: 4 additions & 4 deletions src/QROptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,18 @@ export default class QROptions{
/**
* width of the logo space
*
* @type {number|int}
* @type {number|int|null}
* @protected
*/
_logoSpaceWidth = 0;
_logoSpaceWidth = null;

/**
* height of the logo space
*
* @type {number|int}
* @type {number|int|null}
* @protected
*/
_logoSpaceHeight = 0;
_logoSpaceHeight = null;

/**
* optional horizontal start position of the logo space (top left corner)
Expand Down
15 changes: 1 addition & 14 deletions test/Data/QRDataMode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*/

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

import {beforeEach, suite, test} from 'mocha';
Expand All @@ -34,7 +33,6 @@ suite('QRDataModeTest', function(){
let datamodeProvider = [
{$fqn: AlphaNum, desc: 'AlphaNum'},
{$fqn: Byte, desc: 'Byte'},
// {$fqn: Kanji, desc: 'Kanji'},
{$fqn: Numeric, desc: 'Numeric'},
];

Expand Down Expand Up @@ -77,17 +75,6 @@ suite('QRDataModeTest', function(){
],
}[desc];

// the 8 mask patterns to iterate over
let maskPatternProvider = [
{$maskPattern: PATTERN_000, pattern: 'PATTERN_000'},
{$maskPattern: PATTERN_001, pattern: 'PATTERN_001'},
{$maskPattern: PATTERN_010, pattern: 'PATTERN_010'},
{$maskPattern: PATTERN_011, pattern: 'PATTERN_011'},
{$maskPattern: PATTERN_100, pattern: 'PATTERN_100'},
{$maskPattern: PATTERN_101, pattern: 'PATTERN_101'},
{$maskPattern: PATTERN_110, pattern: 'PATTERN_110'},
{$maskPattern: PATTERN_111, pattern: 'PATTERN_111'},
];

suite(desc, function(){

Expand Down
Loading

0 comments on commit 7fd6cfc

Please sign in to comment.