Просмотр исходного кода

:octocat: QRMarkupSVG: extract path() method to allow modification of the individual <path> elements

smiley 2 лет назад
Родитель
Сommit
426c16a89b

+ 2 - 2
examples/svg.php

@@ -24,7 +24,7 @@ $options = new QROptions([
 	'addQuietzone'        => true,
 	// if set to false, the light modules won't be rendered
 	'drawLightModules'    => true,
-	// empty the default value to remove the fill* attributes from the <path> elements
+	// empty the default value to remove the fill* and opacity* attributes from the <path> elements
 	'markupDark'          => '',
 	'markupLight'         => '',
 	// draw the modules as circles isntead of squares
@@ -32,7 +32,7 @@ $options = new QROptions([
 	'circleRadius'        => 0.4,
 	// connect paths
 	'connectPaths'        => true,
-	// keep modules of thhese types as square
+	// keep modules of these types as square
 	'keepAsSquare'        => [
 		QRMatrix::M_FINDER|QRMatrix::IS_DARK,
 		QRMatrix::M_FINDER_DOT,

+ 8 - 0
examples/svgMeltedModules.php

@@ -23,6 +23,14 @@ require_once __DIR__.'/../vendor/autoload.php';
  */
 class MeltedSVGQRCodeOutput extends QRMarkupSVG{
 
+	/**
+	 * @inheritDoc
+	 */
+	protected function path(string $path, int $M_TYPE):string{
+		// omit the "fill" and "opacity" attributes on the path element
+		return sprintf('<path class="%s" d="%s"/>', $this->getCssClass($M_TYPE), $path);
+	}
+
 	/**
 	 * @inheritDoc
 	 */

+ 8 - 2
examples/svgRandomColoredDots.php

@@ -24,6 +24,14 @@ require_once __DIR__.'/../vendor/autoload.php';
 // the extended SVG output module
 class RandomDotsSVGOutput extends QRMarkupSVG{
 
+	/**
+	 * @inheritDoc
+	 */
+	protected function path(string $path, int $M_TYPE):string{
+		// omit the "fill" and "opacity" attributes on the path element
+		return sprintf('<path class="%s" d="%s"/>', $this->getCssClass($M_TYPE), $path);
+	}
+
 	/**
 	 * To alter the layer a module appears on, we need to re-implement the collection method
 	 *
@@ -122,8 +130,6 @@ $options = new RandomDotsOptions([
 	'imageBase64'         => false,
 	'outputType'          => QROutputInterface::CUSTOM,
 	'outputInterface'     => RandomDotsSVGOutput::class,
-	'markupDark'          => '',
-	'markupLight'         => '',
 	'drawLightModules'    => false,
 
 	'connectPaths'        => true,

+ 8 - 2
examples/svgRoundQuietzone.php

@@ -63,6 +63,14 @@ class RoundQuietzoneSVGoutput extends QRMarkupSVG{
 		return $svg;
 	}
 
+	/**
+	 * @inheritDoc
+	 */
+	protected function path(string $path, int $M_TYPE):string{
+		// omit the "fill" and "opacity" attributes on the path element
+		return sprintf('<path class="%s" d="%s"/>', $this->getCssClass($M_TYPE), $path);
+	}
+
 	/**
 	 * Sets random modules of the quiet zone to dark
 	 */
@@ -172,8 +180,6 @@ $options = new RoundQuietzoneOptions([
 	'imageBase64'         => false, // avoid base64 URI output
 	'outputType'          => QROutputInterface::CUSTOM,
 	'outputInterface'     => RoundQuietzoneSVGoutput::class, // load our own output class
-	'markupDark'          => '', // avoid "fill" attributes on paths
-	'markupLight'         => '',
 	'drawLightModules'    => false, // set to true to add the light modules
 
 	'connectPaths'        => true,

+ 8 - 3
examples/svgWithLogo.php

@@ -39,6 +39,14 @@ class QRSvgWithLogo extends QRMarkupSVG{
 		return $svg;
 	}
 
+	/**
+	 * @inheritDoc
+	 */
+	protected function path(string $path, int $M_TYPE):string{
+		// omit the "fill" and "opacity" attributes on the path element
+		return sprintf('<path class="%s" d="%s"/>', $this->getCssClass($M_TYPE), $path);
+	}
+
 	/**
 	 * returns a <g> element that contains the SVG logo and positions it properly within the QR Code
 	 *
@@ -110,9 +118,6 @@ $options = new SVGWithLogoOptions([
 	'addQuietzone'        => true,
 	// if set to true, the light modules won't be rendered
 	'drawLightModules'    => true,
-	// empty the default value to remove the fill* attributes from the <path> elements
-	'markupDark'          => '',
-	'markupLight'         => '',
 	// draw the modules as circles isntead of squares
 	'drawCircularModules' => true,
 	'circleRadius'        => 0.45,

+ 21 - 14
src/Output/QRMarkupSVG.php

@@ -68,8 +68,6 @@ class QRMarkupSVG extends QRMarkup{
 
 	/**
 	 * returns one or more SVG <path> elements
-	 *
-	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path
 	 */
 	protected function paths():string{
 		$paths = $this->collectModules(fn(int $x, int $y, int $M_TYPE):string => $this->module($x, $y, $M_TYPE));
@@ -91,23 +89,32 @@ class QRMarkupSVG extends QRMarkup{
 				continue;
 			}
 
-			// ignore non-existent module values
-			$format = !isset($this->moduleValues[$M_TYPE]) || empty($this->moduleValues[$M_TYPE])
-				? '<path class="%1$s" d="%2$s"/>'
-				: '<path class="%1$s" fill="%3$s" fill-opacity="%4$s" d="%2$s"/>';
-
-			$svg[] = sprintf(
-				$format,
-				$this->getCssClass($M_TYPE),
-				$path,
-				$this->moduleValues[$M_TYPE] ?? '',
-				$this->options->svgOpacity)
-			;
+			$svg[] = $this->path($path, $M_TYPE);
 		}
 
 		return implode($this->options->eol, $svg);
 	}
 
+	/**
+	 * renders and returns a single <path> element
+	 *
+	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path
+	 */
+	protected function path(string $path, int $M_TYPE):string{
+		// ignore non-existent module values
+		$format = !isset($this->moduleValues[$M_TYPE]) || empty($this->moduleValues[$M_TYPE])
+			? '<path class="%1$s" d="%2$s"/>'
+			: '<path class="%1$s" fill="%3$s" fill-opacity="%4$s" d="%2$s"/>';
+
+		return sprintf(
+			$format,
+			$this->getCssClass($M_TYPE),
+			$path,
+			$this->moduleValues[$M_TYPE] ?? '', // value may or may not exist
+			$this->options->svgOpacity
+		);
+	}
+
 	/**
 	 * @inheritDoc
 	 */