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

:octocat: removed QROptionsTrait::$markupDark and QROptionsTrait::$markupLight, added QROptionsTrait::$svgUseFillAttributes

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

+ 15 - 16
examples/authenticator.php

@@ -26,30 +26,29 @@ $options = new class extends SettingsContainerAbstract{
  *
  * @see https://github.com/chillerlan/php-authenticator
  */
-$options->mode                = AuthenticatorInterface::TOTP;
-$options->digits              = 8;
-$options->algorithm           = AuthenticatorInterface::ALGO_SHA512;
+$options->mode                 = AuthenticatorInterface::TOTP;
+$options->digits               = 8;
+$options->algorithm            = AuthenticatorInterface::ALGO_SHA512;
 
 /*
  * QROptionsTrait
  */
-$options->version             = 7;
-$options->addQuietzone        = false;
-$options->outputBase64        = false;
-$options->svgAddXmlHeader     = false;
-$options->cssClass            = 'my-qrcode';
-$options->drawLightModules    = false;
-$options->markupDark          = '';
-$options->markupLight         = '';
-$options->drawCircularModules = true;
-$options->circleRadius        = 0.4;
-$options->connectPaths        = true;
-$options->keepAsSquare        = [
+$options->version              = 7;
+$options->addQuietzone         = false;
+$options->outputBase64         = false;
+$options->svgAddXmlHeader      = false;
+$options->cssClass             = 'my-qrcode';
+$options->drawLightModules     = false;
+$options->svgUseFillAttributes = false;
+$options->drawCircularModules  = true;
+$options->circleRadius         = 0.4;
+$options->connectPaths         = true;
+$options->keepAsSquare         = [
 	QRMatrix::M_FINDER_DARK,
 	QRMatrix::M_FINDER_DOT,
 	QRMatrix::M_ALIGNMENT_DARK,
 ];
-$options->svgDefs             = '
+$options->svgDefs              = '
 	<linearGradient id="gradient" x1="1" y2="1">
 		<stop id="stop1" offset="0" />
 		<stop id="stop2" offset="0.5"/>

+ 0 - 2
examples/html.php

@@ -19,8 +19,6 @@ $options = new QROptions;
 $options->version      = 5;
 $options->outputType   = QROutputInterface::MARKUP_HTML;
 $options->cssClass     = 'qrcode';
-$options->markupDark   = '#555';
-$options->markupLight  = '#CCC';
 $options->moduleValues = [
 	// finder
 	QRMatrix::M_FINDER_DARK    => '#A71111', // dark (true)

+ 13 - 14
examples/imagickConvertSVGtoPNG.php

@@ -84,24 +84,23 @@ class SVGConvert extends QRMarkupSVG{
 // SVG from the basic example
 $options = new QROptions;
 
-$options->version             = 7;
-$options->outputType          = QROutputInterface::CUSTOM;
-$options->outputInterface     = SVGConvert::class;
-$options->imagickFormat       = 'png32';
-$options->scale               = 20;
-$options->outputBase64        = false;
-$options->drawLightModules    = true;
-$options->markupDark          = '';
-$options->markupLight         = '';
-$options->drawCircularModules = true;
-$options->circleRadius        = 0.4;
-$options->connectPaths        = true;
-$options->keepAsSquare        = [
+$options->version              = 7;
+$options->outputType           = QROutputInterface::CUSTOM;
+$options->outputInterface      = SVGConvert::class;
+$options->imagickFormat        = 'png32';
+$options->scale                = 20;
+$options->outputBase64         = false;
+$options->drawLightModules     = true;
+$options->svgUseFillAttributes = false;
+$options->drawCircularModules  = true;
+$options->circleRadius         = 0.4;
+$options->connectPaths         = true;
+$options->keepAsSquare         = [
 	QRMatrix::M_FINDER_DARK,
 	QRMatrix::M_FINDER_DOT,
 	QRMatrix::M_ALIGNMENT_DARK,
 ];
-$options->svgDefs             = '
+$options->svgDefs              = '
 	<linearGradient id="rainbow" x1="1" y2="1">
 		<stop stop-color="#e2453c" offset="0"/>
 		<stop stop-color="#e07e39" offset="0.2"/>

+ 19 - 12
examples/imagickImageAsBackground.php

@@ -19,6 +19,14 @@ require_once __DIR__.'/../vendor/autoload.php';
 
 class QRImagickImageAsBackground extends QRImagick{
 
+	/**
+	 * @inheritDoc
+	 */
+	protected function getDefaultModuleValue(bool $isDark):ImagickPixel{
+		// RGBA, adjust opacity to increase contrast
+		return $this->prepareModuleValue(($isDark) ? '#00000040' : '#ffffffa0');
+	}
+
 	/**
 	 * @inheritDoc
 	 */
@@ -81,18 +89,17 @@ class ImageAsBackgroundOptions extends QROptions{
 
 $options = new ImageAsBackgroundOptions;
 
-$options->background       = __DIR__.'/background.jpg'; // setting from the augmented options
-$options->version          = 5;
-$options->outputType       = QROutputInterface::CUSTOM;
-$options->outputInterface  = QRImagickImageAsBackground::class; // use the custom output class
-$options->eccLevel         = EccLevel::H;
-$options->outputBase64     = false;
-$options->scale            = 10;
-$options->drawLightModules = true;
-$options->markupDark       = '#00000040'; // RGBA, adjust opacity to increase contrast
-$options->markupLight      = '#ffffffa0';
-$options->invertMatrix     = false;
-$options->quietzoneSize    = 1;
+$options->background           = __DIR__.'/background.jpg'; // setting from the augmented options
+$options->version              = 5;
+$options->outputType           = QROutputInterface::CUSTOM;
+$options->outputInterface      = QRImagickImageAsBackground::class; // use the custom output class
+$options->eccLevel             = EccLevel::H;
+$options->outputBase64         = false;
+$options->scale                = 10;
+$options->drawLightModules     = true;
+$options->svgUseFillAttributes = false;
+$options->invertMatrix         = false;
+$options->quietzoneSize        = 1;
 
 
 // dump the output, with an additional logo

+ 9 - 12
examples/svg.php

@@ -18,21 +18,19 @@ require_once __DIR__.'/../vendor/autoload.php';
 
 $options = new QROptions;
 
-$options->version             = 7;
-$options->outputType          = QROutputInterface::MARKUP_SVG;
-$options->outputBase64        = false;
+$options->version              = 7;
+$options->outputType           = QROutputInterface::MARKUP_SVG;
+$options->outputBase64         = false;
 // if set to false, the light modules won't be rendered
-$options->drawLightModules    = true;
-// empty the default value to remove the fill* and opacity* attributes from the <path> elements
-$options->markupDark          = '';
-$options->markupLight         = '';
+$options->drawLightModules     = true;
+$options->svgUseFillAttributes = false;
 // draw the modules as circles isntead of squares
-$options->drawCircularModules = true;
-$options->circleRadius        = 0.4;
+$options->drawCircularModules  = true;
+$options->circleRadius         = 0.4;
 // connect paths
-$options->connectPaths        = true;
+$options->connectPaths         = true;
 // keep modules of these types as square
-$options->keepAsSquare        = [
+$options->keepAsSquare         = [
 	QRMatrix::M_FINDER_DARK,
 	QRMatrix::M_FINDER_DOT,
 	QRMatrix::M_ALIGNMENT_DARK,
@@ -58,7 +56,6 @@ try{
 }
 catch(Throwable $e){
 	// handle the exception in whatever way you need
-
 	exit($e->getMessage());
 }
 

+ 10 - 11
examples/svgConvertViaCanvas.php

@@ -18,18 +18,17 @@ require_once __DIR__.'/../vendor/autoload.php';
 
 $options = new QROptions;
 
-$options->version             = 7;
-$options->outputType          = QROutputInterface::MARKUP_SVG;
-$options->outputBase64        = false;
-$options->svgAddXmlHeader     = false;
-$options->drawLightModules    = false;
-$options->markupDark          = '';
-$options->markupLight         = '';
-$options->drawCircularModules = true;
-$options->circleRadius        = 0.4;
-$options->connectPaths        = true;
+$options->version              = 7;
+$options->outputType           = QROutputInterface::MARKUP_SVG;
+$options->outputBase64         = false;
+$options->svgAddXmlHeader      = false;
+$options->svgUseFillAttributes = false;
+$options->drawLightModules     = false;
+$options->drawCircularModules  = true;
+$options->circleRadius         = 0.4;
+$options->connectPaths         = true;
 
-$options->keepAsSquare        = [
+$options->keepAsSquare         = [
 	QRMatrix::M_FINDER_DARK,
 	QRMatrix::M_FINDER_DOT,
 	QRMatrix::M_ALIGNMENT_DARK,

+ 1 - 1
src/Output/QRImagick.php

@@ -106,7 +106,7 @@ class QRImagick extends QROutputAbstract{
 	 * @inheritDoc
 	 */
 	protected function getDefaultModuleValue(bool $isDark):ImagickPixel{
-		return $this->prepareModuleValue(($isDark) ? $this->options->markupDark : $this->options->markupLight);
+		return $this->prepareModuleValue(($isDark) ? '#000' : '#fff');
 	}
 
 	/**

+ 1 - 1
src/Output/QRMarkup.php

@@ -65,7 +65,7 @@ abstract class QRMarkup extends QROutputAbstract{
 	 * @inheritDoc
 	 */
 	protected function getDefaultModuleValue(bool $isDark):string{
-		return ($isDark) ? $this->options->markupDark : $this->options->markupLight;
+		return ($isDark) ? '#000' : '#fff';
 	}
 
 	/**

+ 10 - 0
src/Output/QRMarkupSVG.php

@@ -145,6 +145,16 @@ class QRMarkupSVG extends QRMarkup{
 	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path
 	 */
 	protected function path(string $path, int $M_TYPE):string{
+
+		if($this->options->svgUseFillAttributes){
+			return sprintf(
+				'<path class="%s" fill="%s" d="%s"/>',
+				$this->getCssClass($M_TYPE),
+				$this->getModuleValue($M_TYPE),
+				$path
+			);
+		}
+
 		return sprintf('<path class="%s" d="%s"/>', $this->getCssClass($M_TYPE), $path);
 	}
 

+ 4 - 20
src/QROptionsTrait.php

@@ -362,17 +362,6 @@ trait QROptionsTrait{
 	 */
 	protected string $cssClass = 'qrcode';
 
-	/**
-	 * Markup substitute for dark (CSS value)
-	 */
-	protected string $markupDark = '#000';
-
-	/**
-	 * Markup substitute for light (CSS value)
-	 */
-	protected string $markupLight = '#fff';
-
-
 	/*
 	 * QRMarkupSVG settings
 	 */
@@ -384,15 +373,6 @@ trait QROptionsTrait{
 	 */
 	protected bool $svgAddXmlHeader = true;
 
-	/**
-	 * SVG path opacity
-	 *
-	 * Sets the value for the SVG "fill-opacity" on a `<path>` element. Only in effect when non-empty values
-	 * for `QROptions::$markupDark` and `QROptions::$markupLight` are given.
-	 * The opacity value is the same for all paths - please use CSS for more sophisticated implementations.
-	 */
-	protected float $svgOpacity = 1.0;
-
 	/**
 	 * Anything in the SVG `<defs>` tag
 	 *
@@ -420,6 +400,10 @@ trait QROptionsTrait{
 	 */
 	protected string $svgPreserveAspectRatio = 'xMidYMid';
 
+	/**
+	 * Whether to use the SVG `fill` attributes and set them with the given module values
+	 */
+	protected bool $svgUseFillAttributes = true;
 
 	/*
 	 * QRString settings