Sfoglia il codice sorgente

:octocat: reduce calls to QRMatrix::getMatrix() and use QRMatrix::get() instead

smiley 2 anni fa
parent
commit
fd4d208c05

+ 3 - 2
examples/svgMeltedModules.php

@@ -38,8 +38,9 @@ class MeltedSVGQRCodeOutput extends QRMarkupSVG{
 		$paths = [];
 
 		// collect the modules for each type
-		foreach($this->matrix->getMatrix() as $y => $row){
-			foreach($row as $x => $M_TYPE){
+		for($y = 0; $y < $this->moduleCount; $y++){
+			for($x = 0; $x < $this->moduleCount; $x++){
+				$M_TYPE       = $this->matrix->get($x, $y);
 				$M_TYPE_LAYER = $M_TYPE;
 
 				if($this->options->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->options->excludeFromConnect)){

+ 3 - 2
examples/svgRandomColoredDots.php

@@ -41,8 +41,9 @@ class RandomDotsSVGOutput extends QRMarkupSVG{
 		$paths = [];
 
 		// collect the modules for each type
-		foreach($this->matrix->getMatrix() as $y => $row){
-			foreach($row as $x => $M_TYPE){
+		for($y = 0; $y < $this->moduleCount; $y++){
+			for($x = 0; $x < $this->moduleCount; $x++){
+				$M_TYPE       = $this->matrix->get($x, $y);
 				$M_TYPE_LAYER = $M_TYPE;
 
 				if($this->options->connectPaths

+ 6 - 5
examples/svgRoundQuietzone.php

@@ -87,11 +87,11 @@ class RoundQuietzoneSVGoutput extends QRMarkupSVG{
 		// substract 1/2 stroke width and module radius from the circle radius to not cut off modules
 		$r  = ($radius - $this->options->circleRadius * 2);
 
-		foreach($this->matrix->getMatrix() as $y => $row){
-			foreach($row as $x => $value){
+		for($y = 0; $y < $this->moduleCount; $y++){
+			for($x = 0; $x < $this->moduleCount; $x++){
 
 				// skip anything that's not quiet zone
-				if($value !== QRMatrix::M_QUIETZONE){
+				if(!$this->matrix->checkType($x, $y, QRMatrix::M_QUIETZONE)){
 					continue;
 				}
 
@@ -175,8 +175,9 @@ class RoundQuietzoneSVGoutput extends QRMarkupSVG{
 		$paths = [];
 
 		// collect the modules for each type
-		foreach($this->matrix->getMatrix() as $y => $row){
-			foreach($row as $x => $M_TYPE){
+		for($y = 0; $y < $this->moduleCount; $y++){
+			for($x = 0; $x < $this->moduleCount; $x++){
+				$M_TYPE       = $this->matrix->get($x, $y);
 				$M_TYPE_LAYER = $M_TYPE;
 
 				if(!$this->matrix->checkTypeIn($x, $y, $this->options->excludeFromConnect)){

+ 3 - 4
src/Output/QRFpdf.php

@@ -115,15 +115,14 @@ class QRFpdf extends QROutputAbstract{
 
 		$prevColor = null;
 
-		foreach($this->matrix->getMatrix() as $y => $row){
-
-			foreach($row as $x => $M_TYPE){
+		for($y = 0; $y < $this->moduleCount; $y++){
+			for($x = 0; $x < $this->moduleCount; $x++){
 
 				if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
 					continue;
 				}
 
-				$color = $this->getModuleValue($M_TYPE);
+				$color = $this->getModuleValueAt($x, $y);
 
 				if($prevColor !== $color){
 					/** @phan-suppress-next-line PhanParamTooFewUnpack */

+ 8 - 6
src/Output/QRGdImage.php

@@ -229,9 +229,9 @@ class QRGdImage extends QROutputAbstract{
 	 * Creates the QR image
 	 */
 	protected function drawImage():void{
-		foreach($this->matrix->getMatrix() as $y => $row){
-			foreach($row as $x => $M_TYPE){
-				$this->setPixel($x, $y, $M_TYPE);
+		for($y = 0; $y < $this->moduleCount; $y++){
+			for($x = 0; $x < $this->moduleCount; $x++){
+				$this->setPixel($x, $y);
 			}
 		}
 	}
@@ -239,12 +239,14 @@ class QRGdImage extends QROutputAbstract{
 	/**
 	 * Creates a single QR pixel with the given settings
 	 */
-	protected function setPixel(int $x, int $y, int $M_TYPE):void{
+	protected function setPixel(int $x, int $y):void{
 
 		if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
 			return;
 		}
 
+		$color = $this->getModuleValueAt($x, $y);
+
 		$this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)
 			? imagefilledellipse(
 				$this->image,
@@ -252,7 +254,7 @@ class QRGdImage extends QROutputAbstract{
 				(int)(($y * $this->scale) + ($this->scale / 2)),
 				(int)(2 * $this->options->circleRadius * $this->scale),
 				(int)(2 * $this->options->circleRadius * $this->scale),
-				$this->getModuleValue($M_TYPE)
+				$color
 			)
 			: imagefilledrectangle(
 				$this->image,
@@ -260,7 +262,7 @@ class QRGdImage extends QROutputAbstract{
 				($y * $this->scale),
 				(($x + 1) * $this->scale),
 				(($y + 1) * $this->scale),
-				$this->getModuleValue($M_TYPE)
+				$color
 			);
 	}
 

+ 5 - 5
src/Output/QRImagick.php

@@ -186,9 +186,9 @@ class QRImagick extends QROutputAbstract{
 		$this->imagickDraw = new ImagickDraw;
 		$this->imagickDraw->setStrokeWidth(0);
 
-		foreach($this->matrix->getMatrix() as $y => $row){
-			foreach($row as $x => $M_TYPE){
-				$this->setPixel($x, $y, $M_TYPE);
+		for($y = 0; $y < $this->moduleCount; $y++){
+			for($x = 0; $x < $this->moduleCount; $x++){
+				$this->setPixel($x, $y);
 			}
 		}
 
@@ -198,13 +198,13 @@ class QRImagick extends QROutputAbstract{
 	/**
 	 * draws a single pixel at the given position
 	 */
-	protected function setPixel(int $x, int $y, int $M_TYPE):void{
+	protected function setPixel(int $x, int $y):void{
 
 		if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
 			return;
 		}
 
-		$this->imagickDraw->setFillColor($this->getModuleValue($M_TYPE));
+		$this->imagickDraw->setFillColor($this->getModuleValueAt($x, $y));
 
 		$this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)
 			? $this->imagickDraw->circle(

+ 3 - 3
src/Output/QRMarkupHTML.php

@@ -27,11 +27,11 @@ class QRMarkupHTML extends QRMarkup{
 
 		$html .= $this->options->eol;
 
-		foreach($this->matrix->getMatrix() as $row){
+		for($y = 0; $y < $this->moduleCount; $y++){
 			$html .= '<div>';
 
-			foreach($row as $M_TYPE){
-				$html .= sprintf('<span style="background: %s;"></span>', $this->getModuleValue($M_TYPE));
+			for($x = 0; $x < $this->moduleCount; $x++){
+				$html .= sprintf('<span style="background: %s;"></span>', $this->getModuleValueAt($x, $y));
 			}
 
 			$html .= '</div>'.$this->options->eol;

+ 18 - 8
src/Output/QROutputAbstract.php

@@ -92,27 +92,36 @@ abstract class QROutputAbstract implements QROutputInterface{
 	}
 
 	/**
-	 * Returns the prepared value for the given $M_TYPE (return value depends on the output class)
+	 * Returns the prepared value for the given $M_TYPE
 	 *
-	 * @return mixed|null
+	 * @return mixed|null return value depends on the output class
 	 */
 	protected function getModuleValue(int $M_TYPE){
 		return ($this->moduleValues[$M_TYPE] ?? null);
 	}
 
 	/**
-	 * Prepares the value for the given input (return value depends on the output class)
+	 * Returns the prepared module value at the given coordinate [$x, $y] (convenience)
+	 *
+	 * @return mixed|null
+	 */
+	protected function getModuleValueAt(int $x, int $y){
+		return $this->getModuleValue($this->matrix->get($x, $y));
+	}
+
+	/**
+	 * Prepares the value for the given input ()
 	 *
 	 * @param mixed $value
 	 *
-	 * @return mixed
+	 * @return mixed|null return value depends on the output class
 	 */
 	abstract protected function prepareModuleValue($value);
 
 	/**
-	 * Returns a default value for either dark or light modules (return value depends on the output class)
+	 * Returns a default value for either dark or light modules
 	 *
-	 * @return mixed
+	 * @return mixed|null return value depends on the output class
 	 */
 	abstract protected function getDefaultModuleValue(bool $isDark);
 
@@ -161,8 +170,9 @@ abstract class QROutputAbstract implements QROutputInterface{
 		$paths = [];
 
 		// collect the modules for each type
-		foreach($this->matrix->getMatrix() as $y => $row){
-			foreach($row as $x => $M_TYPE){
+		for($y = 0; $y < $this->moduleCount; $y++){
+			for($x = 0; $x < $this->moduleCount; $x++){
+				$M_TYPE       = $this->matrix->get($x, $y);
 				$M_TYPE_LAYER = $M_TYPE;
 
 				if($this->options->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->options->excludeFromConnect)){

+ 3 - 3
src/Output/QRString.php

@@ -66,11 +66,11 @@ class QRString extends QROutputAbstract{
 	protected function text():string{
 		$str = [];
 
-		foreach($this->matrix->getMatrix() as $row){
+		for($y = 0; $y < $this->moduleCount; $y++){
 			$r = [];
 
-			foreach($row as $M_TYPE){
-				$r[] = $this->getModuleValue($M_TYPE);
+			for($x = 0; $x < $this->moduleCount; $x++){
+				$r[] = $this->getModuleValueAt($x, $y);
 			}
 
 			$str[] = implode('', $r);