From 605dc4b1aa233591a3265e62e73f1447e9e5acaf Mon Sep 17 00:00:00 2001 From: smiley Date: Tue, 12 Jul 2022 19:53:52 +0200 Subject: [PATCH] :shower: QRMatrix: quiet zone creation rework --- src/Data/QRMatrix.php | 36 +++++++++++++++++++----------------- src/Decoder/BitMatrix.php | 2 +- tests/Data/QRMatrixTest.php | 2 +- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/Data/QRMatrix.php b/src/Data/QRMatrix.php index 1516bd95e..8e1cf515e 100755 --- a/src/Data/QRMatrix.php +++ b/src/Data/QRMatrix.php @@ -11,7 +11,7 @@ namespace chillerlan\QRCode\Data; use chillerlan\QRCode\Common\{BitBuffer, EccLevel, MaskPattern, ReedSolomonEncoder, Version}; -use function array_fill, array_unshift, count, floor, max, min, range; +use function array_fill, count, floor, range; /** * Holds a numerical representation of the final QR Code; @@ -103,7 +103,14 @@ class QRMatrix{ $this->eccLevel = $eccLevel; $this->maskPattern = $maskPattern; $this->moduleCount = $this->version->getDimension(); - $this->matrix = array_fill(0, $this->moduleCount, array_fill(0, $this->moduleCount, $this::M_NULL)); + $this->matrix = $this->createMatrix($this->moduleCount, $this::M_NULL); + } + + /** + * Creates a 2-dimensional array (square) of the given $size + */ + protected function createMatrix(int $size, int $value):array{ + return array_fill(0, $size, array_fill(0, $size, $value)); } /** @@ -478,31 +485,26 @@ class QRMatrix{ * * @throws \chillerlan\QRCode\Data\QRCodeDataException */ - public function setQuietZone(int $size = null):self{ + public function setQuietZone(int $quietZoneSize):self{ if($this->matrix[$this->moduleCount - 1][$this->moduleCount - 1] === $this::M_NULL){ throw new QRCodeDataException('use only after writing data'); } - $size = $size !== null - ? max(0, min($size, floor($this->moduleCount / 2))) - : 4; + // create a matrix with the new size + $newSize = $this->moduleCount + ($quietZoneSize * 2); + $newMatrix = $this->createMatrix($newSize, $this::M_QUIETZONE); + // copy over the current matrix for($y = 0; $y < $this->moduleCount; $y++){ - for($i = 0; $i < $size; $i++){ - array_unshift($this->matrix[$y], $this::M_QUIETZONE); - $this->matrix[$y][] = $this::M_QUIETZONE; + for($x = 0; $x < $this->moduleCount; $x++){ + $newMatrix[$y + $quietZoneSize][$x + $quietZoneSize] = $this->matrix[$y][$x]; } } - $this->moduleCount += ($size * 2); - - $r = array_fill(0, $this->moduleCount, $this::M_QUIETZONE); - - for($i = 0; $i < $size; $i++){ - array_unshift($this->matrix, $r); - $this->matrix[] = $r; - } + // set the new values + $this->moduleCount = $newSize; + $this->matrix = $newMatrix; return $this; } diff --git a/src/Decoder/BitMatrix.php b/src/Decoder/BitMatrix.php index 752da2e28..98ff387eb 100644 --- a/src/Decoder/BitMatrix.php +++ b/src/Decoder/BitMatrix.php @@ -421,7 +421,7 @@ final class BitMatrix extends QRMatrix{ * @codeCoverageIgnore * @throws \chillerlan\QRCode\Data\QRCodeDataException */ - public function setQuietZone(int $size = null):self{ + public function setQuietZone(int $quietZoneSize = null):self{ throw new QRCodeDataException('not supported'); } diff --git a/tests/Data/QRMatrixTest.php b/tests/Data/QRMatrixTest.php index 919a2ba8a..888882c3b 100755 --- a/tests/Data/QRMatrixTest.php +++ b/tests/Data/QRMatrixTest.php @@ -343,7 +343,7 @@ final class QRMatrixTest extends TestCase{ $this->expectException(QRCodeDataException::class); $this->expectExceptionMessage('use only after writing data'); - $this->matrix->setQuietZone(); + $this->matrix->setQuietZone(42); } /**