mirror of
https://github.com/chillerlan/php-qrcode.git
synced 2026-08-31 20:48:40 +00:00
:octocat: move format info & masking
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
|
||||
namespace chillerlan\QRCode\Common;
|
||||
|
||||
use chillerlan\QRCode\Data\QRData;
|
||||
use chillerlan\QRCode\Data\QRMatrix;
|
||||
use chillerlan\QRCode\QRCodeException;
|
||||
use Closure;
|
||||
use function abs, array_search, count, min;
|
||||
@@ -112,11 +112,12 @@ final class MaskPattern{
|
||||
/**
|
||||
* Evaluates the matrix of the given data interface and returns a new mask pattern instance for the best result
|
||||
*/
|
||||
public static function getBestPattern(QRData $dataInterface):self{
|
||||
public static function getBestPattern(QRMatrix $QRMatrix):self{
|
||||
$penalties = [];
|
||||
|
||||
foreach(self::PATTERNS as $pattern){
|
||||
$matrix = $dataInterface->writeMatrix(new self($pattern))->getMatrix(true);
|
||||
$mp = new self($pattern);
|
||||
$matrix = (clone $QRMatrix)->setFormatInfo($mp)->mask($mp)->getMatrix(true);
|
||||
$penalty = 0;
|
||||
|
||||
for($level = 1; $level <= 4; $level++){
|
||||
|
||||
+2
-4
@@ -10,7 +10,7 @@
|
||||
|
||||
namespace chillerlan\QRCode\Data;
|
||||
|
||||
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, MaskPattern, Mode, Version};
|
||||
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, Mode, Version};
|
||||
use chillerlan\Settings\SettingsContainerInterface;
|
||||
|
||||
use function count;
|
||||
@@ -122,12 +122,10 @@ final class QRData{
|
||||
/**
|
||||
* returns a fresh matrix object with the data written and masked with the given $maskPattern
|
||||
*/
|
||||
public function writeMatrix(MaskPattern $maskPattern):QRMatrix{
|
||||
public function writeMatrix():QRMatrix{
|
||||
return (new QRMatrix($this->version, $this->eccLevel))
|
||||
->initFunctionalPatterns()
|
||||
->writeCodewords($this->bitBuffer)
|
||||
->setFormatInfo($maskPattern)
|
||||
->mask($maskPattern)
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
+13
-5
@@ -236,14 +236,22 @@ class QRCode{
|
||||
* @throws \chillerlan\QRCode\Data\QRCodeDataException
|
||||
*/
|
||||
public function getQRMatrix():QRMatrix{
|
||||
$dataInterface = new QRData($this->options, $this->dataSegments);
|
||||
$maskPattern = $this->options->maskPattern === MaskPattern::AUTO
|
||||
? MaskPattern::getBestPattern($dataInterface)
|
||||
$matrix = (new QRData($this->options, $this->dataSegments))->writeMatrix();
|
||||
|
||||
$maskPattern = $this->options->maskPattern === MaskPattern::AUTO
|
||||
? MaskPattern::getBestPattern($matrix)
|
||||
: new MaskPattern($this->options->maskPattern);
|
||||
|
||||
$matrix = $dataInterface->writeMatrix($maskPattern);
|
||||
$matrix->setFormatInfo($maskPattern)->mask($maskPattern);
|
||||
|
||||
return $this->addMatrixModifications($matrix);
|
||||
}
|
||||
|
||||
/**
|
||||
* add matrix modifications after mask pattern evaluation and before handing over to output
|
||||
*/
|
||||
protected function addMatrixModifications(QRMatrix $matrix):QRMatrix{
|
||||
|
||||
// add matrix modifications after mask pattern evaluation and before handing over to output
|
||||
if($this->options->addLogoSpace){
|
||||
$logoSpaceWidth = $this->options->logoSpaceWidth;
|
||||
$logoSpaceHeight = $this->options->logoSpaceHeight;
|
||||
|
||||
@@ -67,13 +67,15 @@ abstract class DataInterfaceTestAbstract extends TestCase{
|
||||
*
|
||||
* @dataProvider maskPatternProvider
|
||||
*/
|
||||
public function testInitMatrix(int $maskPattern):void{
|
||||
public function testInitMatrix(int $pattern):void{
|
||||
$maskPattern = new MaskPattern($pattern);
|
||||
|
||||
$this->QRData->setData([new static::$FQN(static::$testdata)]);
|
||||
|
||||
$matrix = $this->QRData->writeMatrix(new MaskPattern($maskPattern));
|
||||
$matrix = $this->QRData->writeMatrix()->setFormatInfo($maskPattern)->mask($maskPattern);
|
||||
|
||||
$this::assertInstanceOf(QRMatrix::class, $matrix);
|
||||
$this::assertSame($maskPattern, $matrix->getMaskPattern()->getPattern());
|
||||
$this::assertSame($pattern, $matrix->getMaskPattern()->getPattern());
|
||||
}
|
||||
|
||||
abstract public static function stringValidateProvider():array;
|
||||
|
||||
@@ -42,9 +42,10 @@ final class QRDataTest extends TestCase{
|
||||
|
||||
$options = new QROptions(['version' => 3]);
|
||||
$bitBuffer = new BitBuffer($rawBytes);
|
||||
$QRData = (new QRData($options))->setBitBuffer($bitBuffer);
|
||||
$maskPattern = MaskPattern::getBestPattern($QRData);
|
||||
$matrix = $QRData->writeMatrix($maskPattern);
|
||||
$matrix = (new QRData($options))->setBitBuffer($bitBuffer)->writeMatrix();
|
||||
$maskPattern = MaskPattern::getBestPattern($matrix);
|
||||
|
||||
$matrix->setFormatInfo($maskPattern)->mask($maskPattern);
|
||||
|
||||
$this::assertSame(3, $matrix->getVersion()->getVersionNumber());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user